web/lib/Zend/Form/Element/Checkbox.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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_Form
       
    17  * @subpackage 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_Form_Element_Xhtml */
       
    23 require_once 'Zend/Form/Element/Xhtml.php';
       
    24 
       
    25 /**
       
    26  * Checkbox form element
       
    27  *
       
    28  * @category   Zend
       
    29  * @package    Zend_Form
       
    30  * @subpackage Element
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  * @version    $Id: Checkbox.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    34  */
       
    35 class Zend_Form_Element_Checkbox extends Zend_Form_Element_Xhtml
       
    36 {
       
    37     /**
       
    38      * Is the checkbox checked?
       
    39      * @var bool
       
    40      */
       
    41     public $checked = false;
       
    42 
       
    43     /**
       
    44      * Use formCheckbox view helper by default
       
    45      * @var string
       
    46      */
       
    47     public $helper = 'formCheckbox';
       
    48 
       
    49     /**
       
    50      * Options that will be passed to the view helper
       
    51      * @var array
       
    52      */
       
    53     public $options = array(
       
    54         'checkedValue'   => '1',
       
    55         'uncheckedValue' => '0',
       
    56     );
       
    57 
       
    58     /**
       
    59      * Value when checked
       
    60      * @var string
       
    61      */
       
    62     protected $_checkedValue = '1';
       
    63 
       
    64     /**
       
    65      * Value when not checked
       
    66      * @var string
       
    67      */
       
    68     protected $_uncheckedValue = '0';
       
    69 
       
    70     /**
       
    71      * Current value
       
    72      * @var string 0 or 1
       
    73      */
       
    74     protected $_value = '0';
       
    75 
       
    76     /**
       
    77      * Set options
       
    78      *
       
    79      * Intercept checked and unchecked values and set them early; test stored
       
    80      * value against checked and unchecked values after configuration.
       
    81      *
       
    82      * @param  array $options
       
    83      * @return Zend_Form_Element_Checkbox
       
    84      */
       
    85     public function setOptions(array $options)
       
    86     {
       
    87         if (array_key_exists('checkedValue', $options)) {
       
    88             $this->setCheckedValue($options['checkedValue']);
       
    89             unset($options['checkedValue']);
       
    90         }
       
    91         if (array_key_exists('uncheckedValue', $options)) {
       
    92             $this->setUncheckedValue($options['uncheckedValue']);
       
    93             unset($options['uncheckedValue']);
       
    94         }
       
    95         parent::setOptions($options);
       
    96 
       
    97         $curValue = $this->getValue();
       
    98         $test     = array($this->getCheckedValue(), $this->getUncheckedValue());
       
    99         if (!in_array($curValue, $test)) {
       
   100             $this->setValue($curValue);
       
   101         }
       
   102 
       
   103         return $this;
       
   104     }
       
   105 
       
   106     /**
       
   107      * Set value
       
   108      *
       
   109      * If value matches checked value, sets to that value, and sets the checked
       
   110      * flag to true.
       
   111      *
       
   112      * Any other value causes the unchecked value to be set as the current
       
   113      * value, and the checked flag to be set as false.
       
   114      *
       
   115      *
       
   116      * @param  mixed $value
       
   117      * @return Zend_Form_Element_Checkbox
       
   118      */
       
   119     public function setValue($value)
       
   120     {
       
   121         if ($value == $this->getCheckedValue()) {
       
   122             parent::setValue($value);
       
   123             $this->checked = true;
       
   124         } else {
       
   125             parent::setValue($this->getUncheckedValue());
       
   126             $this->checked = false;
       
   127         }
       
   128         return $this;
       
   129     }
       
   130 
       
   131     /**
       
   132      * Set checked value
       
   133      *
       
   134      * @param  string $value
       
   135      * @return Zend_Form_Element_Checkbox
       
   136      */
       
   137     public function setCheckedValue($value)
       
   138     {
       
   139         $this->_checkedValue = (string) $value;
       
   140         $this->options['checkedValue'] = $value;
       
   141         return $this;
       
   142     }
       
   143 
       
   144     /**
       
   145      * Get value when checked
       
   146      *
       
   147      * @return string
       
   148      */
       
   149     public function getCheckedValue()
       
   150     {
       
   151         return $this->_checkedValue;
       
   152     }
       
   153 
       
   154     /**
       
   155      * Set unchecked value
       
   156      *
       
   157      * @param  string $value
       
   158      * @return Zend_Form_Element_Checkbox
       
   159      */
       
   160     public function setUncheckedValue($value)
       
   161     {
       
   162         $this->_uncheckedValue = (string) $value;
       
   163         $this->options['uncheckedValue'] = $value;
       
   164         return $this;
       
   165     }
       
   166 
       
   167     /**
       
   168      * Get value when not checked
       
   169      *
       
   170      * @return string
       
   171      */
       
   172     public function getUncheckedValue()
       
   173     {
       
   174         return $this->_uncheckedValue;
       
   175     }
       
   176 
       
   177     /**
       
   178      * Set checked flag
       
   179      *
       
   180      * @param  bool $flag
       
   181      * @return Zend_Form_Element_Checkbox
       
   182      */
       
   183     public function setChecked($flag)
       
   184     {
       
   185         $this->checked = (bool) $flag;
       
   186         if ($this->checked) {
       
   187             $this->setValue($this->getCheckedValue());
       
   188         } else {
       
   189             $this->setValue($this->getUncheckedValue());
       
   190         }
       
   191         return $this;
       
   192     }
       
   193 
       
   194     /**
       
   195      * Get checked flag
       
   196      *
       
   197      * @return bool
       
   198      */
       
   199     public function isChecked()
       
   200     {
       
   201         return $this->checked;
       
   202     }
       
   203 }