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.
17 * @subpackage Form_Element
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Dojo_Form_Element_Dijit */
23 require_once 'Zend/Dojo/Form/Element/Dijit.php';
28 * Note: this would be easier with mixins or traits...
30 * @uses Zend_Dojo_Form_Element_Dijit
32 * @subpackage Form_Element
33 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
35 * @version $Id: DijitMulti.php 16204 2009-06-21 18:58:29Z thomas $
37 abstract class Zend_Dojo_Form_Element_DijitMulti
extends Zend_Dojo_Form_Element_Dijit
40 * Array of options for multi-item
43 public $options = array();
46 * Flag: autoregister inArray validator?
49 protected $_registerInArrayValidator = true;
52 * Separator to use between options; defaults to '<br />'.
55 protected $_separator = '<br />';
58 * Which values are translated already?
61 protected $_translated = array();
68 public function getSeparator()
70 return $this->_separator
;
76 * @param mixed $separator
79 public function setSeparator($separator)
81 $this->_separator
= $separator;
86 * Retrieve options array
90 protected function _getMultiOptions()
92 if (null === $this->options ||
!is_array($this->options
)) {
93 $this->options
= array();
96 return $this->options
;
102 * @param string $option
103 * @param string $value
104 * @return Zend_Form_Element_Multi
106 public function addMultiOption($option, $value = '')
108 $option = (string) $option;
109 $this->_getMultiOptions();
110 if (!$this->_translateOption($option, $value)) {
111 $this->options
[$option] = $value;
118 * Add many options at once
120 * @param array $options
121 * @return Zend_Form_Element_Multi
123 public function addMultiOptions(array $options)
125 foreach ($options as $option => $value) {
127 && array_key_exists('key', $value)
128 && array_key_exists('value', $value)
130 $this->addMultiOption($value['key'], $value['value']);
132 $this->addMultiOption($option, $value);
139 * Set all options at once (overwrites)
141 * @param array $options
142 * @return Zend_Form_Element_Multi
144 public function setMultiOptions(array $options)
146 $this->clearMultiOptions();
147 return $this->addMultiOptions($options);
151 * Retrieve single multi option
153 * @param string $option
156 public function getMultiOption($option)
158 $option = (string) $option;
159 $this->_getMultiOptions();
160 if (isset($this->options
[$option])) {
161 $this->_translateOption($option, $this->options
[$option]);
162 return $this->options
[$option];
173 public function getMultiOptions()
175 $this->_getMultiOptions();
176 foreach ($this->options
as $option => $value) {
177 $this->_translateOption($option, $value);
179 return $this->options
;
183 * Remove a single multi option
185 * @param string $option
188 public function removeMultiOption($option)
190 $option = (string) $option;
191 $this->_getMultiOptions();
192 if (isset($this->options
[$option])) {
193 unset($this->options
[$option]);
194 if (isset($this->_translated
[$option])) {
195 unset($this->_translated
[$option]);
206 * @return Zend_Form_Element_Multi
208 public function clearMultiOptions()
210 $this->options
= array();
211 $this->_translated
= array();
216 * Set flag indicating whether or not to auto-register inArray validator
219 * @return Zend_Form_Element_Multi
221 public function setRegisterInArrayValidator($flag)
223 $this->_registerInArrayValidator
= (bool) $flag;
228 * Get status of auto-register inArray validator flag
232 public function registerInArrayValidator()
234 return $this->_registerInArrayValidator
;
238 * Is the value provided valid?
240 * Autoregisters InArray validator if necessary.
242 * @param string $value
243 * @param mixed $context
246 public function isValid($value, $context = null)
248 if ($this->registerInArrayValidator()) {
249 if (!$this->getValidator('InArray')) {
250 $options = $this->getMultiOptions();
254 array(array_keys($options))
258 return parent
::isValid($value, $context);
262 * Translate an option
264 * @param string $option
265 * @param string $value
268 protected function _translateOption($option, $value)
270 if (!isset($this->_translated
[$option])) {
271 $this->options
[$option] = $this->_translateValue($value);
272 if ($this->options
[$option] === $value) {
275 $this->_translated
[$option] = true;
285 * @param array|string $value
286 * @return array|string
288 protected function _translateValue($value)
290 if (is_array($value)) {
291 foreach ($value as $key => $val) {
292 $value[$key] = $this->_translateValue($val);
296 if (null !== ($translator = $this->getTranslator())) {
297 if ($translator->isTranslated($value)) {
298 return $translator->translate($value);