[BUGFIX] Taskcenter: Removed call to deprecated method render()
[Packages/TYPO3.CMS.git] / typo3 / sysext / taskcenter / Classes / Controller / TaskModuleController.php
1 <?php
2 namespace TYPO3\CMS\Taskcenter\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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Module\BaseScriptClass;
20 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
21 use TYPO3\CMS\Backend\Template\ModuleTemplate;
22 use TYPO3\CMS\Backend\Utility\BackendUtility;
23 use TYPO3\CMS\Core\Imaging\Icon;
24 use TYPO3\CMS\Core\Messaging\FlashMessage;
25 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Core\Utility\PathUtility;
28 use TYPO3\CMS\Taskcenter\TaskInterface;
29
30 /**
31 * This class provides a taskcenter for BE users
32 */
33 class TaskModuleController extends BaseScriptClass
34 {
35 /**
36 * @var array
37 */
38 protected $pageinfo;
39
40 /**
41 * ModuleTemplate Container
42 *
43 * @var ModuleTemplate
44 */
45 protected $moduleTemplate;
46
47 /**
48 * The name of the module
49 *
50 * @var string
51 */
52 protected $moduleName = 'user_task';
53
54 /**
55 * Initializes the Module
56 */
57 public function __construct()
58 {
59 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
60 $this->moduleTemplate->getPageRenderer()->addCssFile(ExtensionManagementUtility::extRelPath('taskcenter') . 'Resources/Public/Css/styles.css');
61 $this->getLanguageService()->includeLLFile('EXT:taskcenter/Resources/Private/Language/locallang_task.xlf');
62 $this->MCONF = array(
63 'name' => $this->moduleName
64 );
65 parent::init();
66 }
67
68 /**
69 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
70 *
71 * @return void
72 */
73 public function menuConfig()
74 {
75 $this->MOD_MENU = array('mode' => array());
76 $this->MOD_MENU['mode']['information'] = $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang.xlf:task_overview');
77 $this->MOD_MENU['mode']['tasks'] = $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang.xlf:task_tasks');
78 /* Copied from parent::menuConfig, because parent is hardcoded to menu.function,
79 * however menu.function is already used for the individual tasks.
80 * Therefore we use menu.mode here.
81 */
82 // Page/be_user TSconfig settings and blinding of menu-items
83 $this->modTSconfig = BackendUtility::getModTSconfig($this->id, 'mod.' . $this->moduleName);
84 $this->MOD_MENU['mode'] = $this->mergeExternalItems($this->MCONF['name'], 'mode', $this->MOD_MENU['mode']);
85 $this->MOD_MENU['mode'] = BackendUtility::unsetMenuItems($this->modTSconfig['properties'], $this->MOD_MENU['mode'], 'menu.mode');
86 parent::menuConfig();
87 }
88
89 /**
90 * Generates the menu based on $this->MOD_MENU
91 *
92 * @throws \InvalidArgumentException
93 */
94 protected function generateMenu()
95 {
96 $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
97 $menu->setIdentifier('WebFuncJumpMenu');
98 foreach ($this->MOD_MENU['mode'] as $controller => $title) {
99 $item = $menu
100 ->makeMenuItem()
101 ->setHref(
102 BackendUtility::getModuleUrl(
103 $this->moduleName,
104 [
105 'id' => $this->id,
106 'SET' => [
107 'mode' => $controller
108 ]
109 ]
110 )
111 )
112 ->setTitle($title);
113 if ($controller === $this->MOD_SETTINGS['mode']) {
114 $item->setActive(true);
115 }
116 $menu->addMenuItem($item);
117 }
118 $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
119 }
120
121 /**
122 * Injects the request object for the current request or subrequest
123 * Simply calls main() and writes the content to the response
124 *
125 * @param ServerRequestInterface $request the current request
126 * @param ResponseInterface $response
127 * @return ResponseInterface the response with the content
128 */
129 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
130 {
131 $GLOBALS['SOBE'] = $this;
132 $this->main();
133
134 $this->moduleTemplate->setContent($this->content);
135
136 $response->getBody()->write($this->moduleTemplate->renderContent());
137 return $response;
138 }
139
140 /**
141 * Creates the module's content. In this case it rather acts as a kind of #
142 * dispatcher redirecting requests to specific tasks.
143 *
144 * @return void
145 */
146 public function main()
147 {
148 $this->getButtons();
149 $this->generateMenu();
150 $this->moduleTemplate->addJavaScriptCode(
151 'TaskCenterInlineJavascript',
152 'if (top.fsMod) { top.fsMod.recentIds["web"] = 0; }'
153 );
154
155 // Render content depending on the mode
156 $mode = (string)$this->MOD_SETTINGS['mode'];
157 if ($mode === 'information') {
158 $this->renderInformationContent();
159 } else {
160 $this->renderModuleContent();
161 }
162 // Renders the module page
163 $this->moduleTemplate->setTitle($this->getLanguageService()->getLL('title'));
164 }
165
166 /**
167 * Prints out the module's HTML
168 *
169 * @return void
170 */
171 public function printContent()
172 {
173 echo $this->content;
174 }
175
176 /**
177 * Generates the module content by calling the selected task
178 *
179 * @return void
180 */
181 protected function renderModuleContent()
182 {
183 $chosenTask = (string)$this->MOD_SETTINGS['function'];
184 // Render the taskcenter task as default
185 if (empty($chosenTask) || $chosenTask == 'index') {
186 $chosenTask = 'taskcenter.tasks';
187 }
188 // Render the task
189 $actionContent = '';
190 $flashMessage = null;
191 list($extKey, $taskClass) = explode('.', $chosenTask, 2);
192 if (class_exists($taskClass)) {
193 $taskInstance = GeneralUtility::makeInstance($taskClass, $this);
194 if ($taskInstance instanceof TaskInterface) {
195 // Check if the task is restricted to admins only
196 if ($this->checkAccess($extKey, $taskClass)) {
197 $actionContent .= $taskInstance->getTask();
198 } else {
199 $flashMessage = GeneralUtility::makeInstance(
200 FlashMessage::class,
201 $this->getLanguageService()->getLL('error-access', true),
202 $this->getLanguageService()->getLL('error_header'),
203 FlashMessage::ERROR
204 );
205 }
206 } else {
207 // Error if the task is not an instance of \TYPO3\CMS\Taskcenter\TaskInterface
208 $flashMessage = GeneralUtility::makeInstance(
209 FlashMessage::class,
210 sprintf($this->getLanguageService()->getLL('error_no-instance', true), $taskClass, TaskInterface::class),
211 $this->getLanguageService()->getLL('error_header'),
212 FlashMessage::ERROR
213 );
214 }
215 } else {
216 $flashMessage = GeneralUtility::makeInstance(
217 FlashMessage::class,
218 $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang_mod.xlf:mlang_labels_tabdescr'),
219 $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'),
220 FlashMessage::INFO
221 );
222 }
223
224 if ($flashMessage) {
225 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
226 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
227 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
228 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
229 $defaultFlashMessageQueue->enqueue($flashMessage);
230 }
231
232 $content = '<div id="taskcenter-main">
233 <div id="taskcenter-menu">' . $this->indexAction() . '</div>
234 <div id="taskcenter-item" class="' . htmlspecialchars(($extKey . '-' . $taskClass)) . '">' . $actionContent . '
235 </div>
236 </div>';
237 $this->content .= $content;
238 }
239
240 /**
241 * Generates the information content
242 *
243 * @return void
244 */
245 protected function renderInformationContent()
246 {
247 $content = $this->description($this->getLanguageService()->getLL('mlang_tabs_tab'), $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang_mod.xlf:mlang_labels_tabdescr'));
248 $content .= $this->getLanguageService()->getLL('taskcenter-about');
249 if ($this->getBackendUser()->isAdmin()) {
250 $content .= '<br /><br />' . $this->description($this->getLanguageService()->getLL('taskcenter-adminheader'), $this->getLanguageService()->getLL('taskcenter-admin'));
251 }
252 $this->content .= $content;
253 }
254
255 /**
256 * Render the headline of a task including a title and an optional description.
257 *
258 * @param string $title Title
259 * @param string $description Description
260 * @return string formatted title and description
261 */
262 public function description($title, $description = '')
263 {
264 $content = '<h1>' . nl2br(htmlspecialchars($title)) . '</h1>';
265 if (!empty($description)) {
266 $content .= '<p class="description">' . nl2br(htmlspecialchars($description)) . '</p>';
267 }
268 return $content;
269 }
270
271 /**
272 * Render a list of items as a nicely formated definition list including a
273 * link, icon, title and description.
274 * The keys of a single item are:
275 * - title: Title of the item
276 * - link: Link to the task
277 * - icon: Path to the icon or Icon as HTML if it begins with <img
278 * - description: Description of the task, using htmlspecialchars()
279 * - descriptionHtml: Description allowing HTML tags which will override the
280 * description
281 *
282 * @param array $items List of items to be displayed in the definition list.
283 * @param bool $mainMenu Set it to TRUE to render the main menu
284 * @return string Fefinition list
285 */
286 public function renderListMenu($items, $mainMenu = false)
287 {
288 $content = ($section = '');
289 $count = 0;
290 // Change the sorting of items to the user's one
291 if ($mainMenu) {
292 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Taskcenter/Taskcenter');
293 $userSorting = unserialize($this->getBackendUser()->uc['taskcenter']['sorting']);
294 if (is_array($userSorting)) {
295 $newSorting = array();
296 foreach ($userSorting as $item) {
297 if (isset($items[$item])) {
298 $newSorting[] = $items[$item];
299 unset($items[$item]);
300 }
301 }
302 $items = $newSorting + $items;
303 }
304 }
305 if (is_array($items) && !empty($items)) {
306 foreach ($items as $item) {
307 $title = htmlspecialchars($item['title']);
308 $icon = ($additionalClass = ($collapsedStyle = ''));
309 // Check for custom icon
310 if (!empty($item['icon'])) {
311 if (strpos($item['icon'], '<img ') === false) {
312 $absIconPath = GeneralUtility::getFileAbsFileName($item['icon']);
313 // If the file indeed exists, assemble relative path to it
314 if (file_exists($absIconPath)) {
315 $icon = '../' . str_replace(PATH_site, '', $absIconPath);
316 $icon = '<img src="' . $icon . '" title="' . $title . '" alt="' . $title . '" />';
317 }
318 if (@is_file($icon)) {
319 $icon = '<img src="' . PathUtility::getAbsoluteWebPath($icon) . '" width="16" height="16" title="' . $title . '" alt="' . $title . '" />';
320 }
321 } else {
322 $icon = $item['icon'];
323 }
324 }
325 $description = $item['descriptionHtml'] ?: '<p>' . nl2br(htmlspecialchars($item['description'])) . '</p>';
326 $id = $this->getUniqueKey($item['uid']);
327 // Collapsed & expanded menu items
328 if ($mainMenu && isset($this->getBackendUser()->uc['taskcenter']['states'][$id]) && $this->getBackendUser()->uc['taskcenter']['states'][$id]) {
329 $collapsedStyle = 'style="display:none"';
330 $additionalClass = 'collapsed';
331 } else {
332 $additionalClass = 'expanded';
333 }
334 // First & last menu item
335 if ($count == 0) {
336 $additionalClass .= ' first-item';
337 } elseif ($count + 1 === count($items)) {
338 $additionalClass .= ' last-item';
339 }
340 // Active menu item
341 $active = (string)$this->MOD_SETTINGS['function'] == $item['uid'] ? ' active-task' : '';
342 // Main menu: Render additional syntax to sort tasks
343 if ($mainMenu) {
344 $section = '<div class="down"><i class="fa fa-caret-down fa-fw"></i></div>
345 <div class="drag"><i class="fa fa-arrows"></i></div>';
346 $backgroundClass = 't3-row-header ';
347 } else {
348 $backgroundClass = '';
349 }
350 $content .= '<li class="' . $additionalClass . $active . '" id="el_' . $id . '">
351 ' . $section . '
352 <div class="image">' . $icon . '</div>
353 <div class="' . $backgroundClass . 'link"><a href="' . $item['link'] . '">' . $title . '</a></div>
354 <div class="content " ' . $collapsedStyle . '>' . $description . '</div>
355 </li>';
356 $count++;
357 }
358 $navigationId = $mainMenu ? 'id="task-list"' : '';
359 $content = '<ul ' . $navigationId . ' class="task-list">' . $content . '</ul>';
360 }
361 return $content;
362 }
363
364 /**
365 * Shows an overview list of available reports.
366 *
367 * @return string List of available reports
368 */
369 protected function indexAction()
370 {
371 $content = '';
372 $tasks = array();
373 $icon = ExtensionManagementUtility::extRelPath('taskcenter') . 'Resources/Public/Icons/module-taskcenter.svg';
374 // Render the tasks only if there are any available
375 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter']) && !empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'])) {
376 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'] as $extKey => $extensionReports) {
377 foreach ($extensionReports as $taskClass => $task) {
378 if (!$this->checkAccess($extKey, $taskClass)) {
379 continue;
380 }
381 $link = BackendUtility::getModuleUrl('user_task') . '&SET[function]=' . $extKey . '.' . $taskClass;
382 $taskTitle = $this->getLanguageService()->sL($task['title']);
383 $taskDescriptionHtml = '';
384 // Check for custom icon
385 if (!empty($task['icon'])) {
386 $icon = GeneralUtility::getFileAbsFileName($task['icon']);
387 }
388 if (class_exists($taskClass)) {
389 $taskInstance = GeneralUtility::makeInstance($taskClass, $this);
390 if ($taskInstance instanceof TaskInterface) {
391 $taskDescriptionHtml = $taskInstance->getOverview();
392 }
393 }
394 // Generate an array of all tasks
395 $uniqueKey = $this->getUniqueKey($extKey . '.' . $taskClass);
396 $tasks[$uniqueKey] = array(
397 'title' => $taskTitle,
398 'descriptionHtml' => $taskDescriptionHtml,
399 'description' => $this->getLanguageService()->sL($task['description']),
400 'icon' => $icon,
401 'link' => $link,
402 'uid' => $extKey . '.' . $taskClass
403 );
404 }
405 }
406 $content .= $this->renderListMenu($tasks, true);
407 } else {
408 $flashMessage = GeneralUtility::makeInstance(
409 FlashMessage::class,
410 $this->getLanguageService()->getLL('no-tasks', true),
411 '',
412 FlashMessage::INFO
413 );
414 /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
415 $flashMessageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessageService::class);
416 /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
417 $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
418 $defaultFlashMessageQueue->enqueue($flashMessage);
419 }
420 return $content;
421 }
422
423 /**
424 * Create the panel of buttons for submitting the form or otherwise
425 * perform operations.
426 *
427 * @return void
428 */
429 protected function getButtons()
430 {
431 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
432
433 // Fullscreen Button
434 $url = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
435 $onClick = 'devlogWin=window.open(' . GeneralUtility::quoteJSvalue($url) . ',\'taskcenter\',\'width=790,status=0,menubar=1,resizable=1,location=0,scrollbars=1,toolbar=0\');return false;';
436 $fullscreenButton = $buttonBar->makeLinkButton()
437 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.openInNewWindow'))
438 ->setOnClick($onClick)
439 ->setHref('#')
440 ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-window-open', Icon::SIZE_SMALL))
441 ;
442 $buttonBar->addButton($fullscreenButton, ButtonBar::BUTTON_POSITION_RIGHT, 1);
443
444 // Shortcut
445 $shortcutButton = $buttonBar->makeShortcutButton()
446 ->setModuleName($this->moduleName)
447 ->setSetVariables(['function']);
448 $buttonBar->addButton($shortcutButton);
449 }
450
451 /**
452 * Check the access to a task. Considered are:
453 * - Admins are always allowed
454 * - Tasks can be restriced to admins only
455 * - Tasks can be blinded for Users with TsConfig taskcenter.<extensionkey>.<taskName> = 0
456 *
457 * @param string $extKey Extension key
458 * @param string $taskClass Name of the task
459 * @return bool Access to the task allowed or not
460 */
461 protected function checkAccess($extKey, $taskClass)
462 {
463 // Check if task is blinded with TsConfig (taskcenter.<extkey>.<taskName>
464 $tsConfig = $this->getBackendUser()->getTSConfig('taskcenter.' . $extKey . '.' . $taskClass);
465 if (isset($tsConfig['value']) && (int)$tsConfig['value'] === 0) {
466 return false;
467 }
468 // Admins are always allowed
469 if ($this->getBackendUser()->isAdmin()) {
470 return true;
471 }
472 // Check if task is restricted to admins
473 if ((int)$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'][$extKey][$taskClass]['admin'] === 1) {
474 return false;
475 }
476 return true;
477 }
478
479 /**
480 * Returns HTML code to dislay an url in an iframe at the right side of the taskcenter
481 *
482 * @param string $url Url to display
483 * @return string Code that inserts the iframe (HTML)
484 */
485 public function urlInIframe($url)
486 {
487 return '<iframe scrolling="auto" width="100%" src="' . $url . '" name="list_frame" id="list_frame" frameborder="no"></iframe>';
488 }
489
490 /**
491 * Create a unique key from a string which can be used in JS for sorting
492 * Therefore '_' are replaced
493 *
494 * @param string $string string which is used to generate the identifier
495 * @return string Modified string
496 */
497 protected function getUniqueKey($string)
498 {
499 $search = array('.', '_');
500 $replace = array('-', '');
501 return str_replace($search, $replace, $string);
502 }
503
504 /**
505 * This method prepares the link for opening the devlog in a new window
506 *
507 * @return string Hyperlink with icon and appropriate JavaScript
508 */
509 protected function openInNewWindow()
510 {
511 $url = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
512 $onClick = 'devlogWin=window.open(' . GeneralUtility::quoteJSvalue($url) . ',\'taskcenter\',\'width=790,status=0,menubar=1,resizable=1,location=0,scrollbars=1,toolbar=0\');return false;';
513 $content = '<a href="#" onclick="' . htmlspecialchars($onClick) . '" title="' . $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.openInNewWindow', true) . '">'
514 . $this->moduleTemplate->getIconFactory()->getIcon('actions-window-open', Icon::SIZE_SMALL)->render()
515 . '</a>';
516 return $content;
517 }
518
519 /**
520 * Returns the current BE user.
521 *
522 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
523 */
524 protected function getBackendUser()
525 {
526 return $GLOBALS['BE_USER'];
527 }
528
529 /**
530 * Returns LanguageService
531 *
532 * @return \TYPO3\CMS\Lang\LanguageService
533 */
534 protected function getLanguageService()
535 {
536 return $GLOBALS['LANG'];
537 }
538
539 /**
540 * @return ModuleTemplate
541 */
542 public function getModuleTemplate()
543 {
544 return $this->moduleTemplate;
545 }
546 }