|
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_Dojo |
|
17 * @subpackage View |
|
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: Dijit.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** Zend_View_Helper_HtmlElement */ |
|
24 require_once 'Zend/View/Helper/HtmlElement.php'; |
|
25 |
|
26 /** |
|
27 * Dojo dijit base class |
|
28 * |
|
29 * @uses Zend_View_Helper_Abstract |
|
30 * @package Zend_Dojo |
|
31 * @subpackage View |
|
32 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
33 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
34 */ |
|
35 abstract class Zend_Dojo_View_Helper_Dijit extends Zend_View_Helper_HtmlElement |
|
36 { |
|
37 /** |
|
38 * @var Zend_Dojo_View_Helper_Dojo_Container |
|
39 */ |
|
40 public $dojo; |
|
41 |
|
42 /** |
|
43 * Dijit being used |
|
44 * @var string |
|
45 */ |
|
46 protected $_dijit; |
|
47 |
|
48 /** |
|
49 * Element type |
|
50 * @var string |
|
51 */ |
|
52 protected $_elementType; |
|
53 |
|
54 /** |
|
55 * Parameters that should be JSON encoded |
|
56 * @var array |
|
57 */ |
|
58 protected $_jsonParams = array('constraints'); |
|
59 |
|
60 /** |
|
61 * Dojo module to use |
|
62 * @var string |
|
63 */ |
|
64 protected $_module; |
|
65 |
|
66 /** |
|
67 * Root node element type for layout elements |
|
68 * @var string |
|
69 */ |
|
70 protected $_rootNode = 'div'; |
|
71 |
|
72 /** |
|
73 * Set view |
|
74 * |
|
75 * Set view and enable dojo |
|
76 * |
|
77 * @param Zend_View_Interface $view |
|
78 * @return Zend_Dojo_View_Helper_Dijit |
|
79 */ |
|
80 public function setView(Zend_View_Interface $view) |
|
81 { |
|
82 parent::setView($view); |
|
83 $this->dojo = $this->view->dojo(); |
|
84 $this->dojo->enable(); |
|
85 return $this; |
|
86 } |
|
87 |
|
88 |
|
89 /** |
|
90 * Get root node type |
|
91 * |
|
92 * @return string |
|
93 */ |
|
94 public function getRootNode() |
|
95 { |
|
96 return $this->_rootNode; |
|
97 } |
|
98 |
|
99 /** |
|
100 * Set root node type |
|
101 * |
|
102 * @param string $value |
|
103 * @return Zend_Dojo_View_Helper_Dijit |
|
104 */ |
|
105 public function setRootNode($value) |
|
106 { |
|
107 $this->_rootNode = $value; |
|
108 return $this; |
|
109 } |
|
110 |
|
111 /** |
|
112 * Whether or not to use declarative dijit creation |
|
113 * |
|
114 * @return bool |
|
115 */ |
|
116 protected function _useDeclarative() |
|
117 { |
|
118 return Zend_Dojo_View_Helper_Dojo::useDeclarative(); |
|
119 } |
|
120 |
|
121 /** |
|
122 * Whether or not to use programmatic dijit creation |
|
123 * |
|
124 * @return bool |
|
125 */ |
|
126 protected function _useProgrammatic() |
|
127 { |
|
128 return Zend_Dojo_View_Helper_Dojo::useProgrammatic(); |
|
129 } |
|
130 |
|
131 /** |
|
132 * Whether or not to use programmatic dijit creation w/o script creation |
|
133 * |
|
134 * @return bool |
|
135 */ |
|
136 protected function _useProgrammaticNoScript() |
|
137 { |
|
138 return Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript(); |
|
139 } |
|
140 |
|
141 /** |
|
142 * Create a layout container |
|
143 * |
|
144 * @param int $id |
|
145 * @param string $content |
|
146 * @param array $params |
|
147 * @param array $attribs |
|
148 * @param string|null $dijit |
|
149 * @return string |
|
150 */ |
|
151 protected function _createLayoutContainer($id, $content, array $params, array $attribs, $dijit = null) |
|
152 { |
|
153 $attribs['id'] = $id; |
|
154 $attribs = $this->_prepareDijit($attribs, $params, 'layout', $dijit); |
|
155 |
|
156 $nodeType = $this->getRootNode(); |
|
157 $html = '<' . $nodeType . $this->_htmlAttribs($attribs) . '>' |
|
158 . $content |
|
159 . "</$nodeType>\n"; |
|
160 |
|
161 return $html; |
|
162 } |
|
163 |
|
164 /** |
|
165 * Create HTML representation of a dijit form element |
|
166 * |
|
167 * @param string $id |
|
168 * @param string $value |
|
169 * @param array $params |
|
170 * @param array $attribs |
|
171 * @param string|null $dijit |
|
172 * @return string |
|
173 */ |
|
174 public function _createFormElement($id, $value, array $params, array $attribs, $dijit = null) |
|
175 { |
|
176 if (!array_key_exists('id', $attribs)) { |
|
177 $attribs['id'] = $id; |
|
178 } |
|
179 $attribs['name'] = $id; |
|
180 $attribs['value'] = (string) $value; |
|
181 $attribs['type'] = $this->_elementType; |
|
182 |
|
183 $attribs = $this->_prepareDijit($attribs, $params, 'element', $dijit); |
|
184 |
|
185 $html = '<input' |
|
186 . $this->_htmlAttribs($attribs) |
|
187 . $this->getClosingBracket(); |
|
188 return $html; |
|
189 } |
|
190 |
|
191 /** |
|
192 * Merge attributes and parameters |
|
193 * |
|
194 * Also sets up requires |
|
195 * |
|
196 * @param array $attribs |
|
197 * @param array $params |
|
198 * @param string $type |
|
199 * @param string $dijit Dijit type to use (otherwise, pull from $_dijit) |
|
200 * @return array |
|
201 */ |
|
202 protected function _prepareDijit(array $attribs, array $params, $type, $dijit = null) |
|
203 { |
|
204 $this->dojo->requireModule($this->_module); |
|
205 |
|
206 switch ($type) { |
|
207 case 'layout': |
|
208 $stripParams = array('id'); |
|
209 break; |
|
210 case 'element': |
|
211 $stripParams = array('id', 'name', 'value', 'type'); |
|
212 foreach (array('checked', 'disabled', 'readonly') as $attrib) { |
|
213 if (array_key_exists($attrib, $attribs)) { |
|
214 if ($attribs[$attrib]) { |
|
215 $attribs[$attrib] = $attrib; |
|
216 } else { |
|
217 unset($attribs[$attrib]); |
|
218 } |
|
219 } |
|
220 } |
|
221 break; |
|
222 case 'textarea': |
|
223 $stripParams = array('id', 'name', 'type', 'degrade'); |
|
224 break; |
|
225 default: |
|
226 } |
|
227 |
|
228 foreach ($stripParams as $param) { |
|
229 if (array_key_exists($param, $params)) { |
|
230 unset($params[$param]); |
|
231 } |
|
232 } |
|
233 |
|
234 // Normalize constraints, if present |
|
235 foreach ($this->_jsonParams as $param) { |
|
236 if (array_key_exists($param, $params)) { |
|
237 require_once 'Zend/Json.php'; |
|
238 |
|
239 if (is_array($params[$param])) { |
|
240 $values = array(); |
|
241 foreach ($params[$param] as $key => $value) { |
|
242 if (!is_scalar($value)) { |
|
243 continue; |
|
244 } |
|
245 $values[$key] = $value; |
|
246 } |
|
247 } elseif (is_string($params[$param])) { |
|
248 $values = (array) $params[$param]; |
|
249 } else { |
|
250 $values = array(); |
|
251 } |
|
252 $values = Zend_Json::encode($values); |
|
253 if ($this->_useDeclarative()) { |
|
254 $values = str_replace('"', "'", $values); |
|
255 } |
|
256 $params[$param] = $values; |
|
257 } |
|
258 } |
|
259 |
|
260 $dijit = (null === $dijit) ? $this->_dijit : $dijit; |
|
261 if ($this->_useDeclarative()) { |
|
262 $attribs = array_merge($attribs, $params); |
|
263 if (isset($attribs['required'])) { |
|
264 $attribs['required'] = ($attribs['required']) ? 'true' : 'false'; |
|
265 } |
|
266 $attribs['dojoType'] = $dijit; |
|
267 } elseif (!$this->_useProgrammaticNoScript()) { |
|
268 $this->_createDijit($dijit, $attribs['id'], $params); |
|
269 } |
|
270 |
|
271 return $attribs; |
|
272 } |
|
273 |
|
274 /** |
|
275 * Create a dijit programmatically |
|
276 * |
|
277 * @param string $dijit |
|
278 * @param string $id |
|
279 * @param array $params |
|
280 * @return void |
|
281 */ |
|
282 protected function _createDijit($dijit, $id, array $params) |
|
283 { |
|
284 $params['dojoType'] = $dijit; |
|
285 |
|
286 array_walk_recursive($params, array($this, '_castBoolToString')); |
|
287 |
|
288 $this->dojo->setDijit($id, $params); |
|
289 } |
|
290 |
|
291 /** |
|
292 * Cast a boolean to a string value |
|
293 * |
|
294 * @param mixed $item |
|
295 * @param string $key |
|
296 * @return void |
|
297 */ |
|
298 protected function _castBoolToString(&$item, $key) |
|
299 { |
|
300 if (!is_bool($item)) { |
|
301 return; |
|
302 } |
|
303 $item = ($item) ? "true" : "false"; |
|
304 } |
|
305 |
|
306 /** |
|
307 * Render a hidden element to hold a value |
|
308 * |
|
309 * @param string $id |
|
310 * @param string|int|float $value |
|
311 * @return string |
|
312 */ |
|
313 protected function _renderHiddenElement($id, $value) |
|
314 { |
|
315 $hiddenAttribs = array( |
|
316 'name' => $id, |
|
317 'value' => (string) $value, |
|
318 'type' => 'hidden', |
|
319 ); |
|
320 return '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket(); |
|
321 } |
|
322 |
|
323 /** |
|
324 * Create JS function for retrieving parent form |
|
325 * |
|
326 * @return void |
|
327 */ |
|
328 protected function _createGetParentFormFunction() |
|
329 { |
|
330 $function =<<<EOJ |
|
331 if (zend == undefined) { |
|
332 var zend = {}; |
|
333 } |
|
334 zend.findParentForm = function(elementNode) { |
|
335 while (elementNode.nodeName.toLowerCase() != 'form') { |
|
336 elementNode = elementNode.parentNode; |
|
337 } |
|
338 return elementNode; |
|
339 }; |
|
340 EOJ; |
|
341 |
|
342 $this->dojo->addJavascript($function); |
|
343 } |
|
344 } |