*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Dojo / Form / Element / DijitMulti.php
blob1af26ccd22e1c6a2b5f47f2cbca7533a664e47b3
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Dojo
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';
25 /**
26 * CheckBox dijit
28 * Note: this would be easier with mixins or traits...
30 * @uses Zend_Dojo_Form_Element_Dijit
31 * @package Zend_Dojo
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
39 /**
40 * Array of options for multi-item
41 * @var array
43 public $options = array();
45 /**
46 * Flag: autoregister inArray validator?
47 * @var bool
49 protected $_registerInArrayValidator = true;
51 /**
52 * Separator to use between options; defaults to '<br />'.
53 * @var string
55 protected $_separator = '<br />';
57 /**
58 * Which values are translated already?
59 * @var array
61 protected $_translated = array();
63 /**
64 * Retrieve separator
66 * @return mixed
68 public function getSeparator()
70 return $this->_separator;
73 /**
74 * Set separator
76 * @param mixed $separator
77 * @return self
79 public function setSeparator($separator)
81 $this->_separator = $separator;
82 return $this;
85 /**
86 * Retrieve options array
88 * @return array
90 protected function _getMultiOptions()
92 if (null === $this->options || !is_array($this->options)) {
93 $this->options = array();
96 return $this->options;
99 /**
100 * Add an option
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;
114 return $this;
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) {
126 if (is_array($value)
127 && array_key_exists('key', $value)
128 && array_key_exists('value', $value)
130 $this->addMultiOption($value['key'], $value['value']);
131 } else {
132 $this->addMultiOption($option, $value);
135 return $this;
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
154 * @return mixed
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];
165 return null;
169 * Retrieve options
171 * @return array
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
186 * @return bool
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]);
197 return true;
200 return false;
204 * Clear all options
206 * @return Zend_Form_Element_Multi
208 public function clearMultiOptions()
210 $this->options = array();
211 $this->_translated = array();
212 return $this;
216 * Set flag indicating whether or not to auto-register inArray validator
218 * @param bool $flag
219 * @return Zend_Form_Element_Multi
221 public function setRegisterInArrayValidator($flag)
223 $this->_registerInArrayValidator = (bool) $flag;
224 return $this;
228 * Get status of auto-register inArray validator flag
230 * @return bool
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
244 * @return bool
246 public function isValid($value, $context = null)
248 if ($this->registerInArrayValidator()) {
249 if (!$this->getValidator('InArray')) {
250 $options = $this->getMultiOptions();
251 $this->addValidator(
252 'InArray',
253 true,
254 array(array_keys($options))
258 return parent::isValid($value, $context);
262 * Translate an option
264 * @param string $option
265 * @param string $value
266 * @return bool
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) {
273 return false;
275 $this->_translated[$option] = true;
276 return true;
279 return false;
283 * Translate a value
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);
294 return $value;
295 } else {
296 if (null !== ($translator = $this->getTranslator())) {
297 if ($translator->isTranslated($value)) {
298 return $translator->translate($value);
301 return $value;