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: public function __construct($label = NULL, array $items = NULL)
28: {
29: parent::__construct($label, $items);
30: $this->control->type = 'checkbox';
31: $this->separator = Html::el('br');
32: }
33:
34:
35: 36: 37: 38:
39: public function getControl()
40: {
41: $items = $this->getItems();
42: reset($items);
43: $input = parent::getControl();
44: return Nette\Forms\Helpers::createInputList(
45: $this->translate($items),
46: array_merge($input->attrs, array(
47: 'id' => NULL,
48: 'checked?' => $this->value,
49: 'disabled:' => $this->disabled,
50: 'required' => NULL,
51: 'data-nette-rules:' => array(key($items) => $input->attrs['data-nette-rules']),
52: )),
53: $this->label->attrs,
54: $this->separator
55: );
56: }
57:
58:
59: 60: 61: 62: 63:
64: public function getLabel($caption = NULL)
65: {
66: return parent::getLabel($caption)->for(NULL);
67: }
68:
69:
70: 71: 72: 73:
74: public function getSeparatorPrototype()
75: {
76: return $this->separator;
77: }
78:
79:
80: 81: 82:
83: public function getControlPart($key)
84: {
85: $key = key(array((string) $key => NULL));
86: return parent::getControl()->addAttributes(array(
87: 'id' => $this->getHtmlId() . '-' . $key,
88: 'checked' => in_array($key, (array) $this->value, TRUE),
89: 'disabled' => is_array($this->disabled) ? isset($this->disabled[$key]) : $this->disabled,
90: 'required' => NULL,
91: 'value' => $key,
92: ));
93: }
94:
95:
96: 97: 98:
99: public function getLabelPart($key)
100: {
101: return parent::getLabel($this->items[$key])->for($this->getHtmlId() . '-' . $key);
102: }
103:
104: }
105: