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