1: <?php
2:
3: namespace Zippy\Html\Form;
4:
5: /**
6: * Выводит список чекеров
7: */
8: class CheckBoxList extends HtmlFormDataElement
9: {
10: public $delimiter = " ";
11: public $list = array();
12: protected $event;
13: public $selectedvalue;
14:
15: /**
16: * Конструктор
17: * @param string ID компонента
18: * @param string Разделитель между ссылками
19: */
20: public function __construct($id, $delimiter = '') {
21: parent::__construct($id);
22: $this->delimiter = $delimiter;
23: }
24:
25: /**
26: * @see HtmlComponent
27: */
28: public function RenderImpl() {
29: // $url = $this->owner->getURLNode()."::".$this->id ;
30:
31: $out = "";
32: foreach ($this->list as $id => $item) {
33:
34: $attributes = "";
35: foreach ($item['attributes'] as $key => $value) {
36: $attributes = $attributes . $key . "=\"{$value}\" ";
37: }
38: $checked = $item['value'] === true ? ' checked="on"' : '';
39:
40: $out .= $this->RenderItem($this->id . '::' . $id, $checked, $item['caption'], $attributes, $this->delimiter);
41: }
42: $out = substr($out, 0, strlen($out) - strlen($this->delimiter));
43:
44: $this->getTag()->html($out);
45: }
46:
47: /**
48: * Рендерит чекбокс. Мжет быть перегружен для кастомного рендеринга.
49: *
50: * @param mixed $name
51: * @param mixed $checked
52: * @param mixed $caption
53: * @param mixed $attr
54: * @param mixed $delimiter
55: */
56: public function RenderItem($name, $checked, $caption = "", $attr = "", $delimiter = "") {
57: return "<nobr><input type=\"checkbox\" name=\"{$name}\" {$attr} {$checked} /> {$caption}</nobr>{$delimiter}";
58: }
59:
60: /**
61: * Добавить чекер в список
62: * @param mixed Номер чекера
63: * @param boolean Значение
64: * @param string Текст возле чекера
65: * @param array Список аттрибутов
66: */
67: public function AddCheckBox($itemid, $value, $caption, $attributes = array()) {
68: $this->list[$itemid] = array('value' => $value, 'caption' => $caption, 'attributes' => $attributes);
69: }
70:
71: /**
72: * @see SubmitDataRequest
73: */
74: public function getRequestData() {
75: foreach ($this->list as $id => $item) {
76: $this->list[$id]['value'] = isset($_REQUEST[$this->id . '::' . $id]);
77: }
78: }
79:
80: /**
81: * Устанавливает состояние чекера
82: * @param mixed номер в списке
83: * @param mixed Состояние
84: */
85: public function setChecked($id, $state) {
86: $this->list[$id]['value'] = $state;
87: }
88:
89: /**
90: * Устанавливает состояние всех чекеров в списке
91: * @param mixed Состояние
92: */
93: public function setAllChecked($state) {
94: foreach ($this->list as $id => $item) {
95: $this->list[$id]['value'] = $state;
96: }
97: }
98:
99: /**
100: * Проверка состояния чекера
101: * @param mixed Номер в списке
102: */
103: public function isChecked($id) {
104: return $this->list[$id]['value'];
105: }
106:
107: /**
108: * Список номеров отмеченых чекеров
109: * @return array
110: */
111: public function getCheckedList() {
112: $ids = array();
113: foreach ($this->list as $id => $item) {
114: if ($this->list[$id]['value'] === true) {
115: $ids[] = $id;
116: }
117: }
118: return $ids;
119: }
120:
121:
122: public function clean() {
123: $this->setAllChecked(false);
124: }
125: }
126: