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