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