PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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