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