423e1744eb9c7ea5d64ec52b3d7e4d56c067579d
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / FormDataProvider / DatabaseSystemLanguageRows.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\FormDataProvider;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
18 use TYPO3\CMS\Core\Database\ConnectionPool;
19 use TYPO3\CMS\Core\Localization\LanguageService;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Messaging\FlashMessageService;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Fill the "systemLanguageRows" part of the result array
26 */
27 class DatabaseSystemLanguageRows implements FormDataProviderInterface
28 {
29 /**
30 * Fetch available system languages and resolve iso code if necessary.
31 *
32 * @param array $result
33 * @return array
34 * @throws \UnexpectedValueException
35 */
36 public function addData(array $result)
37 {
38 $languageService = $this->getLanguageService();
39
40 $pageTs = $result['pageTsConfig'];
41 $defaultLanguageLabel = $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage');
42 if (isset($pageTs['mod.']['SHARED.']['defaultLanguageLabel'])) {
43 $defaultLanguageLabel = $pageTs['mod.']['SHARED.']['defaultLanguageLabel'] . ' (' . $languageService->sL($defaultLanguageLabel) . ')';
44 }
45 $defaultLanguageFlag = 'empty-empty';
46 if (isset($pageTs['mod.']['SHARED.']['defaultLanguageFlag'])) {
47 $defaultLanguageFlag = 'flags-' . $pageTs['mod.']['SHARED.']['defaultLanguageFlag'];
48 }
49
50 $languageRows = [
51 -1 => [
52 // -1: "All" languages
53 'uid' => -1,
54 'title' => $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_mod_web_list.xlf:multipleLanguages'),
55 // Same as for 0, but iso is used in flex form context only and duplication handled there
56 // @todo: Maybe drop this if flex form language handling is extracted?
57 'iso' => 'DEF',
58 'flagIconIdentifier' => 'flags-multiple',
59 ],
60 0 => [
61 // 0: "Default" language
62 'uid' => 0,
63 'title' => $defaultLanguageLabel,
64 // Default "DEF" is a fallback preparation for flex form iso codes "lDEF"
65 // @todo: Maybe drop this if flex form language handling is extracted?
66 'iso' => 'DEF',
67 'flagIconIdentifier' => $defaultLanguageFlag,
68 ],
69 ];
70
71 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
72 ->getQueryBuilderForTable('sys_language');
73
74 $queryBuilder->getRestrictions()->removeAll();
75
76 $queryResult = $queryBuilder
77 ->select('uid', 'title', 'language_isocode', 'flag')
78 ->from('sys_language')
79 ->where($queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT)))
80 ->orderBy('sorting')
81 ->execute();
82
83 while ($row = $queryResult->fetch()) {
84 $uid = $row['uid'];
85 $languageRows[$uid] = [
86 'uid' => $uid,
87 'title' => $row['title'],
88 'flagIconIdentifier' => 'flags-' . $row['flag'],
89 ];
90 if (!empty($row['language_isocode'])) {
91 $languageRows[$uid]['iso'] = $row['language_isocode'];
92 } else {
93 // No iso code could be found. This is currently possible in the system but discouraged.
94 // So, code within FormEngine has to be suited to work with an empty iso code. However,
95 // it may impact certain multi language scenarios, so we add a flash message hinting for
96 // incomplete configuration here.
97 // It might be possible to convert this to a non-catchable exception later if
98 // it iso code is enforced on a different layer of the system (tca required + migration wizard).
99 // @todo: This could be relaxed again if flex form language handling is extracted,
100 // @todo: since the rest of the FormEngine code does not rely on iso code?
101 $message = sprintf(
102 $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.missingLanguageIsocode'),
103 $row['title'],
104 $uid
105 );
106 /** @var FlashMessage $flashMessage */
107 $flashMessage = GeneralUtility::makeInstance(
108 FlashMessage::class,
109 $message,
110 '',
111 FlashMessage::ERROR
112 );
113 /** @var $flashMessageService FlashMessageService */
114 $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
115 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
116 $defaultFlashMessageQueue->enqueue($flashMessage);
117 $languageRows[$uid]['iso'] = '';
118 }
119 }
120
121 $result['systemLanguageRows'] = $languageRows;
122
123 return $result;
124 }
125
126 /**
127 * @return LanguageService
128 */
129 protected function getLanguageService()
130 {
131 return $GLOBALS['LANG'];
132 }
133 }