[CLEANUP] Code duplication in DataHandler::copyRecord_processManyToMany() 23/40423/5
authorAndreas Wolf <andreas.wolf@typo3.org>
Wed, 17 Jun 2015 18:54:07 +0000 (20:54 +0200)
committerAndreas Fernandez <typo3@scripting-base.de>
Thu, 18 Jun 2015 11:01:01 +0000 (13:01 +0200)
There was a foreach loop that only differed in one line, but was
duplicated due to the complex condition around it. This commit splits
the condition, introduces helper variables and moves the condition
inside the foreach loop to get rid of the duplication.

Additionally, duplicated lines for purging the items of the relation
analyzer are also removed.

Resolves: #67579
Releases: master
Change-Id: Ibb9277dfb32960cc77aee17839d7df3bf57fc6da
Reviewed-on: http://review.typo3.org/40423
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
Reviewed-by: Stephan GroƟberndt <stephan@grossberndt.de>
Reviewed-by: Andreas Fernandez <typo3@scripting-base.de>
Tested-by: Andreas Fernandez <typo3@scripting-base.de>
typo3/sysext/core/Classes/DataHandling/DataHandler.php

index 3d5f639..375bce9 100644 (file)
@@ -3623,7 +3623,7 @@ class DataHandler {
        }
 
        /**
-        * Processes the children of an MM relation field when the parent record is copied.
+        * Processes the children of an MM relation field (select, group, inline) when the parent record is copied.
         *
         * @param string $table
         * @param int $uid
@@ -3635,8 +3635,7 @@ class DataHandler {
         * @param string $inlineSubType
         * @return mixed
         */
-       protected function copyRecord_processManyToMany($table, $uid, $field, $value, $conf, $language, $localizationMode,
-                                                    $inlineSubType) {
+       protected function copyRecord_processManyToMany($table, $uid, $field, $value, $conf, $language, $localizationMode, $inlineSubType) {
                $allowedTables = $conf['type'] == 'group' ? $conf['allowed'] : $conf['foreign_table'] . ',' . $conf['neg_foreign_table'];
                $prependName = $conf['type'] == 'group' ? $conf['prepend_tname'] : $conf['neg_foreign_table'];
                $mmTable = isset($conf['MM']) && $conf['MM'] ? $conf['MM'] : '';
@@ -3647,31 +3646,27 @@ class DataHandler {
                $dbAnalysis = $this->createRelationHandlerInstance();
                $dbAnalysis->start($value, $allowedTables, $mmTable, $uid, $table, $conf);
                // Localize referenced records of select fields:
-               if ($language > 0 && ($localizeReferences && empty($mmTable) || $localizeChildren && $localizationMode === 'select' && $inlineSubType === 'mm')) {
+               $localizingNonManyToManyFieldReferences = $localizeReferences && empty($mmTable);
+               $isInlineFieldInSelectMode = $localizationMode === 'select' && $inlineSubType === 'mm';
+               $purgeItems = FALSE;
+               if ($language > 0 && ($localizingNonManyToManyFieldReferences || $isInlineFieldInSelectMode)) {
                        foreach ($dbAnalysis->itemArray as $index => $item) {
                                // Since select fields can reference many records, check whether there's already a localization:
                                $recordLocalization = BackendUtility::getRecordLocalization($item['table'], $item['id'], $language);
                                if ($recordLocalization) {
                                        $dbAnalysis->itemArray[$index]['id'] = $recordLocalization[0]['uid'];
                                } elseif ($this->isNestedElementCallRegistered($item['table'], $item['id'], 'localize') === FALSE) {
-                                       $dbAnalysis->itemArray[$index]['id'] = $this->localize($item['table'], $item['id'], $language);
-                               }
-                       }
-                       $dbAnalysis->purgeItemArray();
-                       $value = implode(',', $dbAnalysis->getValueArray($prependName));
-               } elseif ($language > 0 && $localizeChildren === FALSE && $localizationMode === 'select' && $inlineSubType === 'mm') {
-                       foreach ($dbAnalysis->itemArray as $index => $item) {
-                               // Since select fields can reference many records, check whether there's already a localization:
-                               $recordLocalization = BackendUtility::getRecordLocalization($item['table'], $item['id'], $language);
-                               if ($recordLocalization) {
-                                       $dbAnalysis->itemArray[$index]['id'] = $recordLocalization[0]['uid'];
-                               } elseif ($this->isNestedElementCallRegistered($item['table'], $item['id'], 'localize') === FALSE) {
-                                       unset($dbAnalysis->itemArray[$index]);
+                                       if ($localizingNonManyToManyFieldReferences || $localizeChildren) {
+                                               $dbAnalysis->itemArray[$index]['id'] = $this->localize($item['table'], $item['id'], $language);
+                                       } else {
+                                               unset($dbAnalysis->itemArray[$index]);
+                                       }
                                }
                        }
-                       $dbAnalysis->purgeItemArray();
-                       $value = implode(',', $dbAnalysis->getValueArray($prependName));
-               } elseif ($mmTable) {
+                       $purgeItems = TRUE;
+               }
+
+               if ($purgeItems || $mmTable) {
                        $dbAnalysis->purgeItemArray();
                        $value = implode(',', $dbAnalysis->getValueArray($prependName));
                }