42616cc53beb846ba2fe8772d06d8b5d4d7e3641
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Domain / Repository / Localization / LocalizationRepository.php
1 <?php
2 namespace TYPO3\CMS\Backend\Domain\Repository\Localization;
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\Utility\BackendUtility;
18
19 /**
20 * Repository for record localizations
21 */
22 class LocalizationRepository
23 {
24 /**
25 * Fetch the language from which the records of a colPos in a certain language were initially localized
26 *
27 * @param int $pageId
28 * @param int $colPos
29 * @param int $localizedLanguage
30 * @return array|false|null
31 */
32 public function fetchOriginLanguage($pageId, $colPos, $localizedLanguage)
33 {
34 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
35 'tt_content_orig.sys_language_uid',
36 'tt_content,tt_content AS tt_content_orig',
37 'tt_content.colPos = ' . (int)$colPos
38 . ' AND tt_content.pid = ' . (int)$pageId
39 . ' AND tt_content.sys_language_uid = ' . (int)$localizedLanguage
40 . ' AND tt_content.t3_origuid = tt_content_orig.uid'
41 . $this->getExcludeQueryPart()
42 . $this->getAllowedLanguagesForBackendUser(),
43 'tt_content_orig.sys_language_uid'
44 );
45
46 return $record;
47 }
48
49 /**
50 * @param $pageId
51 * @param $colPos
52 * @param $languageId
53 * @return int
54 */
55 public function getLocalizedRecordCount($pageId, $colPos, $languageId)
56 {
57 $rows = (int)$this->getDatabaseConnection()->exec_SELECTcountRows(
58 'uid',
59 'tt_content',
60 'tt_content.sys_language_uid=' . (int)$languageId
61 . ' AND tt_content.colPos = ' . (int)$colPos
62 . ' AND tt_content.pid=' . (int)$pageId
63 . ' AND tt_content.t3_origuid <> 0'
64 . $this->getExcludeQueryPart()
65 );
66
67 return $rows;
68 }
69
70 /**
71 * Fetch all available languages
72 *
73 * @param int $pageId
74 * @param int $colPos
75 * @param int $languageId
76 * @return array
77 */
78 public function fetchAvailableLanguages($pageId, $colPos, $languageId)
79 {
80 $result = $this->getDatabaseConnection()->exec_SELECTgetRows(
81 'sys_language.uid',
82 'tt_content,sys_language',
83 'tt_content.sys_language_uid=sys_language.uid'
84 . ' AND tt_content.colPos = ' . (int)$colPos
85 . ' AND tt_content.pid = ' . (int)$pageId
86 . ' AND sys_language.uid <> ' . (int)$languageId
87 . $this->getExcludeQueryPart()
88 . $this->getAllowedLanguagesForBackendUser(),
89 'sys_language.uid',
90 'sys_language.title'
91 );
92
93 return $result;
94 }
95
96 /**
97 * Builds an additional where clause to exclude deleted records and setting the versioning placeholders
98 *
99 * @return string
100 */
101 public function getExcludeQueryPart()
102 {
103 return BackendUtility::deleteClause('tt_content')
104 . BackendUtility::versioningPlaceholderClause('tt_content');
105 }
106
107 /**
108 * Builds an additional where clause to exclude hidden languages and limit a backend user to its allowed languages,
109 * if the user is not an admin.
110 *
111 * @return string
112 */
113 public function getAllowedLanguagesForBackendUser()
114 {
115 $backendUser = $this->getBackendUser();
116 $additionalWhere = '';
117 if (!$backendUser->isAdmin()) {
118 $additionalWhere .= ' AND sys_language.hidden=0';
119
120 if (!empty($backendUser->user['allowed_languages'])) {
121 $additionalWhere .= ' AND sys_language.uid IN(' . $this->getDatabaseConnection()->cleanIntList($backendUser->user['allowed_languages']) . ')';
122 }
123 }
124
125 return $additionalWhere;
126 }
127
128 /**
129 * Get records for copy process
130 *
131 * @param int $pageId
132 * @param int $colPos
133 * @param int $destLanguageId
134 * @param int $languageId
135 * @param string $fields
136 * @return bool|\mysqli_result|object
137 */
138 public function getRecordsToCopyDatabaseResult($pageId, $colPos, $destLanguageId, $languageId, $fields = '*')
139 {
140 $db = $this->getDatabaseConnection();
141
142 // Get original uid of existing elements triggered language / colpos
143 $originalUids = $db->exec_SELECTgetRows(
144 't3_origuid',
145 'tt_content',
146 'sys_language_uid=' . (int)$destLanguageId
147 . ' AND tt_content.colPos = ' . (int)$colPos
148 . ' AND tt_content.pid=' . (int)$pageId
149 . $this->getExcludeQueryPart(),
150 '',
151 '',
152 '',
153 't3_origuid'
154 );
155 $originalUidList = $db->cleanIntList(implode(',', array_keys($originalUids)));
156
157 $res = $db->exec_SELECTquery(
158 $fields,
159 'tt_content',
160 'tt_content.sys_language_uid=' . (int)$languageId
161 . ' AND tt_content.colPos = ' . (int)$colPos
162 . ' AND tt_content.pid=' . (int)$pageId
163 . ' AND tt_content.uid NOT IN (' . $originalUidList . ')'
164 . $this->getExcludeQueryPart(),
165 '',
166 'tt_content.sorting'
167 );
168
169 return $res;
170 }
171
172 /**
173 * Returns the current BE user.
174 *
175 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
176 */
177 protected function getBackendUser()
178 {
179 return $GLOBALS['BE_USER'];
180 }
181
182 /**
183 * Returns the database connection
184 *
185 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
186 */
187 protected function getDatabaseConnection()
188 {
189 return $GLOBALS['TYPO3_DB'];
190 }
191 }