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