Changeset 3149

Show
Ignore:
Timestamp:
05/03/08 16:46:30 (2 months ago)
Author:
pmjones
Message:

Solar_View_Helper_FormSelect: [ADD] Now supports <optgroup> collections when the option label is an array. Thanks for the patch, Rodrigo Moraes.

fixes #19

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Solar/View/Helper/FormSelect.php

    r2986 r3149  
    6060        // build the list of options 
    6161        $list = array(); 
    62         foreach ($this->_options as $opt_value => $opt_label) { 
    63             $selected = ''; 
    64             if (in_array($opt_value, $this->_value)) { 
    65                 $selected = ' selected="selected"'; 
     62        foreach ($this->_options as $value => $label) { 
     63            if (is_array($label)) { 
     64                 
     65                // Use <optgroup> 
     66                $list[] = '<optgroup label="' 
     67                        . $this->_view->escape($value) 
     68                        . '">'; 
     69                 
     70                foreach ($label as $grp_value => $grp_label) { 
     71                    $list[] = $this->_getOption($grp_value, $grp_label); 
     72                } 
     73                 
     74                $list[] = '</optgroup>'; 
     75                 
     76            } else { 
     77                $list[] = $this->_getOption($value, $label); 
    6678            } 
    67             $list[] = '<option' 
    68                     . ' value="' . $this->_view->escape($opt_value) . '"' 
    69                     . ' label="' . $this->_view->escape($opt_label) . '"' 
    70                     . $selected 
    71                     . '>' . $this->_view->escape($opt_label) . "</option>"; 
    7279        } 
    7380         
     
    7986             . "</select>"; 
    8087    } 
     88     
     89    /** 
     90     * 
     91     * Builds an option for the select. 
     92     * 
     93     * @param string $value The option value. 
     94     * 
     95     * @param string $label The option lavel. 
     96     *  
     97     * @return string The option XHTML. 
     98     *  
     99     */ 
     100    protected function _getOption($value, $label) 
     101    { 
     102        $selected = ''; 
     103         
     104        if (in_array($value, $this->_value)) { 
     105            $selected = ' selected="selected"'; 
     106        } 
     107         
     108        $option = '<option' 
     109                . ' value="' . $this->_view->escape($value) . '"' 
     110                . ' label="' . $this->_view->escape($label) . '"' 
     111                . $selected 
     112                . '>' . $this->_view->escape($label) . "</option>"; 
     113         
     114        return $option;         
     115    } 
    81116}