[BUGFIX] Notice free FormEngine testing
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / TcaCheckboxItems.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
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 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
18
19 /**
20 * Resolve checkbox items and set processed item list in processedTca
21 */
22 class TcaCheckboxItems extends AbstractItemProvider implements FormDataProviderInterface
23 {
24 /**
25 * Resolve checkbox items
26 *
27 * @param array $result
28 * @return array
29 * @throws \UnexpectedValueException
30 */
31 public function addData(array $result)
32 {
33 $languageService = $this->getLanguageService();
34 $table = $result['tableName'];
35
36 foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
37 if (empty($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'check') {
38 continue;
39 }
40
41 if (!is_array($fieldConfig['config']['items'])) {
42 $fieldConfig['config']['items'] = [];
43 }
44
45 $config = $fieldConfig['config'];
46 $items = $this->sanitizeConfiguration($config, $fieldName, $table);
47
48 // Resolve "itemsProcFunc"
49 if (!empty($config['itemsProcFunc'])) {
50 $items = $this->resolveItemProcessorFunction($result, $fieldName, $items);
51 // itemsProcFunc must not be used anymore
52 unset($result['processedTca']['columns'][$fieldName]['config']['itemsProcFunc']);
53 }
54
55 // Set label overrides from pageTsConfig if given
56 if (isset($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
57 && \is_array($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'])
58 ) {
59 foreach ($result['pageTsConfig']['TCEFORM.'][$table . '.'][$fieldName . '.']['altLabels.'] as $itemKey => $label) {
60 if (isset($items[$itemKey][0])) {
61 $items[$itemKey][0] = $languageService->sL($label);
62 }
63 }
64 }
65
66 $result['processedTca']['columns'][$fieldName]['config']['items'] = $items;
67 }
68
69 return $result;
70 }
71
72 /**
73 * @param array $config
74 * @param string $fieldName
75 * @param string $tableName
76 * @return array
77 * @throws \UnexpectedValueException
78 */
79 private function sanitizeConfiguration(array $config, string $fieldName, string $tableName)
80 {
81 $newItems = [];
82 foreach ($config['items'] as $itemKey => $checkboxEntry) {
83 $this->basicChecks($fieldName, $tableName, $checkboxEntry, $itemKey);
84 $newItems[$itemKey] = [
85 $this->getLanguageService()->sL(trim($checkboxEntry[0])),
86 $checkboxEntry[1]
87 ];
88 if (isset($config['renderType']) && $config['renderType'] === 'checkboxToggle') {
89 $newItems = $this->sanitizeToggleCheckbox($checkboxEntry, $itemKey, $newItems);
90 } elseif (isset($config['renderType']) && $config['renderType'] === 'checkboxLabeledToggle') {
91 $newItems = $this->sanitizeLabeledToggleCheckbox($checkboxEntry, $itemKey, $newItems);
92 } else {
93 $newItems = $this->sanitizeIconToggleCheckbox($checkboxEntry, $itemKey, $newItems);
94 }
95 }
96 return $newItems;
97 }
98
99 /**
100 * @param string $fieldName
101 * @param string $tableName
102 * @param $checkboxEntry
103 * @param int $checkboxKey
104 * @throws \UnexpectedValueException
105 */
106 private function basicChecks(string $fieldName, string $tableName, $checkboxEntry, int $checkboxKey)
107 {
108 if (!\is_array($checkboxEntry)) {
109 throw new \UnexpectedValueException(
110 'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' is not an array as expected',
111 1440499337
112 );
113 }
114 if (!\array_key_exists(0, $checkboxEntry)) {
115 throw new \UnexpectedValueException(
116 'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' has no label',
117 1440499338
118 );
119 }
120 }
121
122 /**
123 * @param array $item
124 * @param int $itemKey
125 * @param array $newItems
126 * @return array
127 */
128 private function sanitizeToggleCheckbox(array $item, int $itemKey, array $newItems)
129 {
130 if (array_key_exists('invertStateDisplay', $item)) {
131 $newItems[$itemKey]['invertStateDisplay'] = $item['invertStateDisplay'];
132 } else {
133 $newItems[$itemKey]['invertStateDisplay'] = false;
134 }
135 return $newItems;
136 }
137
138 /**
139 * @param array $item
140 * @param int $itemKey
141 * @param array $newItems
142 * @return array
143 */
144 private function sanitizeLabeledToggleCheckbox(array $item, int $itemKey, array $newItems)
145 {
146 if (array_key_exists('labelChecked', $item)) {
147 $newItems[$itemKey]['labelChecked'] = $item['labelChecked'];
148 }
149 if (array_key_exists('labelUnchecked', $item)) {
150 $newItems[$itemKey]['labelUnchecked'] = $item['labelUnchecked'];
151 }
152 if (array_key_exists('invertStateDisplay', $item)) {
153 $newItems[$itemKey]['invertStateDisplay'] = $item['invertStateDisplay'];
154 } else {
155 $newItems[$itemKey]['invertStateDisplay'] = false;
156 }
157 return $newItems;
158 }
159
160 /**
161 * @param array $item
162 * @param int $itemKey
163 * @param array $newItems
164 * @return array
165 */
166 private function sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
167 {
168 if (array_key_exists('iconIdentifierChecked', $item)) {
169 $newItems[$itemKey]['iconIdentifierChecked'] = $item['iconIdentifierChecked'];
170 }
171 if (array_key_exists('iconIdentifierUnchecked', $item)) {
172 $newItems[$itemKey]['iconIdentifierUnchecked'] = $item['iconIdentifierUnchecked'];
173 }
174 if (array_key_exists('invertStateDisplay', $item)) {
175 $newItems[$itemKey]['invertStateDisplay'] = $item['invertStateDisplay'];
176 } else {
177 $newItems[$itemKey]['invertStateDisplay'] = false;
178 }
179 return $newItems;
180 }
181 }