e8e500dc21d30d55fb1389b402e1ea890cd36ca8
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / SelectViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Form\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * The form wizard controller
19 */
20 class SelectViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\SelectViewHelper
21 {
22 /**
23 * Render the option tags.
24 *
25 * @return array an associative array of options, key will be the value of the option tag
26 */
27 protected function getOptions()
28 {
29 if (!is_array($this->arguments['options']) && !$this->arguments['options'] instanceof \Traversable) {
30 return array();
31 }
32 $options = array();
33 $optionsArgument = $this->arguments['options'];
34 foreach ($optionsArgument as $key => $value) {
35 if (is_string($key)) {
36 $options[$key]['disabled'] = $value['disabled'];
37 $options[$key]['isOptgroup'] = true;
38 $optGroupOptions = $value['options'];
39 foreach ($optGroupOptions as $optionKey => $optionValue) {
40 $option = $this->getOption($optionKey, $optionValue);
41 $options[$key]['options'][key($option)] = current($option);
42 }
43 } else {
44 $option = $this->getOption($key, $value);
45 $options[key($option)] = current($option);
46 }
47 }
48
49 if ($this->arguments['sortByOptionLabel']) {
50 asort($options, SORT_LOCALE_STRING);
51 }
52 return $options;
53 }
54
55 /**
56 * Build a option array.
57 *
58 * @param string $key
59 * @param string $value
60 * @return array an associative array of an option, key will be the value of the option tag
61 */
62 protected function getOption($key, $value)
63 {
64 $option = array();
65 if (is_object($value) || is_array($value)) {
66 if ($this->hasArgument('optionValueField')) {
67 $key = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionValueField']);
68 if (is_object($key)) {
69 if (method_exists($key, '__toString')) {
70 $key = (string)$key;
71 } else {
72 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Identifying value for object of class "' . get_class($value) . '" was an object.', 1247827428);
73 }
74 }
75 // @todo use $this->persistenceManager->isNewObject() once it is implemented
76 } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
77 $key = $this->persistenceManager->getIdentifierByObject($value);
78 } elseif (method_exists($value, '__toString')) {
79 $key = (string)$value;
80 } else {
81 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('No identifying value for object of class "' . get_class($value) . '" found.', 1247826696);
82 }
83 if ($this->hasArgument('optionLabelField')) {
84 $value = \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getPropertyPath($value, $this->arguments['optionLabelField']);
85 if (is_object($value)) {
86 if (method_exists($value, '__toString')) {
87 $value = (string)$value;
88 } else {
89 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Label value for object of class "' . get_class($value) . '" was an object without a __toString() method.', 1247827553);
90 }
91 }
92 } elseif (method_exists($value, '__toString')) {
93 $value = (string)$value;
94 // @todo use $this->persistenceManager->isNewObject() once it is implemented
95 } elseif ($this->persistenceManager->getIdentifierByObject($value) !== null) {
96 $value = $this->persistenceManager->getIdentifierByObject($value);
97 }
98 }
99 $option[$key] = $value;
100 return $option;
101 }
102
103 /**
104 * Render the option tags.
105 *
106 * @param array $options the options for the form.
107 * @return string rendered tags.
108 */
109 protected function renderOptionTags($options)
110 {
111 $output = '';
112 if ($this->hasArgument('prependOptionLabel')) {
113 $value = $this->hasArgument('prependOptionValue') ? $this->arguments['prependOptionValue'] : '';
114 $label = $this->arguments['prependOptionLabel'];
115 $output .= $this->renderOptionTag($value, $label, false) . LF;
116 }
117 foreach ($options as $value => $label) {
118 if (
119 isset($label['isOptgroup'])
120 && $label['isOptgroup'] === true
121 ) {
122 $output .= '<optgroup label="' . htmlspecialchars($value) . '"';
123 if ($label['disabled'] !== null) {
124 $output .= ' disabled="disabled"';
125 }
126 $output .= '>' . LF;
127 foreach ($label['options'] as $optionValue => $optionLabel) {
128 $isSelected = $this->isSelected($optionValue);
129 $output .= $this->renderOptionTag($optionValue, $optionLabel, $isSelected) . LF;
130 }
131 $output .= ' </optgroup>' . LF;
132 } else {
133 $isSelected = $this->isSelected($value);
134 $output .= $this->renderOptionTag($value, $label, $isSelected) . LF;
135 }
136 }
137 return $output;
138 }
139 }