PageRenderTime 60ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Ebozavrik/test-application
PHP | 356 lines | 204 code | 28 blank | 124 comment | 11 complexity | 4715b5036fa3da234edcfffa94078c7a MD5 | raw file
  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-2012 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 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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. *
  71. * @return Zend_Dojo_View_Helper_Dijit
  72. */
  73. public function setView (Zend_View_Interface $view)
  74. {
  75. parent::setView($view);
  76. $this->dojo = $this->view->dojo();
  77. $this->dojo->enable();
  78. return $this;
  79. }
  80. /**
  81. * Get root node type
  82. *
  83. * @return string
  84. */
  85. public function getRootNode ()
  86. {
  87. return $this->_rootNode;
  88. }
  89. /**
  90. * Set root node type
  91. *
  92. * @param string $value
  93. *
  94. * @return Zend_Dojo_View_Helper_Dijit
  95. */
  96. public function setRootNode ($value)
  97. {
  98. $this->_rootNode = $value;
  99. return $this;
  100. }
  101. /**
  102. * Whether or not to use declarative dijit creation
  103. *
  104. * @return bool
  105. */
  106. protected function _useDeclarative ()
  107. {
  108. return Zend_Dojo_View_Helper_Dojo::useDeclarative();
  109. }
  110. /**
  111. * Whether or not to use programmatic dijit creation
  112. *
  113. * @return bool
  114. */
  115. protected function _useProgrammatic ()
  116. {
  117. return Zend_Dojo_View_Helper_Dojo::useProgrammatic();
  118. }
  119. /**
  120. * Whether or not to use programmatic dijit creation w/o script creation
  121. *
  122. * @return bool
  123. */
  124. protected function _useProgrammaticNoScript ()
  125. {
  126. return Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript();
  127. }
  128. /**
  129. * Create a layout container
  130. *
  131. * @param int $id
  132. * @param string $content
  133. * @param array $params
  134. * @param array $attribs
  135. * @param string|null $dijit
  136. *
  137. * @return string
  138. */
  139. protected function _createLayoutContainer ($id, $content, array $params, array $attribs, $dijit = null)
  140. {
  141. $attribs['id'] = $id;
  142. $attribs = $this->_prepareDijit($attribs, $params, 'layout', $dijit);
  143. $nodeType = $this->getRootNode();
  144. $html = '<' . $nodeType . $this->_htmlAttribs($attribs) . '>'
  145. . $content
  146. . "</$nodeType>\n";
  147. return $html;
  148. }
  149. /**
  150. * Create HTML representation of a dijit form element
  151. *
  152. * @param string $id
  153. * @param string $value
  154. * @param array $params
  155. * @param array $attribs
  156. * @param string|null $dijit
  157. *
  158. * @return string
  159. */
  160. public function _createFormElement ($id, $value, array $params, array $attribs, $dijit = null)
  161. {
  162. if (!array_key_exists('id', $attribs)) {
  163. $attribs['id'] = $id;
  164. }
  165. $attribs['name'] = $id;
  166. $attribs['value'] = (string)$value;
  167. $attribs['type'] = $this->_elementType;
  168. $attribs = $this->_prepareDijit($attribs, $params, 'element', $dijit);
  169. $html = '<input'
  170. . $this->_htmlAttribs($attribs)
  171. . $this->getClosingBracket();
  172. return $html;
  173. }
  174. /**
  175. * Merge attributes and parameters
  176. *
  177. * Also sets up requires
  178. *
  179. * @param array $attribs
  180. * @param array $params
  181. * @param string $type
  182. * @param string $dijit Dijit type to use (otherwise, pull from $_dijit)
  183. *
  184. * @return array
  185. */
  186. protected function _prepareDijit (array $attribs, array $params, $type, $dijit = null)
  187. {
  188. $this->dojo->requireModule($this->_module);
  189. switch ($type) {
  190. case 'layout':
  191. $stripParams = array( 'id' );
  192. break;
  193. case 'element':
  194. $stripParams = array( 'id', 'name', 'value', 'type' );
  195. foreach (array( 'checked', 'disabled', 'readonly' ) as $attrib) {
  196. if (array_key_exists($attrib, $attribs)) {
  197. if ($attribs[$attrib]) {
  198. $attribs[$attrib] = $attrib;
  199. } else {
  200. unset( $attribs[$attrib] );
  201. }
  202. }
  203. }
  204. break;
  205. case 'textarea':
  206. $stripParams = array( 'id', 'name', 'type', 'degrade' );
  207. break;
  208. default:
  209. }
  210. foreach ($stripParams as $param) {
  211. if (array_key_exists($param, $params)) {
  212. unset( $params[$param] );
  213. }
  214. }
  215. // Normalize constraints, if present
  216. foreach ($this->_jsonParams as $param) {
  217. if (array_key_exists($param, $params)) {
  218. require_once 'Zend/Json.php';
  219. if (is_array($params[$param])) {
  220. $values = array();
  221. foreach ($params[$param] as $key => $value) {
  222. if (!is_scalar($value)) {
  223. continue;
  224. }
  225. $values[$key] = $value;
  226. }
  227. } elseif (is_string($params[$param])) {
  228. $values = (array)$params[$param];
  229. } else {
  230. $values = array();
  231. }
  232. $values = Zend_Json::encode($values);
  233. if ($this->_useDeclarative()) {
  234. $values = str_replace('"', "'", $values);
  235. }
  236. $params[$param] = $values;
  237. }
  238. }
  239. $dijit = ( null === $dijit ) ? $this->_dijit : $dijit;
  240. if ($this->_useDeclarative()) {
  241. $attribs = array_merge($attribs, $params);
  242. if (isset( $attribs['required'] )) {
  243. $attribs['required'] = ( $attribs['required'] ) ? 'true' : 'false';
  244. }
  245. $attribs['dojoType'] = $dijit;
  246. } elseif (!$this->_useProgrammaticNoScript()) {
  247. $this->_createDijit($dijit, $attribs['id'], $params);
  248. }
  249. return $attribs;
  250. }
  251. /**
  252. * Create a dijit programmatically
  253. *
  254. * @param string $dijit
  255. * @param string $id
  256. * @param array $params
  257. *
  258. * @return void
  259. */
  260. protected function _createDijit ($dijit, $id, array $params)
  261. {
  262. $params['dojoType'] = $dijit;
  263. array_walk_recursive($params, array( $this, '_castBoolToString' ));
  264. $this->dojo->setDijit($id, $params);
  265. }
  266. /**
  267. * Cast a boolean to a string value
  268. *
  269. * @param mixed $item
  270. * @param string $key
  271. *
  272. * @return void
  273. */
  274. protected function _castBoolToString (&$item, $key)
  275. {
  276. if (!is_bool($item)) {
  277. return;
  278. }
  279. $item = ( $item ) ? "true" : "false";
  280. }
  281. /**
  282. * Render a hidden element to hold a value
  283. *
  284. * @param string $id
  285. * @param string|int|float $value
  286. *
  287. * @return string
  288. */
  289. protected function _renderHiddenElement ($id, $value)
  290. {
  291. $hiddenAttribs = array(
  292. 'name' => $id,
  293. 'value' => (string)$value,
  294. 'type' => 'hidden',
  295. );
  296. return '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket();
  297. }
  298. /**
  299. * Create JS function for retrieving parent form
  300. *
  301. * @return void
  302. */
  303. protected function _createGetParentFormFunction ()
  304. {
  305. $function = <<<EOJ
  306. if (zend == undefined) {
  307. var zend = {};
  308. }
  309. zend.findParentForm = function(elementNode) {
  310. while (elementNode.nodeName.toLowerCase() != 'form') {
  311. elementNode = elementNode.parentNode;
  312. }
  313. return elementNode;
  314. };
  315. EOJ;
  316. $this->dojo->addJavascript($function);
  317. }
  318. }