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