Completed
Push — master ( 042ba2...81ff7f )
by
unknown
14:34
created

SelectViewHelper   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 129
dl 0
loc 266
rs 6
c 0
b 0
f 0
wmc 55

9 Methods

Rating   Name   Duplication   Size   Complexity  
A renderOptionTag() 0 8 2
A getSelectedValue() 0 12 4
B isSelected() 0 15 8
A getOptionValueScalar() 0 13 4
B render() 0 59 10
A initializeArguments() 0 17 1
A renderPrependOptionTag() 0 9 3
A renderOptionTags() 0 8 2
D getOptions() 0 48 21

How to fix   Complexity   

Complex Class

Complex classes like SelectViewHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SelectViewHelper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Fluid\ViewHelpers\Form;
17
18
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
19
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
20
21
/**
22
 * This ViewHelper generates a :html:`<select>` dropdown list for the use with a form.
23
 *
24
 * Basic usage
25
 * ===========
26
 *
27
 * The most straightforward way is to supply an associative array as the ``options`` parameter.
28
 * The array key is used as option key, and the value is used as human-readable name.
29
 *
30
 * Basic usage::
31
 *
32
 *    <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" />
33
 *
34
 * Pre select a value
35
 * ------------------
36
 *
37
 * To pre select a value, set ``value`` to the option key which should be selected.
38
 * Default value::
39
 *
40
 *    <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
41
 *
42
 * Generates a dropdown box like above, except that "VISA Card" is selected.
43
 *
44
 * If the select box is a multi-select box :html:`multiple="1"`, then "value" can be an array as well.
45
 *
46
 * Custom options and option group rendering
47
 * -----------------------------------------
48
 *
49
 * Child nodes can be used to create a completely custom set of
50
 * :html:`<option>` and :html:`<optgroup>` tags in a way compatible with the
51
 * HMAC generation.
52
 * To do so, leave out the ``options`` argument and use child ViewHelpers:
53
 *
54
 * Custom options and optgroup::
55
 *
56
 *    <f:form.select name="myproperty">
57
 *       <f:form.select.option value="1">Option one</f:form.select.option>
58
 *       <f:form.select.option value="2">Option two</f:form.select.option>
59
 *       <f:form.select.optgroup>
60
 *          <f:form.select.option value="3">Grouped option one</f:form.select.option>
61
 *          <f:form.select.option value="4">Grouped option twi</f:form.select.option>
62
 *       </f:form.select.optgroup>
63
 *    </f:form.select>
64
 *
65
 * .. note::
66
 *    Do not use vanilla :html:`<option>` or :html:`<optgroup>` tags!
67
 *    They will invalidate the HMAC generation!
68
 *
69
 * Usage on domain objects
70
 * -----------------------
71
 *
72
 * If you want to output domain objects, you can just pass them as array into the ``options`` parameter.
73
 * To define what domain object value should be used as option key, use the ``optionValueField`` variable. Same goes for ``optionLabelField``.
74
 * If neither is given, the Identifier (UID/uid) and the :php:`__toString()` method are tried as fallbacks.
75
 *
76
 * If the ``optionValueField`` variable is set, the getter named after that value is used to retrieve the option key.
77
 * If the ``optionLabelField`` variable is set, the getter named after that value is used to retrieve the option value.
78
 *
79
 * If the ``prependOptionLabel`` variable is set, an option item is added in first position, bearing an empty string or -
80
 * if provided, the value of the ``prependOptionValue`` variable as value.
81
 *
82
 * Domain objects::
83
 *
84
 *    <f:form.select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" />
85
 *
86
 * In the above example, the ``userArray`` is an array of "User" domain objects, with no array key specified.
87
 *
88
 * So, in the above example, the method :php:`$user->getId()` is called to
89
 * retrieve the key, and :php:`$user->getFirstName()` to retrieve the displayed
90
 * value of each entry.
91
 *
92
 * The ``value`` property now expects a domain object, and tests for object equivalence.
93
 */
