[CLEANUP] TranslationConfigurationProvider 58/30258/21
authorSebastian Michaelsen <sebastian@michaelsen.io>
Thu, 29 Jan 2015 19:47:12 +0000 (20:47 +0100)
committerNicole Cordes <typo3@cordes.co>
Sun, 1 Feb 2015 14:07:12 +0000 (15:07 +0100)
* Import class names with use statements
* Encapsulate $GLOBALS['TYPO3_DB'] and $GLOBALS['LANG'] in type-hinted
  getters
* Remove unused $backPath parameter of ->getSystemLanguages
  and adapt the core method calls
* Refactor ->getSystemLanguages method
* Refactor ->translationInfo method
* Break very long lines of code into smaller chunks
* Fix phpDoc issues
* Use lowerCamelCase for variable names

Releases: master
Resolves: #59004
Change-Id: Ib5cbcb19559a47b067a1e666528de3d6e23f406b
Reviewed-on: http://review.typo3.org/30258
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
typo3/sysext/backend/Classes/Configuration/TranslationConfigurationProvider.php
typo3/sysext/backend/Classes/Form/FormEngine.php
typo3/sysext/backend/Classes/RecordList/AbstractRecordList.php

index 0577f81..63a54c5 100644 (file)
@@ -16,6 +16,9 @@ namespace TYPO3\CMS\Backend\Configuration;
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Backend\Utility\IconUtility;
+use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
+use TYPO3\CMS\Lang\LanguageService;
 
 /**
  * Contains translation tools
@@ -25,58 +28,65 @@ use TYPO3\CMS\Backend\Utility\IconUtility;
 class TranslationConfigurationProvider {
 
        /**
+        * @return DatabaseConnection
+        */
+       protected function getDatabaseConnection() {
+               return $GLOBALS['TYPO3_DB'];
+       }
+
+       /**
+        * @return LanguageService
+        */
+       protected function getLanguageService() {
+               return $GLOBALS['LANG'];
+       }
+
+       /**
         * Returns array of system languages
         *
         * The property flagIcon returns a string <flags-xx>. The calling party should call
         * \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon(<flags-xx>) to get an HTML
         * which will represent the flag of this language.
         *
-        * @param int $page_id Page id (only used to get TSconfig configuration setting flag and label for default language)
-        * @param string $backPath Backpath for flags
-        * @return array Array with languages (title, uid, flagIcon - used with IconUtility::getSpriteIcon)
+        * @param int $pageId Page id (used to get TSconfig configuration setting flag and label for default language)
+        * @return array Array with languages (uid, title, ISOcode, flagIcon)
         */
