[FEATURE] Migrate ProcessDataMap hook to Doctrine 32/51232/3
authorStanislas Rolland <typo3@sjbr.ca>
Tue, 10 Jan 2017 05:28:12 +0000 (00:28 -0500)
committerStanislas Rolland <typo3@sjbr.ca>
Tue, 10 Jan 2017 05:33:11 +0000 (06:33 +0100)
Change-Id: I8ad4dd444ce460b32c531edaf28846883a10b7a8
Resolves: #79230
Reviewed-on: https://review.typo3.org/51232
Reviewed-by: Stanislas Rolland <typo3@sjbr.ca>
Tested-by: Stanislas Rolland <typo3@sjbr.ca>
ChangeLog
Classes/Hook/Core/DataHandling/ProcessDataMap.php

index 94861ed..1ee0ef9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,7 @@
        * Follow-up to Resolves #79209: Migrate update script to Doctrine
        * Resolves #79229: Migrate suggest receiver to Doctrine
        * Follow-up to Resolves #79209: Migrate update script to Doctrine
+       * Resolves #79230: Migrate ProcessDataMap hook to Doctrine
 
 2017-01-08  Stanislas Rolland  <typo3(arobas)sjbr.ca>
 
index 8960f09..35f14d3 100644 (file)
 <?php
 namespace SJBR\StaticInfoTables\Hook\Core\DataHandling;
+
 /***************************************************************
-*  Copyright notice
-*
-*  (c) 2013 Stanislas Rolland <typo3(arobas)sjbr.ca>
-*  All rights reserved
-*
-*  This script is part of the Typo3 project. The Typo3 project is
-*  free software; you can redistribute it and/or modify
-*  it under the terms of the GNU General Public License as published by
-*  the Free Software Foundation; either version 2 of the License, or
-*  (at your option) any later version.
-*
-*  The GNU General Public License can be found at
-*  http://www.gnu.org/copyleft/gpl.html.
-*
-*  This script is distributed in the hope that it will be useful,
-*  but WITHOUT ANY WARRANTY; without even the implied warranty of
-*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-*  GNU General Public License for more details.
-*
-*  This copyright notice MUST APPEAR in all copies of the script!
-***************************************************************/
+ *  Copyright notice
+ *
+ *  (c) 2013-2017 Stanislas Rolland <typo3(arobas)sjbr.ca>
+ *  All rights reserved
+ *
+ *  This script is part of the Typo3 project. The Typo3 project is
+ *  free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  The GNU General Public License can be found at
+ *  http://www.gnu.org/copyleft/gpl.html.
+ *
+ *  This script is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  This copyright notice MUST APPEAR in all copies of the script!
+ ***************************************************************/
+
+use SJBR\StaticInfoTables\Domain\Repository\CountryRepository;
+use SJBR\StaticInfoTables\Domain\Repository\CurrencyRepository;
+use SJBR\StaticInfoTables\Domain\Repository\TerritoryRepository;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Object\ObjectManager;
+
 /**
  * Hook on Core/DataHandling/DataHandler to manage redundancy of ISO codes in static info tables
  */
-class ProcessDataMap {
-
+class ProcessDataMap
+{
        /**
         * Post-process redundant ISO codes fields
         *
-        * @param       object          $fobj TCEmain object reference
-        * @return      void
+        * @param object $fobj TCEmain object reference
+        * @return void
         */
-       public function processDatamap_postProcessFieldArray ($status, $table, $id, &$incomingFieldArray, &$fObj) {
+       public function processDatamap_postProcessFieldArray($status, $table, $id, &$incomingFieldArray, &$fObj)
+       {
                switch ($table) {
                        case 'static_territories':
+                               $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
                                //Post-process containing territory ISO numeric code
                                if ($incomingFieldArray['tr_parent_territory_uid']) {
-                                       $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
-                                               'uid,tr_iso_nr',
-                                               'static_territories',
-                                               'uid = ' . intval($incomingFieldArray['tr_parent_territory_uid']) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('static_territories')
-                                       );
-                                       $incomingFieldArray['tr_parent_iso_nr'] = $rows[0]['tr_iso_nr'];
+                                       $territoryRepository = $objectManager->get(TerritoryRepository::class);
+                                       $territory = $territoryRepository->findOneByUid((int)$incomingFieldArray['tr_parent_territory_uid']);
+                                       if (is_object($territory)) {
+                                               $incomingFieldArray['tr_parent_iso_nr'] = $territory->getUnCodeNumber();
+                                       }
                                } else if (isset($incomingFieldArray['tr_parent_territory_uid'])) {
                                        $incomingFieldArray['tr_parent_iso_nr'] = 0;
                                }
                                break;
                        case 'static_countries':
+                               $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
                                //Post-process containing territory ISO numeric code
                                if ($incomingFieldArray['cn_parent_territory_uid']) {
-                                       $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
-                                               'uid,tr_iso_nr',
-                                               'static_territories',
-                                               'uid = ' . intval($incomingFieldArray['cn_parent_territory_uid']) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('static_territories')
-                                       );
-                                       $incomingFieldArray['cn_parent_tr_iso_nr'] = $rows[0]['tr_iso_nr'];
+                                       $territoryRepository = $objectManager->get(TerritoryRepository::class);
+                                       $territory = $territoryRepository->findOneByUid((int)$incomingFieldArray['cn_parent_territory_uid']);
+                                       if (is_object($territory)) {
+                                               $incomingFieldArray['cn_parent_tr_iso_nr'] = $territory->getUnCodeNumber();
+                                       }
                                } else if (isset($incomingFieldArray['cn_parent_territory_uid'])) {
                                        $incomingFieldArray['cn_parent_tr_iso_nr'] = 0;
                                }
                                //Post-process currency ISO numeric and A3 codes
                                if ($incomingFieldArray['cn_currency_uid']) {
-                                       $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
-                                               'uid,cu_iso_nr,cu_iso_3',
-                                               'static_currencies',
-                                               'uid = ' . intval($incomingFieldArray['cn_currency_uid']) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('static_currencies')
-                                       );
-                                       $incomingFieldArray['cn_currency_iso_nr'] = $rows[0]['cu_iso_nr'];
-                                       $incomingFieldArray['cn_currency_iso_3'] = $rows[0]['cu_iso_3'];
+                                       $currencyRepository = $objectManager->get(CurrencyRepository::class);
+                                       $currency = $currencyRepository->findOneByUid((int)$incomingFieldArray['cn_currency_uid']);
+                                       if (is_object($currency)) {
+                                               $incomingFieldArray['cn_currency_iso_nr'] = $currency->getIsoCodeNumber();
+                                               $incomingFieldArray['cn_currency_iso_3'] = $currency->getIsoCodeA3();
+                                       }
                                } else if (isset($incomingFieldArray['cn_currency_uid'])) {
                                        $incomingFieldArray['cn_currency_iso_nr'] = 0;
                                        $incomingFieldArray['cn_currency_iso_3'] = '';
                                }
-                               break;                  
+                               break;
                }
        }
