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