Dijit.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 Form_Element
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Form_Element */
  22. require_once 'Zend/Form/Element.php';
  23. /**
  24. * Base element for dijit elements
  25. *
  26. * @category Zend
  27. * @package Zend_Dojo
  28. * @subpackage Form_Element
  29. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @version $Id$
  32. */
  33. abstract class Zend_Dojo_Form_Element_Dijit extends Zend_Form_Element
  34. {
  35. /**
  36. * Dijit parameters
  37. * @var array
  38. */
  39. public $dijitParams = array();
  40. /**
  41. * View helper to use
  42. * @var string
  43. */
  44. public $helper;
  45. /**
  46. * Constructor
  47. *
  48. * @todo Should we set dojo view helper paths here?
  49. * @param mixed $spec
  50. * @param mixed $options
  51. * @return void
  52. */
  53. public function __construct($spec, $options = null)
  54. {
  55. $this->addPrefixPath('Zend_Dojo_Form_Decorator', 'Zend/Dojo/Form/Decorator', 'decorator');
  56. parent::__construct($spec, $options);
  57. }
  58. /**
  59. * Set a dijit parameter
  60. *
  61. * @param string $key
  62. * @param mixed $value
  63. * @return Zend_Dojo_Form_Element_Dijit
  64. */
  65. public function setDijitParam($key, $value)
  66. {
  67. $key = (string) $key;
  68. $this->dijitParams[$key] = $value;
  69. return $this;
  70. }
  71. /**
  72. * Set multiple dijit params at once
  73. *
  74. * @param array $params
  75. * @return Zend_Dojo_Form_Element_Dijit
  76. */
  77. public function setDijitParams(array $params)
  78. {
  79. $this->dijitParams = array_merge($this->dijitParams, $params);
  80. return $this;
  81. }
  82. /**
  83. * Does the given dijit parameter exist?
  84. *
  85. * @param string $key
  86. * @return bool
  87. */
  88. public function hasDijitParam($key)
  89. {
  90. return array_key_exists($key, $this->dijitParams);
  91. }
  92. /**
  93. * Get a single dijit parameter
  94. *
  95. * @param string $key
  96. * @return mixed
  97. */
  98. public function getDijitParam($key)
  99. {
  100. $key = (string) $key;
  101. if ($this->hasDijitParam($key)) {
  102. return $this->dijitParams[$key];
  103. }
  104. return null;
  105. }
  106. /**
  107. * Retrieve all dijit parameters
  108. *
  109. * @return array
  110. */
  111. public function getDijitParams()
  112. {
  113. return $this->dijitParams;
  114. }
  115. /**
  116. * Remove a single dijit parameter
  117. *
  118. * @param string $key
  119. * @return Zend_Dojo_Form_Element_Dijit
  120. */
  121. public function removeDijitParam($key)
  122. {
  123. $key = (string) $key;
  124. if (array_key_exists($key, $this->dijitParams)) {
  125. unset($this->dijitParams[$key]);
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Clear all dijit parameters
  131. *
  132. * @return Zend_Dojo_Form_Element_Dijit
  133. */
  134. public function clearDijitParams()
  135. {
  136. $this->dijitParams = array();
  137. return $this;
  138. }
  139. /**
  140. * Load default decorators
  141. *
  142. * @return void
  143. */
  144. public function loadDefaultDecorators()
  145. {
  146. if ($this->loadDefaultDecoratorsIsDisabled()) {
  147. return;
  148. }
  149. $decorators = $this->getDecorators();
  150. if (empty($decorators)) {
  151. $this->addDecorator('DijitElement')
  152. ->addDecorator('Errors')
  153. ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
  154. ->addDecorator('HtmlTag', array('tag' => 'dd'))
  155. ->addDecorator('Label', array('tag' => 'dt'));
  156. }
  157. }
  158. /**
  159. * Set the view object
  160. *
  161. * Ensures that the view object has the dojo view helper path set.
  162. *
  163. * @param Zend_View_Interface $view
  164. * @return Zend_Dojo_Form_Element_Dijit
  165. */
  166. public function setView(Zend_View_Interface $view = null)
  167. {
  168. if (null !== $view) {
  169. if (false === $view->getPluginLoader('helper')->getPaths('Zend_Dojo_View_Helper')) {
  170. $view->addHelperPath('Zend/Dojo/View/Helper', 'Zend_Dojo_View_Helper');
  171. }
  172. }
  173. return parent::setView($view);
  174. }
  175. }