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