Resolves #45809: Refactor code and introduce namespacing
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Hook / Backend / Form / ElementRenderingHelper.php
1 <?php
2 namespace SJBR\StaticInfoTables\Hook\Backend\Form;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2013 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 * Custom rendering of some backend forms elements
27 *
28 */
29 class ElementRenderingHelper {
30
31 /*
32 * Add ISO codes to the label of entities
33 */
34 public function addIsoCodeToLabel ($PA, $fObj) {
35 $PA['title'] = $PA['row'][$GLOBALS['TCA'][$PA['table']]['ctrl']['label']];
36 if (TYPO3_MODE == 'BE') {
37 switch ($PA['table']) {
38 case 'static_territories':
39 case 'cc_static_territories':
40 $isoCode = $PA['row']['tr_iso_nr'];
41 if (!$isoCode) {
42 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
43 'uid,tr_iso_nr',
44 $PA['table'],
45 'uid = ' . intval($PA['row']['uid']) . \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields($PA['table'])
46 );
47 $isoCode = $rows[0]['tr_iso_nr'];
48 }
49 if ($isoCode) {
50 $PA['title'] = $PA['title'] . ' (' . $isoCode . ')';
51 }
52 break;
53 case 'static_countries':
54 case 'cc_static_countries':
55 $isoCode = $PA['row']['cn_iso_2'];
56 if (!$isoCode) {
57 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
58 'uid,cn_iso_2',
59 $PA['table'],
60 'uid = ' . intval($PA['row']['uid']) . \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields($PA['table'])
61 );
62 $isoCode = $rows[0]['cn_iso_2'];
63 }
64 if ($isoCode) {
65 $PA['title'] = $PA['title'] . ' (' . $isoCode . ')';
66 }
67 break;
68 case 'static_languages':
69 case 'cc_static_languages':
70 $isoCodes = array($PA['row']['lg_iso_2']);
71 if ($PA['row']['lg_country_iso_2']) {
72 $isoCodes[] = $PA['row']['lg_country_iso_2'];
73 }
74 $isoCode = implode('_', $isoCodes);
75 if (!$isoCode || !$PA['row']['lg_country_iso_2']) {
76 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
77 'uid,lg_iso_2,lg_country_iso_2',
78 $PA['table'],
79 'uid = ' . intval($PA['row']['uid']) . \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields($PA['table'])
80 );
81 $isoCodes = array($rows[0]['lg_iso_2']);
82 if ($rows[0]['lg_country_iso_2']) {
83 $isoCodes[] = $rows[0]['lg_country_iso_2'];
84 }
85 $isoCode = implode('_', $isoCodes);
86 }
87 if ($isoCode) {
88 $PA['title'] = $PA['title'] . ' (' . $isoCode . ')';
89 }
90 break;
91 case 'static_currencies':
92 case 'cc_static_currencies':
93 $isoCode = $PA['row']['cu_iso_3'];
94 if (!$isoCode) {
95 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
96 'uid,cu_iso_3',
97 $PA['table'],
98 'uid = ' . intval($PA['row']['uid']) . \SJBR\StaticInfoTables\Utility\TcaUtility::getEnableFields($PA['table'])
99 );
100 $isoCode = $rows[0]['cu_iso_3'];
101 }
102 if ($isoCode) {
103 $PA['title'] = $PA['title'] . ' (' . $isoCode . ')';
104 }
105 break;
106 default:
107 break;
108 }
109 }
110 }
111
112 /*
113 * Translate and sort the territories selector using the current locale
114 */
115 public function translateTerritoriesSelector ($PA, $fObj) {
116 switch ($PA['table']) {
117 case 'static_territories':
118 case 'cc_static_territories':
119 // Avoid circular relation
120 $row = $PA['row'];
121 foreach ($PA['items'] as $index => $item) {
122 if ($item[1] == $row['uid']) {
123 unset($PA['items'][$index]);
124 }
125 }
126 break;
127 }
128 foreach ($PA['items'] as $index => $item) {
129 if ($PA['items'][$index][1]) {
130 $PA['items'][$index][0] = \SJBR\StaticInfoTables\Utility\LocalizationUtility::translate(array('uid' => $item[1]), 'static_territories');
131 }
132 }
133 asort($PA['items']);
134 }
135
136 /*
137 * Translate and sort the currencies selector using the current locale
138 */
139 public function translateCurrenciesSelector ($PA, $fObj) {
140 foreach ($PA['items'] as $index => $item) {
141 if ($PA['items'][$index][1]) {
142 $PA['items'][$index][0] = \SJBR\StaticInfoTables\Utility\LocalizationUtility::translate(array('uid' => $item[1]), 'static_currencies');
143 }
144 }
145 asort($PA['items']);
146 }
147
148 /*
149 * Translate and sort the languages selector using the current locale
150 */
151 public function translateLanguagesSelector ($PA, $fObj) {
152 foreach ($PA['items'] as $index => $item) {
153 if ($PA['items'][$index][1]) {
154 //Get isocode if present
155 $code = strstr($item[0], '(');
156 $code2 = strstr(substr($code, 1), '(');
157 $code = $code2 ? $code2 : $code;
158 // Translate
159 $PA['items'][$index][0] = \SJBR\StaticInfoTables\Utility\LocalizationUtility::translate(array('uid' => $item[1]), 'static_languages');
160 // Re-append isocode, if present
161 $PA['items'][$index][0] = $PA['items'][$index][0] . ($code ? ' ' . $code : '');
162 }
163 }
164 asort($PA['items']);
165 }
166 }
167 ?>