94
class SelectViewHelper extends AbstractFormFieldViewHelper
95
{
96
    /**
97
     * @var string
98
     */
99
    protected $tagName = 'select';
100
101
    /**
102
     * @var mixed
103
     */
104
    protected $selectedValue;
105
106
    /**
107
     * Initialize arguments.
108
     */
109
    public function initializeArguments()
110
    {
111
        parent::initializeArguments();
112
        $this->registerUniversalTagAttributes();
113
        $this->registerTagAttribute('size', 'string', 'Size of input field');
114
        $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
115
        $this->registerArgument('options', 'array', 'Associative array with internal IDs as key, and the values are displayed in the select box. Can be combined with or replaced by child f:form.select.* nodes.');
116
        $this->registerArgument('optionsAfterContent', 'boolean', 'If true, places auto-generated option tags after those rendered in the tag content. If false, automatic options come first.', false, false);
117
        $this->registerArgument('optionValueField', 'string', 'If specified, will call the appropriate getter on each object to determine the value.');
118
        $this->registerArgument('optionLabelField', 'string', 'If specified, will call the appropriate getter on each object to determine the label.');
119
        $this->registerArgument('sortByOptionLabel', 'boolean', 'If true, List will be sorted by label.', false, false);
120
        $this->registerArgument('selectAllByDefault', 'boolean', 'If specified options are selected if none was set before.', false, false);
121
        $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this ViewHelper', false, 'f3-form-error');
122
        $this->registerArgument('prependOptionLabel', 'string', 'If specified, will provide an option at first position with the specified label.');
123
        $this->registerArgument('prependOptionValue', 'string', 'If specified, will provide an option at first position with the specified value.');
124
        $this->registerArgument('multiple', 'boolean', 'If set multiple options may be selected.', false, false);
125
        $this->registerArgument('required', 'boolean', 'If set no empty value is allowed.', false, false);
126
    }
127
128
    /**
129
     * Render the tag.
130
     *
131
     * @return string rendered tag.
132
     */
133
    public function render()
134
    {
135
        if (isset($this->arguments['required']) && $this->arguments['required']) {
136
            $this->tag->addAttribute('required', 'required');
137
        }
138
        $name = $this->getName();
139
        if (isset($this->arguments['multiple']) && $this->arguments['multiple']) {
140
            $this->tag->addAttribute('multiple', 'multiple');
141
            $name .= '[]';
142
        }
143
        $this->tag->addAttribute('name', $name);
144
        $options = $this->getOptions();
145
146
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
147
148
        $this->addAdditionalIdentityPropertiesIfNeeded();
149
        $this->setErrorClassAttribute();
150
        $content = '';
151
152
        // register field name for token generation.
153
        $this->registerFieldNameForFormTokenGeneration($name);
154
        // in case it is a multi-select, we need to register the field name
155
        // as often as there are elements in the box
156
        if (isset($this->arguments['multiple']) && $this->arguments['multiple']) {
157
            $content .= $this->renderHiddenFieldForEmptyValue();
158
            // Register the field name additional times as required by the total number of
159
            // options. Since we already registered it once above, we start the counter at 1
160
            // instead of 0.
161
            $optionsCount = count($options);
162
            for ($i = 1; $i < $optionsCount; $i++) {
163
                $this->registerFieldNameForFormTokenGeneration($name);
164
            }
165
            // save the parent field name so that any child f:form.select.option
166
            // tag will know to call registerFieldNameForFormTokenGeneration
167
            // this is the reason why "self::class" is used instead of static::class (no LSB)
168
            $viewHelperVariableContainer->addOrUpdate(
169
                self::class,
170
                'registerFieldNameForFormTokenGeneration',
171
                $name
172
            );
173
        }
174
175
        $viewHelperVariableContainer->addOrUpdate(self::class, 'selectedValue', $this->getSelectedValue());
176
        $prependContent = $this->renderPrependOptionTag();
177
        $tagContent = $this->renderOptionTags($options);
178
        $childContent = $this->renderChildren();
179
        $viewHelperVariableContainer->remove(self::class, 'selectedValue');
180
        $viewHelperVariableContainer->remove(self::class, 'registerFieldNameForFormTokenGeneration');
181
        if (isset($this->arguments['optionsAfterContent']) && $this->arguments['optionsAfterContent']) {
182
            $tagContent = $childContent . $tagContent;
183
        } else {
184
            $tagContent .= $childContent;
185
        }
186
        $tagContent = $prependContent . $tagContent;
187
188
        $this->tag->forceClosingTag(true);
189
        $this->tag->setContent($tagContent);
190
        $content .= $this->tag->render();
191
        return $content;
192
    }
193
194
    /**
195
     * Render prepended option tag
196
     *
197
     * @return string rendered prepended empty option
198
     */
199
    protected function renderPrependOptionTag()
200
    {
201
        $output = '';
202
        if ($this->hasArgument('prependOptionLabel')) {
203
            $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
204
            $label = $this->arguments['prependOptionLabel'];
205
            $output .= $this->renderOptionTag($value, $label, false) . LF;
206
        }
207
        return $output;
208
    }
209
210
    /**
211
     * Render the option tags.
212
     *
213
     * @param array $options the options for the form.
214
     * @return string rendered tags.
215
     */
216
    protected function renderOptionTags($options)
217
    {
218
        $output = '';
219
        foreach ($options as $value => $label) {
220
            $isSelected = $this->isSelected($value);
221
            $output .= $this->renderOptionTag($value, $label, $isSelected) . LF;
222
        }
223
        return $output;
224
    }
225
226
    /**
227
     * Render the option tags.
228
     *
229
     * @return array an associative array of options, key will be the value of the option tag
230
     */
231
    protected function getOptions()
232
    {
233
        if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) {
234
            return [];
235
        }
236
        $options = [];
237
        $optionsArgument = $this->arguments['options'];
238
        foreach ($optionsArgument as $key => $value) {
239
            if (is_object($value) || is_array($value)) {
240
                if ($this->hasArgument('optionValueField')) {
241
                    $key = ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']);
242
                    if (is_object($key)) {
243
                        if (method_exists($key, '__toString')) {
244
                            $key = (string)$key;
245
                        } else {
246
                            throw new Exception('Identifying value for object of class "' . (is_object($value) ? get_class($value) : gettype($value)) . '" was an object.', 1247827428);
247
                        }
248
                    }
249
                } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $object of TYPO3\CMS\Extbase\Persis...getIdentifierByObject() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

249
                } elseif ($this->persistenceManager->getIdentifierByObject(/** @scrutinizer ignore-type */ $value) !== null) {
Loading history...
250
                    // @todo use $this->persistenceManager->isNewObject() once it is implemented
251
                    $key = $this->persistenceManager->getIdentifierByObject($value);
252
                } elseif (is_object($value) && method_exists($value, '__toString')) {
253
                    $key = (string)$value;
254
                } elseif (is_object($value)) {
255
                    throw new Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
256
                }
257
                if ($this->hasArgument('optionLabelField')) {
258
                    $value = ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']);
259
                    if (is_object($value)) {
260
                        if (method_exists($value, '__toString')) {
261
                            $value = (string)$value;
262
                        } else {
263
                            throw new Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.', 1247827553);
264
                        }
265
                    }
266
                } elseif (is_object($value) && method_exists($value, '__toString')) {
267
                    $value = (string)$value;
268
                } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
269
                    // @todo use $this->persistenceManager->isNewObject() once it is implemented
270
                    $value = $this->persistenceManager->getIdentifierByObject($value);
271
                }
272
            }
