|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Form |
|
17 * @subpackage Element |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 */ |
|
21 |
|
22 /** Zend_Form_Element_Xhtml */ |
|
23 require_once 'Zend/Form/Element/Xhtml.php'; |
|
24 |
|
25 /** |
|
26 * Base class for multi-option form elements |
|
27 * |
|
28 * @category Zend |
|
29 * @package Zend_Form |
|
30 * @subpackage Element |
|
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 * @version $Id: Multi.php 22322 2010-05-30 11:12:57Z thomas $ |
|
34 */ |
|
35 abstract class Zend_Form_Element_Multi extends Zend_Form_Element_Xhtml |
|
36 { |
|
37 /** |
|
38 * Array of options for multi-item |
|
39 * @var array |
|
40 */ |
|
41 public $options = array(); |
|
42 |
|
43 /** |
|
44 * Flag: autoregister inArray validator? |
|
45 * @var bool |
|
46 */ |
|
47 protected $_registerInArrayValidator = true; |
|
48 |
|
49 /** |
|
50 * Separator to use between options; defaults to '<br />'. |
|
51 * @var string |
|
52 */ |
|
53 protected $_separator = '<br />'; |
|
54 |
|
55 /** |
|
56 * Which values are translated already? |
|
57 * @var array |
|
58 */ |
|
59 protected $_translated = array(); |
|
60 |
|
61 /** |
|
62 * Retrieve separator |
|
63 * |
|
64 * @return mixed |
|
65 */ |
|
66 public function getSeparator() |
|
67 { |
|
68 return $this->_separator; |
|
69 } |
|
70 |
|
71 /** |
|
72 * Set separator |
|
73 * |
|
74 * @param mixed $separator |
|
75 * @return self |
|
76 */ |
|
77 public function setSeparator($separator) |
|
78 { |
|
79 $this->_separator = $separator; |
|
80 return $this; |
|
81 } |
|
82 |
|
83 /** |
|
84 * Retrieve options array |
|
85 * |
|
86 * @return array |
|
87 */ |
|
88 protected function _getMultiOptions() |
|
89 { |
|
90 if (null === $this->options || !is_array($this->options)) { |
|
91 $this->options = array(); |
|
92 } |
|
93 |
|
94 return $this->options; |
|
95 } |
|
96 |
|
97 /** |
|
98 * Add an option |
|
99 * |
|
100 * @param string $option |
|
101 * @param string $value |
|
102 * @return Zend_Form_Element_Multi |
|
103 */ |
|
104 public function addMultiOption($option, $value = '') |
|
105 { |
|
106 $option = (string) $option; |
|
107 $this->_getMultiOptions(); |
|
108 if (!$this->_translateOption($option, $value)) { |
|
109 $this->options[$option] = $value; |
|
110 } |
|
111 |
|
112 return $this; |
|
113 } |
|
114 |
|
115 /** |
|
116 * Add many options at once |
|
117 * |
|
118 * @param array $options |
|
119 * @return Zend_Form_Element_Multi |
|
120 */ |
|
121 public function addMultiOptions(array $options) |
|
122 { |
|
123 foreach ($options as $option => $value) { |
|
124 if (is_array($value) |
|
125 && array_key_exists('key', $value) |
|
126 && array_key_exists('value', $value) |
|
127 ) { |
|
128 $this->addMultiOption($value['key'], $value['value']); |
|
129 } else { |
|
130 $this->addMultiOption($option, $value); |
|
131 } |
|
132 } |
|
133 return $this; |
|
134 } |
|
135 |
|
136 /** |
|
137 * Set all options at once (overwrites) |
|
138 * |
|
139 * @param array $options |
|
140 * @return Zend_Form_Element_Multi |
|
141 */ |
|
142 public function setMultiOptions(array $options) |
|
143 { |
|
144 $this->clearMultiOptions(); |
|
145 return $this->addMultiOptions($options); |
|
146 } |
|
147 |
|
148 /** |
|
149 * Retrieve single multi option |
|
150 * |
|
151 * @param string $option |
|
152 * @return mixed |
|
153 */ |
|
154 public function getMultiOption($option) |
|
155 { |
|
156 $option = (string) $option; |
|
157 $this->_getMultiOptions(); |
|
158 if (isset($this->options[$option])) { |
|
159 $this->_translateOption($option, $this->options[$option]); |
|
160 return $this->options[$option]; |
|
161 } |
|
162 |
|
163 return null; |
|
164 } |
|
165 |
|
166 /** |
|
167 * Retrieve options |
|
168 * |
|
169 * @return array |
|
170 */ |
|
171 public function getMultiOptions() |
|
172 { |
|
173 $this->_getMultiOptions(); |
|
174 foreach ($this->options as $option => $value) { |
|
175 $this->_translateOption($option, $value); |
|
176 } |
|
177 return $this->options; |
|
178 } |
|
179 |
|
180 /** |
|
181 * Remove a single multi option |
|
182 * |
|
183 * @param string $option |
|
184 * @return bool |
|
185 */ |
|
186 public function removeMultiOption($option) |
|
187 { |
|
188 $option = (string) $option; |
|
189 $this->_getMultiOptions(); |
|
190 if (isset($this->options[$option])) { |
|
191 unset($this->options[$option]); |
|
192 if (isset($this->_translated[$option])) { |
|
193 unset($this->_translated[$option]); |
|
194 } |
|
195 return true; |
|
196 } |
|
197 |
|
198 return false; |
|
199 } |
|
200 |
|
201 /** |
|
202 * Clear all options |
|
203 * |
|
204 * @return Zend_Form_Element_Multi |
|
205 */ |
|
206 public function clearMultiOptions() |
|
207 { |
|
208 $this->options = array(); |
|
209 $this->_translated = array(); |
|
210 return $this; |
|
211 } |
|
212 |
|
213 /** |
|
214 * Set flag indicating whether or not to auto-register inArray validator |
|
215 * |
|
216 * @param bool $flag |
|
217 * @return Zend_Form_Element_Multi |
|
218 */ |
|
219 public function setRegisterInArrayValidator($flag) |
|
220 { |
|
221 $this->_registerInArrayValidator = (bool) $flag; |
|
222 return $this; |
|
223 } |
|
224 |
|
225 /** |
|
226 * Get status of auto-register inArray validator flag |
|
227 * |
|
228 * @return bool |
|
229 */ |
|
230 public function registerInArrayValidator() |
|
231 { |
|
232 return $this->_registerInArrayValidator; |
|
233 } |
|
234 |
|
235 /** |
|
236 * Is the value provided valid? |
|
237 * |
|
238 * Autoregisters InArray validator if necessary. |
|
239 * |
|
240 * @param string $value |
|
241 * @param mixed $context |
|
242 * @return bool |
|
243 */ |
|
244 public function isValid($value, $context = null) |
|
245 { |
|
246 if ($this->registerInArrayValidator()) { |
|
247 if (!$this->getValidator('InArray')) { |
|
248 $multiOptions = $this->getMultiOptions(); |
|
249 $options = array(); |
|
250 |
|
251 foreach ($multiOptions as $opt_value => $opt_label) { |
|
252 // optgroup instead of option label |
|
253 if (is_array($opt_label)) { |
|
254 $options = array_merge($options, array_keys($opt_label)); |
|
255 } |
|
256 else { |
|
257 $options[] = $opt_value; |
|
258 } |
|
259 } |
|
260 |
|
261 $this->addValidator( |
|
262 'InArray', |
|
263 true, |
|
264 array($options) |
|
265 ); |
|
266 } |
|
267 } |
|
268 return parent::isValid($value, $context); |
|
269 } |
|
270 |
|
271 /** |
|
272 * Translate an option |
|
273 * |
|
274 * @param string $option |
|
275 * @param string $value |
|
276 * @return bool |
|
277 */ |
|
278 protected function _translateOption($option, $value) |
|
279 { |
|
280 if ($this->translatorIsDisabled()) { |
|
281 return false; |
|
282 } |
|
283 |
|
284 if (!isset($this->_translated[$option]) && !empty($value)) { |
|
285 $this->options[$option] = $this->_translateValue($value); |
|
286 if ($this->options[$option] === $value) { |
|
287 return false; |
|
288 } |
|
289 $this->_translated[$option] = true; |
|
290 return true; |
|
291 } |
|
292 |
|
293 return false; |
|
294 } |
|
295 |
|
296 /** |
|
297 * Translate a multi option value |
|
298 * |
|
299 * @param string $value |
|
300 * @return string |
|
301 */ |
|
302 protected function _translateValue($value) |
|
303 { |
|
304 if (is_array($value)) { |
|
305 foreach ($value as $key => $val) { |
|
306 $value[$key] = $this->_translateValue($val); |
|
307 } |
|
308 return $value; |
|
309 } else { |
|
310 if (null !== ($translator = $this->getTranslator())) { |
|
311 return $translator->translate($value); |
|
312 } |
|
313 |
|
314 return $value; |
|
315 } |
|
316 } |
|
317 } |