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.
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
24
* Abstract class for extension
26
require_once 'Zend/View/Helper/FormElement.php';
30
* Helper to generate a set of radio button elements
35
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
36
* @license http://framework.zend.com/license/new-bsd New BSD License
38
class Zend_View_Helper_FormRadio extends Zend_View_Helper_FormElement
44
protected $_inputType = 'radio';
47
* Whether or not this element represents an array collection by default
50
protected $_isArray = false;
53
* Generates a set of radio button elements.
57
* @param string|array $name If a string, the element name. If an
58
* array, all other parameters are ignored, and the array elements
59
* are extracted in place of added parameters.
61
* @param mixed $value The radio value to mark as 'checked'.
63
* @param array $options An array of key-value pairs where the array
64
* key is the radio value, and the array value is the radio text.
66
* @param array|string $attribs Attributes added to each radio.
68
* @return string The radio buttons XHTML.
70
public function formRadio($name, $value = null, $attribs = null,
71
$options = null, $listsep = "<br />\n")
74
$info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
75
extract($info); // name, value, attribs, options, listsep, disable
77
// retrieve attributes for labels (prefixed with 'label_' or 'label')
78
$label_attribs = array('style' => 'white-space: nowrap;');
79
foreach ($attribs as $key => $val) {
81
$keyLen = strlen($key);
82
if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
83
$tmp = substr($key, 6);
84
} elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
85
$tmp = substr($key, 5);
89
// make sure first char is lowercase
90
$tmp[0] = strtolower($tmp[0]);
91
$label_attribs[$tmp] = $val;
92
unset($attribs[$key]);
96
$labelPlacement = 'append';
97
foreach ($label_attribs as $key => $val) {
98
switch (strtolower($key)) {
100
unset($label_attribs[$key]);
101
$val = strtolower($val);
102
if (in_array($val, array('prepend', 'append'))) {
103
$labelPlacement = $val;
109
// the radio button values and labels
110
$options = (array) $options;
116
// should the name affect an array collection?
117
$name = $this->view->escape($name);
118
if ($this->_isArray && ('[]' != substr($name, -2))) {
122
// ensure value is an array to allow matching multiple times
123
$value = (array) $value;
125
// XHTML or HTML end tag?
127
if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
131
// add radio buttons to the list.
132
require_once 'Zend/Filter/Alnum.php';
133
$filter = new Zend_Filter_Alnum();
134
foreach ($options as $opt_value => $opt_label) {
136
// Should the label be escaped?
138
$opt_label = $this->view->escape($opt_label);
143
if (true === $disable) {
144
$disabled = ' disabled="disabled"';
145
} elseif (is_array($disable) && in_array($opt_value, $disable)) {
146
$disabled = ' disabled="disabled"';
151
if (in_array($opt_value, $value)) {
152
$checked = ' checked="checked"';
156
$optId = $id . '-' . $filter->filter($opt_value);
158
// Wrap the radios in labels
160
. $this->_htmlAttribs($label_attribs) . '>'
161
. (('prepend' == $labelPlacement) ? $opt_label : '')
162
. '<input type="' . $this->_inputType . '"'
163
. ' name="' . $name . '"'
164
. ' id="' . $optId . '"'
165
. ' value="' . $this->view->escape($opt_value) . '"'
168
. $this->_htmlAttribs($attribs)
170
. (('append' == $labelPlacement) ? $opt_label : '')
173
// add to the array of radio buttons
178
$xhtml .= implode($listsep, $list);