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