~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/View/Helper/FormSelect.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

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