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