PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Form/Decorator/ViewScript.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 192 lines | 84 code | 19 blank | 89 comment | 13 complexity | 964392c0ef893fcfe558ca7b582d036d 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. * @subpackage Decorator
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Form_Decorator_Abstract */
  22. require_once 'Zend/Form/Decorator/Abstract.php';
  23. /**
  24. * Zend_Form_Decorator_ViewScript
  25. *
  26. * Render a view script as a decorator
  27. *
  28. * Accepts the options:
  29. * - separator: separator to use between view script content and provided content (defaults to PHP_EOL)
  30. * - placement: whether to append or prepend view script content to provided content (defaults to prepend)
  31. * - viewScript: view script to use
  32. * - viewModule: module that view script is in (optional)
  33. *
  34. * The view script is rendered as a partial; the element being decorated is
  35. * passed in as the 'element' variable:
  36. * <code>
  37. * // in view script:
  38. * echo $this->element->getLabel();
  39. * </code>
  40. *
  41. * Any options other than separator, placement, viewScript, and viewModule are passed to
  42. * the partial as local variables.
  43. *
  44. * @category Zend
  45. * @package Zend_Form
  46. * @subpackage Decorator
  47. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  48. * @license http://framework.zend.com/license/new-bsd New BSD License
  49. * @version $Id: ViewScript.php 23775 2011-03-01 17:25:24Z ralph $
  50. */
  51. class Zend_Form_Decorator_ViewScript extends Zend_Form_Decorator_Abstract
  52. {
  53. /**
  54. * Default placement: append
  55. * @var string
  56. */
  57. protected $_placement = 'APPEND';
  58. /**
  59. * View script to render
  60. * @var string
  61. */
  62. protected $_viewScript;
  63. /**
  64. * View script module
  65. * @var string
  66. */
  67. protected $_viewModule;
  68. /**
  69. * Set view script
  70. *
  71. * @param string $script
  72. * @return Zend_Form_Decorator_ViewScript
  73. */
  74. public function setViewScript($script)
  75. {
  76. $this->_viewScript = (string) $script;
  77. return $this;
  78. }
  79. /**
  80. * Get view script
  81. *
  82. * @return string|null
  83. */
  84. public function getViewScript()
  85. {
  86. if (null === $this->_viewScript) {
  87. if (null !== ($element = $this->getElement())) {
  88. if (null !== ($viewScript = $element->getAttrib('viewScript'))) {
  89. $this->setViewScript($viewScript);
  90. return $viewScript;
  91. }
  92. }
  93. if (null !== ($viewScript = $this->getOption('viewScript'))) {
  94. $this->setViewScript($viewScript)
  95. ->removeOption('viewScript');
  96. }
  97. }
  98. return $this->_viewScript;
  99. }
  100. /**
  101. * Set view script module
  102. *
  103. * @param string $module
  104. * @return Zend_Form_Decorator_ViewScript
  105. */
  106. public function setViewModule($viewModule)
  107. {
  108. $this->_viewModule = (string) $viewModule;
  109. return $this;
  110. }
  111. /**
  112. * Get view script module
  113. *
  114. * @return string|null
  115. */
  116. public function getViewModule()
  117. {
  118. if (null === $this->_viewModule) {
  119. if (null !== ($element = $this->getElement())) {
  120. if (null !== ($viewModule = $element->getAttrib('viewModule'))) {
  121. $this->setViewModule($viewModule);
  122. return $viewModule;
  123. }
  124. }
  125. if (null !== ($viewModule = $this->getOption('viewModule'))) {
  126. $this->setViewModule($viewModule)
  127. ->removeOption('viewModule');
  128. }
  129. }
  130. return $this->_viewModule;
  131. }
  132. /**
  133. * Render a view script
  134. *
  135. * @param string $content
  136. * @return string
  137. */
  138. public function render($content)
  139. {
  140. $element = $this->getElement();
  141. $view = $element->getView();
  142. if (null === $view) {
  143. return $content;
  144. }
  145. $viewScript = $this->getViewScript();
  146. if (empty($viewScript)) {
  147. require_once 'Zend/Form/Exception.php';
  148. throw new Zend_Form_Exception('No view script registered with ViewScript decorator');
  149. }
  150. $separator = $this->getSeparator();
  151. $placement = $this->getPlacement();
  152. $vars = $this->getOptions();
  153. $vars['element'] = $element;
  154. $vars['content'] = $content;
  155. $vars['decorator'] = $this;
  156. $viewModule = $this->getViewModule();
  157. if (empty($viewModule)) {
  158. $renderedContent = $view->partial($viewScript, $vars);
  159. } else {
  160. $renderedContent = $view->partial($viewScript, $viewModule, $vars);
  161. }
  162. // Get placement again to see if it has changed
  163. $placement = $this->getPlacement();
  164. switch ($placement) {
  165. case self::PREPEND:
  166. return $renderedContent . $separator . $content;
  167. case self::APPEND:
  168. return $content . $separator . $renderedContent;
  169. default:
  170. return $renderedContent;
  171. }
  172. }
  173. }