File indexing completed on 2024-06-16 05:30:05

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