-       public function getSystemLanguages($page_id = 0, $backPath = '') {
-               $modSharedTSconfig = BackendUtility::getModTSconfig($page_id, 'mod.SHARED');
-               $languageIconTitles = array();
-               // fallback "old iconstyles"
-               if (preg_match('/\\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'])) {
-                       $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace('.gif', '', $modSharedTSconfig['properties']['defaultLanguageFlag']);
-               }
-               $languageIconTitles[0] = array(
-                       'uid' => 0,
-                       'title' => $modSharedTSconfig['properties']['defaultLanguageLabel'] !== ''
-                                       ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')'
-                                       : $GLOBALS['LANG']->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage'),
-                       'ISOcode' => 'DEF',
-                       'flagIcon' => $modSharedTSconfig['properties']['defaultLanguageFlag'] !== ''
-                                       ? 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag']
-                                       : 'empty-empty'
-               );
-               // Set "All" language:
-               $languageIconTitles[-1] = array(
-                       'uid' => -1,
-                       'title' => $GLOBALS['LANG']->getLL('multipleLanguages'),
-                       'ISOcode' => 'DEF',
-                       'flagIcon' => 'flags-multiple'
-               );
-               // Find all system languages:
-               $sysLanguages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_language', '1=1' . BackendUtility::BEenableFields('sys_language'));
-               foreach ($sysLanguages as $row) {
-                       $languageIconTitles[$row['uid']] = $row;
-                       if (!empty($row['language_isocode'])) {
-                               $languageIconTitles[$row['uid']]['ISOcode'] = $row['language_isocode'];
-                       } elseif ($row['static_lang_isocode'] && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('static_info_tables')) {
-                               \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('Usage of the field "static_lang_isocode" is discouraged, and will stop working with CMS 8. Use the built-in language field "language_isocode" in your sys_language records.');
-                               $staticLangRow = BackendUtility::getRecord('static_languages', $row['static_lang_isocode'], 'lg_iso_2');
-                               if ($staticLangRow['lg_iso_2']) {
-                                       $languageIconTitles[$row['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
-                               }
-                       }
-                       if ($row['flag'] !== '') {
-                               $languageIconTitles[$row['uid']]['flagIcon'] = IconUtility::mapRecordTypeToSpriteIconName('sys_language', $row);
-                       }
-               }
-               return $languageIconTitles;
+       public function getSystemLanguages($pageId = 0) {
+               $modSharedTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.SHARED');
+
+               // default language and "all languages" are always present
+               $languages = array(
+                       // 0: default language
+                       0 => array(
+                               'uid' => 0,
+                               'title' => $this->getDefaultLanguageLabel($modSharedTSconfig),
+                               'ISOcode' => 'DEF',
+                               'flagIcon' => $this->getDefaultLanguageFlag($modSharedTSconfig),
+                       ),
+                       // -1: all languages
+                       -1 => array(
+                               'uid' => -1,
+                               'title' => $this->getLanguageService()->getLL('multipleLanguages'),
+                               'ISOcode' => 'DEF',
+                               'flagIcon' => 'flags-multiple',
+                       ),
+               );
+
+               // add the additional languages from database records
+               $languageRecords = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_language', '');
+               foreach ($languageRecords as $languageRecord) {
+                       $languages[$languageRecord['uid']] = $languageRecord;
+                       if ($languageRecord['static_lang_isocode'] && ExtensionManagementUtility::isLoaded('static_info_tables')) {
+                               $staticLangRow = BackendUtility::getRecord('static_languages', $languageRecord['static_lang_isocode'], 'lg_iso_2');
+                               if ($staticLangRow['lg_iso_2']) {
+                                       $languages[$languageRecord['uid']]['ISOcode'] = $staticLangRow['lg_iso_2'];
+                               }
+                       }
+                       if ($languageRecord['flag'] !== '') {
+                               $languages[$languageRecord['uid']]['flagIcon'] = IconUtility::mapRecordTypeToSpriteIconName('sys_language', $languageRecord);
+                       }
+               }
+               return $languages;
        }
 
        /**
@@ -85,63 +95,66 @@ class TranslationConfigurationProvider {
         *
         * @param string $table Table name
         * @param int $uid Record uid
-        * @param int $sys_language_uid Language uid. If zero, then all languages are selected.
+        * @param int $languageUid Language uid. If 0, then all languages are selected.
         * @param array $row The record to be translated
-        * @param array $selFieldList Select fields for the query which fetches the translations of the current record
-        * @return array Array with information. Errors will return string with message.
+        * @param string $selFieldList Select fields for the query which fetches the translations of the current record
+        * @return mixed Array with information or error message as a string.
         */
-       public function translationInfo($table, $uid, $sys_language_uid = 0, $row = NULL, $selFieldList = '') {
-               if ($GLOBALS['TCA'][$table] && $uid) {
-                       if ($row === NULL) {
-                               $row = BackendUtility::getRecordWSOL($table, $uid);
-                       }
-                       if (is_array($row)) {
-                               $trTable = $this->getTranslationTable($table);
-                               if ($trTable) {
-                                       if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] <= 0) {
-                                               if ($trTable !== $table || $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] == 0) {
-                                                       // Look for translations of this record, index by language field value:
-                                                       $translationsTemp = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($selFieldList ? $selFieldList : 'uid,' . $GLOBALS['TCA'][$trTable]['ctrl']['languageField'], $trTable, $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerField'] . '=' . (int)$uid . ' AND pid=' . (int)($table === 'pages' ? $row['uid'] : $row['pid']) . ' AND ' . $GLOBALS['TCA'][$trTable]['ctrl']['languageField'] . (!$sys_language_uid ? '>0' : '=' . (int)$sys_language_uid) . BackendUtility::deleteClause($trTable) . BackendUtility::versioningPlaceholderClause($trTable));
-                                                       $translations = array();
-                                                       $translations_errors = array();
-                                                       foreach ($translationsTemp as $r) {
-                                                               if (!isset($translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]])) {
-                                                                       $translations[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]] = $r;
-                                                               } else {
-                                                                       $translations_errors[$r[$GLOBALS['TCA'][$trTable]['ctrl']['languageField']]][] = $r;
-                                                               }
-                                                       }
-                                                       return array(
-                                                               'table' => $table,
-                                                               'uid' => $uid,
-                                                               'CType' => $row['CType'],
-                                                               'sys_language_uid' => $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']],
-                                                               'translation_table' => $trTable,
-                                                               'translations' => $translations,
-                                                               'excessive_translations' => $translations_errors
-                                                       );
-                                               } else {
-                                                       return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
-                                               }
-                                       } else {
-                                               return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a language value "' . $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] . '", relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
-                                       }
-                               } else {
-                                       return 'Translation is not supported for this table!';
-                               }
+       public function translationInfo($table, $uid, $languageUid = 0, array $row = NULL, $selFieldList = '') {
+               if (!$GLOBALS['TCA'][$table] || !$uid) {
+                       return 'No table "' . $table . '" or no UID value';
+               }
+               if ($row === NULL) {
+                       $row = BackendUtility::getRecordWSOL($table, $uid);
+               }
+               if (!is_array($row)) {
+                       return 'Record "' . $table . '_' . $uid . '" was not found';
+               }
+               $translationTable = $this->getTranslationTable($table);
+               if ($translationTable === '') {
+                       return 'Translation is not supported for this table!';
+               }
+               if ($translationTable === $table && $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] > 0) {
+                       return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a language value "' . $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']] . '", relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
+               }
+               if ($translationTable === $table && $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] != 0) {
+                       return 'Record "' . $table . '_' . $uid . '" seems to be a translation already (has a relation to record "' . $row[$GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']] . '")';
+               }
+               // Look for translations of this record, index by language field value:
+               if (!$selFieldList) {
+                       $selFieldList = 'uid,' . $GLOBALS['TCA'][$translationTable]['ctrl']['languageField'];
+               }
+               $where = $GLOBALS['TCA'][$translationTable]['ctrl']['transOrigPointerField'] . '=' . (int)$uid .
+                       ' AND pid=' . (int)($table === 'pages' ? $row['uid'] : $row['pid']) .
+                       ' AND ' . $GLOBALS['TCA'][$translationTable]['ctrl']['languageField'] . (! $languageUid ? '>0' : '=' . (int)$languageUid) .
+                       BackendUtility::deleteClause($translationTable) .
+                       BackendUtility::versioningPlaceholderClause($translationTable);
+               $translationRecords = $this->getDatabaseConnection()->exec_SELECTgetRows($selFieldList, $translationTable, $where);
+               $translations = array();
+               $translationsErrors = array();
+               foreach ($translationRecords as $translationRecord) {
+                       if (!isset($translations[$translationRecord[$GLOBALS['TCA'][$translationTable]['ctrl']['languageField']]])) {
+                               $translations[$translationRecord[$GLOBALS['TCA'][$translationTable]['ctrl']['languageField']]] = $translationRecord;
                        } else {
-                               return 'Record "' . $table . '_' . $uid . '" was not found';
+                               $translationsErrors[$translationRecord[$GLOBALS['TCA'][$translationTable]['ctrl']['languageField']]][] = $translationRecord;
                        }
-               } else {
-                       return 'No table "' . $table . '" or no UID value';
                }
+               return array(
+                       'table' => $table,
+                       'uid' => $uid,
+                       'CType' => $row['CType'],
+                       'sys_language_uid' => $row[$GLOBALS['TCA'][$table]['ctrl']['languageField']],
+                       'translation_table' => $translationTable,
+                       'translations' => $translations,
+                       'excessive_translations' => $translationsErrors
+               );
        }
 
        /**
         * Returns the table in which translations for input table is found.
         *
         * @param string $table The table name
-        * @return bool
+        * @return string
         */
        public function getTranslationTable($table) {
                return $this->isTranslationInOwnTable($table) ? $table : $this->foreignTranslationTable($table);
@@ -164,10 +177,47 @@ class TranslationConfigurationProvider {
         * @return string Translation foreign table
         */
        public function foreignTranslationTable($table) {
-               $trTable = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'];
-               if ($trTable && $GLOBALS['TCA'][$trTable] && $GLOBALS['TCA'][$trTable]['ctrl']['languageField'] && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerField'] && $GLOBALS['TCA'][$trTable]['ctrl']['transOrigPointerTable'] === $table) {
-                       return $trTable;
+               $translationTable = $GLOBALS['TCA'][$table]['ctrl']['transForeignTable'];
+               if (
+                       !$translationTable ||
+                       !$GLOBALS['TCA'][$translationTable] ||
+                       !$GLOBALS['TCA'][$translationTable]['ctrl']['languageField'] ||
+                       !$GLOBALS['TCA'][$translationTable]['ctrl']['transOrigPointerField'] ||
+                       $GLOBALS['TCA'][$translationTable]['ctrl']['transOrigPointerTable'] !== $table
+               ) {
+                       $translationTable = '';
+               }
+               return $translationTable;
+       }
+
+       /**
+        * @param array $modSharedTSconfig
+        * @return string
+        */
+       protected function getDefaultLanguageFlag(array $modSharedTSconfig) {
+               if (strlen($modSharedTSconfig['properties']['defaultLanguageFlag'])) {
+                       // fallback "old iconstyles"
+                       if (preg_match('/\\.gif$/', $modSharedTSconfig['properties']['defaultLanguageFlag'])) {
+                               $modSharedTSconfig['properties']['defaultLanguageFlag'] = str_replace('.gif', '', $modSharedTSconfig['properties']['defaultLanguageFlag']);
+                       }
+                       $defaultLanguageFlag = 'flags-' . $modSharedTSconfig['properties']['defaultLanguageFlag'];
+               } else {
+                       $defaultLanguageFlag = 'empty-empty';
+               }
+               return $defaultLanguageFlag;
+       }
+
+       /**
+        * @param array $modSharedTSconfig
+        * @return string
+        */
+       protected function getDefaultLanguageLabel(array $modSharedTSconfig) {
+               if (strlen($modSharedTSconfig['properties']['defaultLanguageLabel'])) {
+                       $defaultLanguageLabel = $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage') . ')';
+               } else {
+                       $defaultLanguageLabel = $this->getLanguageService()->sl('LLL:EXT:lang/locallang_mod_web_list.xlf:defaultLanguage');
                }
+               return $defaultLanguageLabel;
        }
 
 }
index 8ae0776..32d3202 100644 (file)
@@ -4420,7 +4420,7 @@ class FormEngine {
                        list($tscPID) = BackendUtility::getTSCpidCached($table, $row['uid'], $row['pid']);
                        /** @var $t8Tools \TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider */
                        $t8Tools = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider::class);
-                       $this->cachedLanguageFlag[$mainKey] = $t8Tools->getSystemLanguages($tscPID, $this->backPath);
+                       $this->cachedLanguageFlag[$mainKey] = $t8Tools->getSystemLanguages($tscPID);
                }
                // Convert sys_language_uid to sys_language_uid if input was in fact a string (ISO code expected then)
                if (!MathUtility::canBeInterpretedAsInteger($sys_language_uid)) {
index 3777193..2d5804e 100644 (file)
@@ -396,7 +396,7 @@ abstract class AbstractRecordList {
        public function initializeLanguages() {
                // Look up page overlays:
                $this->pageOverlays = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'pages_language_overlay', 'pid=' . (int)$this->id . BackendUtility::deleteClause('pages_language_overlay') . BackendUtility::versioningPlaceholderClause('pages_language_overlay'), '', '', '', 'sys_language_uid');
-               $this->languageIconTitles = $this->getTranslateTools()->getSystemLanguages($this->id, $this->backPath);
+               $this->languageIconTitles = $this->getTranslateTools()->getSystemLanguages($this->id);
        }
 
        /**