File indexing completed on 2024-12-22 05:37:13

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 "select" list of options
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_FormSelect extends Zend_View_Helper_FormElement
0040 {
0041     /**
0042      * Generates 'select' list of options.
0043      *
0044      * @access public
0045      *
0046      * @param string|array $name If a string, the element name.  If an
0047      * array, all other parameters are ignored, and the array elements
0048      * are extracted in place of added parameters.
0049      *
0050      * @param mixed $value The option value to mark as 'selected'; if an
0051      * array, will mark all values in the array as 'selected' (used for
0052      * multiple-select elements).
0053      *
0054      * @param array|string $attribs Attributes added to the 'select' tag.
0055      * the optional 'optionClasses' attribute is used to add a class to
0056      * the options within the select (associative array linking the option
0057      * value to the desired class)
0058      *
0059      * @param array $options An array of key-value pairs where the array
0060      * key is the radio value, and the array value is the radio text.
0061      *
0062      * @param string $listsep When disabled, use this list separator string
0063      * between list values.
0064      *
0065      * @return string The select tag and options XHTML.
0066      */
0067     public function formSelect($name, $value = null, $attribs = null,
0068         $options = null, $listsep = "<br />\n")
0069     {
0070         $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
0071         extract($info); // name, id, value, attribs, options, listsep, disable
0072 
0073         // force $value to array so we can compare multiple values to multiple
0074         // options; also ensure it's a string for comparison purposes.
0075         $value = array_map('strval', (array) $value);
0076 
0077         // check if element may have multiple values
0078         $multiple = '';
0079 
0080         if (substr($name, -2) == '[]') {
0081             // multiple implied by the name
0082             $multiple = ' multiple="multiple"';
0083         }
0084 
0085         if (isset($attribs['multiple'])) {
0086             // Attribute set
0087             if ($attribs['multiple']) {
0088                 // True attribute; set multiple attribute
0089                 $multiple = ' multiple="multiple"';
0090 
0091                 // Make sure name indicates multiple values are allowed
0092                 if (!empty($multiple) && (substr($name, -2) != '[]')) {
0093                     $name .= '[]';
0094                 }
0095             } else {
0096                 // False attribute; ensure attribute not set
0097                 $multiple = '';
0098             }
0099             unset($attribs['multiple']);
0100         }
0101 
0102         // handle the options classes
0103         $optionClasses = array();
0104         if (isset($attribs['optionClasses'])) {
0105             $optionClasses = $attribs['optionClasses'];
0106             unset($attribs['optionClasses']);
0107         }
0108         
0109         // now start building the XHTML.
0110         $disabled = '';
0111         if (true === $disable) {
0112             $disabled = ' disabled="disabled"';
0113         }
0114 
0115         // Build the surrounding select element first.
0116         $xhtml = '<select'
0117                 . ' name="' . $this->view->escape($name) . '"'
0118                 . ' id="' . $this->view->escape($id) . '"'
0119                 . $multiple
0120                 . $disabled
0121                 . $this->_htmlAttribs($attribs)
0122                 . ">\n    ";
0123 
0124         // build the list of options
0125         $list       = array();
0126         $translator = $this->getTranslator();
0127         foreach ((array) $options as $opt_value => $opt_label) {
0128             if (is_array($opt_label)) {
0129                 $opt_disable = '';
0130                 if (is_array($disable) && in_array($opt_value, $disable)) {
0131                     $opt_disable = ' disabled="disabled"';
0132                 }
0133                 if (null !== $translator) {
0134                     $opt_value = $translator->translate($opt_value);
0135                 }
0136                 $opt_id = ' id="' . $this->view->escape($id) . '-optgroup-'
0137                         . $this->view->escape($opt_value) . '"';
0138                 $list[] = '<optgroup'
0139                         . $opt_disable
0140                         . $opt_id
0141                         . ' label="' . $this->view->escape($opt_value) .'">';
0142                 foreach ($opt_label as $val => $lab) {
0143                     $list[] = $this->_build($val, $lab, $value, $disable, $optionClasses);
0144                 }
0145                 $list[] = '</optgroup>';
0146             } else {
0147                 $list[] = $this->_build($opt_value, $opt_label, $value, $disable, $optionClasses);
0148             }
0149         }
0150 
0151         // add the options to the xhtml and close the select
0152         $xhtml .= implode("\n    ", $list) . "\n</select>";
0153 
0154         return $xhtml;
0155     }
0156 
0157     /**
0158      * Builds the actual <option> tag
0159      *
0160      * @param string $value Options Value
0161      * @param string $label Options Label
0162      * @param array  $selected The option value(s) to mark as 'selected'
0163      * @param array|bool $disable Whether the select is disabled, or individual options are
0164      * @param array $optionClasses The classes to associate with each option value
0165      * @return string Option Tag XHTML
0166      */
0167     protected function _build($value, $label, $selected, $disable, $optionClasses = array())
0168     {
0169         if (is_bool($disable)) {
0170             $disable = array();
0171         }
0172 
0173         $class = null;
0174         if (array_key_exists($value, $optionClasses)) {
0175             $class = $optionClasses[$value];
0176         }
0177 
0178 
0179         $opt = '<option'
0180              . ' value="' . $this->view->escape($value) . '"';
0181 
0182              if ($class) {
0183              $opt .= ' class="' . $class . '"';
0184          }
0185         // selected?
0186         if (in_array((string) $value, $selected)) {
0187             $opt .= ' selected="selected"';
0188         }
0189 
0190         // disabled?
0191         if (in_array($value, $disable)) {
0192             $opt .= ' disabled="disabled"';
0193         }
0194 
0195         $opt .= '>' . $this->view->escape($label) . "</option>";
0196 
0197         return $opt;
0198     }
0199 
0200 }