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