PageRenderTime 42ms CodeModel.GetById 31ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/View/Helper/FormSelect.php

https://bitbucket.org/hamidrezas/melobit
PHP | 181 lines | 82 code | 21 blank | 78 comment | 17 complexity | 93d5b1715034058c5aadb780c249c07f MD5 | raw file
Possible License(s): AGPL-1.0
  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-2012 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 24594 2012-01-05 21:27:01Z matthew $
 21 */
 22
 23
 24/**
 25 * Abstract class for extension
 26 */
 27require_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-2012 Zend Technologies USA Inc. (http://www.zend.com)
 37 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 38 */
 39class 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                $opt_id = ' id="' . $this->view->escape($id) . '-optgroup-'
127                        . $this->view->escape($opt_value) . '"';
128                $list[] = '<optgroup'
129                        . $opt_disable
130                        . $opt_id
131                        . ' label="' . $this->view->escape($opt_value) .'">';
132                foreach ($opt_label as $val => $lab) {
133                    $list[] = $this->_build($val, $lab, $value, $disable);
134                }
135                $list[] = '</optgroup>';
136            } else {
137                $list[] = $this->_build($opt_value, $opt_label, $value, $disable);
138            }
139        }
140
141        // add the options to the xhtml and close the select
142        $xhtml .= implode("\n    ", $list) . "\n</select>";
143
144        return $xhtml;
145    }
146
147    /**
148     * Builds the actual <option> tag
149     *
150     * @param string $value Options Value
151     * @param string $label Options Label
152     * @param array  $selected The option value(s) to mark as 'selected'
153     * @param array|bool $disable Whether the select is disabled, or individual options are
154     * @return string Option Tag XHTML
155     */
156    protected function _build($value, $label, $selected, $disable)
157    {
158        if (is_bool($disable)) {
159            $disable = array();
160        }
161
162        $opt = '<option'
163             . ' value="' . $this->view->escape($value) . '"'
164             . ' label="' . $this->view->escape($label) . '"';
165
166        // selected?
167        if (in_array((string) $value, $selected)) {
168            $opt .= ' selected="selected"';
169        }
170
171        // disabled?
172        if (in_array($value, $disable)) {
173            $opt .= ' disabled="disabled"';
174        }
175
176        $opt .= '>' . $this->view->escape($label) . "</option>";
177
178        return $opt;
179    }
180
181}