web/lib/Zend/View/Helper/FormRadio.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     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.
       
    14  *
       
    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: FormRadio.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * Abstract class for extension
       
    26  */
       
    27 require_once 'Zend/View/Helper/FormElement.php';
       
    28 
       
    29 
       
    30 /**
       
    31  * Helper to generate a set of radio button elements
       
    32  *
       
    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
       
    38  */
       
    39 class Zend_View_Helper_FormRadio extends Zend_View_Helper_FormElement
       
    40 {
       
    41     /**
       
    42      * Input type to use
       
    43      * @var string
       
    44      */
       
    45     protected $_inputType = 'radio';
       
    46 
       
    47     /**
       
    48      * Whether or not this element represents an array collection by default
       
    49      * @var bool
       
    50      */
       
    51     protected $_isArray = false;
       
    52 
       
    53     /**
       
    54      * Generates a set of radio button elements.
       
    55      *
       
    56      * @access public
       
    57      *
       
    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.
       
    61      *
       
    62      * @param mixed $value The radio value to mark as 'checked'.
       
    63      *
       
    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.
       
    66      *
       
    67      * @param array|string $attribs Attributes added to each radio.
       
    68      *
       
    69      * @return string The radio buttons XHTML.
       
    70      */
       
    71     public function formRadio($name, $value = null, $attribs = null,
       
    72         $options = null, $listsep = "<br />\n")
       
    73     {
       
    74 
       
    75         $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
       
    76         extract($info); // name, value, attribs, options, listsep, disable
       
    77 
       
    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);
       
    87             }
       
    88 
       
    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]);
       
    94             }
       
    95         }
       
    96 
       
    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;
       
   105                     }
       
   106                     break;
       
   107             }
       
   108         }
       
   109 
       
   110         // the radio button values and labels
       
   111         $options = (array) $options;
       
   112 
       
   113         // build the element
       
   114         $xhtml = '';
       
   115         $list  = array();
       
   116 
       
   117         // should the name affect an array collection?
       
   118         $name = $this->view->escape($name);
       
   119         if ($this->_isArray && ('[]' != substr($name, -2))) {
       
   120             $name .= '[]';
       
   121         }
       
   122 
       
   123         // ensure value is an array to allow matching multiple times
       
   124         $value = (array) $value;
       
   125 
       
   126         // XHTML or HTML end tag?
       
   127         $endTag = ' />';
       
   128         if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
       
   129             $endTag= '>';
       
   130         }
       
   131 
       
   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) {
       
   136 
       
   137             // Should the label be escaped?
       
   138             if ($escape) {
       
   139                 $opt_label = $this->view->escape($opt_label);
       
   140             }
       
   141 
       
   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"';
       
   148             }
       
   149 
       
   150             // is it checked?
       
   151             $checked = '';
       
   152             if (in_array($opt_value, $value)) {
       
   153                 $checked = ' checked="checked"';
       
   154             }
       
   155 
       
   156             // generate ID
       
   157             $optId = $id . '-' . $filter->filter($opt_value);
       
   158 
       
   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>';
       
   173 
       
   174             // add to the array of radio buttons
       
   175             $list[] = $radio;
       
   176         }
       
   177 
       
   178         // done!
       
   179         $xhtml .= implode($listsep, $list);
       
   180 
       
   181         return $xhtml;
       
   182     }
       
   183 }