18be7fae4c023c679c31d3104e14c1d8cf960cfd
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Controller / TranslationStatusController.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Controller;
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\Tree\View\PageTreeView;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Backend\Utility\IconUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Class for displaying translation status of pages in the tree.
24 *
25 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
26 */
27 class TranslationStatusController extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule {
28
29 /**
30 * Returns the menu array
31 *
32 * @return array
33 */
34 public function modMenu() {
35 $lang = $this->getLanguageService();
36 $menuArray = array(
37 'depth' => array(
38 0 => $lang->getLL('depth_0'),
39 1 => $lang->getLL('depth_1'),
40 2 => $lang->getLL('depth_2'),
41 3 => $lang->getLL('depth_3'),
42 999 => $lang->sL('LLL:EXT:lang/locallang_core.xlf:labels.depth_infi')
43 )
44 );
45 // Languages:
46 $lang = $this->getSystemLanguages();
47 $menuArray['lang'] = array(
48 0 => '[All]'
49 );
50 foreach ($lang as $langRec) {
51 $menuArray['lang'][$langRec['uid']] = $langRec['title'];
52 }
53 return $menuArray;
54 }
55
56 /**
57 * MAIN function for page information of localization
58 *
59 * @return string Output HTML for the module.
60 */
61 public function main() {
62 $theOutput = $this->pObj->doc->header($this->getLanguageService()->sL('LLL:EXT:cms/web_info/locallang.xlf:lang_title'));
63 if ($this->pObj->id) {
64 // Depth selector:
65 $h_func = BackendUtility::getFuncMenu($this->pObj->id, 'SET[depth]', $this->pObj->MOD_SETTINGS['depth'], $this->pObj->MOD_MENU['depth']);
66 $h_func .= BackendUtility::getFuncMenu($this->pObj->id, 'SET[lang]', $this->pObj->MOD_SETTINGS['lang'], $this->pObj->MOD_MENU['lang']);
67 $theOutput .= $h_func;
68 // Add CSH:
69 $theOutput .= BackendUtility::cshItem('_MOD_web_info', 'lang', NULL, '|<br />');
70 // Showing the tree:
71 // Initialize starting point of page tree:
72 $treeStartingPoint = (int)$this->pObj->id;
73 $treeStartingRecord = BackendUtility::getRecordWSOL('pages', $treeStartingPoint);
74 $depth = $this->pObj->MOD_SETTINGS['depth'];
75 // Initialize tree object:
76 $tree = GeneralUtility::makeInstance(PageTreeView::class);
77 $tree->init('AND ' . $this->getBackendUser()->getPagePermsClause(1));
78 $tree->addField('l18n_cfg');
79 // Creating top icon; the current page
80 $HTML = IconUtility::getSpriteIconForRecord('pages', $treeStartingRecord);
81 $tree->tree[] = array(
82 'row' => $treeStartingRecord,
83 'HTML' => $HTML
84 );
85 // Create the tree from starting point:
86 if ($depth) {
87 $tree->getTree($treeStartingPoint, $depth, '');
88 }
89 // Render information table:
90 $theOutput .= $this->renderL10nTable($tree);
91 }
92 return $theOutput;
93 }
94
95 /**
96 * Rendering the localization information table.
97 *
98 * @param array $tree The Page tree data
99 * @return string HTML for the localization information table.
100 */
101 public function renderL10nTable(&$tree) {
102 $lang = $this->getLanguageService();
103 // System languages retrieved:
104 $languages = $this->getSystemLanguages();
105 // Title length:
106 $titleLen = $this->getBackendUser()->uc['titleLen'];
107 // Put together the TREE:
108 $output = '';
109 $newOL_js = array();
110 $langRecUids = array();
111 foreach ($tree->tree as $data) {
112 $tCells = array();
113 $langRecUids[0][] = $data['row']['uid'];
114 // Page icons / titles etc.
115 $tCells[] = '<td' . ($data['row']['_CSSCLASS'] ? ' class="' . $data['row']['_CSSCLASS'] . '"' : '') . '>' .
116 $GLOBALS['SOBE']->doc->wrapClickMenuOnIcon($data['HTML'], 'pages', $data['row']['uid']) .
117 '<a href="#" onclick="' . htmlspecialchars(
118 'top.loadEditId(' . (int)$data['row']['uid'] . ',"&SET[language]=0"); return false;'
119 ) . '" title="' . $lang->sL('LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_editPage') . '">' .
120 htmlspecialchars(GeneralUtility::fixed_lgd_cs($data['row']['title'], $titleLen)) .
121 '</a>' .
122 ((string)$data['row']['nav_title'] !== '' ? ' [Nav: <em>' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($data['row']['nav_title'], $titleLen)) . '</em>]' : '') .
123 '</td>';
124 // DEFAULT language:
125 // "View page" link is created:
126 $viewPageLink = '<a href="#" onclick="' . htmlspecialchars(BackendUtility::viewOnClick(
127 $data['row']['uid'], $GLOBALS['BACK_PATH'], '', '', '', '&L=###LANG_UID###')
128 ) . '" title="' . $lang->sL('LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_viewPage') . '">' .
129 IconUtility::getSpriteIcon('actions-document-view') . '</a>';
130 $status = $data['row']['l18n_cfg'] & 1 ? 'danger' : 'success';
131 // Create links:
132 $info = '';
133 $editUid = $data['row']['uid'];
134 $params = '&edit[pages][' . $editUid . ']=edit';
135 $info .= '<a href="#" onclick="' . htmlspecialchars(BackendUtility::editOnClick(
136 $params, $GLOBALS['BACK_PATH'])
137 ) . '" title="' . $lang->sL(
138 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_editDefaultLanguagePage'
139 ) . '">' . IconUtility::getSpriteIcon('actions-document-open') . '</a>';
140 $info .= str_replace('###LANG_UID###', '0', $viewPageLink);
141 $info .= '&nbsp;';
142 $info .= $data['row']['l18n_cfg'] & 1 ? '<span title="' . $lang->sL('LLL:EXT:cms/locallang_tca.xlf:pages.l18n_cfg.I.1', TRUE) . '">D</span>' : '&nbsp;';
143 $info .= GeneralUtility::hideIfNotTranslated($data['row']['l18n_cfg']) ? '<span title="' . $lang->sL('LLL:EXT:cms/locallang_tca.xlf:pages.l18n_cfg.I.2', TRUE) . '">N</span>' : '&nbsp;';
144 // Put into cell:
145 $tCells[] = '<td class="' . $status . ' col-border-left">' . $info . '</td>';
146 $tCells[] = '<td class="' . $status . '" title="' . $lang->sL(
147 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_CEcount'
148 ) . '" align="center">' . $this->getContentElementCount($data['row']['uid'], 0) . '</td>';
149 $modSharedTSconfig = BackendUtility::getModTSconfig($data['row']['uid'], 'mod.SHARED');
150 $disableLanguages = isset($modSharedTSconfig['properties']['disableLanguages']) ? GeneralUtility::trimExplode(',', $modSharedTSconfig['properties']['disableLanguages'], TRUE) : array();
151 // Traverse system languages:
152 foreach ($languages as $langRow) {
153 if ($this->pObj->MOD_SETTINGS['lang'] == 0 || (int)$this->pObj->MOD_SETTINGS['lang'] === (int)$langRow['uid']) {
154 $row = $this->getLangStatus($data['row']['uid'], $langRow['uid']);
155 $info = '';
156 if (is_array($row)) {
157 $langRecUids[$langRow['uid']][] = $row['uid'];
158 $status = $row['_HIDDEN'] ? (GeneralUtility::hideIfNotTranslated($data['row']['l18n_cfg']) || $data['row']['l18n_cfg'] & 1 ? 'danger' : '') : 'success';
159 $icon = IconUtility::getSpriteIconForRecord(
160 'pages_language_overlay',
161 $row,
162 array('class' => 'c-recIcon')
163 );
164 $info = $icon . htmlspecialchars(
165 GeneralUtility::fixed_lgd_cs($row['title'], $titleLen)
166 ) . ((string)$row['nav_title'] !== '' ? ' [Nav: <em>' . htmlspecialchars(
167 GeneralUtility::fixed_lgd_cs($row['nav_title'], $titleLen)
168 ) . '</em>]' : '') . ($row['_COUNT'] > 1 ? '<div>' . $lang->sL(
169 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_badThingThereAre'
170 ) . '</div>' : '');
171 $tCells[] = '<td class="' . $status . ' col-border-left">' .
172 '<a href="#" onclick="' . htmlspecialchars(
173 'top.loadEditId(' . (int)$data['row']['uid'] . ',"&SET[language]=' . $langRow['uid'] . '"); return false;'
174 ) . '" title="' . $lang->sL(
175 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_editPageLang'
176 ) . '">' . $info . '</a></td>';
177 // Edit whole record:
178 $info = '';
179 $editUid = $row['uid'];
180 $params = '&edit[pages_language_overlay][' . $editUid . ']=edit';
181 $info .= '<a href="#" onclick="' . htmlspecialchars(
182 BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])
183 ) . '" title="' . $lang->sL(
184 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_editLanguageOverlayRecord'
185 ) . '">' . IconUtility::getSpriteIcon('actions-document-open') . '</a>';
186 $info .= str_replace('###LANG_UID###', $langRow['uid'], $viewPageLink);
187 $tCells[] = '<td class="' . $status . '">' . $info . '</td>';
188 $tCells[] = '<td class="' . $status . '" title="' . $lang->sL(
189 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_CEcount'
190 ) . '" align="center">' . $this->getContentElementCount($data['row']['uid'], $langRow['uid']) . '</td>';
191 } else {
192 if (in_array($langRow['uid'], $disableLanguages)) {
193 // Language has been disabled for this page
194 $status = 'danger';
195 $info = '';
196 } else {
197 $status = GeneralUtility::hideIfNotTranslated($data['row']['l18n_cfg']) || $data['row']['l18n_cfg'] & 1 ? 'danger' : '';
198 $info = '<input type="checkbox" name="newOL[' . $langRow['uid'] . '][' . $data['row']['uid'] . ']" value="1" />';
199 $newOL_js[$langRow['uid']] .= '
200 +(document.webinfoForm[\'newOL[' . $langRow['uid'] . '][' . $data['row']['uid'] . ']\'].checked ? \'&edit[pages_language_overlay][' . $data['row']['uid'] . ']=new\' : \'\')
201 ';
202 }
203 $tCells[] = '<td class="' . $status . ' col-border-left">&nbsp;</td>';
204 $tCells[] = '<td class="' . $status . '">&nbsp;</td>';
205 $tCells[] = '<td class="' . $status . '">' . $info . '</td>';
206 }
207 }
208 }
209 $output .= '
210 <tr>
211 ' . implode('
212 ', $tCells) . '
213 </tr>';
214 }
215 // Put together HEADER:
216 $tCells = array();
217 $tCells[] = '<td>' . $lang->sL('LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_page') . ':</td>';
218 if (is_array($langRecUids[0])) {
219 $params = '&edit[pages][' . implode(',', $langRecUids[0]) . ']=edit&columnsOnly=title,nav_title,l18n_cfg,hidden';
220 $editIco = '<a href="#" onclick="' . htmlspecialchars(
221 BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])
222 ) . '" title="' . $lang->sL(
223 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_editPageProperties'
224 ) . '">' . IconUtility::getSpriteIcon('actions-document-new') . '</a>';
225 } else {
226 $editIco = '';
227 }
228 $tCells[] = '<td class="col-border-left" colspan="2">' . $lang->sL(
229 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_default'
230 ) . ':' . $editIco . '</td>';
231 foreach ($languages as $langRow) {
232 if ($this->pObj->MOD_SETTINGS['lang'] == 0 || (int)$this->pObj->MOD_SETTINGS['lang'] === (int)$langRow['uid']) {
233 // Title:
234 $tCells[] = '<td class="col-border-left">' . htmlspecialchars($langRow['title']) . '</td>';
235 // Edit language overlay records:
236 if (is_array($langRecUids[$langRow['uid']])) {
237 $params = '&edit[pages_language_overlay][' .
238 implode(',', $langRecUids[$langRow['uid']]) .
239 ']=edit&columnsOnly=title,nav_title,hidden';
240 $tCells[] = '<td><a href="#" onclick="' . htmlspecialchars(
241 BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])
242 ) . '" title="' . $lang->sL(
243 'LLL:EXT:cms/web_info/locallang.xlf:lang_renderl10n_editLangOverlays'
244 ) . '">' . IconUtility::getSpriteIcon('actions-document-open') . '</a></td>';
245 } else {
246 $tCells[] = '<td>&nbsp;</td>';
247 }
248 // Create new overlay records:
249 $params = '\'' .
250 $newOL_js[$langRow['uid']] .
251 '+\'&columnsOnly=title,hidden,sys_language_uid&defVals[pages_language_overlay][sys_language_uid]=' .
252 $langRow['uid'];
253 $tCells[] = '<td><a href="#" onclick="' . htmlspecialchars(
254 BackendUtility::editOnClick($params, $GLOBALS['BACK_PATH'])
255 ) . '" title="' . $lang->sL(
256 'LLL:EXT:cms/web_info/locallang.xlf:lang_getlangsta_createNewTranslationHeaders'
257 ) . '">' . IconUtility::getSpriteIcon('actions-document-new') . '</a></td>';
258 }
259 }
260
261 $output =
262 '<div class="table-fit">' .
263 '<table class="table table-striped table-hover" id="langTable">' .
264 '<thead>' .
265 '<tr>' .
266 implode('', $tCells) .
267 '</tr>' .
268 '</thead>' .
269 '<tbody>' .
270 $output .
271 '</tbody>' .
272 '</table>' .
273 '</div>';
274 return $output;
275 }
276
277 /**
278 * Selects all system languages (from sys_language)
279 *
280 * @return array System language records in an array.
281 */
282 public function getSystemLanguages() {
283 if (!$this->getBackendUser()->user['admin'] && $this->getBackendUser()->groupData['allowed_languages'] !== '') {
284 $allowed_languages = array_flip(explode(',', $this->getBackendUser()->groupData['allowed_languages']));
285 }
286 $res = $this->getDatabaseConnection()->exec_SELECTquery('*', 'sys_language', '1=1' . BackendUtility::deleteClause('sys_language'));
287 $outputArray = array();
288 while ($row = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {
289 if (is_array($allowed_languages) && count($allowed_languages)) {
290 if (isset($allowed_languages[$row['uid']])) {
291 $outputArray[] = $row;
292 }
293 } else {
294 $outputArray[] = $row;
295 }
296 }
297 $this->getDatabaseConnection()->sql_free_result($res);
298 return $outputArray;
299 }
300
301 /**
302 * Get an alternative language record for a specific page / language
303 *
304 * @param int $pageId Page ID to look up for.
305 * @param int $langId Language UID to select for.
306 * @return array pages_languages_overlay record
307 */
308 public function getLangStatus($pageId, $langId) {
309 $res = $this->getDatabaseConnection()->exec_SELECTquery(
310 '*',
311 'pages_language_overlay',
312 'pid=' . (int)$pageId .
313 ' AND sys_language_uid=' . (int)$langId .
314 BackendUtility::deleteClause('pages_language_overlay') .
315 BackendUtility::versioningPlaceholderClause('pages_language_overlay')
316 );
317 $row = $this->getDatabaseConnection()->sql_fetch_assoc($res);
318 BackendUtility::workspaceOL('pages_language_overlay', $row);
319 if (is_array($row)) {
320 $row['_COUNT'] = $this->getDatabaseConnection()->sql_num_rows($res);
321 $row['_HIDDEN'] = $row['hidden'] || (int)$row['endtime'] > 0 && (int)$row['endtime'] < $GLOBALS['EXEC_TIME'] || $GLOBALS['EXEC_TIME'] < (int)$row['starttime'];
322 }
323 return $row;
324 }
325
326 /**
327 * Counting content elements for a single language on a page.
328 *
329 * @param int $pageId Page id to select for.
330 * @param int $sysLang Sys language uid
331 * @return int Number of content elements from the PID where the language is set to a certain value.
332 */
333 public function getContentElementCount($pageId, $sysLang) {
334 $count = $this->getDatabaseConnection()->exec_SELECTcountRows('uid', 'tt_content', 'pid=' . (int)$pageId . ' AND sys_language_uid=' . (int)$sysLang . BackendUtility::deleteClause('tt_content') . BackendUtility::versioningPlaceholderClause('tt_content'));
335 return $count ?: '-';
336 }
337
338 /**
339 * Returns LanguageService
340 *
341 * @return \TYPO3\CMS\Lang\LanguageService
342 */
343 protected function getLanguageService() {
344 return $GLOBALS['LANG'];
345 }
346
347 /**
348 * Returns the database connection
349 *
350 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
351 */
352 protected function getDatabaseConnection() {
353 return $GLOBALS['TYPO3_DB'];
354 }
355
356 /**
357 * Returns the current BE user.
358 *
359 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
360 */
361 protected function getBackendUser() {
362 return $GLOBALS['BE_USER'];
363 }
364
365 }