File indexing completed on 2024-12-22 05:36:42
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Form 0017 * @subpackage Element 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 /** Zend_Form_Element_Xhtml */ 0023 // require_once 'Zend/Form/Element/Xhtml.php'; 0024 0025 /** 0026 * Base class for multi-option form elements 0027 * 0028 * @category Zend 0029 * @package Zend_Form 0030 * @subpackage Element 0031 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0032 * @license http://framework.zend.com/license/new-bsd New BSD License 0033 * @version $Id$ 0034 */ 0035 abstract class Zend_Form_Element_Multi extends Zend_Form_Element_Xhtml 0036 { 0037 /** 0038 * Array of options for multi-item 0039 * @var array 0040 */ 0041 public $options = array(); 0042 0043 /** 0044 * Flag: autoregister inArray validator? 0045 * @var bool 0046 */ 0047 protected $_registerInArrayValidator = true; 0048 0049 /** 0050 * Separator to use between options; defaults to '<br />'. 0051 * @var string 0052 */ 0053 protected $_separator = '<br />'; 0054 0055 /** 0056 * Which values are translated already? 0057 * @var array 0058 */ 0059 protected $_translated = array(); 0060 0061 /** 0062 * Retrieve separator 0063 * 0064 * @return mixed 0065 */ 0066 public function getSeparator() 0067 { 0068 return $this->_separator; 0069 } 0070 0071 /** 0072 * Set separator 0073 * 0074 * @param mixed $separator 0075 * @return self 0076 */ 0077 public function setSeparator($separator) 0078 { 0079 $this->_separator = $separator; 0080 return $this; 0081 } 0082 0083 /** 0084 * Retrieve options array 0085 * 0086 * @return array 0087 */ 0088 protected function _getMultiOptions() 0089 { 0090 if (null === $this->options || !is_array($this->options)) { 0091 $this->options = array(); 0092 } 0093 0094 return $this->options; 0095 } 0096 0097 /** 0098 * Add an option 0099 * 0100 * @param string $option 0101 * @param string $value 0102 * @return Zend_Form_Element_Multi 0103 */ 0104 public function addMultiOption($option, $value = '') 0105 { 0106 $option = (string) $option; 0107 $this->_getMultiOptions(); 0108 if (!$this->_translateOption($option, $value)) { 0109 $this->options[$option] = $value; 0110 } 0111 0112 return $this; 0113 } 0114 0115 /** 0116 * Add many options at once 0117 * 0118 * @param array $options 0119 * @return Zend_Form_Element_Multi 0120 */ 0121 public function addMultiOptions(array $options) 0122 { 0123 foreach ($options as $option => $value) { 0124 if (is_array($value) 0125 && array_key_exists('key', $value) 0126 && array_key_exists('value', $value) 0127 ) { 0128 $this->addMultiOption($value['key'], $value['value']); 0129 } else { 0130 $this->addMultiOption($option, $value); 0131 } 0132 } 0133 return $this; 0134 } 0135 0136 /** 0137 * Set all options at once (overwrites) 0138 * 0139 * @param array $options 0140 * @return Zend_Form_Element_Multi 0141 */ 0142 public function setMultiOptions(array $options) 0143 { 0144 $this->clearMultiOptions(); 0145 return $this->addMultiOptions($options); 0146 } 0147 0148 /** 0149 * Retrieve single multi option 0150 * 0151 * @param string $option 0152 * @return mixed 0153 */ 0154 public function getMultiOption($option) 0155 { 0156 $option = (string) $option; 0157 $this->_getMultiOptions(); 0158 if (isset($this->options[$option])) { 0159 $this->_translateOption($option, $this->options[$option]); 0160 return $this->options[$option]; 0161 } 0162 0163 return null; 0164 } 0165 0166 /** 0167 * Retrieve options 0168 * 0169 * @return array 0170 */ 0171 public function getMultiOptions() 0172 { 0173 $this->_getMultiOptions(); 0174 foreach ($this->options as $option => $value) { 0175 $this->_translateOption($option, $value); 0176 } 0177 return $this->options; 0178 } 0179 0180 /** 0181 * Remove a single multi option 0182 * 0183 * @param string $option 0184 * @return bool 0185 */ 0186 public function removeMultiOption($option) 0187 { 0188 $option = (string) $option; 0189 $this->_getMultiOptions(); 0190 if (isset($this->options[$option])) { 0191 unset($this->options[$option]); 0192 if (isset($this->_translated[$option])) { 0193 unset($this->_translated[$option]); 0194 } 0195 return true; 0196 } 0197 0198 return false; 0199 } 0200 0201 /** 0202 * Clear all options 0203 * 0204 * @return Zend_Form_Element_Multi 0205 */ 0206 public function clearMultiOptions() 0207 { 0208 $this->options = array(); 0209 $this->_translated = array(); 0210 return $this; 0211 } 0212 0213 /** 0214 * Set flag indicating whether or not to auto-register inArray validator 0215 * 0216 * @param bool $flag 0217 * @return Zend_Form_Element_Multi 0218 */ 0219 public function setRegisterInArrayValidator($flag) 0220 { 0221 $this->_registerInArrayValidator = (bool) $flag; 0222 return $this; 0223 } 0224 0225 /** 0226 * Get status of auto-register inArray validator flag 0227 * 0228 * @return bool 0229 */ 0230 public function registerInArrayValidator() 0231 { 0232 return $this->_registerInArrayValidator; 0233 } 0234 0235 /** 0236 * Is the value provided valid? 0237 * 0238 * Autoregisters InArray validator if necessary. 0239 * 0240 * @param string $value 0241 * @param mixed $context 0242 * @return bool 0243 */ 0244 public function isValid($value, $context = null) 0245 { 0246 if ($this->registerInArrayValidator()) { 0247 if (!$this->getValidator('InArray')) { 0248 $multiOptions = $this->getMultiOptions(); 0249 $options = array(); 0250 0251 foreach ($multiOptions as $opt_value => $opt_label) { 0252 // optgroup instead of option label 0253 if (is_array($opt_label)) { 0254 $options = array_merge($options, array_keys($opt_label)); 0255 } 0256 else { 0257 $options[] = $opt_value; 0258 } 0259 } 0260 0261 $this->addValidator( 0262 'InArray', 0263 true, 0264 array($options) 0265 ); 0266 } 0267 } 0268 return parent::isValid($value, $context); 0269 } 0270 0271 /** 0272 * Translate an option 0273 * 0274 * @param string $option 0275 * @param string $value 0276 * @return bool 0277 */ 0278 protected function _translateOption($option, $value) 0279 { 0280 if ($this->translatorIsDisabled()) { 0281 return false; 0282 } 0283 0284 if (!isset($this->_translated[$option]) && !empty($value)) { 0285 $this->options[$option] = $this->_translateValue($value); 0286 if ($this->options[$option] === $value) { 0287 return false; 0288 } 0289 $this->_translated[$option] = true; 0290 return true; 0291 } 0292 0293 return false; 0294 } 0295 0296 /** 0297 * Translate a multi option value 0298 * 0299 * @param string $value 0300 * @return string 0301 */ 0302 protected function _translateValue($value) 0303 { 0304 if (is_array($value)) { 0305 foreach ($value as $key => $val) { 0306 $value[$key] = $this->_translateValue($val); 0307 } 0308 return $value; 0309 } else { 0310 if (null !== ($translator = $this->getTranslator())) { 0311 return $translator->translate($value); 0312 } 0313 0314 return $value; 0315 } 0316 } 0317 }