web/lib/Zend/View/Helper/FormCheckbox.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_View
       
    17  * @subpackage Helper
       
    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  * @version    $Id: FormCheckbox.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * Abstract class for extension
       
    26  */
       
    27 require_once 'Zend/View/Helper/FormElement.php';
       
    28 
       
    29 
       
    30 /**
       
    31  * Helper to generate a "checkbox" element
       
    32  *
       
    33  * @category   Zend
       
    34  * @package    Zend_View
       
    35  * @subpackage Helper
       
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    38  */
       
    39 class Zend_View_Helper_FormCheckbox extends Zend_View_Helper_FormElement
       
    40 {
       
    41     /**
       
    42      * Default checked/unchecked options
       
    43      * @var array
       
    44      */
       
    45     protected static $_defaultCheckedOptions = array(
       
    46         'checkedValue'   => '1',
       
    47         'uncheckedValue' => '0'
       
    48     );
       
    49 
       
    50     /**
       
    51      * Generates a 'checkbox' element.
       
    52      *
       
    53      * @access public
       
    54      *
       
    55      * @param string|array $name If a string, the element name.  If an
       
    56      * array, all other parameters are ignored, and the array elements
       
    57      * are extracted in place of added parameters.
       
    58      * @param mixed $value The element value.
       
    59      * @param array $attribs Attributes for the element tag.
       
    60      * @return string The element XHTML.
       
    61      */
       
    62     public function formCheckbox($name, $value = null, $attribs = null, array $checkedOptions = null)
       
    63     {
       
    64         $info = $this->_getInfo($name, $value, $attribs);
       
    65         extract($info); // name, id, value, attribs, options, listsep, disable
       
    66 
       
    67         $checked = false;
       
    68         if (isset($attribs['checked']) && $attribs['checked']) {
       
    69             $checked = true;
       
    70             unset($attribs['checked']);
       
    71         } elseif (isset($attribs['checked'])) {
       
    72             $checked = false;
       
    73             unset($attribs['checked']);
       
    74         }
       
    75 
       
    76         $checkedOptions = self::determineCheckboxInfo($value, $checked, $checkedOptions);
       
    77 
       
    78         // is the element disabled?
       
    79         $disabled = '';
       
    80         if ($disable) {
       
    81             $disabled = ' disabled="disabled"';
       
    82         }
       
    83 
       
    84         // XHTML or HTML end tag?
       
    85         $endTag = ' />';
       
    86         if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
       
    87             $endTag= '>';
       
    88         }
       
    89 
       
    90         // build the element
       
    91         $xhtml = '';
       
    92         if (!$disable && !strstr($name, '[]')) {
       
    93             $xhtml = $this->_hidden($name, $checkedOptions['uncheckedValue']);
       
    94         }
       
    95         $xhtml .= '<input type="checkbox"'
       
    96                 . ' name="' . $this->view->escape($name) . '"'
       
    97                 . ' id="' . $this->view->escape($id) . '"'
       
    98                 . ' value="' . $this->view->escape($checkedOptions['checkedValue']) . '"'
       
    99                 . $checkedOptions['checkedString']
       
   100                 . $disabled
       
   101                 . $this->_htmlAttribs($attribs)
       
   102                 . $endTag;
       
   103 
       
   104         return $xhtml;
       
   105     }
       
   106 
       
   107     /**
       
   108      * Determine checkbox information
       
   109      *
       
   110      * @param  string $value
       
   111      * @param  bool $checked
       
   112      * @param  array|null $checkedOptions
       
   113      * @return array
       
   114      */
       
   115     public static function determineCheckboxInfo($value, $checked, array $checkedOptions = null)
       
   116     {
       
   117         // Checked/unchecked values
       
   118         $checkedValue   = null;
       
   119         $uncheckedValue = null;
       
   120         if (is_array($checkedOptions)) {
       
   121             if (array_key_exists('checkedValue', $checkedOptions)) {
       
   122                 $checkedValue = (string) $checkedOptions['checkedValue'];
       
   123                 unset($checkedOptions['checkedValue']);
       
   124             }
       
   125             if (array_key_exists('uncheckedValue', $checkedOptions)) {
       
   126                 $uncheckedValue = (string) $checkedOptions['uncheckedValue'];
       
   127                 unset($checkedOptions['uncheckedValue']);
       
   128             }
       
   129             if (null === $checkedValue) {
       
   130                 $checkedValue = (string) array_shift($checkedOptions);
       
   131             }
       
   132             if (null === $uncheckedValue) {
       
   133                 $uncheckedValue = (string) array_shift($checkedOptions);
       
   134             }
       
   135         } elseif ($value !== null) {
       
   136             $uncheckedValue = self::$_defaultCheckedOptions['uncheckedValue'];
       
   137         } else {
       
   138             $checkedValue   = self::$_defaultCheckedOptions['checkedValue'];
       
   139             $uncheckedValue = self::$_defaultCheckedOptions['uncheckedValue'];
       
   140         }
       
   141 
       
   142         // is the element checked?
       
   143         $checkedString = '';
       
   144         if ($checked || ((string) $value === $checkedValue)) {
       
   145             $checkedString = ' checked="checked"';
       
   146             $checked = true;
       
   147         } else {
       
   148             $checked = false;
       
   149         }
       
   150 
       
   151         // Checked value should be value if no checked options provided
       
   152         if ($checkedValue == null) {
       
   153             $checkedValue = $value;
       
   154         }
       
   155 
       
   156         return array(
       
   157             'checked'        => $checked,
       
   158             'checkedString'  => $checkedString,
       
   159             'checkedValue'   => $checkedValue,
       
   160             'uncheckedValue' => $uncheckedValue,
       
   161         );
       
   162     }
       
   163 }