/Nette/Forms/Controls/RadioList.php
PHP | 197 lines | 78 code | 46 blank | 73 comment | 10 complexity | b71f6203e93d4f5589d754ef14640e7c MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- /**
- * Nette Framework
- *
- * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
- *
- * This source file is subject to the "Nette license" that is bundled
- * with this package in the file license.txt.
- *
- * For more information please see http://nettephp.com
- *
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @license http://nettephp.com/license Nette license
- * @link http://nettephp.com
- * @category Nette
- * @package Nette\Forms
- */
- /*namespace Nette\Forms;*/
- require_once dirname(__FILE__) . '/../../Forms/Controls/FormControl.php';
- /**
- * Set of radio button controls.
- *
- * @author David Grudl
- * @copyright Copyright (c) 2004, 2009 David Grudl
- * @package Nette\Forms
- *
- * @property array $items
- * @property-read Nette\Web\Html $separatorPrototype
- * @property-read Nette\Web\Html $containerPrototype
- */
- class RadioList extends FormControl
- {
- /** @var Nette\Web\Html separator element template */
- protected $separator;
- /** @var Nette\Web\Html container element template */
- protected $container;
- /** @var array */
- protected $items = array();
- /**
- * @param string label
- * @param array options from which to choose
- */
- public function __construct($label = NULL, array $items = NULL)
- {
- parent::__construct($label);
- $this->control->type = 'radio';
- $this->container = /*Nette\Web\*/Html::el();
- $this->separator = /*Nette\Web\*/Html::el('br');
- if ($items !== NULL) $this->setItems($items);
- }
- /**
- * Returns selected radio value.
- * @param bool
- * @return mixed
- */
- public function getValue($raw = FALSE)
- {
- return is_scalar($this->value) && ($raw || isset($this->items[$this->value])) ? $this->value : NULL;
- }
- /**
- * Sets options from which to choose.
- * @param array
- * @return RadioList provides a fluent interface
- */
- public function setItems(array $items)
- {
- $this->items = $items;
- return $this;
- }
- /**
- * Returns options from which to choose.
- * @return array
- */
- final public function getItems()
- {
- return $this->items;
- }
- /**
- * Returns separator HTML element template.
- * @return Nette\Web\Html
- */
- final public function getSeparatorPrototype()
- {
- return $this->separator;
- }
- /**
- * Returns container HTML element template.
- * @return Nette\Web\Html
- */
- final public function getContainerPrototype()
- {
- return $this->container;
- }
- /**
- * Generates control's HTML element.
- * @param mixed
- * @return Nette\Web\Html
- */
- public function getControl($key = NULL)
- {
- if ($key === NULL) {
- $container = clone $this->container;
- $separator = (string) $this->separator;
- } elseif (!isset($this->items[$key])) {
- return NULL;
- }
- $control = parent::getControl();
- $id = $control->id;
- $counter = -1;
- $value = $this->value === NULL ? NULL : (string) $this->getValue();
- $label = /*Nette\Web\*/Html::el('label');
- foreach ($this->items as $k => $val) {
- $counter++;
- if ($key !== NULL && $key != $k) continue; // intentionally ==
- $control->id = $label->for = $id . '-' . $counter;
- $control->checked = (string) $k === $value;
- $control->value = $k;
- if ($val instanceof /*Nette\Web\*/Html) {
- $label->setHtml($val);
- } else {
- $label->setText($this->translate($val));
- }
- if ($key !== NULL) {
- return (string) $control . (string) $label;
- }
- $container->add((string) $control . (string) $label . $separator);
- // TODO: separator after last item?
- }
- return $container;
- }
- /**
- * Generates label's HTML element.
- * @param string
- * @return void
- */
- public function getLabel($caption = NULL)
- {
- $label = parent::getLabel($caption);
- $label->for = NULL;
- return $label;
- }
- /**
- * Filled validator: has been any radio button selected?
- * @param IFormControl
- * @return bool
- */
- public static function validateFilled(IFormControl $control)
- {
- return $control->getValue() !== NULL;
- }
- }