Resolves #59896: TCA Selection of countries with different key
authorstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Tue, 23 Sep 2014 05:30:27 +0000 (05:30 +0000)
committerstan <stan@735d13b6-9817-0410-8766-e36946ffe9aa>
Tue, 23 Sep 2014 05:30:27 +0000 (05:30 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/static_info_tables/trunk@86392 735d13b6-9817-0410-8766-e36946ffe9aa

ChangeLog
Classes/Domain/Repository/AbstractEntityRepository.php
Classes/Hook/Backend/Form/ElementRenderingHelper.php
doc/manual.sxw

index a545788..dbb5787 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,7 @@
        * Resolves #61769: Polish zones should be written beginning from lower letter
        * Rename directory Configuration/Tca to Configuration/TCA
        * Resolves #60725: Reduce used memory of the Domain Models (Thanks to Tim Lochm├╝ller)
+       * Resolves #59896: TCA Selection of countries with different key
 
 2014-07-29  Stanislas Rolland  <typo3@sjbr.ca>
 
index a52676f..ad68112 100644 (file)
@@ -69,6 +69,23 @@ abstract class AbstractEntityRepository extends \TYPO3\CMS\Extbase\Persistence\R
        }
 
        /**
+        * Find all objects with uid in list
+        *
+        * @param string $list: list of uid's
+        * @return \TYPO3\CMS\Extbase\Persistence\QueryResultInterface|array all entries
+        */
+       public function findAllByUidInList($list = '') {
+               if (empty($list)) {
+                       return array();
+               } else {
+                       $query = $this->createQuery();
+                       $list = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $list, TRUE);
+                       $query->matching($query->in('uid', $list));
+                       return $query->execute();
+               }
+       }
+
+       /**
         * Find all ordered by the localized name
         *
         * @param string $orderDirection may be "asc" or "desc". Default is "asc".
index cae723e..4f5e287 100644 (file)
@@ -1,8 +1,5 @@
 <?php
 namespace SJBR\StaticInfoTables\Hook\Backend\Form;
-use SJBR\StaticInfoTables\Utility\TcaUtility;
-use SJBR\StaticInfoTables\Utility\LocalizationUtility;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
 /***************************************************************
 *  Copyright notice
 *
@@ -29,6 +26,12 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  * Custom rendering of some backend forms elements
  *
  */
