[CLEANUP] Early returns in DataHandler::localize() 85/40385/5
authorAndreas Wolf <andreas.wolf@typo3.org>
Sun, 7 Jun 2015 09:23:02 +0000 (11:23 +0200)
committerMarkus Klein <markus.klein@typo3.org>
Thu, 25 Jun 2015 17:26:50 +0000 (19:26 +0200)
Resolves: #67583
Releases: master
Change-Id: Ib3971ac6fa4c531b45227f41307d79e3e434b89f
Reviewed-on: http://review.typo3.org/40385
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/core/Classes/DataHandling/DataHandler.php

index 0c5cb91..ea2482d 100644 (file)
@@ -4318,107 +4318,134 @@ class DataHandler {
        public function localize($table, $uid, $language) {
                $newId = FALSE;
                $uid = (int)$uid;
-               if ($GLOBALS['TCA'][$table] && $uid && $this->isNestedElementCallRegistered($table, $uid, 'localize') === FALSE) {
-                       $this->registerNestedElementCall($table, $uid, 'localize');
-                       if ($GLOBALS['TCA'][$table]['ctrl']['languageField'] && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] && !$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerTable'] || $table === 'pages') {
-                               if ($langRec = BackendUtility::getRecord('sys_language', (int)$language, 'uid,title')) {
-                                       if ($this->doesRecordExist($table, $uid, 'show')) {
-                                               // Getting workspace overlay if possible - this will localize versions in workspace if any
-                                               $row = BackendUtility::getRecordWSOL($table, $uid);
-                                               if (is_array($row)) {
-                                                       if ($row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] <= 0 || $table === 'pages') {
-                                                               if ($row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] == 0 || $table === 'pages') {
-                                                                       if ($table === 'pages') {
-                                                                               $pass = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'] === 'pages_language_overlay' && !BackendUtility::getRecordsByField('pages_language_overlay', 'pid', $uid, (' AND ' . $GLOBALS['TCA']['pages_language_overlay']['ctrl']['languageField'] . '=' . (int)$langRec['uid']));
-                                                                               $Ttable = 'pages_language_overlay';
-                                                                       } else {
-                                                                               $pass = !BackendUtility::getRecordLocalization($table, $uid, $langRec['uid'], ('AND pid=' . (int)$row['pid']));
-                                                                               $Ttable = $table;
-                                                                       }
-                                                                       if ($pass) {
-                                                                               // Initialize:
-                                                                               $overrideValues = array();
-                                                                               $excludeFields = array();
-                                                                               // Set override values:
-                                                                               $overrideValues[$GLOBALS['TCA'][$Ttable]['ctrl']['languageField']] = $langRec['uid'];
-                                                                               $overrideValues[$GLOBALS['TCA'][$Ttable]['ctrl']['transOrigPointerField']] = $uid;
-                                                                               // Copy the type (if defined in both tables) from the original record so that translation has same type as original record
-                                                                               if (isset($GLOBALS['TCA'][$table]['ctrl']['type']) && isset($GLOBALS['TCA'][$Ttable]['ctrl']['type'])) {
-                                                                                       $overrideValues[$GLOBALS['TCA'][$Ttable]['ctrl']['type']] = $row[$GLOBALS['TCA'][$table]['ctrl']['type']];
-                                                                               }
-                                                                               // Set exclude Fields:
-                                                                               foreach ($GLOBALS['TCA'][$Ttable]['columns'] as $fN => $fCfg) {
-                                                                                       // Check if we are just prefixing:
-                                                                                       if ($fCfg['l10n_mode'] == 'prefixLangTitle') {
-                                                                                               if (($fCfg['config']['type'] == 'text' || $fCfg['config']['type'] == 'input') && (string)$row[$fN] !== '') {
-                                                                                                       list($tscPID) = BackendUtility::getTSCpid($table, $uid, '');
-                                                                                                       $TSConfig = $this->getTCEMAIN_TSconfig($tscPID);
-                                                                                                       if (!empty($TSConfig['translateToMessage'])) {
-                                                                                                               $translateToMsg = $GLOBALS['LANG'] ? $GLOBALS['LANG']->sL($TSConfig['translateToMessage']) : $TSConfig['translateToMessage'];
-                                                                                                               $translateToMsg = @sprintf($translateToMsg, $langRec['title']);
-                                                                                                       }
-                                                                                                       if (!strlen($translateToMsg)) {
-                                                                                                               $translateToMsg = 'Translate to ' . $langRec['title'] . ':';
-                                                                                                       } else {
-                                                                                                               $translateToMsg = @sprintf($TSConfig['translateToMessage'], $langRec['title']);
-                                                                                                       }
-                                                                                                       $overrideValues[$fN] = '[' . $translateToMsg . '] ' . $row[$fN];
-                                                                                               }
-                                                                                       } elseif (
-                                                                                               ($fCfg['l10n_mode'] === 'exclude' || $fCfg['l10n_mode'] === 'noCopy' || $fCfg['l10n_mode'] === 'mergeIfNotBlank')
-                                                                                               && $fN != $GLOBALS['TCA'][$Ttable]['ctrl']['languageField']
-                                                                                               && $fN != $GLOBALS['TCA'][$Ttable]['ctrl']['transOrigPointerField']
-                                                                                        ) {
-                                                                                               // Otherwise, do not copy field (unless it is the language field or
-                                                                                               // pointer to the original language)
-                                                                                               $excludeFields[] = $fN;
-                                                                                       }
-                                                                               }
-                                                                               if ($Ttable === $table) {
-                                                                                       // Get the uid of record after which this localized record should be inserted
-                                                                                       $previousUid = $this->getPreviousLocalizedRecordUid($table, $uid, $row['pid'], $language);
-                                                                                       // Execute the copy:
-                                                                                       $newId = $this->copyRecord($table, $uid, -$previousUid, 1, $overrideValues, implode(',', $excludeFields), $language);
-                                                                                       $autoVersionNewId = $this->getAutoVersionId($table, $newId);
-                                                                                       if (is_null($autoVersionNewId) === FALSE) {
-                                                                                               $this->triggerRemapAction($table, $newId, array($this, 'placeholderShadowing'), array($table, $autoVersionNewId), TRUE);
-                                                                                       }
-                                                                               } else {
-                                                                                       // Create new record:
-                                                                                       /** @var $copyTCE DataHandler */
-                                                                                       $copyTCE = $this->getLocalTCE();
-                                                                                       $copyTCE->start(array($Ttable => array('NEW' => $overrideValues)), '', $this->BE_USER);
-                                                                                       $copyTCE->process_datamap();
-                                                                                       // Getting the new UID as if it had been copied:
-                                                                                       $theNewSQLID = $copyTCE->substNEWwithIDs['NEW'];
-                                                                                       if ($theNewSQLID) {
-                                                                                               // If is by design that $Ttable is used and not $table! See "l10nmgr" extension. Could be debated, but this is what I chose for this "pseudo case"
-                                                                                               $this->copyMappingArray[$Ttable][$uid] = $theNewSQLID;
-                                                                                               $newId = $theNewSQLID;
-                                                                                       }
-                                                                               }
-                                                                       } elseif ($this->enableLogging) {
-                                                                               $this->newlog('Localization failed; There already was a localization for this language of the record!', 1);
-                                                                       }
-                                                               } elseif ($this->enableLogging) {
-                                                                       $this->newlog('Localization failed; Source record contained a reference to an original default record (which is strange)!', 1);
-                                                               }
-                                                       } elseif ($this->enableLogging) {
-                                                               $this->newlog('Localization failed; Source record had another language than "Default" or "All" defined!', 1);
-                                                       }
-                                               } elseif ($this->enableLogging) {
-                                                       $this->newlog('Attempt to localize record that did not exist!', 1);
-                                               }
-                                       } elseif ($this->enableLogging) {
-                                               $this->newlog('Attempt to localize record without permission', 1);
-                                       }
-                               } elseif ($this->enableLogging) {
-                                       $this->newlog('Sys language UID "' . $language . '" not found valid!', 1);
-                               }
-                       } elseif ($this->enableLogging) {
+               if (!$GLOBALS['TCA'][$table] || !$uid || $this->isNestedElementCallRegistered($table, $uid, 'localize') !== FALSE) {
+                       return FALSE;
+               }
+
+               $this->registerNestedElementCall($table, $uid, 'localize');
+               if ((!$GLOBALS['TCA'][$table]['ctrl']['languageField'] || !$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] || $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerTable']) && $table !== 'pages') {
+                       if ($this->enableLogging) {
                                $this->newlog('Localization failed; "languageField" and "transOrigPointerField" must be defined for the table!', 1);
                        }
+                       return FALSE;
+               }
+
+               $langRec = BackendUtility::getRecord('sys_language', (int)$language, 'uid,title');
+               if (!$langRec) {
+                       if ($this->enableLogging) {
+                               $this->newlog('Sys language UID "' . $language . '" not found valid!', 1);
+                       }
+                       return FALSE;
+               }
+
+               if (!$this->doesRecordExist($table, $uid, 'show')) {
+                       if ($this->enableLogging) {
+                               $this->newlog('Attempt to localize record without permission', 1);
+                       }
+                       return FALSE;
                }
+
+               // Getting workspace overlay if possible - this will localize versions in workspace if any
+               $row = BackendUtility::getRecordWSOL($table, $uid);
+               if (!is_array($row)) {
+                       if ($this->enableLogging) {
+                               $this->newlog('Attempt to localize record that did not exist!', 1);
+                       }
+                       return FALSE;
+               }
+
+               if ($row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0 && $table !== 'pages') {
+                       if ($this->enableLogging) {
+                               $this->newlog('Localization failed; Source record had another language than "Default" or "All" defined!', 1);
+                       }
+                       return FALSE;
+               }
+
+               if ($row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] != 0 && $table !== 'pages') {
+                       if ($this->enableLogging) {
+                               $this->newlog('Localization failed; Source record contained a reference to an original default record (which is strange)!', 1);
+                       }
+                       return FALSE;
+               }
+
+               if ($table === 'pages') {
+                       $pass = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'] === 'pages_language_overlay' && !BackendUtility::getRecordsByField('pages_language_overlay', 'pid', $uid, (' AND ' . $GLOBALS['TCA']['pages_language_overlay']['ctrl']['languageField'] . '=' . (int)$langRec['uid']));
+                       $Ttable = 'pages_language_overlay';
+               } else {
+                       $pass = !BackendUtility::getRecordLocalization($table, $uid, $langRec['uid'], ('AND pid=' . (int)$row['pid']));
+                       $Ttable = $table;
+               }
+
+               if (!$pass) {
+                       if ($this->enableLogging) {
+                               $this->newlog('Localization failed; There already was a localization for this language of the record!', 1);
+                       }
+                       return FALSE;
+               }
+
+               // Initialize:
+               $overrideValues = array();
+               $excludeFields = array();
+               // Set override values:
+               $overrideValues[$GLOBALS['TCA'][$Ttable]['ctrl']['languageField']] = $langRec['uid'];
+               $overrideValues[$GLOBALS['TCA'][$Ttable]['ctrl']['transOrigPointerField']] = $uid;
+               // Copy the type (if defined in both tables) from the original record so that translation has same type as original record
+               if (isset($GLOBALS['TCA'][$table]['ctrl']['type']) && isset($GLOBALS['TCA'][$Ttable]['ctrl']['type'])) {
+                       $overrideValues[$GLOBALS['TCA'][$Ttable]['ctrl']['type']] = $row[$GLOBALS['TCA'][$table]['ctrl']['type']];
+               }
+               // Set exclude Fields:
+               foreach ($GLOBALS['TCA'][$Ttable]['columns'] as $fN => $fCfg) {
+                       // Check if we are just prefixing:
+                       if ($fCfg['l10n_mode'] == 'prefixLangTitle') {
+                               if (($fCfg['config']['type'] == 'text' || $fCfg['config']['type'] == 'input') && (string)$row[$fN] !== '') {
+                                       list($tscPID) = BackendUtility::getTSCpid($table, $uid, '');
+                                       $TSConfig = $this->getTCEMAIN_TSconfig($tscPID);
+                                       if (!empty($TSConfig['translateToMessage'])) {
+                                               $translateToMsg = $GLOBALS['LANG'] ? $GLOBALS['LANG']->sL($TSConfig['translateToMessage']) : $TSConfig['translateToMessage'];
+                                               $translateToMsg = @sprintf($translateToMsg, $langRec['title']);
+                                       }
+                                       if (!strlen($translateToMsg)) {
+                                               $translateToMsg = 'Translate to ' . $langRec['title'] . ':';
+                                       } else {
+                                               $translateToMsg = @sprintf($TSConfig['translateToMessage'], $langRec['title']);
+                                       }
+                                       $overrideValues[$fN] = '[' . $translateToMsg . '] ' . $row[$fN];
+                               }
+                       } elseif (
+                               ($fCfg['l10n_mode'] === 'exclude' || $fCfg['l10n_mode'] === 'noCopy' || $fCfg['l10n_mode'] === 'mergeIfNotBlank')
+                                       && $fN != $GLOBALS['TCA'][$Ttable]['ctrl']['languageField']
+                                       && $fN != $GLOBALS['TCA'][$Ttable]['ctrl']['transOrigPointerField']
+                        ) {
+                               // Otherwise, do not copy field (unless it is the language field or
+                               // pointer to the original language)
+                               $excludeFields[] = $fN;
+                       }
+               }
+               if ($Ttable === $table) {
+                       // Get the uid of record after which this localized record should be inserted
+                       $previousUid = $this->getPreviousLocalizedRecordUid($table, $uid, $row['pid'], $language);
+                       // Execute the copy:
+                       $newId = $this->copyRecord($table, $uid, -$previousUid, 1, $overrideValues, implode(',', $excludeFields), $language);
+                       $autoVersionNewId = $this->getAutoVersionId($table, $newId);
+                       if (is_null($autoVersionNewId) === FALSE) {
+                               $this->triggerRemapAction($table, $newId, array($this, 'placeholderShadowing'), array($table, $autoVersionNewId), TRUE);
+                       }
+               } else {
+                       // Create new record:
+                       /** @var $copyTCE DataHandler */
+                       $copyTCE = $this->getLocalTCE();
+                       $copyTCE->start(array($Ttable => array('NEW' => $overrideValues)), '', $this->BE_USER);
+                       $copyTCE->process_datamap();
+                       // Getting the new UID as if it had been copied:
+                       $theNewSQLID = $copyTCE->substNEWwithIDs['NEW'];
+                       if ($theNewSQLID) {
+                               // If is by design that $Ttable is used and not $table! See "l10nmgr" extension. Could be debated, but this is what I chose for this "pseudo case"
+                               $this->copyMappingArray[$Ttable][$uid] = $theNewSQLID;
+                               $newId = $theNewSQLID;
+                       }
+               }
+
+
                return $newId;
        }