ACC SHELL
<?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
* @package Nette\Forms\Controls
*/
/**
* Set of radio button controls.
*
* @author David Grudl
*
* @property array $items
* @property-read NHtml $separatorPrototype
* @property-read NHtml $containerPrototype
* @package Nette\Forms\Controls
*/
class NRadioList extends NFormControl
{
/** @var NHtml separator element template */
protected $separator;
/** @var NHtml 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 = NHtml::el();
$this->separator = NHtml::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;
}
/**
* Has been any radio button selected?
* @return bool
*/
public function isFilled()
{
return $this->getValue() !== NULL;
}
/**
* Sets options from which to choose.
* @param array
* @return self
*/
public function setItems(array $items)
{
$this->items = $items;
return $this;
}
/**
* Returns options from which to choose.
* @return array
*/
public function getItems()
{
return $this->items;
}
/**
* Returns separator HTML element template.
* @return NHtml
*/
public function getSeparatorPrototype()
{
return $this->separator;
}
/**
* Returns container HTML element template.
* @return NHtml
*/
public function getContainerPrototype()
{
return $this->container;
}
/**
* Generates control's HTML element.
* @param mixed
* @return NHtml
*/
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 = NHtml::el('label');
foreach ($this->items as $k => $val) {
$counter++;
if ($key !== NULL && (string) $key !== (string) $k) {
continue;
}
$control->id = $label->for = $id . '-' . $counter;
$control->checked = (string) $k === $value;
$control->value = $k;
if ($val instanceof NHtml) {
$label->setHtml($val);
} else {
$label->setText($this->translate((string) $val));
}
if ($key !== NULL) {
return NHtml::el()->add($control)->add($label);
}
$container->add((string) $control . (string) $label . $separator);
$control->data('nette-rules', NULL);
// TODO: separator after last item?
}
return $container;
}
/**
* Generates label's HTML element.
* @param string
* @return NHtml
*/
public function getLabel($caption = NULL)
{
$label = parent::getLabel($caption);
$label->for = NULL;
return $label;
}
}
ACC SHELL 2018