[FEATURE] Adapt to new TCA and Form Engine API of TYPO3 CMS 7
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Hook / Backend / Form / FormDataProvider / TcaSelectItemsProcessor.php
1 <?php
2 namespace SJBR\StaticInfoTables\Hook\Backend\Form\FormDataProvider;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2013-2015 Stanislas Rolland <typo3(arobas)sjbr.ca>
7 * All rights reserved
8 *
9 * This script is part of the Typo3 project. The Typo3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25 /**
26 * Processor for TCA select items
27 */
28
29 use SJBR\StaticInfoTables\Utility\ModelUtility;
30 use SJBR\StaticInfoTables\Utility\TcaUtility;
31 use SJBR\StaticInfoTables\Utility\LocalizationUtility;
32 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaSelectItems;
33 use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35 class TcaSelectItemsProcessor {
36
37 /**
38 * Translate and sort the territories selector using the current locale
39 *
40 * @param array $PA: parameters: items, config, TSconfig, table, row, field
41 * @param DataPreprocessor $fObj
42 * @return void
43 */
44 public function translateTerritoriesSelector($PA, TcaSelectItems $fObj) {
45 switch ($PA['table']) {
46 case 'static_territories':
47 // Avoid circular relation
48 $row = $PA['row'];
49 foreach ($PA['items'] as $index => $item) {
50 if ($item[1] == $row['uid']) {
51 unset($PA['items'][$index]);
52 }
53 }
54 break;
55 }
56 $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_territories');
57 $PA['items'] = $this->replaceSelectorIndexField($PA);
58 }
59
60 /**
61 * Translate and sort the countries selector using the current locale
62 *
63 * @param array $PA: parameters: items, config, TSconfig, table, row, field
64 * @param DataPreprocessor $fObj
65 * @return void
66 */
67 public function translateCountriesSelector($PA, TcaSelectItems $fObj) {
68 $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_countries');
69 $PA['items'] = $this->replaceSelectorIndexField($PA);
70 }
71
72 /**
73 * Translate and sort the country zones selector using the current locale
74 *
75 * @param array $PA: parameters: items, config, TSconfig, table, row, field
76 * @param DataPreprocessor $fObj
77 * @return void
78 */
79 public function translateCountryZonesSelector($PA, TcaSelectItems $fObj) {
80 $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_country_zones');
81 $PA['items'] = $this->replaceSelectorIndexField($PA);
82 }
83
84 /**
85 * Translate and sort the currencies selector using the current locale
86 *
87 * @param array $PA: parameters: items, config, TSconfig, table, row, field
88 * @param DataPreprocessor $fObj
89 * @return void
90 */
91 public function translateCurrenciesSelector($PA, TcaSelectItems $fObj) {
92 $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_currencies');
93 $PA['items'] = $this->replaceSelectorIndexField($PA);
94 }
95
96 /**
97 * Translate and sort the languages selector using the current locale
98 *
99 * @param array $PA: parameters: items, config, TSconfig, table, row, field
100 * @param DataPreprocessor $fObj
101 * @return void
102 */
103 public function translateLanguagesSelector($PA, TcaSelectItems $fObj) {
104
105 $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_languages');
106 $PA['items'] = $this->replaceSelectorIndexField($PA);
107 }
108
109 /**
110 * Translate selector items array
111 *
112 * @param array $items: array of value/label pairs
113 * @param string $tableName: name of static info tables
114 * @return array array of value/translated label pairs
115 */
116 protected function translateSelectorItems($items, $tableName) {
117 $translatedItems = $items;
118 if (isset($translatedItems) && is_array($translatedItems)) {
119 foreach ($translatedItems as $key => $item) {
120 if ($translatedItems[$key][1]) {
121 //Get isocode if present
122 $code = strstr($item[0], '(');
123 $code2 = strstr(substr($code, 1), '(');
124 $code = $code2 ? $code2 : $code;
125 // Translate
126 $translatedItems[$key][0] = LocalizationUtility::translate(array('uid' => $item[1]), $tableName);
127 // Re-append isocode, if present
128 $translatedItems[$key][0] = $translatedItems[$key][0] . ($code ? ' ' . $code : '');
129 }
130 }
131 $currentLocale = setlocale(LC_COLLATE, '0');
132 $locale = LocalizationUtility::setCollatingLocale();
133 if ($locale !== FALSE) {
134 uasort($translatedItems, array($this, 'strcollOnLabels'));
135 }
136 setlocale(LC_COLLATE, $currentLocale);
137 }
138 $items = $translatedItems;
139 return $items;
140 }
141
142 /**
143 * Using strcoll comparison on labels
144 *
145 * @return integer see strcoll
146 */
147 protected function strcollOnLabels($itemA, $itemB) {
148 return strcoll($itemA[0], $itemB[0]);
149 }
150
151 /**
152 * Replace the selector's uid index with configured indexField
153 *
154 * @param array $PA: TCA select field parameters array
155 * @return array The new $items array
156 */
157 protected function replaceSelectorIndexField($PA) {
158 $items = $PA['items'];
159 $indexFields = GeneralUtility::trimExplode(',', $PA['config']['itemsProcFunc_config']['indexField'], TRUE);
160 if (!empty($indexFields)) {
161 $rows = array();
162 // Collect items uid's
163 $uids = array();
164 foreach ($items as $key => $item) {
165 if ($items[$key][1]) {
166 $uids[] = $item[1];
167 }
168 }
169 $uidList = implode(',', $uids);
170 if (!empty($uidList)) {
171 /** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
172 $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
173 switch ($PA['config']['foreign_table']) {
174 case 'static_territories':
175 /** @var $territoryRepository SJBR\StaticInfoTables\Domain\Repository\TerritoryRepository */
176 $territoryRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\TerritoryRepository');
177 $objects = $territoryRepository->findAllByUidInList($uidList)->toArray();
178 $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Territory', ModelUtility::MAPPING_COLUMNS);
179 break;
180 case 'static_countries':
181 /** @var $countryRepository SJBR\StaticInfoTables\Domain\Repository\CountryRepository */
182 $countryRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\CountryRepository');
183 $objects = $countryRepository->findAllByUidInList($uidList)->toArray();
184 $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Country', ModelUtility::MAPPING_COLUMNS);
185 break;
186 case 'static_country_zones':
187 /** @var $countryZoneRepository SJBR\StaticInfoTables\Domain\Repository\CountryZoneRepository */
188 $countryZoneRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\CountryZoneRepository');
189 $objects = $countryZoneRepository->findAllByUidInList($uidList)->toArray();
190 $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\CountryZone', ModelUtility::MAPPING_COLUMNS);
191 break;
192 case 'static_languages':
193 /** @var $languageRepository SJBR\StaticInfoTables\Domain\Repository\LanguageRepository */
194 $languageRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\LanguageRepository');
195 $objects = $languageRepository->findAllByUidInList($uidList)->toArray();
196 $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Language', ModelUtility::MAPPING_COLUMNS);
197 break;
198 case 'static_currencies':
199 /** @var $currencyRepository SJBR\StaticInfoTables\Domain\Repository\CurrencyRepository */
200 $currencyRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\CurrencyRepository');
201 $objects = $currencyRepository->findAllByUidInList($uidList)->toArray();
202 $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Currency', ModelUtility::MAPPING_COLUMNS);
203 break;
204 default:
205 break;
206 }
207 if (!empty($objects)) {
208 // Map table column to object property
209 $indexProperties = array();
210 foreach ($indexFields as $indexField) {
211 if ($columnsMapping[$indexField]['mapOnProperty']) {
212 $indexProperties[] = $columnsMapping[$indexField]['mapOnProperty'];
213 } else {
214 $indexProperties[] = GeneralUtility::underscoredToUpperCamelCase($indexField);
215 }
216 }
217 // Index rows by uid
218 $uidIndexedRows = array();
219 foreach ($objects as $object) {
220 $uidIndexedObjects[$object->getUid()] = $object;
221 }
222 // Replace the items index field
223 foreach ($items as $key => $item) {
224 if ($items[$key][1]) {
225 $object = $uidIndexedObjects[$items[$key][1]];
226 $items[$key][1] = $object->_getProperty($indexProperties[0]);
227 if ($indexFields[1] && $object->_getProperty($indexProperties[1])) {
228 $items[$key][1] .= '_' . $object->_getProperty($indexProperties[1]);
229 }
230 }
231 }
232 }
233 }
234 }
235 return $items;
236 }
237 }