+
        /**
         * Post-process redundant ISO codes fields of IRRE child
         *
-        * @param       object          $fobj TCEmain object reference
-        * @return      void
+        * @param object $fobj TCEmain object reference
+        * @return void
         */
-       public function processDatamap_afterDatabaseOperations ($status, $table, $id, &$fieldArray, &$fObj) {
+       public function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray, &$fObj)
+       {
                switch ($table) {
                        case 'static_countries':
                                //Post-process country ISO numeric, A2 and A3 codes on country zones
                                // Get the country record uid
-                               if ($status == 'new') {
+                               if ($status === 'new') {
                                        $id = $fObj->substNEWwithIDs[$id];
                                }
+                               // Get the country
+                               $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
+                               $countryRepository = $objectManager->get(CountryRepository::class);
+                               $country = $countryRepository->findOneByUid((int)$id);
                                // Get the country zones
-                               $countryZones = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
-                                       '*',
-                                       'static_country_zones',
-                                       'zn_country_uid = ' . intval($id) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause('static_country_zones')
-                               );
-                               if (is_array($countryZones) && count($countryZones)) {
-                                       $countries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
-                                               'uid,cn_iso_nr,cn_iso_2,cn_iso_3',
-                                               $table,
-                                               'uid = ' . intval($id) . \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($table)
-                                       );
-                                       foreach ($countryZones as $countryZone) {
-                                               $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
-                                                       'static_country_zones',
-                                                       'uid = ' . intval($countryZone['uid']),
-                                                       array (
-                                                               'zn_country_iso_nr' => intval($countries[0]['cn_iso_nr']),
-                                                               'zn_country_iso_2' => $countries[0]['cn_iso_2'],
-                                                               'zn_country_iso_3' => $countries[0]['cn_iso_3']
-                                                       )
-                                               );
+                               $countryZones = $country->getCountryZones()->toArray();
+                               if (class_exists(\TYPO3\CMS\Core\Database\ConnectionPool::class)) {
+                                       if (count($countryZones)) {
+                                               $connection = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)->getConnectionForTable('static_country_zones');
+                                               foreach ($countryZones as $countryZone) {
+                                                       $connection->update(
+                                                               'static_country_zones',
+                                                               [
+                                                                       'zn_country_iso_nr' => (int)$country->getIsoCodeNumber(),
+                                                                       'zn_country_iso_2' => $country->getIsoCodeA2(),
+                                                                       'zn_country_iso_3' => $country->getIsoCodeA3()
+                                                               ],
+                                                               ['uid' => (int)$countryZone->getUid()]
+                                                       );
+                                               }
+                                       }
+                               } else {
+                                       // TYPO3 CMS 7 LTS
+                                       if (count($countryZones)) {
+                                               foreach ($countryZones as $countryZone) {
+                                                       $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
+                                                               'static_country_zones',
+                                                               'uid = ' . (int)$countryZone->getUid(),
+                                                               [
+                                                                       'zn_country_iso_nr' => (int)$country->getIsoCodeNumber(),
+                                                                       'zn_country_iso_2' => $country->getIsoCodeA2(),
+                                                                       'zn_country_iso_3' => $country->getIsoCodeA3()
+                                                               ]
+                                                       );
+                                               }
                                        }
                                }
-                               break;                          
+                               break;
                }
        }
-}
-?>
+}
\ No newline at end of file