Resolves #45809: Refactor code and introduce namespacing
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Hook / Core / DataHandling / ProcessDataMap.php
1 <?php
2 namespace SJBR\StaticInfoTables\Hook\Core\DataHandling;
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 * Hook on Core/DataHandling/DataHandler to manage redundancy of ISO codes in static info tables
27 */
28 class ProcessDataMap {
29
30 /**
31 * Post-process redundant ISO codes fields
32 *
33 * @param object $fobj TCEmain object reference
34 * @return void
35 */
36 public function processDatamap_postProcessFieldArray ($status, $table, $id, &$incomingFieldArray, &$fObj) {
37 switch ($table) {
38 case 'static_territories':
39 case 'cc_static_territories':
40 //Post-process containing territory ISO numeric code
41 $tablePrefix = ($table == 'cc_static_territories' ? 'cc_': '');
42 if ($incomingFieldArray['tr_parent_territory_uid']) {
43 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
44 'uid,tr_iso_nr',
45 $tablePrefix . 'static_territories',
46 'uid = ' . intval($incomingFieldArray['tr_parent_territory_uid']) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tablePrefix . 'static_territories')
47 );
48 $incomingFieldArray['tr_parent_iso_nr'] = $rows[0]['tr_iso_nr'];
49 } else if (isset($incomingFieldArray['tr_parent_territory_uid'])) {
50 $incomingFieldArray['tr_parent_iso_nr'] = 0;
51 }
52 break;
53 case 'static_countries':
54 case 'cc_static_countries':
55 $tablePrefix = ($table == 'cc_static_countries' ? 'cc_': '');
56 //Post-process containing territory ISO numeric code
57 if ($incomingFieldArray['cn_parent_territory_uid']) {
58 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
59 'uid,tr_iso_nr',
60 $tablePrefix . 'static_territories',
61 'uid = ' . intval($incomingFieldArray['cn_parent_territory_uid']) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tablePrefix . 'static_territories')
62 );
63 $incomingFieldArray['cn_parent_tr_iso_nr'] = $rows[0]['tr_iso_nr'];
64 } else if (isset($incomingFieldArray['cn_parent_territory_uid'])) {
65 $incomingFieldArray['cn_parent_tr_iso_nr'] = 0;
66 }
67 //Post-process currency ISO numeric and A3 codes
68 if ($incomingFieldArray['cn_currency_uid']) {
69 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
70 'uid,cu_iso_nr,cu_iso_3',
71 $tablePrefix . 'static_currencies',
72 'uid = ' . intval($incomingFieldArray['cn_currency_uid']) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tablePrefix . 'static_currencies')
73 );
74 $incomingFieldArray['cn_currency_iso_nr'] = $rows[0]['cu_iso_nr'];
75 $incomingFieldArray['cn_currency_iso_3'] = $rows[0]['cu_iso_3'];
76 } else if (isset($incomingFieldArray['cn_currency_uid'])) {
77 $incomingFieldArray['cn_currency_iso_nr'] = 0;
78 $incomingFieldArray['cn_currency_iso_3'] = '';
79 }
80 break;
81 }
82 }
83 /**
84 * Post-process redundant ISO codes fields of IRRE child
85 *
86 * @param object $fobj TCEmain object reference
87 * @return void
88 */
89 public function processDatamap_afterDatabaseOperations ($status, $table, $id, &$fieldArray, &$fObj) {
90 switch ($table) {
91 case 'static_countries':
92 case 'cc_static_countries':
93 //Post-process country ISO numeric, A2 and A3 codes on country zones
94 $tablePrefix = ($table == 'cc_static_countries' ? 'cc_': '');
95 // Get the country record uid
96 if ($status == 'new') {
97 $id = $fObj->substNEWwithIDs[$id];
98 }
99 // Get the country zones
100 $countryZones = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
101 '*',
102 $tablePrefix . 'static_country_zones',
103 'zn_country_uid = ' . intval($id) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tablePrefix . 'static_country_zones')
104 );
105 if (is_array($countryZones) && count($countryZones)) {
106 $countries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
107 'uid,cn_iso_nr,cn_iso_2,cn_iso_3',
108 $table,
109 'uid = ' . intval($id) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($table)
110 );
111 foreach ($countryZones as $countryZone) {
112 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
113 $tablePrefix . 'static_country_zones',
114 'uid = ' . intval($countryZone['uid']),
115 array (
116 'zn_country_iso_nr' => intval($countries[0]['cn_iso_nr']),
117 'zn_country_iso_2' => $countries[0]['cn_iso_2'],
118 'zn_country_iso_3' => $countries[0]['cn_iso_3']
119 )
120 );
121 }
122 }
123 break;
124 }
125 }
126 }
127 ?>