FormRadio.php
6.08 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?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: FormRadio.php 24160 2011-06-28 16:37:04Z adamlundrigan $
*/
/**
* Abstract class for extension
*/
require_once 'Zend/View/Helper/FormElement.php';
/**
* Helper to generate a set of radio button elements
*
* @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_FormRadio extends Zend_View_Helper_FormElement
{
/**
* Input type to use
* @var string
*/
protected $_inputType = 'radio';
/**
* Whether or not this element represents an array collection by default
* @var bool
*/
protected $_isArray = false;
/**
* Generates a set of radio button elements.
*
* @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 radio value to mark as 'checked'.
*
* @param array $options An array of key-value pairs where the array
* key is the radio value, and the array value is the radio text.
*
* @param array|string $attribs Attributes added to each radio.
*
* @return string The radio buttons XHTML.
*/
public function formRadio($name, $value = null, $attribs = null,
$options = null, $listsep = "<br />\n")
{
$info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
extract($info); // name, value, attribs, options, listsep, disable
// retrieve attributes for labels (prefixed with 'label_' or 'label')
$label_attribs = array();
foreach ($attribs as $key => $val) {
$tmp = false;
$keyLen = strlen($key);
if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
$tmp = substr($key, 6);
} elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
$tmp = substr($key, 5);
}
if ($tmp) {
// make sure first char is lowercase
$tmp[0] = strtolower($tmp[0]);
$label_attribs[$tmp] = $val;
unset($attribs[$key]);
}
}
$labelPlacement = 'append';
foreach ($label_attribs as $key => $val) {
switch (strtolower($key)) {
case 'placement':
unset($label_attribs[$key]);
$val = strtolower($val);
if (in_array($val, array('prepend', 'append'))) {
$labelPlacement = $val;
}
break;
}
}
// the radio button values and labels
$options = (array) $options;
// build the element
$xhtml = '';
$list = array();
// should the name affect an array collection?
$name = $this->view->escape($name);
if ($this->_isArray && ('[]' != substr($name, -2))) {
$name .= '[]';
}
// ensure value is an array to allow matching multiple times
$value = (array) $value;
// XHTML or HTML end tag?
$endTag = ' />';
if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
$endTag= '>';
}
// Set up the filter - Alnum + hyphen + underscore
require_once 'Zend/Filter/PregReplace.php';
$pattern = @preg_match('/\pL/u', 'a')
? '/[^\p{L}\p{N}\-\_]/u' // Unicode
: '/[^a-zA-Z0-9\-\_]/'; // No Unicode
$filter = new Zend_Filter_PregReplace($pattern, "");
// add radio buttons to the list.
foreach ($options as $opt_value => $opt_label) {
// Should the label be escaped?
if ($escape) {
$opt_label = $this->view->escape($opt_label);
}
// is it disabled?
$disabled = '';
if (true === $disable) {
$disabled = ' disabled="disabled"';
} elseif (is_array($disable) && in_array($opt_value, $disable)) {
$disabled = ' disabled="disabled"';
}
// is it checked?
$checked = '';
if (in_array($opt_value, $value)) {
$checked = ' checked="checked"';
}
// generate ID
$optId = $id . '-' . $filter->filter($opt_value);
// Wrap the radios in labels
$radio = '<label'
. $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
. (('prepend' == $labelPlacement) ? $opt_label : '')
. '<input type="' . $this->_inputType . '"'
. ' name="' . $name . '"'
. ' id="' . $optId . '"'
. ' value="' . $this->view->escape($opt_value) . '"'
. $checked
. $disabled
. $this->_htmlAttribs($attribs)
. $endTag
. (('append' == $labelPlacement) ? $opt_label : '')
. '</label>';
// add to the array of radio buttons
$list[] = $radio;
}
// done!
$xhtml .= implode($listsep, $list);
return $xhtml;
}
}