File indexing completed on 2025-03-02 05:29:58
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 set of radio button elements 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_FormRadio extends Zend_View_Helper_FormElement 0040 { 0041 /** 0042 * Input type to use 0043 * @var string 0044 */ 0045 protected $_inputType = 'radio'; 0046 0047 /** 0048 * Whether or not this element represents an array collection by default 0049 * @var bool 0050 */ 0051 protected $_isArray = false; 0052 0053 /** 0054 * Generates a set of radio button elements. 0055 * 0056 * @access public 0057 * 0058 * @param string|array $name If a string, the element name. If an 0059 * array, all other parameters are ignored, and the array elements 0060 * are extracted in place of added parameters. 0061 * 0062 * @param mixed $value The radio value to mark as 'checked'. 0063 * 0064 * @param array $options An array of key-value pairs where the array 0065 * key is the radio value, and the array value is the radio text. 0066 * 0067 * @param array|string $attribs Attributes added to each radio. 0068 * 0069 * @return string The radio buttons XHTML. 0070 */ 0071 public function formRadio($name, $value = null, $attribs = null, 0072 $options = null, $listsep = "<br />\n") 0073 { 0074 0075 $info = $this->_getInfo($name, $value, $attribs, $options, $listsep); 0076 extract($info); // name, value, attribs, options, listsep, disable 0077 0078 // retrieve attributes for labels (prefixed with 'label_' or 'label') 0079 $label_attribs = array(); 0080 foreach ($attribs as $key => $val) { 0081 $tmp = false; 0082 $keyLen = strlen($key); 0083 if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) { 0084 $tmp = substr($key, 6); 0085 } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) { 0086 $tmp = substr($key, 5); 0087 } 0088 0089 if ($tmp) { 0090 // make sure first char is lowercase 0091 $tmp[0] = strtolower($tmp[0]); 0092 $label_attribs[$tmp] = $val; 0093 unset($attribs[$key]); 0094 } 0095 } 0096 0097 $labelPlacement = 'append'; 0098 foreach ($label_attribs as $key => $val) { 0099 switch (strtolower($key)) { 0100 case 'placement': 0101 unset($label_attribs[$key]); 0102 $val = strtolower($val); 0103 if (in_array($val, array('prepend', 'append'))) { 0104 $labelPlacement = $val; 0105 } 0106 break; 0107 } 0108 } 0109 0110 // the radio button values and labels 0111 $options = (array) $options; 0112 0113 // build the element 0114 $xhtml = ''; 0115 $list = array(); 0116 0117 // should the name affect an array collection? 0118 $name = $this->view->escape($name); 0119 if ($this->_isArray && ('[]' != substr($name, -2))) { 0120 $name .= '[]'; 0121 } 0122 0123 // ensure value is an array to allow matching multiple times 0124 $value = (array) $value; 0125 0126 // Set up the filter - Alnum + hyphen + underscore 0127 // require_once 'Zend/Filter/PregReplace.php'; 0128 $pattern = @preg_match('/\pL/u', 'a') 0129 ? '/[^\p{L}\p{N}\-\_]/u' // Unicode 0130 : '/[^a-zA-Z0-9\-\_]/'; // No Unicode 0131 $filter = new Zend_Filter_PregReplace($pattern, ""); 0132 0133 // add radio buttons to the list. 0134 foreach ($options as $opt_value => $opt_label) { 0135 0136 // Should the label be escaped? 0137 if ($escape) { 0138 $opt_label = $this->view->escape($opt_label); 0139 } 0140 0141 // is it disabled? 0142 $disabled = ''; 0143 if (true === $disable) { 0144 $disabled = ' disabled="disabled"'; 0145 } elseif (is_array($disable) && in_array($opt_value, $disable)) { 0146 $disabled = ' disabled="disabled"'; 0147 } 0148 0149 // is it checked? 0150 $checked = ''; 0151 if (in_array($opt_value, $value)) { 0152 $checked = ' checked="checked"'; 0153 } 0154 0155 // generate ID 0156 $optId = $id . '-' . $filter->filter($opt_value); 0157 0158 // Wrap the radios in labels 0159 $radio = '<label' 0160 . $this->_htmlAttribs($label_attribs) . '>' 0161 . (('prepend' == $labelPlacement) ? $opt_label : '') 0162 . '<input type="' . $this->_inputType . '"' 0163 . ' name="' . $name . '"' 0164 . ' id="' . $optId . '"' 0165 . ' value="' . $this->view->escape($opt_value) . '"' 0166 . $checked 0167 . $disabled 0168 . $this->_htmlAttribs($attribs) 0169 . $this->getClosingBracket() 0170 . (('append' == $labelPlacement) ? $opt_label : '') 0171 . '</label>'; 0172 0173 // add to the array of radio buttons 0174 $list[] = $radio; 0175 } 0176 0177 // XHTML or HTML for standard list separator? 0178 if (!$this->_isXhtml() && false !== strpos($listsep, '<br />')) { 0179 $listsep = str_replace('<br />', '<br>', $listsep); 0180 } 0181 0182 // done! 0183 $xhtml .= implode($listsep, $list); 0184 0185 return $xhtml; 0186 } 0187 }