1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Pry\Form\Element;
13:
14: 15: 16: 17: 18: 19: 20: 21:
22: class Select extends Multi
23: {
24:
25: 26: 27: 28: 29: 30:
31: private $name;
32:
33: 34: 35: 36:
37: private $jQuerymultiple;
38:
39: 40: 41: 42:
43: private $jQueryTexts;
44:
45: 46: 47: 48:
49: private $jQuerySelectedList;
50:
51: 52: 53: 54: 55: 56:
57: public function __construct($nom, $form)
58: {
59: parent::__construct($nom, $form);
60: $this->name = $nom;
61: $this->jQuerymultiple = false;
62: $this->jQueryTexts = array('checkAll' => 'Tout sélectionner',
63: 'unCheckAll' => 'Tout déselectionner',
64: 'selected' => '# sélectionnée(s)',
65: 'noneSelected' => 'Choisir...');
66: $this->jQuerySelectedList = 0;
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76:
77: public function multiple($bool = true, $size = 5)
78: {
79: if ($bool)
80: {
81: $this->attrs['multiple'] = 'multiple';
82: $this->attrs['name'] = $this->attrs['name'] . '[]';
83: $this->attrs['size'] = $size;
84: }
85: else
86: {
87: unset($this->attrs['multiple']);
88: unset($this->attrs['size']);
89: }
90:
91: return $this;
92: }
93:
94: 95: 96: 97: 98:
99: public function enableJQPlugin($bool = true)
100: {
101: $this->jQuerymultiple = $bool;
102: return $this;
103: }
104:
105: 106: 107: 108: 109: 110:
111: public function translateJQPlugin(array $texts)
112: {
113: if (is_array($texts))
114: array_merge($this->jQueryTexts, $texts);
115: else
116: throw new InvalidArgumentException('La traduction doit se faire via un array');
117:
118: return $this;
119: }
120:
121: 122: 123: 124: 125: 126:
127: public function setJQSelectedList($num)
128: {
129: $this->jQuerySelectedList = intval($num);
130: return $this;
131: }
132:
133: 134: 135: 136: 137: 138:
139: public function __toString()
140: {
141: $css = $this->cssClass();
142: $label = '';
143: $options = '';
144:
145: if (!empty($this->label))
146: {
147: $label = "\t" . '<label for="' . $this->attrs['id'] . '" class="' . $this->cssLabel . '">' . $this->label . '</label>' . "\n";
148: if ($this->labelNewLine)
149: $label.="\t" . '<br />' . "\n";
150: }
151: $attributs = $this->attrsToString();
152:
153: $value = $this->form->getPostedvalue($this->name);
154: if ($value == '')
155: $value = $this->value;
156:
157: foreach ($this->choix as $itemHtml => $itemAffichage) {
158: if (is_array($itemAffichage))
159: {
160: $options.="\t\t" . '<optgroup label="' . $itemHtml . '">' . "\n";
161: foreach ($itemAffichage as $cle => $valeur) {
162: if (isset($this->attrs['multiple']) && is_array($value))
163: {
164: if (in_array($cle, $value))
165: $selected = 'selected="selected"';
166: else
167: $selected = '';
168: }
169: else
170: {
171: if ($value == $cle)
172: $selected = 'selected="selected"';
173: else
174: $selected = '';
175: }
176:
177: $options .="\t\t" . '<option value="' . htmlspecialchars($cle) . '" ' . $selected . '>' . $valeur . '</option>' . "\n";
178: }
179: $options.="\t\t" . '</optgroup>' . "\n";
180: }
181: else
182: {
183: if (isset($this->attrs['multiple']) && is_array($value))
184: {
185: if (in_array($itemHtml, $value))
186: $selected = 'selected="selected"';
187: else
188: $selected = '';
189: }
190: else
191: {
192: if ($value == $itemHtml)
193: $selected = 'selected="selected"';
194: else
195: $selected = '';
196: }
197:
198: $options .="\t\t" . '<option value="' . htmlspecialchars($itemHtml) . '" ' . $selected . '>' . $itemAffichage . '</option>' . "\n";
199: }
200: }
201: $error = '';
202: if (!is_null($this->errorMsg))
203: {
204: $error = '<span class="' . $this->errorClass . '">' . $this->errorMsg . '</span><br />';
205: }
206: $field = "\t" . '<select ' . $attributs . ' ' . $css . ' >' . "\n" . $options . "\n\t" . '</select>' . "\n";
207: if (!empty($this->info))
208: $field.="\t" . '<img src="' . $this->imgInfo . '" id="' . $this->attrs['name'] . '_tooltip" class="form_tooltip" title="' . $this->info . '" alt="" style="cursor:help;" />' . "\n";
209: if ($this->fieldNewLine)
210: $field.="\t" . '<br />' . "\n";
211:
212: if ($this->jQuerymultiple)
213: {
214: $this->form->javascript .= '$(\'#' . $this->attrs['id'] . '\').multiSelect({selectedList:' . $this->jQuerySelectedList . ',
215: checkAllText:\'' . $this->jQueryTexts['checkAll'] . '\',
216: unCheckAllText:\'' . $this->jQueryTexts['unCheckAll'] . '\',
217: selectedText:\'' . $this->jQueryTexts['selected'] . '\',
218: noneSelected:\'' . $this->jQueryTexts['noneSelected'] . '\'});';
219: }
220: return $label . $field . $error;
221: }
222:
223: }
224:
225: ?>