273
            $options[$key] = $value;
274
        }
275
        if ($this->arguments['sortByOptionLabel']) {
276
            asort($options, SORT_LOCALE_STRING);
277
        }
278
        return $options;
279
    }
280
281
    /**
282
     * Render the option tags.
283
     *
284
     * @param mixed $value Value to check for
285
     * @return bool TRUE if the value should be marked a s selected; FALSE otherwise
286
     */
287
    protected function isSelected($value)
288
    {
289
        $selectedValue = $this->getSelectedValue();
290
        if ($value === $selectedValue || (string)$value === $selectedValue) {
291
            return true;
292
        }
293
        if ($this->hasArgument('multiple')) {
294
            if ($selectedValue === null && $this->arguments['selectAllByDefault'] === true) {
0 ignored issues
show
introduced by
The condition $selectedValue === null is always false.
Loading history...
295
                return true;
296
            }
297
            if (is_array($selectedValue) && in_array($value, $selectedValue)) {
298
                return true;
299
            }
300
        }
301
        return false;
302
    }
303
304
    /**
305
     * Retrieves the selected value(s)
306
     *
307
     * @return mixed value string or an array of strings
308
     */
309
    protected function getSelectedValue()
310
    {
311
        $this->setRespectSubmittedDataValue(true);
312
        $value = $this->getValueAttribute();
313
        if (!is_array($value) && !$value instanceof \Traversable) {
314
            return $this->getOptionValueScalar($value);
315
        }
316
        $selectedValues = [];
317
        foreach ($value as $selectedValueElement) {
318
            $selectedValues[] = $this->getOptionValueScalar($selectedValueElement);
319
        }
320
        return $selectedValues;
321
    }
322
323
    /**
324
     * Get the option value for an object
325
     *
326
     * @param mixed $valueElement
327
     * @return string
328
     */
329
    protected function getOptionValueScalar($valueElement)
330
    {
331
        if (is_object($valueElement)) {
332
            if ($this->hasArgument('optionValueField')) {
333
                return ObjectAccess::getPropertyPath($valueElement, $this->arguments['optionValueField']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return TYPO3\CMS\Extbase...ts['optionValueField']) also could return the type object which is incompatible with the documented return type string.
Loading history...
334
            }
335
            // @todo use $this->persistenceManager->isNewObject() once it is implemented
336
            if ($this->persistenceManager->getIdentifierByObject($valueElement) !== null) {
337
                return $this->persistenceManager->getIdentifierByObject($valueElement);
338
            }
339
            return (string)$valueElement;
340
        }
341
        return $valueElement;
342
    }
343
344
    /**
345
     * Render one option tag
346
     *
347
     * @param string $value value attribute of the option tag (will be escaped)
348
     * @param string $label content of the option tag (will be escaped)
349
     * @param bool $isSelected specifies whether or not to add selected attribute
350
     * @return string the rendered option tag
351
     */
352
    protected function renderOptionTag($value, $label, $isSelected)
353
    {
354
        $output = '<option value="' . htmlspecialchars($value) . '"';
355
        if ($isSelected) {
356
            $output .= ' selected="selected"';
357
        }
358
        $output .= '>' . htmlspecialchars($label) . '</option>';
359
        return $output;
360
    }
361
}
362