|
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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: FormElement.php 22290 2010-05-25 14:27:12Z matthew $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_View_Helper_HtmlElement |
|
25 */ |
|
26 require_once 'Zend/View/Helper/HtmlElement.php'; |
|
27 |
|
28 /** |
|
29 * Base helper for form elements. Extend this, don't use it on its own. |
|
30 * |
|
31 * @category Zend |
|
32 * @package Zend_View |
|
33 * @subpackage Helper |
|
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
35 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
36 */ |
|
37 abstract class Zend_View_Helper_FormElement extends Zend_View_Helper_HtmlElement |
|
38 { |
|
39 /** |
|
40 * @var Zend_Translate |
|
41 */ |
|
42 protected $_translator; |
|
43 |
|
44 /** |
|
45 * Get translator |
|
46 * |
|
47 * @return Zend_Translate |
|
48 */ |
|
49 public function getTranslator() |
|
50 { |
|
51 return $this->_translator; |
|
52 } |
|
53 |
|
54 /** |
|
55 * Set translator |
|
56 * |
|
57 * @param $translator|null Zend_Translate |
|
58 * @return Zend_View_Helper_FormElement |
|
59 */ |
|
60 public function setTranslator($translator = null) |
|
61 { |
|
62 if (null === $translator) { |
|
63 $this->_translator = null; |
|
64 } elseif ($translator instanceof Zend_Translate_Adapter) { |
|
65 $this->_translator = $translator; |
|
66 } elseif ($translator instanceof Zend_Translate) { |
|
67 $this->_translator = $translator->getAdapter(); |
|
68 } else { |
|
69 require_once 'Zend/View/Exception.php'; |
|
70 $e = new Zend_View_Exception('Invalid translator specified'); |
|
71 $e->setView($this->view); |
|
72 throw $e; |
|
73 } |
|
74 return $this; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Converts parameter arguments to an element info array. |
|
79 * |
|
80 * E.g, formExample($name, $value, $attribs, $options, $listsep) is |
|
81 * the same thing as formExample(array('name' => ...)). |
|
82 * |
|
83 * Note that you cannot pass a 'disable' param; you need to pass |
|
84 * it as an 'attribs' key. |
|
85 * |
|
86 * @access protected |
|
87 * |
|
88 * @return array An element info array with keys for name, value, |
|
89 * attribs, options, listsep, disable, and escape. |
|
90 */ |
|
91 protected function _getInfo($name, $value = null, $attribs = null, |
|
92 $options = null, $listsep = null |
|
93 ) { |
|
94 // the baseline info. note that $name serves a dual purpose; |
|
95 // if an array, it's an element info array that will override |
|
96 // these baseline values. as such, ignore it for the 'name' |
|
97 // if it's an array. |
|
98 $info = array( |
|
99 'name' => is_array($name) ? '' : $name, |
|
100 'id' => is_array($name) ? '' : $name, |
|
101 'value' => $value, |
|
102 'attribs' => $attribs, |
|
103 'options' => $options, |
|
104 'listsep' => $listsep, |
|
105 'disable' => false, |
|
106 'escape' => true, |
|
107 ); |
|
108 |
|
109 // override with named args |
|
110 if (is_array($name)) { |
|
111 // only set keys that are already in info |
|
112 foreach ($info as $key => $val) { |
|
113 if (isset($name[$key])) { |
|
114 $info[$key] = $name[$key]; |
|
115 } |
|
116 } |
|
117 |
|
118 // If all helper options are passed as an array, attribs may have |
|
119 // been as well |
|
120 if (null === $attribs) { |
|
121 $attribs = $info['attribs']; |
|
122 } |
|
123 } |
|
124 |
|
125 $attribs = (array)$attribs; |
|
126 |
|
127 // Normalize readonly tag |
|
128 if (array_key_exists('readonly', $attribs)) { |
|
129 $attribs['readonly'] = 'readonly'; |
|
130 } |
|
131 |
|
132 // Disable attribute |
|
133 if (array_key_exists('disable', $attribs)) { |
|
134 if (is_scalar($attribs['disable'])) { |
|
135 // disable the element |
|
136 $info['disable'] = (bool)$attribs['disable']; |
|
137 } else if (is_array($attribs['disable'])) { |
|
138 $info['disable'] = $attribs['disable']; |
|
139 } |
|
140 } |
|
141 |
|
142 // Set ID for element |
|
143 if (array_key_exists('id', $attribs)) { |
|
144 $info['id'] = (string)$attribs['id']; |
|
145 } else if ('' !== $info['name']) { |
|
146 $info['id'] = trim(strtr($info['name'], |
|
147 array('[' => '-', ']' => '')), '-'); |
|
148 } |
|
149 |
|
150 // Determine escaping from attributes |
|
151 if (array_key_exists('escape', $attribs)) { |
|
152 $info['escape'] = (bool)$attribs['escape']; |
|
153 } |
|
154 |
|
155 // Determine listsetp from attributes |
|
156 if (array_key_exists('listsep', $attribs)) { |
|
157 $info['listsep'] = (string)$attribs['listsep']; |
|
158 } |
|
159 |
|
160 // Remove attribs that might overwrite the other keys. We do this LAST |
|
161 // because we needed the other attribs values earlier. |
|
162 foreach ($info as $key => $val) { |
|
163 if (array_key_exists($key, $attribs)) { |
|
164 unset($attribs[$key]); |
|
165 } |
|
166 } |
|
167 $info['attribs'] = $attribs; |
|
168 |
|
169 // done! |
|
170 return $info; |
|
171 } |
|
172 |
|
173 /** |
|
174 * Creates a hidden element. |
|
175 * |
|
176 * We have this as a common method because other elements often |
|
177 * need hidden elements for their operation. |
|
178 * |
|
179 * @access protected |
|
180 * |
|
181 * @param $name The element name. |
|
182 * |
|
183 * @param $value The element value. |
|
184 * |
|
185 * @param $attribs Attributes for the element. |
|
186 * |
|
187 * @return string A hidden element. |
|
188 */ |
|
189 protected function _hidden($name, $value = null, $attribs = null) |
|
190 { |
|
191 return '<input type="hidden"' |
|
192 . ' name="' . $this->view->escape($name) . '"' |
|
193 . ' value="' . $this->view->escape($value) . '"' |
|
194 . $this->_htmlAttribs($attribs) . $this->getClosingBracket(); |
|
195 } |
|
196 } |