PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Dojo/View/Helper/Dijit.php

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