web/lib/Zend/View/Helper/FormSelect.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: FormSelect.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 "select" list of options
       
    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_FormSelect extends Zend_View_Helper_FormElement
       
    40 {
       
    41     /**
       
    42      * Generates 'select' list of options.
       
    43      *
       
    44      * @access public
       
    45      *
       
    46      * @param string|array $name If a string, the element name.  If an
       
    47      * array, all other parameters are ignored, and the array elements
       
    48      * are extracted in place of added parameters.
       
    49      *
       
    50      * @param mixed $value The option value to mark as 'selected'; if an
       
    51      * array, will mark all values in the array as 'selected' (used for
       
    52      * multiple-select elements).
       
    53      *
       
    54      * @param array|string $attribs Attributes added to the 'select' tag.
       
    55      *
       
    56      * @param array $options An array of key-value pairs where the array
       
    57      * key is the radio value, and the array value is the radio text.
       
    58      *
       
    59      * @param string $listsep When disabled, use this list separator string
       
    60      * between list values.
       
    61      *
       
    62      * @return string The select tag and options XHTML.
       
    63      */
       
    64     public function formSelect($name, $value = null, $attribs = null,
       
    65         $options = null, $listsep = "<br />\n")
       
    66     {
       
    67         $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
       
    68         extract($info); // name, id, value, attribs, options, listsep, disable
       
    69 
       
    70         // force $value to array so we can compare multiple values to multiple
       
    71         // options; also ensure it's a string for comparison purposes.
       
    72         $value = array_map('strval', (array) $value);
       
    73 
       
    74         // check if element may have multiple values
       
    75         $multiple = '';
       
    76 
       
    77         if (substr($name, -2) == '[]') {
       
    78             // multiple implied by the name
       
    79             $multiple = ' multiple="multiple"';
       
    80         }
       
    81 
       
    82         if (isset($attribs['multiple'])) {
       
    83             // Attribute set
       
    84             if ($attribs['multiple']) {
       
    85                 // True attribute; set multiple attribute
       
    86                 $multiple = ' multiple="multiple"';
       
    87 
       
    88                 // Make sure name indicates multiple values are allowed
       
    89                 if (!empty($multiple) && (substr($name, -2) != '[]')) {
       
    90                     $name .= '[]';
       
    91                 }
       
    92             } else {
       
    93                 // False attribute; ensure attribute not set
       
    94                 $multiple = '';
       
    95             }
       
    96             unset($attribs['multiple']);
       
    97         }
       
    98 
       
    99         // now start building the XHTML.
       
   100         $disabled = '';
       
   101         if (true === $disable) {
       
   102             $disabled = ' disabled="disabled"';
       
   103         }
       
   104 
       
   105         // Build the surrounding select element first.
       
   106         $xhtml = '<select'
       
   107                 . ' name="' . $this->view->escape($name) . '"'
       
   108                 . ' id="' . $this->view->escape($id) . '"'
       
   109                 . $multiple
       
   110                 . $disabled
       
   111                 . $this->_htmlAttribs($attribs)
       
   112                 . ">\n    ";
       
   113 
       
   114         // build the list of options
       
   115         $list       = array();
       
   116         $translator = $this->getTranslator();
       
   117         foreach ((array) $options as $opt_value => $opt_label) {
       
   118             if (is_array($opt_label)) {
       
   119                 $opt_disable = '';
       
   120                 if (is_array($disable) && in_array($opt_value, $disable)) {
       
   121                     $opt_disable = ' disabled="disabled"';
       
   122                 }
       
   123                 if (null !== $translator) {
       
   124                     $opt_value = $translator->translate($opt_value);
       
   125                 }
       
   126                 $list[] = '<optgroup'
       
   127                         . $opt_disable
       
   128                         . ' label="' . $this->view->escape($opt_value) .'">';
       
   129                 foreach ($opt_label as $val => $lab) {
       
   130                     $list[] = $this->_build($val, $lab, $value, $disable);
       
   131                 }
       
   132                 $list[] = '</optgroup>';
       
   133             } else {
       
   134                 $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
       
   135             }
       
   136         }
       
   137 
       
   138         // add the options to the xhtml and close the select
       
   139         $xhtml .= implode("\n    ", $list) . "\n</select>";
       
   140 
       
   141         return $xhtml;
       
   142     }
       
   143 
       
   144     /**
       
   145      * Builds the actual <option> tag
       
   146      *
       
   147      * @param string $value Options Value
       
   148      * @param string $label Options Label
       
   149      * @param array  $selected The option value(s) to mark as 'selected'
       
   150      * @param array|bool $disable Whether the select is disabled, or individual options are
       
   151      * @return string Option Tag XHTML
       
   152      */
       
   153     protected function _build($value, $label, $selected, $disable)
       
   154     {
       
   155         if (is_bool($disable)) {
       
   156             $disable = array();
       
   157         }
       
   158 
       
   159         $opt = '<option'
       
   160              . ' value="' . $this->view->escape($value) . '"'
       
   161              . ' label="' . $this->view->escape($label) . '"';
       
   162 
       
   163         // selected?
       
   164         if (in_array((string) $value, $selected)) {
       
   165             $opt .= ' selected="selected"';
       
   166         }
       
   167 
       
   168         // disabled?
       
   169         if (in_array($value, $disable)) {
       
   170             $opt .= ' disabled="disabled"';
       
   171         }
       
   172 
       
   173         $opt .= '>' . $this->view->escape($label) . "</option>";
       
   174 
       
   175         return $opt;
       
   176     }
       
   177 
       
   178 }