|
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-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 */ |
|
20 |
|
21 /** Zend_Form_Decorator_Abstract */ |
|
22 require_once 'Zend/Form/Decorator/Abstract.php'; |
|
23 |
|
24 /** |
|
25 * Zend_Form_Decorator_ViewHelper |
|
26 * |
|
27 * Decorate an element by using a view helper to render it. |
|
28 * |
|
29 * Accepts the following options: |
|
30 * - separator: string with which to separate passed in content and generated content |
|
31 * - placement: whether to append or prepend the generated content to the passed in content |
|
32 * - helper: the name of the view helper to use |
|
33 * |
|
34 * Assumes the view helper accepts three parameters, the name, value, and |
|
35 * optional attributes; these will be provided by the element. |
|
36 * |
|
37 * @category Zend |
|
38 * @package Zend_Form |
|
39 * @subpackage Decorator |
|
40 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
41 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
42 * @version $Id: ViewHelper.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
43 */ |
|
44 class Zend_Form_Decorator_ViewHelper extends Zend_Form_Decorator_Abstract |
|
45 { |
|
46 /** |
|
47 * Element types that represent buttons |
|
48 * @var array |
|
49 */ |
|
50 protected $_buttonTypes = array( |
|
51 'Zend_Form_Element_Button', |
|
52 'Zend_Form_Element_Reset', |
|
53 'Zend_Form_Element_Submit', |
|
54 ); |
|
55 |
|
56 /** |
|
57 * View helper to use when rendering |
|
58 * @var string |
|
59 */ |
|
60 protected $_helper; |
|
61 |
|
62 /** |
|
63 * Set view helper to use when rendering |
|
64 * |
|
65 * @param string $helper |
|
66 * @return Zend_Form_Decorator_Element_ViewHelper |
|
67 */ |
|
68 public function setHelper($helper) |
|
69 { |
|
70 $this->_helper = (string) $helper; |
|
71 return $this; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Retrieve view helper for rendering element |
|
76 * |
|
77 * @return string |
|
78 */ |
|
79 public function getHelper() |
|
80 { |
|
81 if (null === $this->_helper) { |
|
82 $options = $this->getOptions(); |
|
83 if (isset($options['helper'])) { |
|
84 $this->setHelper($options['helper']); |
|
85 $this->removeOption('helper'); |
|
86 } else { |
|
87 $element = $this->getElement(); |
|
88 if (null !== $element) { |
|
89 if (null !== ($helper = $element->getAttrib('helper'))) { |
|
90 $this->setHelper($helper); |
|
91 } else { |
|
92 $type = $element->getType(); |
|
93 if ($pos = strrpos($type, '_')) { |
|
94 $type = substr($type, $pos + 1); |
|
95 } |
|
96 $this->setHelper('form' . ucfirst($type)); |
|
97 } |
|
98 } |
|
99 } |
|
100 } |
|
101 |
|
102 return $this->_helper; |
|
103 } |
|
104 |
|
105 /** |
|
106 * Get name |
|
107 * |
|
108 * If element is a Zend_Form_Element, will attempt to namespace it if the |
|
109 * element belongs to an array. |
|
110 * |
|
111 * @return string |
|
112 */ |
|
113 public function getName() |
|
114 { |
|
115 if (null === ($element = $this->getElement())) { |
|
116 return ''; |
|
117 } |
|
118 |
|
119 $name = $element->getName(); |
|
120 |
|
121 if (!$element instanceof Zend_Form_Element) { |
|
122 return $name; |
|
123 } |
|
124 |
|
125 if (null !== ($belongsTo = $element->getBelongsTo())) { |
|
126 $name = $belongsTo . '[' |
|
127 . $name |
|
128 . ']'; |
|
129 } |
|
130 |
|
131 if ($element->isArray()) { |
|
132 $name .= '[]'; |
|
133 } |
|
134 |
|
135 return $name; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Retrieve element attributes |
|
140 * |
|
141 * Set id to element name and/or array item. |
|
142 * |
|
143 * @return array |
|
144 */ |
|
145 public function getElementAttribs() |
|
146 { |
|
147 if (null === ($element = $this->getElement())) { |
|
148 return null; |
|
149 } |
|
150 |
|
151 $attribs = $element->getAttribs(); |
|
152 if (isset($attribs['helper'])) { |
|
153 unset($attribs['helper']); |
|
154 } |
|
155 |
|
156 if (method_exists($element, 'getSeparator')) { |
|
157 if (null !== ($listsep = $element->getSeparator())) { |
|
158 $attribs['listsep'] = $listsep; |
|
159 } |
|
160 } |
|
161 |
|
162 if (isset($attribs['id'])) { |
|
163 return $attribs; |
|
164 } |
|
165 |
|
166 $id = $element->getName(); |
|
167 |
|
168 if ($element instanceof Zend_Form_Element) { |
|
169 if (null !== ($belongsTo = $element->getBelongsTo())) { |
|
170 $belongsTo = preg_replace('/\[([^\]]+)\]/', '-$1', $belongsTo); |
|
171 $id = $belongsTo . '-' . $id; |
|
172 } |
|
173 } |
|
174 |
|
175 $element->setAttrib('id', $id); |
|
176 $attribs['id'] = $id; |
|
177 |
|
178 return $attribs; |
|
179 } |
|
180 |
|
181 /** |
|
182 * Get value |
|
183 * |
|
184 * If element type is one of the button types, returns the label. |
|
185 * |
|
186 * @param Zend_Form_Element $element |
|
187 * @return string|null |
|
188 */ |
|
189 public function getValue($element) |
|
190 { |
|
191 if (!$element instanceof Zend_Form_Element) { |
|
192 return null; |
|
193 } |
|
194 |
|
195 foreach ($this->_buttonTypes as $type) { |
|
196 if ($element instanceof $type) { |
|
197 if (stristr($type, 'button')) { |
|
198 $element->content = $element->getLabel(); |
|
199 return null; |
|
200 } |
|
201 return $element->getLabel(); |
|
202 } |
|
203 } |
|
204 |
|
205 return $element->getValue(); |
|
206 } |
|
207 |
|
208 /** |
|
209 * Render an element using a view helper |
|
210 * |
|
211 * Determine view helper from 'viewHelper' option, or, if none set, from |
|
212 * the element type. Then call as |
|
213 * helper($element->getName(), $element->getValue(), $element->getAttribs()) |
|
214 * |
|
215 * @param string $content |
|
216 * @return string |
|
217 * @throws Zend_Form_Decorator_Exception if element or view are not registered |
|
218 */ |
|
219 public function render($content) |
|
220 { |
|
221 $element = $this->getElement(); |
|
222 |
|
223 $view = $element->getView(); |
|
224 if (null === $view) { |
|
225 require_once 'Zend/Form/Decorator/Exception.php'; |
|
226 throw new Zend_Form_Decorator_Exception('ViewHelper decorator cannot render without a registered view object'); |
|
227 } |
|
228 |
|
229 if (method_exists($element, 'getMultiOptions')) { |
|
230 $element->getMultiOptions(); |
|
231 } |
|
232 |
|
233 $helper = $this->getHelper(); |
|
234 $separator = $this->getSeparator(); |
|
235 $value = $this->getValue($element); |
|
236 $attribs = $this->getElementAttribs(); |
|
237 $name = $element->getFullyQualifiedName(); |
|
238 $id = $element->getId(); |
|
239 $attribs['id'] = $id; |
|
240 |
|
241 $helperObject = $view->getHelper($helper); |
|
242 if (method_exists($helperObject, 'setTranslator')) { |
|
243 $helperObject->setTranslator($element->getTranslator()); |
|
244 } |
|
245 |
|
246 $elementContent = $view->$helper($name, $value, $attribs, $element->options); |
|
247 switch ($this->getPlacement()) { |
|
248 case self::APPEND: |
|
249 return $content . $separator . $elementContent; |
|
250 case self::PREPEND: |
|
251 return $elementContent . $separator . $content; |
|
252 default: |
|
253 return $elementContent; |
|
254 } |
|
255 } |
|
256 } |