~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/View/Helper/FormRadio.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 a set of radio button elements
 
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_FormRadio extends Zend_View_Helper_FormElement
 
39
{
 
40
    /**
 
41
     * Input type to use
 
42
     * @var string
 
43
     */
 
44
    protected $_inputType = 'radio';
 
45
 
 
46
    /**
 
47
     * Whether or not this element represents an array collection by default
 
48
     * @var bool
 
49
     */
 
50
    protected $_isArray = false;
 
51
 
 
52
    /**
 
53
     * Generates a set of radio button elements.
 
54
     *
 
55
     * @access public
 
56
     *
 
57
     * @param string|array $name If a string, the element name.  If an
 
58
     * array, all other parameters are ignored, and the array elements
 
59
     * are extracted in place of added parameters.
 
60
     *
 
61
     * @param mixed $value The radio value to mark as 'checked'.
 
62
     *
 
63
     * @param array $options An array of key-value pairs where the array
 
64
     * key is the radio value, and the array value is the radio text.
 
65
     *
 
66
     * @param array|string $attribs Attributes added to each radio.
 
67
     *
 
68
     * @return string The radio buttons XHTML.
 
69
     */
 
70
    public function formRadio($name, $value = null, $attribs = null,
 
71
        $options = null, $listsep = "<br />\n")
 
72
    {
 
73
 
 
74
        $info = $this->_getInfo($name, $value, $attribs, $options, $listsep);
 
75
        extract($info); // name, value, attribs, options, listsep, disable
 
76
 
 
77
        // retrieve attributes for labels (prefixed with 'label_' or 'label')
 
78
        $label_attribs = array('style' => 'white-space: nowrap;');
 
79
        foreach ($attribs as $key => $val) {
 
80
            $tmp    = false;
 
81
            $keyLen = strlen($key);
 
82
            if ((6 < $keyLen) && (substr($key, 0, 6) == 'label_')) {
 
83
                $tmp = substr($key, 6);
 
84
            } elseif ((5 < $keyLen) && (substr($key, 0, 5) == 'label')) {
 
85
                $tmp = substr($key, 5);
 
86
            }
 
87
 
 
88
            if ($tmp) {
 
89
                // make sure first char is lowercase
 
90
                $tmp[0] = strtolower($tmp[0]);
 
91
                $label_attribs[$tmp] = $val;
 
92
                unset($attribs[$key]);
 
93
            }
 
94
        }
 
95
 
 
96
        $labelPlacement = 'append';
 
97
        foreach ($label_attribs as $key => $val) {
 
98
            switch (strtolower($key)) {
 
99
                case 'placement':
 
100
                    unset($label_attribs[$key]);
 
101
                    $val = strtolower($val);
 
102
                    if (in_array($val, array('prepend', 'append'))) {
 
103
                        $labelPlacement = $val;
 
104
                    }
 
105
                    break;
 
106
            }
 
107
        }
 
108
 
 
109
        // the radio button values and labels
 
110
        $options = (array) $options;
 
111
 
 
112
        // build the element
 
113
        $xhtml = '';
 
114
        $list  = array();
 
115
        
 
116
        // should the name affect an array collection?
 
117
        $name = $this->view->escape($name);
 
118
        if ($this->_isArray && ('[]' != substr($name, -2))) {
 
119
            $name .= '[]';
 
120
        }
 
121
 
 
122
        // ensure value is an array to allow matching multiple times
 
123
        $value = (array) $value;
 
124
 
 
125
        // XHTML or HTML end tag?
 
126
        $endTag = ' />';
 
127
        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
 
128
            $endTag= '>';
 
129
        }
 
130
 
 
131
        // add radio buttons to the list.
 
132
        require_once 'Zend/Filter/Alnum.php';
 
133
        $filter = new Zend_Filter_Alnum();
 
134
        foreach ($options as $opt_value => $opt_label) {
 
135
 
 
136
            // Should the label be escaped?
 
137
            if ($escape) {
 
138
                $opt_label = $this->view->escape($opt_label);
 
139
            }
 
140
 
 
141
            // is it disabled?
 
142
            $disabled = '';
 
143
            if (true === $disable) {
 
144
                $disabled = ' disabled="disabled"';
 
145
            } elseif (is_array($disable) && in_array($opt_value, $disable)) {
 
146
                $disabled = ' disabled="disabled"';
 
147
            }
 
148
 
 
149
            // is it checked?
 
150
            $checked = '';
 
151
            if (in_array($opt_value, $value)) {
 
152
                $checked = ' checked="checked"';
 
153
            }
 
154
 
 
155
            // generate ID
 
156
            $optId = $id . '-' . $filter->filter($opt_value);
 
157
 
 
158
            // Wrap the radios in labels
 
159
            $radio = '<label'
 
160
                    . $this->_htmlAttribs($label_attribs) . '>'
 
161
                    . (('prepend' == $labelPlacement) ? $opt_label : '')
 
162
                    . '<input type="' . $this->_inputType . '"'
 
163
                    . ' name="' . $name . '"'
 
164
                    . ' id="' . $optId . '"'
 
165
                    . ' value="' . $this->view->escape($opt_value) . '"'
 
166
                    . $checked
 
167
                    . $disabled
 
168
                    . $this->_htmlAttribs($attribs) 
 
169
                    . $endTag
 
170
                    . (('append' == $labelPlacement) ? $opt_label : '')
 
171
                    . '</label>';
 
172
 
 
173
            // add to the array of radio buttons
 
174
            $list[] = $radio;
 
175
        }
 
176
 
 
177
        // done!
 
178
        $xhtml .= implode($listsep, $list);
 
179
 
 
180
        return $xhtml;
 
181
    }
 
182
}