f7e234ce83b8eafb5861a282051d5001ef3433ca
[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/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 3308 2009-10-09 12:59:02Z sebastian $
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
114 $options = $this->getOptions();
115 $this->tag->setContent($this->renderOptionTags($options));
116
117 $this->setErrorClassAttribute();
118
119 // register field name for token generation.
120 // in case it is a multi-select, we need to register the field name
121 // as often as there are elements in the box
122 if ($this->arguments->hasArgument('multiple')) {
123 for ($i=0; $i<count($options); $i++) {
124 $this->registerFieldNameForFormTokenGeneration($name);
125 }
126 } else {
127 $this->registerFieldNameForFormTokenGeneration($name);
128 }
129
130 return $this->tag->render();
131 }
132
133 /**
134 * Render the option tags.
135 *
136 * @param array $options the options for the form.
137 * @return string rendered tags.
138 * @author Bastian Waidelich <bastian@typo3.org>
139 */
140 protected function renderOptionTags($options) {
141 $output = '';
142
143 foreach ($options as $value => $label) {
144 $isSelected = $this->isSelected($value);
145 $output.= $this->renderOptionTag($value, $label, $isSelected) . chr(10);
146 }
147 return $output;
148 }
149
150 /**
151 * Render the option tags.
152 *
153 * @return array an associative array of options, key will be the value of the option tag
154 * @author Bastian Waidelich <bastian@typo3.org>
155 * @author Karsten Dambekalns <karsten@typo3.org>
156 */
157 protected function getOptions() {
158 $options = array();
159 foreach ($this->arguments['options'] as $key => $value) {
160 if (is_object($value)) {
161
162 if ($this->arguments->hasArgument('optionValueField')) {
163 $key = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionValueField']);
164 if (is_object($key)) {
165 if (method_exists($key, '__toString')) {
166 $key = (string)$key;
167 } else {
168 throw new Tx_Fluid_Core_ViewHelper_Exception('Identifying value for object of class "' . get_class($value) . '" was an object.' , 1247827428);
169 }
170 }
171 } elseif ($this->persistenceManager->getBackend()->getIdentifierByObject($value) !== NULL) {
172 $key = $this->persistenceManager->getBackend()->getIdentifierByObject($value);
173 } elseif (method_exists($value, '__toString')) {
174 $key = (string)$value;
175 } else {
176 throw new Tx_Fluid_Core_ViewHelper_Exception('No identifying value for object of class "' . get_class($value) . '" found.' , 1247826696);
177 }
178
179 if ($this->arguments->hasArgument('optionLabelField')) {
180 $value = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionLabelField']);
181 if (is_object($value)) {
182 if (method_exists($value, '__toString')) {
183 $value = (string)$value;
184 } else {
185 throw new Tx_Fluid_Core_ViewHelper_Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.' , 1247827553);
186 }
187 }
188 } elseif (method_exists($value, '__toString')) {
189 $value = (string)$value;
190 } elseif ($this->persistenceManager->getBackend()->getIdentifierByObject($value) !== NULL) {
191 $value = $this->persistenceManager->getBackend()->getIdentifierByObject($value);
192 }
193 }
194 $options[$key] = $value;
195 }
196 return $options;
197 }
198
199 /**
200 * Render the option tags.
201 *
202 * @return boolean true if the
203 * @author Bastian Waidelich <bastian@typo3.org>
204 */
205 protected function isSelected($value) {
206 $selectedValue = $this->getSelectedValue();
207 if ($value === $selectedValue || (string)$value === $selectedValue) {
208 return TRUE;
209 }
210 if ($this->arguments->hasArgument('multiple') && is_array($selectedValue) && in_array($value, $selectedValue)) {
211 return TRUE;
212 }
213 return FALSE;
214 }
215
216 /**
217 * Retrieves the selected value(s)
218 *
219 * @return mixed value string or an array of strings
220 * @author Bastian Waidelich <bastian@typo3.org>
221 */
222 protected function getSelectedValue() {
223 $value = $this->getValue();
224 if (!$this->arguments->hasArgument('optionValueField')) {
225 return $value;
226 }
227 if (!is_array($value)) {
228 if (is_object($value)) {
229 return Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $this->arguments['optionValueField']);
230 } else {
231 return $value;
232 }
233 }
234 $selectedValues = array();
235 foreach($value as $selectedValueElement) {
236 if (is_object($selectedValueElement)) {
237 $selectedValues[] = Tx_Extbase_Reflection_ObjectAccess::getProperty($selectedValueElement, $this->arguments['optionValueField']);
238 } else {
239 $selectedValues[] = $selectedValueElement;
240 }
241 }
242 return $selectedValues;
243 }
244
245 /**
246 * Render one option tag
247 *
248 * @param string $value value attribute of the option tag (will be escaped)
249 * @param string $label content of the option tag (will be escaped)
250 * @param boolean $isSelected specifies wheter or not to add selected attribute
251 * @return string the rendered option tag
252 * @author Bastian Waidelich <bastian@typo3.org>
253 */
254 protected function renderOptionTag($value, $label, $isSelected) {
255 $output = '<option value="' . htmlspecialchars($value) . '"';
256 if ($isSelected) {
257 $output.= ' selected="selected"';
258 }
259 $output.= '>' . htmlspecialchars($label) . '</option>';
260
261 return $output;
262 }
263 }
264
265 ?>