[~FEATURE] Fluid (ViewHelpers): Adjusted AbstractFormViewHelper to be able to namespa...
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Form / SelectViewHelper.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 package "Fluid". *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * This view helper generates a <select> dropdown list for the use with a form.
25 *
26 * = Basic usage =
27 *
28 * The most straightforward way is to supply an associative array as the "options" parameter.
29 * The array key is used as option key, and the value is used as human-readable name.
30 *
31 * <code title="Basic usage">
32 * <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" />
33 * </code>
34 *
35 * = Pre-select a value =
36 *
37 * To pre-select a value, set "value" to the option key which should be selected.
38 * <code title="Default value">
39 * <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
40 * </code>
41 * Generates a dropdown box like above, except that "VISA Card" is selected.
42 *
43 * If the select box is a multi-select box (multiple="true"), then "value" can be an array as well.
44 *
45 * = Usage on domain objects =
46 *
47 * If you want to output domain objects, you can just pass them as array into the "options" parameter.
48 * To define what domain object value should be used as option key, use the "optionValueField" variable. Same goes for optionLabelField.
49 * If neither is given, the Identifier (UID) and the __toString() method are tried as fallbacks.
50 *
51 * If the optionValueField variable is set, the getter named after that value is used to retrieve the option key.
52 * If the optionLabelField variable is set, the getter named after that value is used to retrieve the option value.
53 *
54 * <code title="Domain objects">
55 * <f:form.select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" />
56 * </code>
57 * In the above example, the userArray is an array of "User" domain objects, with no array key specified.
58 *
59 * So, in the above example, the method $user->getId() is called to retrieve the key, and $user->getFirstName() to retrieve the displayed value of each entry.
60 *
61 * The "value" property now expects a domain object, and tests for object equivalence.
62 *
63 * @version $Id: SelectViewHelper.php 2914 2009-07-28 18:26:38Z bwaidelich $
64 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
65 * @scope prototype
66 */
67 class Tx_Fluid_ViewHelpers_Form_SelectViewHelper extends Tx_Fluid_ViewHelpers_Form_AbstractFormFieldViewHelper {
68
69 /**
70 * @var string
71 */
72 protected $tagName = 'select';
73
74 /**
75 * @var mixed the selected value
76 */
77 protected $selectedValue = NULL;
78
79 /**
80 * Initialize arguments.
81 *
82 * @return void
83 * @author Sebastian Kurfürst <sebastian@typo3.org>
84 * @api
85 */
86 public function initializeArguments() {
87 parent::initializeArguments();
88 $this->registerUniversalTagAttributes();
89 $this->registerTagAttribute('multiple', 'string', 'if set, multiple select field');
90 $this->registerTagAttribute('size', 'string', 'Size of input field');
91 $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
92 $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box', TRUE);
93 $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
94 $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
95 $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', FALSE, 'f3-form-error');
96 }
97
98 /**
99 * Render the tag.
100 *
101 * @return string rendered tag.
102 * @author Sebastian Kurfürst <sebastian@typo3.org>
103 * @author Bastian Waidelich <bastian@typo3.org>
104 * @api
105 */
106 public function render() {
107 $name = $this->getName();
108 if ($this->arguments->hasArgument('multiple')) {
109 $name .= '[]';
110 }
111
112 $this->tag->addAttribute('name', $name);
113 $this->tag->setContent($this->renderOptionTags());
114
115 $this->setErrorClassAttribute();
116
117 return $this->tag->render();
118 }
119
120 /**
121 * Render the option tags.
122 *
123 * @return string rendered tags.
124 * @author Bastian Waidelich <bastian@typo3.org>
125 */
126 protected function renderOptionTags() {
127 $output = '';
128 $options = $this->getOptions();
129 foreach ($options as $value => $label) {
130 $isSelected = $this->isSelected($value);
131 $output.= $this->renderOptionTag($value, $label, $isSelected) . chr(10);
132 }
133 return $output;
134 }
135
136 /**
137 * Render the option tags.
138 *
139 * @return array an associative array of options, key will be the value of the option tag
140 * @author Bastian Waidelich <bastian@typo3.org>
141 * @author Karsten Dambekalns <karsten@typo3.org>
142 */
143 protected function getOptions() {
144 $options = array();
145 foreach ($this->arguments['options'] as $key => $value) {
146 if (is_object($value)) {
147
148 if ($this->arguments->hasArgument('optionValueField')) {
149 $key = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionValueField']);
150 if (is_object($key)) {
151 if (method_exists($key, '__toString')) {
152 $key = (string)$key;
153 } else {
154 throw new Tx_Fluid_Core_ViewHelper_Exception('Identifying value for object of class "' . get_class($value) . '" was an object.' , 1247827428);
155 }
156 }
157 } elseif ($this->persistenceManager->getBackend()->getIdentifierByObject($value) !== NULL) {
158 $key = $this->persistenceManager->getBackend()->getIdentifierByObject($value);
159 } elseif (method_exists($value, '__toString')) {
160 $key = (string)$value;
161 } else {
162 throw new Tx_Fluid_Core_ViewHelper_Exception('No identifying value for object of class "' . get_class($value) . '" found.' , 1247826696);
163 }
164
165 if ($this->arguments->hasArgument('optionLabelField')) {
166 $value = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionLabelField']);
167 if (is_object($value)) {
168 if (method_exists($value, '__toString')) {
169 $value = (string)$value;
170 } else {
171 throw new Tx_Fluid_Core_ViewHelper_Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.' , 1247827553);
172 }
173 }
174 } elseif (method_exists($value, '__toString')) {
175 $value = (string)$value;
176 } elseif ($this->persistenceManager->getBackend()->getIdentifierByObject($value) !== NULL) {
177 $value = $this->persistenceManager->getBackend()->getIdentifierByObject($value);
178 }
179 }
180 $options[$key] = $value;
181 }
182 return $options;
183 }
184
185 /**
186 * Render the option tags.
187 *
188 * @return boolean true if the
189 * @author Bastian Waidelich <bastian@typo3.org>
190 */
191 protected function isSelected($value) {
192 $selectedValue = $this->getSelectedValue();
193 if ($value === $selectedValue || (string)$value === $selectedValue) {
194 return TRUE;
195 }
196 if ($this->arguments->hasArgument('multiple') && is_array($selectedValue) && in_array($value, $selectedValue)) {
197 return TRUE;
198 }
199 return FALSE;
200 }
201
202 /**
203 * Retrieves the selected value(s)
204 *
205 * @return mixed value string or an array of strings
206 * @author Bastian Waidelich <bastian@typo3.org>
207 */
208 protected function getSelectedValue() {
209 $value = $this->getValue();
210 if (!$this->arguments->hasArgument('optionValueField')) {
211 return $value;
212 }
213 if (!is_array($value)) {
214 if (is_object($value)) {
215 return Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionValueField']);
216 } else {
217 return $value;
218 }
219 }
220 $selectedValues = array();
221 foreach($value as $selectedValueElement) {
222 if (is_object($selectedValueElement)) {
223 $selectedValues[] = Tx_Extbase_Reflection_ObjectAccess::getProperty($selectedValueElement, $this->arguments['optionValueField']);
224 } else {
225 $selectedValues[] = $selectedValueElement;
226 }
227 }
228 return $selectedValues;
229 }
230
231 /**
232 * Render one option tag
233 *
234 * @param string $value value attribute of the option tag (will be escaped)
235 * @param string $label content of the option tag (will be escaped)
236 * @param boolean $isSelected specifies wheter or not to add selected attribute
237 * @return string the rendered option tag
238 * @author Bastian Waidelich <bastian@typo3.org>
239 */
240 protected function renderOptionTag($value, $label, $isSelected) {
241 $output = '<option value="' . htmlspecialchars($value) . '"';
242 if ($isSelected) {
243 $output.= ' selected="selected"';
244 }
245 $output.= '>' . htmlspecialchars($label) . '</option>';
246
247 return $output;
248 }
249 }
250
251 ?>