+
+use SJBR\StaticInfoTables\Utility\ModelUtility;
+use SJBR\StaticInfoTables\Utility\TcaUtility;
+use SJBR\StaticInfoTables\Utility\LocalizationUtility;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
 class ElementRenderingHelper {
        /**
         * Translate selected items before rendering
@@ -46,7 +49,7 @@ class ElementRenderingHelper {
                }
        }
 
-       /*
+       /**
         * Add ISO codes to the label of entities
         */
        public function addIsoCodeToLabel (&$PA, &$fObj) {
@@ -121,7 +124,7 @@ class ElementRenderingHelper {
                }
        }
 
-       /*
+       /**
         * Translate and sort the territories selector using the current locale
         */
        public function translateTerritoriesSelector($PA, $fObj) {
@@ -137,20 +140,31 @@ class ElementRenderingHelper {
                                break;
                }
                $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_territories');
+               $PA['items'] = $this->replaceSelectorIndexField($PA);
        }
 
-       /*
+       /**
         * Translate and sort the countries selector using the current locale
         */
        public function translateCountriesSelector($PA, $fObj) {
                $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_countries');
+               $PA['items'] = $this->replaceSelectorIndexField($PA);
+       }
+
+       /**
+        * Translate and sort the country zones selector using the current locale
+        */
+       public function translateCountryZonesSelector($PA, $fObj) {
+               $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_country_zones');
+               $PA['items'] = $this->replaceSelectorIndexField($PA);
        }
 
-       /*
+       /**
         * Translate and sort the currencies selector using the current locale
         */
        public function translateCurrenciesSelector($PA, $fObj) {
                $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_currencies');
+               $PA['items'] = $this->replaceSelectorIndexField($PA);
        }
 
        /**
@@ -158,6 +172,7 @@ class ElementRenderingHelper {
         */
        public function translateLanguagesSelector($PA, $fObj) {
                $PA['items'] = $this->translateSelectorItems($PA['items'], 'static_languages');
+               $PA['items'] = $this->replaceSelectorIndexField($PA);
        }
 
        /**
@@ -229,5 +244,91 @@ class ElementRenderingHelper {
        protected function strcollOnLabels($itemA, $itemB) {
                return strcoll($itemA[0], $itemB[0]);
        }
+
+       /**
+        * Replace the selector's uid index with configured indexField
+        *
+        * @param array  $PA: TCA select field parameters array
+        * @return array The new $items array
+        */
+       protected function replaceSelectorIndexField($PA) {
+               $items = $PA['items'];
+               $indexFields = GeneralUtility::trimExplode(',', $PA['config']['itemsProcFunc_config']['indexField']);
+               if (!empty($indexFields)) {
+                       $rows = array();
+                       // Collect items uid's
+                       $uids = array();
+                       foreach ($items as $key => $item) {
+                               if ($items[$key][1]) {
+                                       $uids[] = $item[1];
+                               }
+                       }
+                       $uidList = implode(',', $uids);
+                       if (!empty($uidList)) {
+                               /** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
+                               $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
+                               switch ($PA['config']['foreign_table']) {
+                                       case 'static_territories':
+                                               /** @var $territoryRepository SJBR\StaticInfoTables\Domain\Repository\TerritoryRepository */
+                                               $territoryRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\TerritoryRepository');
+                                               $objects = $territoryRepository->findAllByUidInList($uidList)->toArray();
+                                               $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Territory', ModelUtility::MAPPING_COLUMNS);
+                                               break;
+                                       case 'static_countries':
+                                               /** @var $countryRepository SJBR\StaticInfoTables\Domain\Repository\CountryRepository */
+                                               $countryRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\CountryRepository');
+                                               $objects = $countryRepository->findAllByUidInList($uidList)->toArray();
+                                               $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Country', ModelUtility::MAPPING_COLUMNS);
+                                               break;
+                                       case 'static_country_zones':
+                                               /** @var $countryZoneRepository SJBR\StaticInfoTables\Domain\Repository\CountryZoneRepository */
+                                               $countryZoneRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\CountryZoneRepository');
+                                               $objects = $countryZoneRepository->findAllByUidInList($uidList)->toArray();
+                                               $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\CountryZone', ModelUtility::MAPPING_COLUMNS);
+                                               break;                                  
+                                       case 'static_languages':
+                                               /** @var $languageRepository SJBR\StaticInfoTables\Domain\Repository\LanguageRepository */
+                                               $languageRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\LanguageRepository');
+                                               $objects = $languageRepository->findAllByUidInList($uidList)->toArray();
+                                               $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Language', ModelUtility::MAPPING_COLUMNS);
+                                               break;
+                                       case 'static_currencies':
+                                               /** @var $currencyRepository SJBR\StaticInfoTables\Domain\Repository\CurrencyRepository */
+                                               $currencyRepository = $objectManager->get('SJBR\\StaticInfoTables\\Domain\\Repository\\CurrencyRepository');
+                                               $objects = $currencyRepository->findAllByUidInList($uidList)->toArray();
+                                               $columnsMapping = ModelUtility::getModelMapping('SJBR\\StaticInfoTables\\Domain\\Model\\Currency', ModelUtility::MAPPING_COLUMNS);
+                                               break;
+                                       default:
+                                               break;
+                               }
+                               if (!empty($objects)) {
+                                       // Map table column to object property
+                                       $indexProperties = array();
+                                       foreach ($indexFields as $indexField) {
+                                               if ($columnsMapping[$indexField]['mapOnProperty']) {
+                                                       $indexProperties[] = $columnsMapping[$indexField]['mapOnProperty'];
+                                               } else {
+                                                       $indexProperties[] = GeneralUtility::underscoredToUpperCamelCase($indexField);
+                                               }
+                                       }
+                                       // Index rows by uid
+                                       $uidIndexedRows = array();
+                                       foreach ($objects as $object) {
+                                               $uidIndexedObjects[$object->getUid()] = $object;
+                                       }
+                                       // Replace the items index field
+                                       foreach ($items as $key => $item) {
+                                               if ($items[$key][1]) {
+                                                       $object = $uidIndexedObjects[$items[$key][1]];
+                                                       $items[$key][1] = $object->_getProperty($indexProperties[0]);
+                                                       if ($indexFields[1] && $object->_getProperty($indexProperties[1])) {
+                                                               $items[$key][1] .=  '_' . $object->_getProperty($indexProperties[1]);
+                                                       }
+                                               }
+                                       }                                               
+                               }
+                       }
+               }
+               return $items;
+       }
 }
-?>
index db25df9..5d49968 100644 (file)
Binary files a/doc/manual.sxw and b/doc/manual.sxw differ