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 class Zend_Dojo_Form_Element_CheckBox extends Zend_Dojo_Form_Element_Dijit
0038 {
0039     /**
0040      * Is the checkbox checked?
0041      * @var bool
0042      */
0043     public $checked = false;
0044 
0045     /**
0046      * Use formCheckbox view helper by default
0047      * @var string
0048      */
0049     public $helper = 'CheckBox';
0050 
0051     /**
0052      * Options that will be passed to the view helper
0053      * @var array
0054      */
0055     public $options = array(
0056         'checkedValue'   => '1',
0057         'uncheckedValue' => '0',
0058     );
0059 
0060     /**
0061      * Value when checked
0062      * @var string
0063      */
0064     protected $_checkedValue = '1';
0065 
0066     /**
0067      * Value when not checked
0068      * @var string
0069      */
0070     protected $_uncheckedValue = '0';
0071 
0072     /**
0073      * Current value
0074      * @var string 0 or 1
0075      */
0076     protected $_value = '0';
0077 
0078     /**
0079      * Set options
0080      *
0081      * Intercept checked and unchecked values and set them early; test stored
0082      * value against checked and unchecked values after configuration.
0083      *
0084      * @param  array $options
0085      * @return Zend_Form_Element_Checkbox
0086      */
0087     public function setOptions(array $options)
0088     {
0089         if (array_key_exists('checkedValue', $options)) {
0090             $this->setCheckedValue($options['checkedValue']);
0091             unset($options['checkedValue']);
0092         }
0093         if (array_key_exists('uncheckedValue', $options)) {
0094             $this->setUncheckedValue($options['uncheckedValue']);
0095             unset($options['uncheckedValue']);
0096         }
0097         parent::setOptions($options);
0098 
0099         $curValue = $this->getValue();
0100         $test     = array($this->getCheckedValue(), $this->getUncheckedValue());
0101         if (!in_array($curValue, $test)) {
0102             $this->setValue($curValue);
0103         }
0104 
0105         return $this;
0106     }
0107 
0108     /**
0109      * Set value
0110      *
0111      * If value matches checked value, sets to that value, and sets the checked
0112      * flag to true.
0113      *
0114      * Any other value causes the unchecked value to be set as the current
0115      * value, and the checked flag to be set as false.
0116      *
0117      *
0118      * @param  mixed $value
0119      * @return Zend_Form_Element_Checkbox
0120      */
0121     public function setValue($value)
0122     {
0123         if ($value == $this->getCheckedValue()) {
0124             parent::setValue($value);
0125             $this->checked = true;
0126         } else {
0127             parent::setValue($this->getUncheckedValue());
0128             $this->checked = false;
0129         }
0130         return $this;
0131     }
0132 
0133     /**
0134      * Set checked value
0135      *
0136      * @param  string $value
0137      * @return Zend_Form_Element_Checkbox
0138      */
0139     public function setCheckedValue($value)
0140     {
0141         $this->_checkedValue = (string) $value;
0142         $this->options['checkedValue'] = $value;
0143         return $this;
0144     }
0145 
0146     /**
0147      * Get value when checked
0148      *
0149      * @return string
0150      */
0151     public function getCheckedValue()
0152     {
0153         return $this->_checkedValue;
0154     }
0155 
0156     /**
0157      * Set unchecked value
0158      *
0159      * @param  string $value
0160      * @return Zend_Form_Element_Checkbox
0161      */
0162     public function setUncheckedValue($value)
0163     {
0164         $this->_uncheckedValue = (string) $value;
0165         $this->options['uncheckedValue'] = $value;
0166         return $this;
0167     }
0168 
0169     /**
0170      * Get value when not checked
0171      *
0172      * @return string
0173      */
0174     public function getUncheckedValue()
0175     {
0176         return $this->_uncheckedValue;
0177     }
0178 
0179     /**
0180      * Set checked flag
0181      *
0182      * @param  bool $flag
0183      * @return Zend_Form_Element_Checkbox
0184      */
0185     public function setChecked($flag)
0186     {
0187         $this->checked = (bool) $flag;
0188         if ($this->checked) {
0189             $this->setValue($this->getCheckedValue());
0190         } else {
0191             $this->setValue($this->getUncheckedValue());
0192         }
0193         return $this;
0194     }
0195 
0196     /**
0197      * Get checked flag
0198      *
0199      * @return bool
0200      */
0201     public function isChecked()
0202     {
0203         return $this->checked;
0204     }
0205 }
0206