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();
47: }
48:
49:
50: 51: 52: 53:
54: public function getControl()
55: {
56: $input = parent::getControl();
57: $items = $this->getItems();
58: $ids = array();
59: if ($this->generateId) {
60: foreach ($items as $value => $label) {
61: $ids[$value] = $input->id . '-' . $value;
62: }
63: }
64:
65: return $this->container->setHtml(
66: Nette\Forms\Helpers::createInputList(
67: $this->translate($items),
68: array_merge($input->attrs, array(
69: 'id:' => $ids,
70: 'checked?' => $this->value,
71: 'disabled:' => $this->disabled,
72: 'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
73: )),
74: array('for:' => $ids) + $this->itemLabel->attrs,
75: $this->separator
76: )
77: );
78: }
79:
80:
81: 82: 83: 84: 85:
86: public function getLabel($caption = NULL)
87: {
88: return parent::getLabel($caption)->for(NULL);
89: }
90:
91:
92: 93: 94:
95: public function getControlPart($key)
96: {
97: $key = key(array((string) $key => NULL));
98: return parent::getControl()->addAttributes(array(
99: 'id' => $this->getHtmlId() . '-' . $key,
100: 'checked' => in_array($key, (array) $this->value, TRUE),
101: 'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
102: 'value' => $key,
103: ));
104: }
105:
106:
107: 108: 109:
110: public function getLabelPart($key = NULL)
111: {
112: return func_num_args()
113: ? parent::getLabel($this->items[$key])->for($this->getHtmlId() . '-' . $key)
114: : $this->getLabel();
115: }
116:
117:
118: 119: 120: 121:
122: public function getSeparatorPrototype()
123: {
124: return $this->separator;
125: }
126:
127:
128: 129: 130: 131:
132: public function getContainerPrototype()
133: {
134: return $this->container;
135: }
136:
137:
138: 139: 140: 141:
142: public function getItemLabelPrototype()
143: {
144: return $this->itemLabel;
145: }
146:
147: }
148: