TrackManiaControl/libs/FML/Components/CheckBox.php

158 lines
3.5 KiB
PHP
Raw Normal View History

2014-05-14 23:24:00 +02:00
<?php
namespace FML\Components;
use FML\Controls\Entry;
use FML\Controls\Frame;
use FML\Controls\Quad;
use FML\Models\CheckBoxDesign;
use FML\Script\Features\CheckBoxFeature;
2014-05-16 22:44:22 +02:00
use FML\Script\Features\ScriptFeature;
2014-05-14 23:24:00 +02:00
use FML\Types\Renderable;
use FML\Types\ScriptFeatureable;
/**
* CheckBox Component
*
2014-05-20 15:44:45 +02:00
* @author steeffeen <mail@steeffeen.com>
2014-05-14 23:24:00 +02:00
* @copyright FancyManiaLinks Copyright © 2014 Steffen Schröder
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
class CheckBox implements Renderable, ScriptFeatureable {
/*
2014-06-21 03:18:21 +02:00
* Protected properties
2014-05-14 23:24:00 +02:00
*/
protected $name = null;
protected $feature = null;
/**
* Create a new CheckBox Component
*
2014-06-21 03:18:21 +02:00
* @param string $name (optional) CheckBox name
* @param bool $default (optional) Default value
* @param Quad $quad (optional) CheckBox quad
2014-05-14 23:24:00 +02:00
*/
public function __construct($name = null, $default = null, Quad $quad = null) {
2014-05-18 19:45:50 +02:00
$this->feature = new CheckBoxFeature();
2014-05-14 23:24:00 +02:00
$this->setName($name);
$this->setDefault($default);
2014-05-18 19:45:50 +02:00
$this->setQuad($quad);
2014-05-14 23:24:00 +02:00
}
/**
2014-06-21 03:18:21 +02:00
* Set the name
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $name CheckBox name
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
public function setName($name) {
$this->name = (string)$name;
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the default value
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param bool $default Default value
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
public function setDefault($default) {
2014-05-18 19:45:50 +02:00
$this->feature->setDefault($default);
2014-05-14 23:24:00 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the enabled Design
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $style Style name or image url
* @param string $subStyle SubStyle name
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
public function setEnabledDesign($style, $subStyle = null) {
2014-08-11 23:15:53 +02:00
if ($style instanceof CheckBoxDesign) {
2014-06-21 03:18:21 +02:00
$this->feature->setEnabledDesign($style);
} else {
$checkBoxDesign = new CheckBoxDesign($style, $subStyle);
$this->feature->setEnabledDesign($checkBoxDesign);
}
2014-05-14 23:24:00 +02:00
return $this;
}
/**
2014-06-21 03:18:21 +02:00
* Set the disabled Design
2014-05-14 23:24:00 +02:00
*
2014-06-21 03:18:21 +02:00
* @param string $style Style name or image url
* @param string $subStyle SubStyle name
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
public function setDisabledDesign($style, $subStyle = null) {
2014-08-11 23:15:53 +02:00
if ($style instanceof CheckBoxDesign) {
2014-06-21 03:18:21 +02:00
$this->feature->setDisabledDesign($style);
} else {
$checkBoxDesign = new CheckBoxDesign($style, $subStyle);
$this->feature->setDisabledDesign($checkBoxDesign);
}
2014-05-14 23:24:00 +02:00
return $this;
}
/**
* Set the CheckBox Quad
*
* @param Quad $quad CheckBox Quad
2014-07-03 22:34:47 +02:00
* @return static
2014-05-14 23:24:00 +02:00
*/
2014-05-18 19:45:50 +02:00
public function setQuad(Quad $quad = null) {
2014-05-14 23:24:00 +02:00
$this->feature->setQuad($quad);
return $this;
}
/**
* @see \FML\Types\ScriptFeatureable::getScriptFeatures()
*/
public function getScriptFeatures() {
2014-05-16 22:44:22 +02:00
return ScriptFeature::collect($this->feature, $this->getQuad(), $this->feature->getEntry());
}
/**
* Get the CheckBox Quad
*
* @param bool $createIfEmpty (optional) Create the Quad if it's not set
* @return \FML\Controls\Quad
*/
public function getQuad($createIfEmpty = true) {
if (!$this->feature->getQuad() && $createIfEmpty) {
$quad = new Quad();
$quad->setSize(10, 10);
2014-05-18 19:45:50 +02:00
$this->setQuad($quad);
2014-05-16 22:44:22 +02:00
}
return $this->feature->getQuad();
2014-05-14 23:24:00 +02:00
}
/**
2014-07-03 22:34:47 +02:00
* @see \FML\Types\Renderable::render()
2014-05-14 23:24:00 +02:00
*/
public function render(\DOMDocument $domDocument) {
$frame = new Frame();
$quad = $this->getQuad();
$frame->add($quad);
2014-05-18 19:45:50 +02:00
$entry = $this->buildEntry();
$frame->add($entry);
$this->feature->setEntry($entry);
2014-05-14 23:24:00 +02:00
return $frame->render($domDocument);
}
/**
* Build the hidden Entry
*
2014-07-03 22:34:47 +02:00
* @return \FML\Controls\Entry
2014-05-14 23:24:00 +02:00
*/
protected function buildEntry() {
$entry = new Entry();
2014-06-21 03:18:21 +02:00
$entry->setVisible(false)->setName($this->name);
2014-05-14 23:24:00 +02:00
return $entry;
}
}