849cfb8e53e7e65e4b82570788565eaf5b86892d
[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 switch ($config['renderType']) {
89 case 'checkboxToggle':
90 $newItems = $this->sanitizeToggleCheckbox($checkboxEntry, $itemKey, $newItems);
91 break;
92 case 'checkboxLabeledToggle':
93 $newItems = $this->sanitizeLabeledToggleCheckbox($checkboxEntry, $itemKey, $newItems);
94 break;
95 default:
96 $newItems = $this->sanitizeIconToggleCheckbox($checkboxEntry, $itemKey, $newItems);
97 }
98 }
99 return $newItems;
100 }
101
102 /**
103 * @param string $fieldName
104 * @param string $tableName
105 * @param $checkboxEntry
106 * @param int $checkboxKey
107 * @throws \UnexpectedValueException
108 */
109 private function basicChecks(string $fieldName, string $tableName, $checkboxEntry, int $checkboxKey)
110 {
111 if (!\is_array($checkboxEntry)) {
112 throw new \UnexpectedValueException(
113 'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' is not an array as expected',
114 1440499337
115 );
116 }
117 if (!\array_key_exists(0, $checkboxEntry)) {
118 throw new \UnexpectedValueException(
119 'Item ' . $checkboxKey . ' of field ' . $fieldName . ' of TCA table ' . $tableName . ' has no label',
120 1440499338
121 );
122 }
123 }
124
125 /**
126 * @param array $item
127 * @param int $itemKey
128 * @param array $newItems
129 * @return array
130 */
131 private function sanitizeToggleCheckbox(array $item, int $itemKey, array $newItems)
132 {
133 if (array_key_exists('invertStateDisplay', $item)) {
134 $newItems[$itemKey]['invertStateDisplay'] = $item['invertStateDisplay'];
135 } else {
136 $newItems[$itemKey]['invertStateDisplay'] = false;
137 }
138 return $newItems;
139 }
140
141 /**
142 * @param array $item
143 * @param int $itemKey
144 * @param array $newItems
145 * @return array
146 */
147 private function sanitizeLabeledToggleCheckbox(array $item, int $itemKey, array $newItems)
148 {
149 if (array_key_exists('labelChecked', $item)) {
150 $newItems[$itemKey]['labelChecked'] = $item['labelChecked'];
151 }
152 if (array_key_exists('labelUnchecked', $item)) {
153 $newItems[$itemKey]['labelUnchecked'] = $item['labelUnchecked'];
154 }
155 if (array_key_exists('invertStateDisplay', $item)) {
156 $newItems[$itemKey]['invertStateDisplay'] = $item['invertStateDisplay'];
157 } else {
158 $newItems[$itemKey]['invertStateDisplay'] = false;
159 }
160 return $newItems;
161 }
162
163 /**
164 * @param array $item
165 * @param int $itemKey
166 * @param array $newItems
167 * @return array
168 */
169 private function sanitizeIconToggleCheckbox(array $item, int $itemKey, array $newItems)
170 {
171 if (array_key_exists('iconIdentifierChecked', $item)) {
172 $newItems[$itemKey]['iconIdentifierChecked'] = $item['iconIdentifierChecked'];
173 }
174 if (array_key_exists('iconIdentifierUnchecked', $item)) {
175 $newItems[$itemKey]['iconIdentifierUnchecked'] = $item['iconIdentifierUnchecked'];
176 }
177 if (array_key_exists('invertStateDisplay', $item)) {
178 $newItems[$itemKey]['invertStateDisplay'] = $item['invertStateDisplay'];
179 } else {
180 $newItems[$itemKey]['invertStateDisplay'] = false;
181 }
182 return $newItems;
183 }
184 }