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