1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Forms\Controls;
9:
10: use Nette,
11: Nette\Utils\Html;
12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class RadioList extends ChoiceControl
24: {
25:
26: public $generateId = FALSE;
27:
28:
29: protected $separator;
30:
31:
32: protected $container;
33:
34:
35: protected $itemLabel;
36:
37:
38: 39: 40: 41:
42: public function __construct($label = NULL, array $items = NULL)
43: {
44: parent::__construct($label, $items);
45: $this->control->type = 'radio';
46: $this->container = Html::el();
47: $this->separator = Html::el('br');
48: $this->itemLabel = Html::el();
49: }
50:
51:
52: 53: 54: 55:
56: public function getValue()
57: {
58: return parent::getValue();
59: }
60:
61:
62: 63: 64: 65:
66: public function getSeparatorPrototype()
67: {
68: return $this->separator;
69: }
70:
71:
72: 73: 74: 75:
76: public function getContainerPrototype()
77: {
78: return $this->container;
79: }
80:
81:
82: 83: 84: 85:
86: public function getItemLabelPrototype()
87: {
88: return $this->itemLabel;
89: }
90:
91:
92: 93: 94: 95:
96: public function getControl()
97: {
98: $input = parent::getControl();
99: $items = $this->getItems();
100: $ids = array();
101: if ($this->generateId) {
102: foreach ($items as $value => $label) {
103: $ids[$value] = $input->id . '-' . $value;
104: }
105: }
106:
107: return $this->container->setHtml(
108: Nette\Forms\Helpers::createInputList(
109: $this->translate($items),
110: array_merge($input->attrs, array(
111: 'id:' => $ids,
112: 'checked?' => $this->value,
113: 'disabled:' => $this->disabled,
114: 'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
115: )),
116: array('for:' => $ids) + $this->itemLabel->attrs,
117: $this->separator
118: )
119: );
120: }
121:
122:
123: 124: 125: 126: 127:
128: public function getLabel($caption = NULL)
129: {
130: return parent::getLabel($caption)->for(NULL);
131: }
132:
133:
134: 135: 136:
137: public function getControlPart($key)
138: {
139: return parent::getControl()->addAttributes(array(
140: 'id' => $this->getHtmlId() . '-' . $key,
141: 'checked' => in_array($key, (array) $this->value, TRUE),
142: 'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
143: 'value' => $key,
144: ));
145: }
146:
147:
148: 149: 150:
151: public function getLabelPart($key)
152: {
153: return parent::getLabel($this->items[$key])->for($this->getHtmlId() . '-' . $key);
154: }
155:
156: }
157: