Apply TCA userFuncs to Static Info Tables Manager tables
[TYPO3CMS/Extensions/static_info_tables.git] / Classes / Hook / Core / DataHandling / class.tx_staticinfotables_processdatamap.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2013 Stanislas Rolland <typo3(arobas)sjbr.ca>
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 /**
25 * Hook on Core/DataHandling/DataHandler to manage redundancy of ISO codes in static info tables
26 */
27 class tx_staticinfotables_processDataMap {
28
29 /**
30 * Post-process redundant ISO codes fields
31 *
32 * @param object $fobj TCEmain object reference
33 * @return void
34 */
35 public function processDatamap_postProcessFieldArray ($status, $table, $id, &$incomingFieldArray, &$fObj) {
36 switch ($table) {
37 case 'static_territories':
38 case 'cc_static_territories':
39 //Post-process containing territory ISO numeric code
40 $tablePrefix = ($table == 'cc_static_territories' ? 'cc_': '');
41 if ($incomingFieldArray['tr_parent_territory_uid']) {
42 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
43 'uid,tr_iso_nr',
44 $tablePrefix . 'static_territories',
45 'uid = ' . intval($incomingFieldArray['tr_parent_territory_uid']) . t3lib_befunc::deleteClause($tablePrefix . 'static_territories')
46 );
47 $incomingFieldArray['tr_parent_iso_nr'] = $rows[0]['tr_iso_nr'];
48 } else if (isset($incomingFieldArray['tr_parent_territory_uid'])) {
49 $incomingFieldArray['tr_parent_iso_nr'] = NULL;
50 }
51 break;
52 case 'static_countries':
53 case 'cc_static_countries':
54 $tablePrefix = ($table == 'cc_static_countries' ? 'cc_': '');
55 //Post-process containing territory ISO numeric code
56 if ($incomingFieldArray['cn_parent_territory_uid']) {
57 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
58 'uid,tr_iso_nr',
59 $tablePrefix . 'static_territories',
60 'uid = ' . intval($incomingFieldArray['cn_parent_territory_uid']) . t3lib_befunc::deleteClause($tablePrefix . 'static_territories')
61 );
62 $incomingFieldArray['cn_parent_tr_iso_nr'] = $rows[0]['tr_iso_nr'];
63 } else if (isset($incomingFieldArray['cn_parent_territory_uid'])) {
64 $incomingFieldArray['cn_parent_tr_iso_nr'] = NULL;
65 }
66 //Post-process currency ISO numeric and A3 codes
67 if ($incomingFieldArray['cn_currency_uid']) {
68 $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
69 'uid,cu_iso_nr,cu_iso_3',
70 $tablePrefix . 'static_currencies',
71 'uid = ' . intval($incomingFieldArray['cn_currency_uid']) . t3lib_befunc::deleteClause($tablePrefix . 'static_currencies')
72 );
73 $incomingFieldArray['cn_currency_iso_nr'] = $rows[0]['cu_iso_nr'];
74 $incomingFieldArray['cn_currency_iso_3'] = $rows[0]['cu_iso_3'];
75 } else if (isset($incomingFieldArray['cn_currency_uid'])) {
76 $incomingFieldArray['cn_currency_iso_nr'] = NULL;
77 $incomingFieldArray['cn_currency_iso_3'] = NULL;
78 }
79 break;
80 }
81 }
82 /**
83 * Post-process redundant ISO codes fields of IRRE child
84 *
85 * @param object $fobj TCEmain object reference
86 * @return void
87 */
88 public function processDatamap_afterDatabaseOperations ($status, $table, $id, &$fieldArray, &$fObj) {
89 switch ($table) {
90 case 'static_countries':
91 case 'cc_static_countries':
92 //Post-process country ISO numeric, A2 and A3 codes on country zones
93 $tablePrefix = ($table == 'cc_static_countries' ? 'cc_': '');
94 // Get the country record uid
95 if ($status == 'new') {
96 $id = $fObj->substNEWwithIDs[$id];
97 }
98 // Get the country zones
99 $countryZones = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
100 '*',
101 $tablePrefix . 'static_country_zones',
102 'zn_country_uid = ' . intval($id) . t3lib_befunc::deleteClause($tablePrefix . 'static_country_zones')
103 );
104 if (is_array($countryZones) && count($countryZones)) {
105 $countries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
106 'uid,cn_iso_nr,cn_iso_2,cn_iso_3',
107 $table,
108 'uid = ' . intval($id) . t3lib_befunc::deleteClause($table)
109 );
110 foreach ($countryZones as $countryZone) {
111 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
112 $tablePrefix . 'static_country_zones',
113 'uid = ' . intval($countryZone['uid']),
114 array (
115 'zn_country_iso_nr' => intval($countries[0]['cn_iso_nr']),
116 'zn_country_iso_2' => $countries[0]['cn_iso_2'],
117 'zn_country_iso_3' => $countries[0]['cn_iso_3']
118 )
119 );
120 }
121 }
122 break;
123 }
124 }
125 }
126 ?>