[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / View / Helper / FormRadio.php
blob0c217d6f2dbe8b454dc0354e56496c93e4978508
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
24 /**
25 * Abstract class for extension
27 require_once 'Zend/View/Helper/FormElement.php';
30 /**
31 * Helper to generate a set of radio button elements
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
39 class Zend_View_Helper_FormRadio extends Zend_View_Helper_FormElement
41 /**
42 * Input type to use
43 * @var string
45 protected $_inputType = 'radio';
47 /**
48 * Whether or not this element represents an array collection by default
49 * @var bool
51 protected $_isArray = false;
53 /**
54 * Generates a set of radio button elements.
56 * @access public
58 * @param string|array $name If a string, the element name. If an
59 * array, all other parameters are ignored, and the array elements
60 * are extracted in place of added parameters.
62 * @param mixed $value The radio value to mark as 'checked'.
64 * @param array $options An array of key-value pairs where the array
65 * key is the radio value, and the array value is the radio text.
67 * @param array|string $attribs Attributes added to each radio.
69 * @return string The radio buttons XHTML.
71 public function formRadio($name, $value = null, $attribs = null,
72 $options = null, $listsep = "<br />\n")
75 $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
76 extract($info); // name, value, attribs, options, listsep, disable
78 // retrieve attributes for labels (prefixed with 'label_' or 'label')
79 $label_attribs = array();
80 foreach ($attribs as $key => $val) {
81 $tmp = false;
82 $keyLen = strlen($key);
83 if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
84 $tmp = substr($key, 6);
85 } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
86 $tmp = substr($key, 5);
89 if ($tmp) {
90 // make sure first char is lowercase
91 $tmp[0] = strtolower($tmp[0]);
92 $label_attribs[$tmp] = $val;
93 unset($attribs[$key]);
97 $labelPlacement = 'append';
98 foreach ($label_attribs as $key => $val) {
99 switch (strtolower($key)) {
100 case 'placement':
101 unset($label_attribs[$key]);
102 $val = strtolower($val);
103 if (in_array($val, array('prepend', 'append'))) {
104 $labelPlacement = $val;
106 break;
110 // the radio button values and labels
111 $options = (array) $options;
113 // build the element
114 $xhtml = '';
115 $list = array();
117 // should the name affect an array collection?
118 $name = $this->view->escape($name);
119 if ($this->_isArray && ('[]' != substr($name, -2))) {
120 $name .= '[]';
123 // ensure value is an array to allow matching multiple times
124 $value = (array) $value;
126 // XHTML or HTML end tag?
127 $endTag = ' />';
128 if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
129 $endTag= '>';
132 // add radio buttons to the list.
133 require_once 'Zend/Filter/Alnum.php';
134 $filter = new Zend_Filter_Alnum();
135 foreach ($options as $opt_value => $opt_label) {
137 // Should the label be escaped?
138 if ($escape) {
139 $opt_label = $this->view->escape($opt_label);
142 // is it disabled?
143 $disabled = '';
144 if (true === $disable) {
145 $disabled = ' disabled="disabled"';
146 } elseif (is_array($disable) && in_array($opt_value, $disable)) {
147 $disabled = ' disabled="disabled"';
150 // is it checked?
151 $checked = '';
152 if (in_array($opt_value, $value)) {
153 $checked = ' checked="checked"';
156 // generate ID
157 $optId = $id . '-' . $filter->filter($opt_value);
159 // Wrap the radios in labels
160 $radio = '<label'
161 . $this->_htmlAttribs($label_attribs) . ' for="' . $optId . '">'
162 . (('prepend' == $labelPlacement) ? $opt_label : '')
163 . '<input type="' . $this->_inputType . '"'
164 . ' name="' . $name . '"'
165 . ' id="' . $optId . '"'
166 . ' value="' . $this->view->escape($opt_value) . '"'
167 . $checked
168 . $disabled
169 . $this->_htmlAttribs($attribs)
170 . $endTag
171 . (('append' == $labelPlacement) ? $opt_label : '')
172 . '</label>';
174 // add to the array of radio buttons
175 $list[] = $radio;
178 // done!
179 $xhtml .= implode($listsep, $list);
181 return $xhtml;