File indexing completed on 2025-03-02 05:29:19

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_Dojo
0017  * @subpackage Form_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_Dojo_Form_Element_Dijit */
0023 // require_once 'Zend/Dojo/Form/Element/Dijit.php';
0024 
0025 /**
0026  * CheckBox dijit
0027  *
0028  * Note: this would be easier with mixins or traits...
0029  *
0030  * @uses       Zend_Dojo_Form_Element_Dijit
0031  * @package    Zend_Dojo
0032  * @subpackage Form_Element
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  * @version    $Id$
0036  */
0037 abstract class Zend_Dojo_Form_Element_DijitMulti extends Zend_Dojo_Form_Element_Dijit
0038 {
0039     /**
0040      * Array of options for multi-item
0041      * @var array
0042      */
0043     public $options = array();
0044 
0045     /**
0046      * Flag: autoregister inArray validator?
0047      * @var bool
0048      */
0049     protected $_registerInArrayValidator = true;
0050 
0051     /**
0052      * Separator to use between options; defaults to '<br />'.
0053      * @var string
0054      */
0055     protected $_separator = '<br />';
0056 
0057     /**
0058      * Which values are translated already?
0059      * @var array
0060      */
0061     protected $_translated = array();
0062 
0063     /**
0064      * Retrieve separator
0065      *
0066      * @return mixed
0067      */
0068     public function getSeparator()
0069     {
0070         return $this->_separator;
0071     }
0072 
0073     /**
0074      * Set separator
0075      *
0076      * @param mixed $separator
0077      * @return self
0078      */
0079     public function setSeparator($separator)
0080     {
0081         $this->_separator = $separator;
0082         return $this;
0083     }
0084 
0085     /**
0086      * Retrieve options array
0087      *
0088      * @return array
0089      */
0090     protected function _getMultiOptions()
0091     {
0092         if (null === $this->options || !is_array($this->options)) {
0093             $this->options = array();
0094         }
0095 
0096         return $this->options;
0097     }
0098 
0099     /**
0100      * Add an option
0101      *
0102      * @param  string $option
0103      * @param  string $value
0104      * @return Zend_Form_Element_Multi
0105      */
0106     public function addMultiOption($option, $value = '')
0107     {
0108         $option  = (string) $option;
0109         $this->_getMultiOptions();
0110         if (!$this->_translateOption($option, $value)) {
0111             $this->options[$option] = $value;
0112         }
0113 
0114         return $this;
0115     }
0116 
0117     /**
0118      * Add many options at once
0119      *
0120      * @param  array $options
0121      * @return Zend_Form_Element_Multi
0122      */
0123     public function addMultiOptions(array $options)
0124     {
0125         foreach ($options as $option => $value) {
0126             if (is_array($value)
0127                 && array_key_exists('key', $value)
0128                 && array_key_exists('value', $value)
0129             ) {
0130                 $this->addMultiOption($value['key'], $value['value']);
0131             } else {
0132                 $this->addMultiOption($option, $value);
0133             }
0134         }
0135         return $this;
0136     }
0137 
0138     /**
0139      * Set all options at once (overwrites)
0140      *
0141      * @param  array $options
0142      * @return Zend_Form_Element_Multi
0143      */
0144     public function setMultiOptions(array $options)
0145     {
0146         $this->clearMultiOptions();
0147         return $this->addMultiOptions($options);
0148     }
0149 
0150     /**
0151      * Retrieve single multi option
0152      *
0153      * @param  string $option
0154      * @return mixed
0155      */
0156     public function getMultiOption($option)
0157     {
0158         $option  = (string) $option;
0159         $this->_getMultiOptions();
0160         if (isset($this->options[$option])) {
0161             $this->_translateOption($option, $this->options[$option]);
0162             return $this->options[$option];
0163         }
0164 
0165         return null;
0166     }
0167 
0168     /**
0169      * Retrieve options
0170      *
0171      * @return array
0172      */
0173     public function getMultiOptions()
0174     {
0175         $this->_getMultiOptions();
0176         foreach ($this->options as $option => $value) {
0177             $this->_translateOption($option, $value);
0178         }
0179         return $this->options;
0180     }
0181 
0182     /**
0183      * Remove a single multi option
0184      *
0185      * @param  string $option
0186      * @return bool
0187      */
0188     public function removeMultiOption($option)
0189     {
0190         $option  = (string) $option;
0191         $this->_getMultiOptions();
0192         if (isset($this->options[$option])) {
0193             unset($this->options[$option]);
0194             if (isset($this->_translated[$option])) {
0195                 unset($this->_translated[$option]);
0196             }
0197             return true;
0198         }
0199 
0200         return false;
0201     }
0202 
0203     /**
0204      * Clear all options
0205      *
0206      * @return Zend_Form_Element_Multi
0207      */
0208     public function clearMultiOptions()
0209     {
0210         $this->options = array();
0211         $this->_translated = array();
0212         return $this;
0213     }
0214 
0215     /**
0216      * Set flag indicating whether or not to auto-register inArray validator
0217      *
0218      * @param  bool $flag
0219      * @return Zend_Form_Element_Multi
0220      */
0221     public function setRegisterInArrayValidator($flag)
0222     {
0223         $this->_registerInArrayValidator = (bool) $flag;
0224         return $this;
0225     }
0226 
0227     /**
0228      * Get status of auto-register inArray validator flag
0229      *
0230      * @return bool
0231      */
0232     public function registerInArrayValidator()
0233     {
0234         return $this->_registerInArrayValidator;
0235     }
0236 
0237     /**
0238      * Is the value provided valid?
0239      *
0240      * Autoregisters InArray validator if necessary.
0241      *
0242      * @param  string $value
0243      * @param  mixed $context
0244      * @return bool
0245      */
0246     public function isValid($value, $context = null)
0247     {
0248         if ($this->registerInArrayValidator()) {
0249             if (!$this->getValidator('InArray')) {
0250                 $options = $this->getMultiOptions();
0251                 $this->addValidator(
0252                     'InArray',
0253                     true,
0254                     array(array_keys($options))
0255                 );
0256             }
0257         }
0258         return parent::isValid($value, $context);
0259     }
0260 
0261     /**
0262      * Translate an option
0263      *
0264      * @param  string $option
0265      * @param  string $value
0266      * @return bool
0267      */
0268     protected function _translateOption($option, $value)
0269     {
0270         if (!isset($this->_translated[$option])) {
0271             $this->options[$option] = $this->_translateValue($value);
0272             if ($this->options[$option] === $value) {
0273                 return false;
0274             }
0275             $this->_translated[$option] = true;
0276             return true;
0277         }
0278 
0279         return false;
0280     }
0281 
0282     /**
0283      * Translate a value
0284      *
0285      * @param  array|string $value
0286      * @return array|string
0287      */
0288     protected function _translateValue($value)
0289     {
0290         if (is_array($value)) {
0291             foreach ($value as $key => $val) {
0292                 $value[$key] = $this->_translateValue($val);
0293             }
0294             return $value;
0295         } else {
0296             if (null !== ($translator = $this->getTranslator())) {
0297                 return $translator->translate($value);
0298             }
0299 
0300             return $value;
0301         }
0302     }
0303 }