Dijit.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2009 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. * Set view
  61. *
  62. * Set view and enable dojo
  63. *
  64. * @param Zend_View_Interface $view
  65. * @return Zend_Dojo_View_Helper_Dijit
  66. */
  67. public function setView(Zend_View_Interface $view)
  68. {
  69. parent::setView($view);
  70. $this->dojo = $this->view->dojo();
  71. $this->dojo->enable();
  72. return $this;
  73. }
  74. /**
  75. * Whether or not to use declarative dijit creation
  76. *
  77. * @return bool
  78. */
  79. protected function _useDeclarative()
  80. {
  81. return Zend_Dojo_View_Helper_Dojo::useDeclarative();
  82. }
  83. /**
  84. * Whether or not to use programmatic dijit creation
  85. *
  86. * @return bool
  87. */
  88. protected function _useProgrammatic()
  89. {
  90. return Zend_Dojo_View_Helper_Dojo::useProgrammatic();
  91. }
  92. /**
  93. * Whether or not to use programmatic dijit creation w/o script creation
  94. *
  95. * @return bool
  96. */
  97. protected function _useProgrammaticNoScript()
  98. {
  99. return Zend_Dojo_View_Helper_Dojo::useProgrammaticNoScript();
  100. }
  101. /**
  102. * Create a layout container
  103. *
  104. * @param int $id
  105. * @param string $content
  106. * @param array $params
  107. * @param array $attribs
  108. * @param string|null $dijit
  109. * @return string
  110. */
  111. protected function _createLayoutContainer($id, $content, array $params, array $attribs, $dijit = null)
  112. {
  113. $attribs['id'] = $id;
  114. $attribs = $this->_prepareDijit($attribs, $params, 'layout', $dijit);
  115. $html = '<div' . $this->_htmlAttribs($attribs) . '>'
  116. . $content
  117. . "</div>\n";
  118. return $html;
  119. }
  120. /**
  121. * Create HTML representation of a dijit form element
  122. *
  123. * @param string $id
  124. * @param string $value
  125. * @param array $params
  126. * @param array $attribs
  127. * @param string|null $dijit
  128. * @return string
  129. */
  130. public function _createFormElement($id, $value, array $params, array $attribs, $dijit = null)
  131. {
  132. if (!array_key_exists('id', $attribs)) {
  133. $attribs['id'] = $id;
  134. }
  135. $attribs['name'] = $id;
  136. $attribs['value'] = (string) $value;
  137. $attribs['type'] = $this->_elementType;
  138. $attribs = $this->_prepareDijit($attribs, $params, 'element', $dijit);
  139. $html = '<input'
  140. . $this->_htmlAttribs($attribs)
  141. . $this->getClosingBracket();
  142. return $html;
  143. }
  144. /**
  145. * Merge attributes and parameters
  146. *
  147. * Also sets up requires
  148. *
  149. * @param array $attribs
  150. * @param array $params
  151. * @param string $type
  152. * @param string $dijit Dijit type to use (otherwise, pull from $_dijit)
  153. * @return array
  154. */
  155. protected function _prepareDijit(array $attribs, array $params, $type, $dijit = null)
  156. {
  157. $this->dojo->requireModule($this->_module);
  158. switch ($type) {
  159. case 'layout':
  160. $stripParams = array('id');
  161. break;
  162. case 'element':
  163. $stripParams = array('id', 'name', 'value', 'type');
  164. foreach (array('checked', 'disabled', 'readonly') as $attrib) {
  165. if (array_key_exists($attrib, $attribs)) {
  166. if ($attribs[$attrib]) {
  167. $attribs[$attrib] = $attrib;
  168. } else {
  169. unset($attribs[$attrib]);
  170. }
  171. }
  172. }
  173. break;
  174. case 'textarea':
  175. $stripParams = array('id', 'name', 'type');
  176. break;
  177. default:
  178. }
  179. foreach ($stripParams as $param) {
  180. if (array_key_exists($param, $params)) {
  181. unset($params[$param]);
  182. }
  183. }
  184. // Normalize constraints, if present
  185. foreach ($this->_jsonParams as $param) {
  186. if (array_key_exists($param, $params)) {
  187. require_once 'Zend/Json.php';
  188. if (is_array($params[$param])) {
  189. $values = array();
  190. foreach ($params[$param] as $key => $value) {
  191. if (!is_scalar($value)) {
  192. continue;
  193. }
  194. $values[$key] = $value;
  195. }
  196. } elseif (is_string($params[$param])) {
  197. $values = (array) $params[$param];
  198. } else {
  199. $values = array();
  200. }
  201. $values = Zend_Json::encode($values);
  202. if ($this->_useDeclarative()) {
  203. $values = str_replace('"', "'", $values);
  204. }
  205. $params[$param] = $values;
  206. }
  207. }
  208. $dijit = (null === $dijit) ? $this->_dijit : $dijit;
  209. if ($this->_useDeclarative()) {
  210. $attribs = array_merge($attribs, $params);
  211. $attribs['dojoType'] = $dijit;
  212. } elseif (!$this->_useProgrammaticNoScript()) {
  213. $this->_createDijit($dijit, $attribs['id'], $params);
  214. }
  215. return $attribs;
  216. }
  217. /**
  218. * Create a dijit programmatically
  219. *
  220. * @param string $dijit
  221. * @param string $id
  222. * @param array $params
  223. * @return void
  224. */
  225. protected function _createDijit($dijit, $id, array $params)
  226. {
  227. $params['dojoType'] = $dijit;
  228. array_walk_recursive($params, array($this, '_castBoolToString'));
  229. $this->dojo->setDijit($id, $params);
  230. }
  231. /**
  232. * Cast a boolean to a string value
  233. *
  234. * @param mixed $item
  235. * @param string $key
  236. * @return void
  237. */
  238. protected function _castBoolToString(&$item, $key)
  239. {
  240. if (!is_bool($item)) {
  241. return;
  242. }
  243. $item = ($item) ? "true" : "false";
  244. }
  245. /**
  246. * Render a hidden element to hold a value
  247. *
  248. * @param string $id
  249. * @param string|int|float $value
  250. * @return string
  251. */
  252. protected function _renderHiddenElement($id, $value)
  253. {
  254. $hiddenAttribs = array(
  255. 'name' => $id,
  256. 'value' => (string) $value,
  257. 'type' => 'hidden',
  258. );
  259. return '<input' . $this->_htmlAttribs($hiddenAttribs) . $this->getClosingBracket();
  260. }
  261. /**
  262. * Create JS function for retrieving parent form
  263. *
  264. * @return void
  265. */
  266. protected function _createGetParentFormFunction()
  267. {
  268. $function =<<<EOJ
  269. if (zend == undefined) {
  270. var zend = {};
  271. }
  272. zend.findParentForm = function(elementNode) {
  273. while (elementNode.nodeName.toLowerCase() != 'form') {
  274. elementNode = elementNode.parentNode;
  275. }
  276. return elementNode;
  277. };
  278. EOJ;
  279. $this->dojo->addJavascript($function);
  280. }
  281. }