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