File indexing completed on 2024-12-22 05:37:13
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_View 0017 * @subpackage Helper 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 * @version $Id$ 0021 */ 0022 0023 0024 /** 0025 * Abstract class for extension 0026 */ 0027 // require_once 'Zend/View/Helper/FormElement.php'; 0028 0029 0030 /** 0031 * Helper to generate a "checkbox" element 0032 * 0033 * @category Zend 0034 * @package Zend_View 0035 * @subpackage Helper 0036 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0037 * @license http://framework.zend.com/license/new-bsd New BSD License 0038 */ 0039 class Zend_View_Helper_FormCheckbox extends Zend_View_Helper_FormElement 0040 { 0041 /** 0042 * Default checked/unchecked options 0043 * @var array 0044 */ 0045 protected static $_defaultCheckedOptions = array( 0046 'checkedValue' => '1', 0047 'uncheckedValue' => '0' 0048 ); 0049 0050 /** 0051 * Generates a 'checkbox' element. 0052 * 0053 * @access public 0054 * 0055 * @param string|array $name If a string, the element name. If an 0056 * array, all other parameters are ignored, and the array elements 0057 * are extracted in place of added parameters. 0058 * @param mixed $value The element value. 0059 * @param array $attribs Attributes for the element tag. 0060 * @return string The element XHTML. 0061 */ 0062 public function formCheckbox($name, $value = null, $attribs = null, array $checkedOptions = null) 0063 { 0064 $info = $this->_getInfo($name, $value, $attribs); 0065 extract($info); // name, id, value, attribs, options, listsep, disable 0066 0067 $checked = false; 0068 if (isset($attribs['checked']) && $attribs['checked']) { 0069 $checked = true; 0070 unset($attribs['checked']); 0071 } elseif (isset($attribs['checked'])) { 0072 $checked = false; 0073 unset($attribs['checked']); 0074 } 0075 0076 $checkedOptions = self::determineCheckboxInfo($value, $checked, $checkedOptions); 0077 0078 // is the element disabled? 0079 $disabled = ''; 0080 if ($disable) { 0081 $disabled = ' disabled="disabled"'; 0082 } 0083 0084 // build the element 0085 $xhtml = ''; 0086 if ((!$disable && !strstr($name, '[]')) 0087 && (empty($attribs['disableHidden']) || !$attribs['disableHidden']) 0088 ) { 0089 $xhtml = $this->_hidden($name, $checkedOptions['uncheckedValue']); 0090 } 0091 0092 if (array_key_exists('disableHidden', $attribs)) { 0093 unset($attribs['disableHidden']); 0094 } 0095 0096 $xhtml .= '<input type="checkbox"' 0097 . ' name="' . $this->view->escape($name) . '"' 0098 . ' id="' . $this->view->escape($id) . '"' 0099 . ' value="' . $this->view->escape($checkedOptions['checkedValue']) . '"' 0100 . $checkedOptions['checkedString'] 0101 . $disabled 0102 . $this->_htmlAttribs($attribs) 0103 . $this->getClosingBracket(); 0104 0105 return $xhtml; 0106 } 0107 0108 /** 0109 * Determine checkbox information 0110 * 0111 * @param string $value 0112 * @param bool $checked 0113 * @param array|null $checkedOptions 0114 * @return array 0115 */ 0116 public static function determineCheckboxInfo($value, $checked, array $checkedOptions = null) 0117 { 0118 // Checked/unchecked values 0119 $checkedValue = null; 0120 $uncheckedValue = null; 0121 if (is_array($checkedOptions)) { 0122 if (array_key_exists('checkedValue', $checkedOptions)) { 0123 $checkedValue = (string) $checkedOptions['checkedValue']; 0124 unset($checkedOptions['checkedValue']); 0125 } 0126 if (array_key_exists('uncheckedValue', $checkedOptions)) { 0127 $uncheckedValue = (string) $checkedOptions['uncheckedValue']; 0128 unset($checkedOptions['uncheckedValue']); 0129 } 0130 if (null === $checkedValue) { 0131 $checkedValue = (string) array_shift($checkedOptions); 0132 } 0133 if (null === $uncheckedValue) { 0134 $uncheckedValue = (string) array_shift($checkedOptions); 0135 } 0136 } elseif ($value !== null) { 0137 $uncheckedValue = self::$_defaultCheckedOptions['uncheckedValue']; 0138 } else { 0139 $checkedValue = self::$_defaultCheckedOptions['checkedValue']; 0140 $uncheckedValue = self::$_defaultCheckedOptions['uncheckedValue']; 0141 } 0142 0143 // is the element checked? 0144 $checkedString = ''; 0145 if ($checked || ((string) $value === $checkedValue)) { 0146 $checkedString = ' checked="checked"'; 0147 $checked = true; 0148 } else { 0149 $checked = false; 0150 } 0151 0152 // Checked value should be value if no checked options provided 0153 if ($checkedValue == null) { 0154 $checkedValue = $value; 0155 } 0156 0157 return array( 0158 'checked' => $checked, 0159 'checkedString' => $checkedString, 0160 'checkedValue' => $checkedValue, 0161 'uncheckedValue' => $uncheckedValue, 0162 ); 0163 } 0164 }