UiWidget.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 ZendX
  16. * @package ZendX_JQuery
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2010 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. require_once "Zend/Form/Element.php";
  23. /**
  24. * Base Form Element for jQuery View Helpers
  25. *
  26. * @package ZendX_JQuery
  27. * @subpackage Form
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class ZendX_JQuery_Form_Element_UiWidget extends Zend_Form_Element
  32. {
  33. /**
  34. * jQuery related parameters of this form element.
  35. *
  36. * @var array
  37. */
  38. public $jQueryParams = array();
  39. /**
  40. * Just here to prevent errors.
  41. *
  42. * @var array
  43. */
  44. public $options = array();
  45. /**
  46. * Constructor
  47. *
  48. * @param mixed $spec
  49. * @param mixed $options
  50. * @return void
  51. */
  52. public function __construct($spec, $options = null)
  53. {
  54. $this->addPrefixPath('ZendX_JQuery_Form_Decorator', 'ZendX/JQuery/Form/Decorator', 'decorator');
  55. parent::__construct($spec, $options);
  56. }
  57. /**
  58. * Get jQuery related parameter of this form element
  59. *
  60. * @param string $key
  61. * @return string
  62. */
  63. public function getJQueryParam($key)
  64. {
  65. $key = (string) $key;
  66. return $this->jQueryParams[$key];
  67. }
  68. /**
  69. * Get all currently known jQuery related parameters of this element
  70. *
  71. * @return array
  72. */
  73. public function getJQueryParams()
  74. {
  75. return $this->jQueryParams;
  76. }
  77. /**
  78. * Set a jQuery related parameter of this form element.
  79. *
  80. * @param string $key
  81. * @param string $value
  82. * @return ZendX_JQuery_Form_Element_UiWidget
  83. */
  84. public function setJQueryParam($key, $value)
  85. {
  86. $key = (string) $key;
  87. $this->jQueryParams[$key] = $value;
  88. return $this;
  89. }
  90. /**
  91. * Set an array of jQuery related options for this element (merging with old options).
  92. *
  93. * @param Array $params
  94. * @return ZendX_JQuery_Form_Element_UiWidget
  95. */
  96. public function setJQueryParams($params)
  97. {
  98. $this->jQueryParams = array_merge($this->jQueryParams, $params);
  99. return $this;
  100. }
  101. /**
  102. * Load default decorators
  103. *
  104. * @return void
  105. */
  106. public function loadDefaultDecorators()
  107. {
  108. if ($this->loadDefaultDecoratorsIsDisabled()) {
  109. return;
  110. }
  111. $decorators = $this->getDecorators();
  112. if (empty($decorators)) {
  113. $this->addDecorator('UiWidgetElement')
  114. ->addDecorator('Errors')
  115. ->addDecorator('Description', array('tag' => 'p', 'class' => 'description'))
  116. ->addDecorator('HtmlTag', array('tag' => 'dd'))
  117. ->addDecorator('Label', array('tag' => 'dt'));
  118. }
  119. }
  120. /**
  121. * Set the view object
  122. *
  123. * Ensures that the view object has the jQuery view helper path set.
  124. *
  125. * @param Zend_View_Interface $view
  126. * @return ZendX_JQuery_Form_Element_UiWidget
  127. */
  128. public function setView(Zend_View_Interface $view = null)
  129. {
  130. if (null !== $view) {
  131. if (false === $view->getPluginLoader('helper')->getPaths('ZendX_JQuery_View_Helper')) {
  132. $view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');
  133. }
  134. }
  135. return parent::setView($view);
  136. }
  137. /**
  138. * Retrieve all decorators
  139. *
  140. * @throws ZendX_JQuery_Form_Exception
  141. * @return array
  142. */
  143. public function getDecorators()
  144. {
  145. $decorators = parent::getDecorators();
  146. if(count($decorators) > 0) {
  147. // Only check this if there are decorators present, otherwise it could
  148. // be that the decorators have not been initialized yet.
  149. $foundUiWidgetElementMarker = false;
  150. foreach($decorators AS $decorator) {
  151. if($decorator instanceof ZendX_JQuery_Form_Decorator_UiWidgetElementMarker) {
  152. $foundUiWidgetElementMarker = true;
  153. }
  154. }
  155. if($foundUiWidgetElementMarker === false) {
  156. require_once "ZendX/JQuery/Form/Exception.php";
  157. throw new ZendX_JQuery_Form_Exception(
  158. "Cannot render jQuery form element without at least one decorator ".
  159. "implementing the 'ZendX_JQuery_Form_Decorator_UiWidgetElementMarker' interface. ".
  160. "Default decorator for this marker interface is the 'ZendX_JQuery_Form_Decorator_UiWidgetElement'. ".
  161. "Hint: The ViewHelper decorator does not render jQuery elements correctly."
  162. );
  163. }
  164. }
  165. return $decorators;
  166. }
  167. }