PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Zend/Dojo/Form/Decorator/DijitContainer.php

https://github.com/shopaholiccompany/shopaholic
PHP | 200 lines | 96 code | 20 blank | 84 comment | 23 complexity | 561c7d63d8c283d5ff3ea0ddbbfb0bd2 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  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-2009 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_Abstract */
  21. // require_once 'Zend/Form/Decorator/Abstract.php';
  22. /**
  23. * Zend_Dojo_Form_Decorator_DijitContainer
  24. *
  25. * Render a dojo dijit layout container via a view helper
  26. *
  27. * Accepts the following options:
  28. * - helper: the name of the view helper to use
  29. *
  30. * Assumes the view helper accepts four parameters, the id, content, dijit
  31. * parameters, and (X)HTML attributes; these will be provided by the element.
  32. *
  33. * @uses Zend_Form_Decorator_Abstract
  34. * @package Zend_Dojo
  35. * @subpackage Form_Decorator
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @version $Id: DijitContainer.php 16204 2009-06-21 18:58:29Z thomas $
  39. */
  40. abstract class Zend_Dojo_Form_Decorator_DijitContainer extends Zend_Form_Decorator_Abstract
  41. {
  42. /**
  43. * View helper
  44. * @var string
  45. */
  46. protected $_helper;
  47. /**
  48. * Element attributes
  49. * @var array
  50. */
  51. protected $_attribs;
  52. /**
  53. * Dijit option parameters
  54. * @var array
  55. */
  56. protected $_dijitParams;
  57. /**
  58. * Container title
  59. * @var string
  60. */
  61. protected $_title;
  62. /**
  63. * Get view helper for rendering container
  64. *
  65. * @return string
  66. */
  67. public function getHelper()
  68. {
  69. if (null === $this->_helper) {
  70. // require_once 'Zend/Form/Decorator/Exception.php';
  71. throw new Zend_Form_Decorator_Exception('No view helper specified fo DijitContainer decorator');
  72. }
  73. return $this->_helper;
  74. }
  75. /**
  76. * Get element attributes
  77. *
  78. * @return array
  79. */
  80. public function getAttribs()
  81. {
  82. if (null === $this->_attribs) {
  83. $attribs = $this->getElement()->getAttribs();
  84. if (array_key_exists('dijitParams', $attribs)) {
  85. unset($attribs['dijitParams']);
  86. }
  87. $this->_attribs = $attribs;
  88. }
  89. return $this->_attribs;
  90. }
  91. /**
  92. * Get dijit option parameters
  93. *
  94. * @return array
  95. */
  96. public function getDijitParams()
  97. {
  98. if (null === $this->_dijitParams) {
  99. $attribs = $this->getElement()->getAttribs();
  100. if (array_key_exists('dijitParams', $attribs)) {
  101. $this->_dijitParams = $attribs['dijitParams'];
  102. } else {
  103. $this->_dijitParams = array();
  104. }
  105. $options = $this->getOptions();
  106. if (array_key_exists('dijitParams', $options)) {
  107. $this->_dijitParams = array_merge($this->_dijitParams, $options['dijitParams']);
  108. $this->removeOption('dijitParams');
  109. }
  110. }
  111. // Ensure we have a title param
  112. if (!array_key_exists('title', $this->_dijitParams)) {
  113. $this->_dijitParams['title'] = $this->getTitle();
  114. }
  115. return $this->_dijitParams;
  116. }
  117. /**
  118. * Get title
  119. *
  120. * @return string
  121. */
  122. public function getTitle()
  123. {
  124. if (null === $this->_title) {
  125. $title = null;
  126. if (null !== ($element = $this->getElement())) {
  127. if (method_exists($element, 'getLegend')) {
  128. $title = $element->getLegend();
  129. }
  130. }
  131. if (empty($title) && (null !== ($title = $this->getOption('legend')))) {
  132. $this->removeOption('legend');
  133. }
  134. if (empty($title) && (null !== ($title = $this->getOption('title')))) {
  135. $this->removeOption('title');
  136. }
  137. if (!empty($title)) {
  138. if (null !== ($translator = $element->getTranslator())) {
  139. $title = $translator->translate($title);
  140. }
  141. $this->_title = $title;
  142. }
  143. }
  144. return (empty($this->_title) ? '' : $this->_title);
  145. }
  146. /**
  147. * Render a dijit layout container
  148. *
  149. * Replaces $content entirely from currently set element.
  150. *
  151. * @param string $content
  152. * @return string
  153. */
  154. public function render($content)
  155. {
  156. $element = $this->getElement();
  157. $view = $element->getView();
  158. if (null === $view) {
  159. return $content;
  160. }
  161. $dijitParams = $this->getDijitParams();
  162. $attribs = array_merge($this->getAttribs(), $this->getOptions());
  163. if (array_key_exists('legend', $attribs)) {
  164. if (!array_key_exists('title', $dijitParams) || empty($dijitParams['title'])) {
  165. $dijitParams['title'] = $attribs['legend'];
  166. }
  167. unset($attribs['legend']);
  168. }
  169. $helper = $this->getHelper();
  170. $id = $element->getId() . '-' . $helper;
  171. if ($view->dojo()->hasDijit($id)) {
  172. trigger_error(sprintf('Duplicate dijit ID detected for id "%s; temporarily generating uniqid"', $id), E_USER_WARNING);
  173. $base = $id;
  174. do {
  175. $id = $base . '-' . uniqid();
  176. } while ($view->dojo()->hasDijit($id));
  177. }
  178. return $view->$helper($id, $content, $dijitParams, $attribs);
  179. }
  180. }