PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Dojo/Form/Decorator/DijitElement.php

https://gitlab.com/devtoannh/cafe
PHP | 193 lines | 86 code | 17 blank | 90 comment | 8 complexity | d1195c942646a0d492b183db4ed97072 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_Form
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Form_Decorator_ViewHelper */
  21. require_once 'Zend/Form/Decorator/ViewHelper.php';
  22. /**
  23. * Zend_Dojo_Form_Decorator_DijitElement
  24. *
  25. * Render a dojo dijit element via a view helper
  26. *
  27. * Accepts the following options:
  28. * - separator: string with which to separate passed in content and generated content
  29. * - placement: whether to append or prepend the generated content to the passed in content
  30. * - helper: the name of the view helper to use
  31. *
  32. * Assumes the view helper accepts three parameters, the name, value, and
  33. * optional attributes; these will be provided by the element.
  34. *
  35. * @package Zend_Dojo
  36. * @subpackage Form_Decorator
  37. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @version $Id: DijitElement.php 23775 2011-03-01 17:25:24Z ralph $
  40. */
  41. class Zend_Dojo_Form_Decorator_DijitElement extends Zend_Form_Decorator_ViewHelper
  42. {
  43. /**
  44. * Element attributes
  45. * @var array
  46. */
  47. protected $_attribs;
  48. /**
  49. * Element types that represent buttons
  50. * @var array
  51. */
  52. protected $_buttonTypes = array(
  53. 'Zend_Dojo_Form_Element_Button',
  54. 'Zend_Form_Element_Button',
  55. 'Zend_Form_Element_Reset',
  56. 'Zend_Form_Element_Submit',
  57. );
  58. /**
  59. * Dijit option parameters
  60. * @var array
  61. */
  62. protected $_dijitParams = array();
  63. /**
  64. * Get element attributes
  65. *
  66. * @return array
  67. */
  68. public function getElementAttribs()
  69. {
  70. if (null === $this->_attribs) {
  71. $this->_attribs = parent::getElementAttribs();
  72. if (array_key_exists('dijitParams', $this->_attribs)) {
  73. $this->setDijitParams($this->_attribs['dijitParams']);
  74. unset($this->_attribs['dijitParams']);
  75. }
  76. }
  77. return $this->_attribs;
  78. }
  79. /**
  80. * Set a single dijit option parameter
  81. *
  82. * @param string $key
  83. * @param mixed $value
  84. * @return Zend_Dojo_Form_Decorator_DijitContainer
  85. */
  86. public function setDijitParam($key, $value)
  87. {
  88. $this->_dijitParams[(string) $key] = $value;
  89. return $this;
  90. }
  91. /**
  92. * Set dijit option parameters
  93. *
  94. * @param array $params
  95. * @return Zend_Dojo_Form_Decorator_DijitContainer
  96. */
  97. public function setDijitParams(array $params)
  98. {
  99. $this->_dijitParams = array_merge($this->_dijitParams, $params);
  100. return $this;
  101. }
  102. /**
  103. * Retrieve a single dijit option parameter
  104. *
  105. * @param string $key
  106. * @return mixed|null
  107. */
  108. public function getDijitParam($key)
  109. {
  110. $this->getElementAttribs();
  111. $key = (string) $key;
  112. if (array_key_exists($key, $this->_dijitParams)) {
  113. return $this->_dijitParams[$key];
  114. }
  115. return null;
  116. }
  117. /**
  118. * Get dijit option parameters
  119. *
  120. * @return array
  121. */
  122. public function getDijitParams()
  123. {
  124. $this->getElementAttribs();
  125. return $this->_dijitParams;
  126. }
  127. /**
  128. * Render an element using a view helper
  129. *
  130. * Determine view helper from 'helper' option, or, if none set, from
  131. * the element type. Then call as
  132. * helper($element->getName(), $element->getValue(), $element->getAttribs())
  133. *
  134. * @param string $content
  135. * @return string
  136. * @throws Zend_Form_Decorator_Exception if element or view are not registered
  137. */
  138. public function render($content)
  139. {
  140. $element = $this->getElement();
  141. $view = $element->getView();
  142. if (null === $view) {
  143. require_once 'Zend/Form/Decorator/Exception.php';
  144. throw new Zend_Form_Decorator_Exception('DijitElement decorator cannot render without a registered view object');
  145. }
  146. $options = null;
  147. $helper = $this->getHelper();
  148. $separator = $this->getSeparator();
  149. $value = $this->getValue($element);
  150. $attribs = $this->getElementAttribs();
  151. $name = $element->getFullyQualifiedName();
  152. $dijitParams = $this->getDijitParams();
  153. $dijitParams['required'] = $element->isRequired();
  154. $id = $element->getId();
  155. if ($view->dojo()->hasDijit($id)) {
  156. trigger_error(sprintf('Duplicate dijit ID detected for id "%s; temporarily generating uniqid"', $id), E_USER_NOTICE);
  157. $base = $id;
  158. do {
  159. $id = $base . '-' . uniqid();
  160. } while ($view->dojo()->hasDijit($id));
  161. }
  162. $attribs['id'] = $id;
  163. if (array_key_exists('options', $attribs)) {
  164. $options = $attribs['options'];
  165. }
  166. $elementContent = $view->$helper($name, $value, $dijitParams, $attribs, $options);
  167. switch ($this->getPlacement()) {
  168. case self::APPEND:
  169. return $content . $separator . $elementContent;
  170. case self::PREPEND:
  171. return $elementContent . $separator . $content;
  172. default:
  173. return $elementContent;
  174. }
  175. }
  176. }