7a2f1991296df36c277e1d2f342b85fa5e552f44
[Packages/TYPO3.CMS.git] / typo3 / sysext / info / Classes / Controller / TranslationStatusController.php
1 <?php
2
3 namespace TYPO3\CMS\Info\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Backend\Tree\View\PageTreeView;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Restriction\BackendWorkspaceRestriction;
22 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
23 use TYPO3\CMS\Core\Imaging\Icon;
24 use TYPO3\CMS\Core\Imaging\IconFactory;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27 /**
28 * Class for displaying translation status of pages in the tree.
29 */
30 class TranslationStatusController extends \TYPO3\CMS\Backend\Module\AbstractFunctionModule
31 {
32 /**
33 * @var IconFactory
34 */
35 protected $iconFactory;
36
37 /**
38 * Construct for initialize class variables
39 */
40 public function __construct()
41 {
42 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
43 }
44
45 /**
46 * Returns the menu array
47 *
48 * @return array
49 */
50 public function modMenu()
51 {
52 $lang = $this->getLanguageService();
53 $menuArray = [
54 'depth' => [
55 0 => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_0'),
56 1 => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_1'),
57 2 => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_2'),
58 3 => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_3'),
59 4 => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_4'),
60 999 => $lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.depth_infi')
61 ]
62 ];
63 // Languages:
64 $lang = $this->getSystemLanguages();
65 $menuArray['lang'] = [
66 0 => '[All]'
67 ];
68 foreach ($lang as $langRec) {
69 $menuArray['lang'][$langRec['uid']] = $langRec['title'];
70 }
71 return $menuArray;
72 }
73
74 /**
75 * MAIN function for page information of localization
76 *
77 * @return string Output HTML for the module.
78 */
79 public function main()
80 {
81 $theOutput = '<h1>' . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_title')) . '</h1>';
82 if ($this->pObj->id) {
83 // Depth selector:
84 $theOutput .= '<div class="form-inline form-inline-spaced">';
85 $h_func = BackendUtility::getDropdownMenu($this->pObj->id, 'SET[depth]', $this->pObj->MOD_SETTINGS['depth'], $this->pObj->MOD_MENU['depth']);
86 $h_func .= BackendUtility::getDropdownMenu($this->pObj->id, 'SET[lang]', $this->pObj->MOD_SETTINGS['lang'], $this->pObj->MOD_MENU['lang']);
87 $theOutput .= $h_func;
88 // Add CSH:
89 $theOutput .= BackendUtility::cshItem('_MOD_web_info', 'lang', null, '<div class="form-group"><span class="btn btn-default btn-sm">|</span></div><br />');
90 $theOutput .= '</div>';
91 // Showing the tree:
92 // Initialize starting point of page tree:
93 $treeStartingPoint = (int)$this->pObj->id;
94 $treeStartingRecord = BackendUtility::getRecordWSOL('pages', $treeStartingPoint);
95 $depth = $this->pObj->MOD_SETTINGS['depth'];
96 // Initialize tree object:
97 $tree = GeneralUtility::makeInstance(PageTreeView::class);
98 $tree->init('AND ' . $this->getBackendUser()->getPagePermsClause(1));
99 $tree->addField('l18n_cfg');
100 // Creating top icon; the current page
101 $HTML = $this->iconFactory->getIconForRecord('pages', $treeStartingRecord, Icon::SIZE_SMALL)->render();
102 $tree->tree[] = [
103 'row' => $treeStartingRecord,
104 'HTML' => $HTML
105 ];
106 // Create the tree from starting point:
107 if ($depth) {
108 $tree->getTree($treeStartingPoint, $depth, '');
109 }
110 // Render information table:
111 $theOutput .= $this->renderL10nTable($tree);
112 }
113 return $theOutput;
114 }
115
116 /**
117 * Rendering the localization information table.
118 *
119 * @param array $tree The Page tree data
120 * @return string HTML for the localization information table.
121 */
122 public function renderL10nTable(&$tree)
123 {
124 $lang = $this->getLanguageService();
125 // System languages retrieved:
126 $languages = $this->getSystemLanguages();
127 // Title length:
128 $titleLen = $this->getBackendUser()->uc['titleLen'];
129 // Put together the TREE:
130 $output = '';
131 $newOL_js = [];
132 $langRecUids = [];
133 foreach ($tree->tree as $data) {
134 $tCells = [];
135 $langRecUids[0][] = $data['row']['uid'];
136 // Page icons / titles etc.
137 $tCells[] = '<td' . ($data['row']['_CSSCLASS'] ? ' class="' . $data['row']['_CSSCLASS'] . '"' : '') . '>' .
138 ($data['depthData'] ?: '') .
139 BackendUtility::wrapClickMenuOnIcon($data['HTML'], 'pages', $data['row']['uid']) .
140 '<a href="#" onclick="' . htmlspecialchars(
141 'top.loadEditId(' . (int)$data['row']['uid'] . ',"&SET[language]=0"); return false;'
142 ) . '" title="' . $lang->sL('LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_editPage') . '">' .
143 htmlspecialchars(GeneralUtility::fixed_lgd_cs($data['row']['title'], $titleLen)) .
144 '</a>' .
145 ((string)$data['row']['nav_title'] !== '' ? ' [Nav: <em>' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($data['row']['nav_title'], $titleLen)) . '</em>]' : '') .
146 '</td>';
147 // DEFAULT language:
148 // "View page" link is created:
149 $viewPageLink = '<a href="#" onclick="' . htmlspecialchars(
150 BackendUtility::viewOnClick(
151 $data['row']['uid'],
152 '',
153 null,
154 '',
155 '',
156 '&L=###LANG_UID###'
157 )
158 ) . '" class="btn btn-default" title="' . $lang->sL('LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_viewPage') . '">' .
159 $this->iconFactory->getIcon('actions-view', Icon::SIZE_SMALL)->render() . '</a>';
160 $status = GeneralUtility::hideIfDefaultLanguage($data['row']['l18n_cfg']) ? 'danger' : 'success';
161 // Create links:
162 $editUrl = BackendUtility::getModuleUrl('record_edit', [
163 'edit' => [
164 'pages' => [
165 $data['row']['uid'] => 'edit'
166 ]
167 ],
168 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
169 ]);
170 $info = '<a href="#" onclick="' . htmlspecialchars(
171 BackendUtility::viewOnClick(
172 $data['row']['uid'],
173 '',
174 null,
175 '',
176 '',
177 ''
178 )
179 ) . '" class="btn btn-default" title="' . $lang->sL('LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_viewPage') . '">' .
180 $this->iconFactory->getIcon('actions-view-page', Icon::SIZE_SMALL)->render() . '</a>';
181 $info .= '<a href="' . htmlspecialchars($editUrl)
182 . '" class="btn btn-default" title="' . $lang->sL(
183 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_editDefaultLanguagePage'
184 ) . '">' . $this->iconFactory->getIcon('actions-page-open', Icon::SIZE_SMALL)->render() . '</a>';
185 $info .= '&nbsp;';
186 $info .= GeneralUtility::hideIfDefaultLanguage($data['row']['l18n_cfg']) ? '<span title="' . htmlspecialchars($lang->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:pages.l18n_cfg.I.1')) . '">D</span>' : '&nbsp;';
187 $info .= GeneralUtility::hideIfNotTranslated($data['row']['l18n_cfg']) ? '<span title="' . htmlspecialchars($lang->sL('LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:pages.l18n_cfg.I.2')) . '">N</span>' : '&nbsp;';
188 // Put into cell:
189 $tCells[] = '<td class="' . $status . ' col-border-left"><div class="btn-group">' . $info . '</div></td>';
190 $tCells[] = '<td class="' . $status . '" title="' . $lang->sL(
191 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_CEcount'
192 ) . '" align="center">' . $this->getContentElementCount($data['row']['uid'], 0) . '</td>';
193 $modSharedTSconfig = BackendUtility::getModTSconfig($data['row']['uid'], 'mod.SHARED');
194 $disableLanguages = isset($modSharedTSconfig['properties']['disableLanguages']) ? GeneralUtility::trimExplode(',', $modSharedTSconfig['properties']['disableLanguages'], true) : [];
195 // Traverse system languages:
196 foreach ($languages as $langRow) {
197 if ($this->pObj->MOD_SETTINGS['lang'] == 0 || (int)$this->pObj->MOD_SETTINGS['lang'] === (int)$langRow['uid']) {
198 $row = $this->getLangStatus($data['row']['uid'], $langRow['uid']);
199 $info = '';
200 if (is_array($row)) {
201 $langRecUids[$langRow['uid']][] = $row['uid'];
202 $status = $row['_HIDDEN'] ? (GeneralUtility::hideIfNotTranslated($data['row']['l18n_cfg']) || GeneralUtility::hideIfDefaultLanguage($data['row']['l18n_cfg']) ? 'danger' : '') : 'success';
203 $icon = $this->iconFactory->getIconForRecord('pages', $row, Icon::SIZE_SMALL)->render();
204 $info = $icon . htmlspecialchars(
205 GeneralUtility::fixed_lgd_cs($row['title'], $titleLen)
206 ) . ((string)$row['nav_title'] !== '' ? ' [Nav: <em>' . htmlspecialchars(
207 GeneralUtility::fixed_lgd_cs($row['nav_title'], $titleLen)
208 ) . '</em>]' : '') . ($row['_COUNT'] > 1 ? '<div>' . $lang->sL(
209 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_badThingThereAre'
210 ) . '</div>' : '');
211 $tCells[] = '<td class="' . $status . ' col-border-left">' .
212 '<a href="#" onclick="' . htmlspecialchars(
213 'top.loadEditId(' . (int)$data['row']['uid'] . ',"&SET[language]=' . $langRow['uid'] . '"); return false;'
214 ) . '" title="' . $lang->sL(
215 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_editPageLang'
216 ) . '">' . $info . '</a></td>';
217 // Edit whole record:
218 // Create links:
219 $editUrl = BackendUtility::getModuleUrl('record_edit', [
220 'edit' => [
221 'pages' => [
222 $row['uid'] => 'edit'
223 ]
224 ],
225 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
226 ]);
227 $info = str_replace('###LANG_UID###', $langRow['uid'], $viewPageLink);
228 $info .= '<a href="' . htmlspecialchars($editUrl)
229 . '" class="btn btn-default" title="' . $lang->sL(
230 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_editLanguageOverlayRecord'
231 ) . '">' . $this->iconFactory->getIcon('actions-open', Icon::SIZE_SMALL)->render() . '</a>';
232 $tCells[] = '<td class="' . $status . '"><div class="btn-group">' . $info . '</div></td>';
233 $tCells[] = '<td class="' . $status . '" title="' . $lang->sL(
234 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_CEcount'
235 ) . '" align="center">' . $this->getContentElementCount($data['row']['uid'], $langRow['uid']) . '</td>';
236 } else {
237 if (in_array($langRow['uid'], $disableLanguages)) {
238 // Language has been disabled for this page
239 $status = 'danger';
240 $info = '';
241 } else {
242 $status = GeneralUtility::hideIfNotTranslated($data['row']['l18n_cfg']) || GeneralUtility::hideIfDefaultLanguage($data['row']['l18n_cfg']) ? 'danger' : '';
243 $info = '<div class="btn-group"><label class="btn btn-default btn-checkbox">';
244 $info .= '<input type="checkbox" name="newOL[' . $langRow['uid'] . '][' . $data['row']['uid'] . ']" value="1" />';
245 $info .= '<span class="t3-icon fa"></span></label></div>';
246 $newOL_js[$langRow['uid']] .= '
247 +(document.webinfoForm['
248 . GeneralUtility::quoteJSvalue('newOL[' . $langRow['uid'] . '][' . $data['row']['uid'] . ']')
249 . '].checked ? '
250 . GeneralUtility::quoteJSvalue('&edit[pages][' . $data['row']['uid'] . ']=new')
251 . ' : \'\')
252 ';
253 }
254 $tCells[] = '<td class="' . $status . ' col-border-left">&nbsp;</td>';
255 $tCells[] = '<td class="' . $status . '">&nbsp;</td>';
256 $tCells[] = '<td class="' . $status . '">' . $info . '</td>';
257 }
258 }
259 }
260 $output .= '
261 <tr>
262 ' . implode('
263 ', $tCells) . '
264 </tr>';
265 }
266 // Put together HEADER:
267 $tCells = [];
268 $tCells[] = '<td>' . $lang->sL('LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_page') . ':</td>';
269 if (is_array($langRecUids[0])) {
270 $editUrl = BackendUtility::getModuleUrl('record_edit', [
271 'edit' => [
272 'pages' => [
273 implode(',', $langRecUids[0]) => 'edit'
274 ]
275 ],
276 'columnsOnly' => 'title,nav_title,l18n_cfg,hidden',
277 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
278 ]);
279 $editIco = '<a href="' . htmlspecialchars($editUrl)
280 . '" class="btn btn-default" title="' . $lang->sL(
281 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_editPageProperties'
282 ) . '">' . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() . '</a>';
283 } else {
284 $editIco = '';
285 }
286 $tCells[] = '<td class="col-border-left" colspan="2">' . $lang->sL(
287 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_default'
288 ) . ':' . $editIco . '</td>';
289 foreach ($languages as $langRow) {
290 if ($this->pObj->MOD_SETTINGS['lang'] == 0 || (int)$this->pObj->MOD_SETTINGS['lang'] === (int)$langRow['uid']) {
291 // Title:
292 $tCells[] = '<td class="col-border-left">' . htmlspecialchars($langRow['title']) . '</td>';
293 // Edit language overlay records:
294 if (is_array($langRecUids[$langRow['uid']])) {
295 $editUrl = BackendUtility::getModuleUrl('record_edit', [
296 'edit' => [
297 'pages' => [
298 implode(',', $langRecUids[$langRow['uid']]) => 'edit'
299 ]
300 ],
301 'columnsOnly' => 'title,nav_title,hidden',
302 'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')
303 ]);
304 $editButton = '<a href="' . htmlspecialchars($editUrl)
305 . '" class="btn btn-default" title="' . $lang->sL(
306 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_renderl10n_editLangOverlays'
307 ) . '">' . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() . '</a>';
308 } else {
309 $editButton = '';
310 }
311 // Create new overlay records:
312 $params = '&columnsOnly=title,hidden,sys_language_uid&overrideVals[pages][sys_language_uid]=' . $langRow['uid'];
313 $onClick = BackendUtility::editOnClick($params);
314 if (!empty($newOL_js[$langRow['uid']])) {
315 $onClickArray = explode('?', $onClick, 2);
316 $lastElement = array_pop($onClickArray);
317 $onClickArray[] = '\'' . $newOL_js[$langRow['uid']] . ' + \'&' . $lastElement;
318 $onClick = implode('?', $onClickArray);
319 }
320 $newButton = '<a href="#" class="btn btn-default" onclick="' . htmlspecialchars($onClick)
321 . '" title="' . $lang->sL(
322 'LLL:EXT:info/Resources/Private/Language/locallang_webinfo.xlf:lang_getlangsta_createNewTranslationHeaders'
323 ) . '">' . $this->iconFactory->getIcon('actions-document-new', Icon::SIZE_SMALL)->render() . '</a>';
324
325 $tCells[] = '<td class="btn-group">' . $editButton . $newButton . '</td>';
326 $tCells[] = '<td>&nbsp;</td>';
327 }
328 }
329
330 $output =
331 '<div class="table-fit">' .
332 '<table class="table table-striped table-hover" id="langTable">' .
333 '<thead>' .
334 '<tr>' .
335 implode('', $tCells) .
336 '</tr>' .
337 '</thead>' .
338 '<tbody>' .
339 $output .
340 '</tbody>' .
341 '</table>' .
342 '</div>';
343 return $output;
344 }
345
346 /**
347 * Selects all system languages (from sys_language)
348 *
349 * @return array System language records in an array.
350 */
351 public function getSystemLanguages()
352 {
353 if (!$this->getBackendUser()->isAdmin() && $this->getBackendUser()->groupData['allowed_languages'] !== '') {
354 $allowed_languages = array_flip(explode(',', $this->getBackendUser()->groupData['allowed_languages']));
355 }
356 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
357 ->getQueryBuilderForTable('sys_language');
358 $queryBuilder
359 ->getRestrictions()
360 ->removeAll();
361 $queryBuilder
362 ->select('*')
363 ->from('sys_language')
364 ->orderBy('sorting');
365 $res = $queryBuilder->execute();
366 $outputArray = [];
367 if (is_array($allowed_languages) && !empty($allowed_languages)) {
368 while ($output = $res->fetch()) {
369 if (isset($allowed_languages[$output['uid']])) {
370 $outputArray[] = $output;
371 }
372 }
373 } else {
374 $outputArray = $res->fetchAll();
375 }
376 return $outputArray;
377 }
378
379 /**
380 * Get an alternative language record for a specific page / language
381 *
382 * @param int $pageId Page ID to look up for.
383 * @param int $langId Language UID to select for.
384 * @return array translated pages record
385 */
386 public function getLangStatus($pageId, $langId)
387 {
388 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
389 ->getQueryBuilderForTable('pages');
390 $queryBuilder
391 ->getRestrictions()
392 ->removeAll()
393 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class))
394 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
395 $result = $queryBuilder
396 ->select('*')
397 ->from('pages')
398 ->where(
399 $queryBuilder->expr()->eq(
400 $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'],
401 $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
402 )
403 )
404 ->andWhere(
405 $queryBuilder->expr()->eq(
406 $GLOBALS['TCA']['pages']['ctrl']['languageField'],
407 $queryBuilder->createNamedParameter($langId, \PDO::PARAM_INT)
408 )
409 )
410 ->execute();
411
412 $row = $result->fetch();
413 BackendUtility::workspaceOL('pages', $row);
414 if (is_array($row)) {
415 $row['_COUNT'] = $result->rowCount();
416 $row['_HIDDEN'] = $row['hidden'] || (int)$row['endtime'] > 0 && (int)$row['endtime'] < $GLOBALS['EXEC_TIME'] || $GLOBALS['EXEC_TIME'] < (int)$row['starttime'];
417 }
418 $result->closeCursor();
419 return $row;
420 }
421
422 /**
423 * Counting content elements for a single language on a page.
424 *
425 * @param int $pageId Page id to select for.
426 * @param int $sysLang Sys language uid
427 * @return int Number of content elements from the PID where the language is set to a certain value.
428 */
429 public function getContentElementCount($pageId, $sysLang)
430 {
431 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
432 ->getQueryBuilderForTable('pages');
433 $queryBuilder->getRestrictions()
434 ->removeAll()
435 ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
436 ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
437 $count = $queryBuilder
438 ->count('uid')
439 ->from('tt_content')
440 ->where(
441 $queryBuilder->expr()->eq(
442 'pid',
443 $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
444 )
445 )
446 ->andWhere(
447 $queryBuilder->expr()->eq(
448 'sys_language_uid',
449 $queryBuilder->createNamedParameter($sysLang, \PDO::PARAM_INT)
450 )
451 )
452 ->execute()
453 ->fetchColumn(0);
454 return $count ?: '-';
455 }
456
457 /**
458 * Returns LanguageService
459 *
460 * @return \TYPO3\CMS\Core\Localization\LanguageService
461 */
462 protected function getLanguageService()
463 {
464 return $GLOBALS['LANG'];
465 }
466
467 /**
468 * Returns the current BE user.
469 *
470 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
471 */
472 protected function getBackendUser()
473 {
474 return $GLOBALS['BE_USER'];
475 }
476 }