web/lib/Zend/View/Helper/FormSelect.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_View
    16  * @package    Zend_View
    17  * @subpackage Helper
    17  * @subpackage Helper
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    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 $
    20  * @version    $Id: FormSelect.php 25187 2013-01-08 08:21:00Z frosch $
    21  */
    21  */
    22 
    22 
    23 
    23 
    24 /**
    24 /**
    25  * Abstract class for extension
    25  * Abstract class for extension
    31  * Helper to generate "select" list of options
    31  * Helper to generate "select" list of options
    32  *
    32  *
    33  * @category   Zend
    33  * @category   Zend
    34  * @package    Zend_View
    34  * @package    Zend_View
    35  * @subpackage Helper
    35  * @subpackage Helper
    36  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    38  */
    38  */
    39 class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
    39 class Zend_View_Helper_FormSelect extends Zend_View_Helper_FormElement
    40 {
    40 {
    41     /**
    41     /**
    50      * @param mixed $value The option value to mark as 'selected'; if an
    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
    51      * array, will mark all values in the array as 'selected' (used for
    52      * multiple-select elements).
    52      * multiple-select elements).
    53      *
    53      *
    54      * @param array|string $attribs Attributes added to the 'select' tag.
    54      * @param array|string $attribs Attributes added to the 'select' tag.
       
    55      * the optional 'optionClasses' attribute is used to add a class to
       
    56      * the options within the select (associative array linking the option
       
    57      * value to the desired class)
    55      *
    58      *
    56      * @param array $options An array of key-value pairs where the array
    59      * @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.
    60      * key is the radio value, and the array value is the radio text.
    58      *
    61      *
    59      * @param string $listsep When disabled, use this list separator string
    62      * @param string $listsep When disabled, use this list separator string
    94                 $multiple = '';
    97                 $multiple = '';
    95             }
    98             }
    96             unset($attribs['multiple']);
    99             unset($attribs['multiple']);
    97         }
   100         }
    98 
   101 
       
   102         // handle the options classes
       
   103         $optionClasses = array();
       
   104         if (isset($attribs['optionClasses'])) {
       
   105             $optionClasses = $attribs['optionClasses'];
       
   106             unset($attribs['optionClasses']);
       
   107         }
       
   108         
    99         // now start building the XHTML.
   109         // now start building the XHTML.
   100         $disabled = '';
   110         $disabled = '';
   101         if (true === $disable) {
   111         if (true === $disable) {
   102             $disabled = ' disabled="disabled"';
   112             $disabled = ' disabled="disabled"';
   103         }
   113         }
   121                     $opt_disable = ' disabled="disabled"';
   131                     $opt_disable = ' disabled="disabled"';
   122                 }
   132                 }
   123                 if (null !== $translator) {
   133                 if (null !== $translator) {
   124                     $opt_value = $translator->translate($opt_value);
   134                     $opt_value = $translator->translate($opt_value);
   125                 }
   135                 }
       
   136                 $opt_id = ' id="' . $this->view->escape($id) . '-optgroup-'
       
   137                         . $this->view->escape($opt_value) . '"';
   126                 $list[] = '<optgroup'
   138                 $list[] = '<optgroup'
   127                         . $opt_disable
   139                         . $opt_disable
       
   140                         . $opt_id
   128                         . ' label="' . $this->view->escape($opt_value) .'">';
   141                         . ' label="' . $this->view->escape($opt_value) .'">';
   129                 foreach ($opt_label as $val => $lab) {
   142                 foreach ($opt_label as $val => $lab) {
   130                     $list[] = $this->_build($val, $lab, $value, $disable);
   143                     $list[] = $this->_build($val, $lab, $value, $disable, $optionClasses);
   131                 }
   144                 }
   132                 $list[] = '</optgroup>';
   145                 $list[] = '</optgroup>';
   133             } else {
   146             } else {
   134                 $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
   147                 $list[] = $this->_build($opt_value, $opt_label, $value, $disable, $optionClasses);
   135             }
   148             }
   136         }
   149         }
   137 
   150 
   138         // add the options to the xhtml and close the select
   151         // add the options to the xhtml and close the select
   139         $xhtml .= implode("\n    ", $list) . "\n</select>";
   152         $xhtml .= implode("\n    ", $list) . "\n</select>";
   146      *
   159      *
   147      * @param string $value Options Value
   160      * @param string $value Options Value
   148      * @param string $label Options Label
   161      * @param string $label Options Label
   149      * @param array  $selected The option value(s) to mark as 'selected'
   162      * @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
   163      * @param array|bool $disable Whether the select is disabled, or individual options are
       
   164      * @param array $optionClasses The classes to associate with each option value
   151      * @return string Option Tag XHTML
   165      * @return string Option Tag XHTML
   152      */
   166      */
   153     protected function _build($value, $label, $selected, $disable)
   167     protected function _build($value, $label, $selected, $disable, $optionClasses = array())
   154     {
   168     {
   155         if (is_bool($disable)) {
   169         if (is_bool($disable)) {
   156             $disable = array();
   170             $disable = array();
   157         }
   171         }
   158 
   172 
       
   173         $class = null;
       
   174         if (array_key_exists($value, $optionClasses)) {
       
   175             $class = $optionClasses[$value];
       
   176         }
       
   177 
       
   178 
   159         $opt = '<option'
   179         $opt = '<option'
   160              . ' value="' . $this->view->escape($value) . '"'
   180              . ' value="' . $this->view->escape($value) . '"';
   161              . ' label="' . $this->view->escape($label) . '"';
   181 
   162 
   182              if ($class) {
       
   183              $opt .= ' class="' . $class . '"';
       
   184          }
   163         // selected?
   185         // selected?
   164         if (in_array((string) $value, $selected)) {
   186         if (in_array((string) $value, $selected)) {
   165             $opt .= ' selected="selected"';
   187             $opt .= ' selected="selected"';
   166         }
   188         }
   167 
   189