web/enmi/Zend/Dojo/Form/Element/DijitMulti.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     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_Dojo
       
    17  * @subpackage Form_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_Dojo_Form_Element_Dijit */
       
    23 require_once 'Zend/Dojo/Form/Element/Dijit.php';
       
    24 
       
    25 /**
       
    26  * CheckBox dijit
       
    27  *
       
    28  * Note: this would be easier with mixins or traits...
       
    29  *
       
    30  * @uses       Zend_Dojo_Form_Element_Dijit
       
    31  * @package    Zend_Dojo
       
    32  * @subpackage Form_Element
       
    33  * @copyright  Copyright (c) 2005-2010 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 22322 2010-05-30 11:12:57Z thomas $
       
    36  */
       
    37 abstract class Zend_Dojo_Form_Element_DijitMulti extends Zend_Dojo_Form_Element_Dijit
       
    38 {
       
    39     /**
       
    40      * Array of options for multi-item
       
    41      * @var array
       
    42      */
       
    43     public $options = array();
       
    44 
       
    45     /**
       
    46      * Flag: autoregister inArray validator?
       
    47      * @var bool
       
    48      */
       
    49     protected $_registerInArrayValidator = true;
       
    50 
       
    51     /**
       
    52      * Separator to use between options; defaults to '<br />'.
       
    53      * @var string
       
    54      */
       
    55     protected $_separator = '<br />';
       
    56 
       
    57     /**
       
    58      * Which values are translated already?
       
    59      * @var array
       
    60      */
       
    61     protected $_translated = array();
       
    62 
       
    63     /**
       
    64      * Retrieve separator
       
    65      *
       
    66      * @return mixed
       
    67      */
       
    68     public function getSeparator()
       
    69     {
       
    70         return $this->_separator;
       
    71     }
       
    72 
       
    73     /**
       
    74      * Set separator
       
    75      *
       
    76      * @param mixed $separator
       
    77      * @return self
       
    78      */
       
    79     public function setSeparator($separator)
       
    80     {
       
    81         $this->_separator = $separator;
       
    82         return $this;
       
    83     }
       
    84 
       
    85     /**
       
    86      * Retrieve options array
       
    87      *
       
    88      * @return array
       
    89      */
       
    90     protected function _getMultiOptions()
       
    91     {
       
    92         if (null === $this->options || !is_array($this->options)) {
       
    93             $this->options = array();
       
    94         }
       
    95 
       
    96         return $this->options;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Add an option
       
   101      *
       
   102      * @param  string $option
       
   103      * @param  string $value
       
   104      * @return Zend_Form_Element_Multi
       
   105      */
       
   106     public function addMultiOption($option, $value = '')
       
   107     {
       
   108         $option  = (string) $option;
       
   109         $this->_getMultiOptions();
       
   110         if (!$this->_translateOption($option, $value)) {
       
   111             $this->options[$option] = $value;
       
   112         }
       
   113 
       
   114         return $this;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Add many options at once
       
   119      *
       
   120      * @param  array $options
       
   121      * @return Zend_Form_Element_Multi
       
   122      */
       
   123     public function addMultiOptions(array $options)
       
   124     {
       
   125         foreach ($options as $option => $value) {
       
   126             if (is_array($value)
       
   127                 && array_key_exists('key', $value)
       
   128                 && array_key_exists('value', $value)
       
   129             ) {
       
   130                 $this->addMultiOption($value['key'], $value['value']);
       
   131             } else {
       
   132                 $this->addMultiOption($option, $value);
       
   133             }
       
   134         }
       
   135         return $this;
       
   136     }
       
   137 
       
   138     /**
       
   139      * Set all options at once (overwrites)
       
   140      *
       
   141      * @param  array $options
       
   142      * @return Zend_Form_Element_Multi
       
   143      */
       
   144     public function setMultiOptions(array $options)
       
   145     {
       
   146         $this->clearMultiOptions();
       
   147         return $this->addMultiOptions($options);
       
   148     }
       
   149 
       
   150     /**
       
   151      * Retrieve single multi option
       
   152      *
       
   153      * @param  string $option
       
   154      * @return mixed
       
   155      */
       
   156     public function getMultiOption($option)
       
   157     {
       
   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];
       
   163         }
       
   164 
       
   165         return null;
       
   166     }
       
   167 
       
   168     /**
       
   169      * Retrieve options
       
   170      *
       
   171      * @return array
       
   172      */
       
   173     public function getMultiOptions()
       
   174     {
       
   175         $this->_getMultiOptions();
       
   176         foreach ($this->options as $option => $value) {
       
   177             $this->_translateOption($option, $value);
       
   178         }
       
   179         return $this->options;
       
   180     }
       
   181 
       
   182     /**
       
   183      * Remove a single multi option
       
   184      *
       
   185      * @param  string $option
       
   186      * @return bool
       
   187      */
       
   188     public function removeMultiOption($option)
       
   189     {
       
   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]);
       
   196             }
       
   197             return true;
       
   198         }
       
   199 
       
   200         return false;
       
   201     }
       
   202 
       
   203     /**
       
   204      * Clear all options
       
   205      *
       
   206      * @return Zend_Form_Element_Multi
       
   207      */
       
   208     public function clearMultiOptions()
       
   209     {
       
   210         $this->options = array();
       
   211         $this->_translated = array();
       
   212         return $this;
       
   213     }
       
   214 
       
   215     /**
       
   216      * Set flag indicating whether or not to auto-register inArray validator
       
   217      *
       
   218      * @param  bool $flag
       
   219      * @return Zend_Form_Element_Multi
       
   220      */
       
   221     public function setRegisterInArrayValidator($flag)
       
   222     {
       
   223         $this->_registerInArrayValidator = (bool) $flag;
       
   224         return $this;
       
   225     }
       
   226 
       
   227     /**
       
   228      * Get status of auto-register inArray validator flag
       
   229      *
       
   230      * @return bool
       
   231      */
       
   232     public function registerInArrayValidator()
       
   233     {
       
   234         return $this->_registerInArrayValidator;
       
   235     }
       
   236 
       
   237     /**
       
   238      * Is the value provided valid?
       
   239      *
       
   240      * Autoregisters InArray validator if necessary.
       
   241      *
       
   242      * @param  string $value
       
   243      * @param  mixed $context
       
   244      * @return bool
       
   245      */
       
   246     public function isValid($value, $context = null)
       
   247     {
       
   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))
       
   255                 );
       
   256             }
       
   257         }
       
   258         return parent::isValid($value, $context);
       
   259     }
       
   260 
       
   261     /**
       
   262      * Translate an option
       
   263      *
       
   264      * @param  string $option
       
   265      * @param  string $value
       
   266      * @return bool
       
   267      */
       
   268     protected function _translateOption($option, $value)
       
   269     {
       
   270         if (!isset($this->_translated[$option])) {
       
   271             $this->options[$option] = $this->_translateValue($value);
       
   272             if ($this->options[$option] === $value) {
       
   273                 return false;
       
   274             }
       
   275             $this->_translated[$option] = true;
       
   276             return true;
       
   277         }
       
   278 
       
   279         return false;
       
   280     }
       
   281 
       
   282     /**
       
   283      * Translate a value
       
   284      *
       
   285      * @param  array|string $value
       
   286      * @return array|string
       
   287      */
       
   288     protected function _translateValue($value)
       
   289     {
       
   290         if (is_array($value)) {
       
   291             foreach ($value as $key => $val) {
       
   292                 $value[$key] = $this->_translateValue($val);
       
   293             }
       
   294             return $value;
       
   295         } else {
       
   296             if (null !== ($translator = $this->getTranslator())) {
       
   297                 return $translator->translate($value);
       
   298             }
       
   299 
       
   300             return $value;
       
   301         }
       
   302     }
       
   303 }