[TASK] BACK_PATH DocumentTemplate
[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 TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Messaging\FlashMessage;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Taskcenter\TaskInterface;
23
24 /**
25 * This class provides a taskcenter for BE users
26 */
27 class TaskModuleController extends \TYPO3\CMS\Backend\Module\BaseScriptClass {
28
29 /**
30 * @var array
31 */
32 protected $pageinfo;
33
34 /**
35 * The name of the module
36 *
37 * @var string
38 */
39 protected $moduleName = 'user_task';
40
41 /**
42 * Initializes the Module
43 */
44 public function __construct() {
45 $this->getLanguageService()->includeLLFile('EXT:taskcenter/Resources/Private/Language/locallang_task.xlf');
46 $this->MCONF = array(
47 'name' => $this->moduleName
48 );
49 parent::init();
50 // Initialize document
51 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
52 $this->doc->setModuleTemplate(ExtensionManagementUtility::extPath('taskcenter') . 'Resources/Private/Templates/mod_template.html');
53 $this->getPageRenderer()->loadJquery();
54 $this->doc->addStyleSheet('tx_taskcenter', '../' . ExtensionManagementUtility::siteRelPath('taskcenter') . 'Resources/Public/Css/styles.css');
55 }
56
57 /**
58 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
59 *
60 * @return void
61 */
62 public function menuConfig() {
63 $this->MOD_MENU = array('mode' => array());
64 $this->MOD_MENU['mode']['information'] = $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang.xlf:task_overview');
65 $this->MOD_MENU['mode']['tasks'] = $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang.xlf:task_tasks');
66 /* Copied from parent::menuConfig, because parent is hardcoded to menu.function,
67 * however menu.function is already used for the individual tasks.
68 * Therefore we use menu.mode here.
69 */
70 // Page/be_user TSconfig settings and blinding of menu-items
71 $this->modTSconfig = BackendUtility::getModTSconfig($this->id, 'mod.' . $this->moduleName);
72 $this->MOD_MENU['mode'] = $this->mergeExternalItems($this->MCONF['name'], 'mode', $this->MOD_MENU['mode']);
73 $this->MOD_MENU['mode'] = BackendUtility::unsetMenuItems($this->modTSconfig['properties'], $this->MOD_MENU['mode'], 'menu.mode');
74 parent::menuConfig();
75 }
76
77 /**
78 * Creates the module's content. In this case it rather acts as a kind of #
79 * dispatcher redirecting requests to specific tasks.
80 *
81 * @return void
82 */
83 public function main() {
84 $docHeaderButtons = $this->getButtons();
85 $markers = array();
86 $this->doc->postCode = $this->doc->wrapScriptTags('if (top.fsMod) { top.fsMod.recentIds["web"] = 0; }');
87
88 // Render content depending on the mode
89 $mode = (string)$this->MOD_SETTINGS['mode'];
90 if ($mode === 'information') {
91 $this->renderInformationContent();
92 } else {
93 $this->renderModuleContent();
94 }
95 // Compile document
96 $markers['FUNC_MENU'] = BackendUtility::getFuncMenu(0, 'SET[mode]', $this->MOD_SETTINGS['mode'], $this->MOD_MENU['mode']);
97 $markers['CONTENT'] = $this->content;
98 // Build the <body> for the module
99 $this->content = $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
100 // Renders the module page
101 $this->content = $this->doc->render($this->getLanguageService()->getLL('title'), $this->content);
102 }
103
104 /**
105 * Prints out the module's HTML
106 *
107 * @return void
108 */
109 public function printContent() {
110 echo $this->content;
111 }
112
113 /**
114 * Generates the module content by calling the selected task
115 *
116 * @return void
117 */
118 protected function renderModuleContent() {
119 $chosenTask = (string)$this->MOD_SETTINGS['function'];
120 // Render the taskcenter task as default
121 if (empty($chosenTask) || $chosenTask == 'index') {
122 $chosenTask = 'taskcenter.tasks';
123 }
124 // Render the task
125 $actionContent = '';
126 list($extKey, $taskClass) = explode('.', $chosenTask, 2);
127 if (class_exists($taskClass)) {
128 $taskInstance = GeneralUtility::makeInstance($taskClass, $this);
129 if ($taskInstance instanceof TaskInterface) {
130 // Check if the task is restricted to admins only
131 if ($this->checkAccess($extKey, $taskClass)) {
132 $actionContent .= $taskInstance->getTask();
133 } else {
134 $flashMessage = GeneralUtility::makeInstance(
135 FlashMessage::class,
136 $this->getLanguageService()->getLL('error-access', TRUE),
137 $this->getLanguageService()->getLL('error_header'),
138 FlashMessage::ERROR
139 );
140 $actionContent .= $flashMessage->render();
141 }
142 } else {
143 // Error if the task is not an instance of \TYPO3\CMS\Taskcenter\TaskInterface
144 $flashMessage = GeneralUtility::makeInstance(
145 FlashMessage::class,
146 sprintf($this->getLanguageService()->getLL('error_no-instance', TRUE), $taskClass, TaskInterface::class),
147 $this->getLanguageService()->getLL('error_header'),
148 FlashMessage::ERROR
149 );
150 $actionContent .= $flashMessage->render();
151 }
152 } else {
153 $flashMessage = GeneralUtility::makeInstance(
154 FlashMessage::class,
155 $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang_mod.xlf:mlang_labels_tabdescr'),
156 $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'),
157 FlashMessage::INFO
158 );
159 $actionContent .= $flashMessage->render();
160 }
161 $content = '<div id="taskcenter-main">
162 <div id="taskcenter-menu">' . $this->indexAction() . '</div>
163 <div id="taskcenter-item" class="' . htmlspecialchars(($extKey . '-' . $taskClass)) . '">' . $actionContent . '
164 </div>
165 </div>';
166 $this->content .= $content;
167 }
168
169 /**
170 * Generates the information content
171 *
172 * @return void
173 */
174 protected function renderInformationContent() {
175 $content = $this->description($this->getLanguageService()->getLL('mlang_tabs_tab'), $this->getLanguageService()->sL('LLL:EXT:taskcenter/Resources/Private/Language/locallang_mod.xlf:mlang_labels_tabdescr'));
176 $content .= $this->getLanguageService()->getLL('taskcenter-about');
177 if ($this->getBackendUser()->isAdmin()) {
178 $content .= '<br /><br />' . $this->description($this->getLanguageService()->getLL('taskcenter-adminheader'), $this->getLanguageService()->getLL('taskcenter-admin'));
179 }
180 $this->content .= $content;
181 }
182
183 /**
184 * Render the headline of a task including a title and an optional description.
185 *
186 * @param string $title Title
187 * @param string $description Description
188 * @return string formatted title and description
189 */
190 public function description($title, $description = '') {
191 $content = '<h1>' . nl2br(htmlspecialchars($title)) . '</h1>';
192 if (!empty($description)) {
193 $content .= '<p class="description">' . nl2br(htmlspecialchars($description)) . '</p>';
194 }
195 return $content;
196 }
197
198 /**
199 * Render a list of items as a nicely formated definition list including a
200 * link, icon, title and description.
201 * The keys of a single item are:
202 * - title: Title of the item
203 * - link: Link to the task
204 * - icon: Path to the icon or Icon as HTML if it begins with <img
205 * - description: Description of the task, using htmlspecialchars()
206 * - descriptionHtml: Description allowing HTML tags which will override the
207 * description
208 *
209 * @param array $items List of items to be displayed in the definition list.
210 * @param bool $mainMenu Set it to TRUE to render the main menu
211 * @return string Fefinition list
212 */
213 public function renderListMenu($items, $mainMenu = FALSE) {
214 $content = ($section = '');
215 $count = 0;
216 // Change the sorting of items to the user's one
217 if ($mainMenu) {
218 $this->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Taskcenter/Taskcenter');
219 $userSorting = unserialize($this->getBackendUser()->uc['taskcenter']['sorting']);
220 if (is_array($userSorting)) {
221 $newSorting = array();
222 foreach ($userSorting as $item) {
223 if (isset($items[$item])) {
224 $newSorting[] = $items[$item];
225 unset($items[$item]);
226 }
227 }
228 $items = $newSorting + $items;
229 }
230 }
231 if (is_array($items) && !empty($items)) {
232 foreach ($items as $item) {
233 $title = htmlspecialchars($item['title']);
234 $icon = ($additionalClass = ($collapsedStyle = ''));
235 // Check for custom icon
236 if (!empty($item['icon'])) {
237 if (strpos($item['icon'], '<img ') === FALSE) {
238 $absIconPath = GeneralUtility::getFileAbsFilename($item['icon']);
239 // If the file indeed exists, assemble relative path to it
240 if (file_exists($absIconPath)) {
241 $icon = $GLOBALS['BACK_PATH'] . '../' . str_replace(PATH_site, '', $absIconPath);
242 $icon = '<img src="' . $icon . '" title="' . $title . '" alt="' . $title . '" />';
243 }
244 if (@is_file($icon)) {
245 $icon = '<img' . IconUtility::skinImg($GLOBALS['BACK_PATH'], $icon, 'width="16" height="16"') . ' title="' . $title . '" alt="' . $title . '" />';
246 }
247 } else {
248 $icon = $item['icon'];
249 }
250 }
251 $description = $item['descriptionHtml'] ?: '<p>' . nl2br(htmlspecialchars($item['description'])) . '</p>';
252 $id = $this->getUniqueKey($item['uid']);
253 // Collapsed & expanded menu items
254 if ($mainMenu && isset($this->getBackendUser()->uc['taskcenter']['states'][$id]) && $this->getBackendUser()->uc['taskcenter']['states'][$id]) {
255 $collapsedStyle = 'style="display:none"';
256 $additionalClass = 'collapsed';
257 } else {
258 $additionalClass = 'expanded';
259 }
260 // First & last menu item
261 if ($count == 0) {
262 $additionalClass .= ' first-item';
263 } elseif ($count + 1 === count($items)) {
264 $additionalClass .= ' last-item';
265 }
266 // Active menu item
267 $active = (string)$this->MOD_SETTINGS['function'] == $item['uid'] ? ' active-task' : '';
268 // Main menu: Render additional syntax to sort tasks
269 if ($mainMenu) {
270 $section = '<div class="down"><i class="fa fa-caret-down fa-fw"></i></div>
271 <div class="drag"><i class="fa fa-arrows"></i></div>';
272 $backgroundClass = 't3-row-header ';
273 } else {
274 $backgroundClass = '';
275 }
276 $content .= '<li class="' . $additionalClass . $active . '" id="el_' . $id . '">
277 ' . $section . '
278 <div class="image">' . $icon . '</div>
279 <div class="' . $backgroundClass . 'link"><a href="' . $item['link'] . '">' . $title . '</a></div>
280 <div class="content " ' . $collapsedStyle . '>' . $description . '</div>
281 </li>';
282 $count++;
283 }
284 $navigationId = $mainMenu ? 'id="task-list"' : '';
285 $content = '<ul ' . $navigationId . ' class="task-list">' . $content . '</ul>';
286 }
287 return $content;
288 }
289
290 /**
291 * Shows an overview list of available reports.
292 *
293 * @return string List of available reports
294 */
295 protected function indexAction() {
296 $content = '';
297 $tasks = array();
298 $icon = ExtensionManagementUtility::extRelPath('taskcenter') . 'Resources/Public/Icons/module-taskcenter.svg';
299 // Render the tasks only if there are any available
300 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter']) && !empty($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'])) {
301 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'] as $extKey => $extensionReports) {
302 foreach ($extensionReports as $taskClass => $task) {
303 if (!$this->checkAccess($extKey, $taskClass)) {
304 continue;
305 }
306 $link = BackendUtility::getModuleUrl('user_task') . '&SET[function]=' . $extKey . '.' . $taskClass;
307 $taskTitle = $this->getLanguageService()->sL($task['title']);
308 $taskDescriptionHtml = '';
309 // Check for custom icon
310 if (!empty($task['icon'])) {
311 $icon = GeneralUtility::getFileAbsFilename($task['icon']);
312 }
313 if (class_exists($taskClass)) {
314 $taskInstance = GeneralUtility::makeInstance($taskClass, $this);
315 if ($taskInstance instanceof TaskInterface) {
316 $taskDescriptionHtml = $taskInstance->getOverview();
317 }
318 }
319 // Generate an array of all tasks
320 $uniqueKey = $this->getUniqueKey($extKey . '.' . $taskClass);
321 $tasks[$uniqueKey] = array(
322 'title' => $taskTitle,
323 'descriptionHtml' => $taskDescriptionHtml,
324 'description' => $this->getLanguageService()->sL($task['description']),
325 'icon' => $icon,
326 'link' => $link,
327 'uid' => $extKey . '.' . $taskClass
328 );
329 }
330 }
331 $content .= $this->renderListMenu($tasks, TRUE);
332 } else {
333 $flashMessage = GeneralUtility::makeInstance(
334 FlashMessage::class,
335 $this->getLanguageService()->getLL('no-tasks', TRUE),
336 '',
337 FlashMessage::INFO
338 );
339 $this->content .= $flashMessage->render();
340 }
341 return $content;
342 }
343
344 /**
345 * Create the panel of buttons for submitting the form or otherwise
346 * perform operations.
347 *
348 * @return array All available buttons as an assoc. array
349 */
350 protected function getButtons() {
351 $buttons = array(
352 'shortcut' => '',
353 'open_new_window' => $this->openInNewWindow()
354 );
355 // Shortcut
356 if ($this->getBackendUser()->mayMakeShortcut()) {
357 $buttons['shortcut'] = $this->doc->makeShortcutIcon('', 'function', $this->moduleName);
358 }
359 return $buttons;
360 }
361
362 /**
363 * Check the access to a task. Considered are:
364 * - Admins are always allowed
365 * - Tasks can be restriced to admins only
366 * - Tasks can be blinded for Users with TsConfig taskcenter.<extensionkey>.<taskName> = 0
367 *
368 * @param string $extKey Extension key
369 * @param string $taskClass Name of the task
370 * @return bool Access to the task allowed or not
371 */
372 protected function checkAccess($extKey, $taskClass) {
373 // Check if task is blinded with TsConfig (taskcenter.<extkey>.<taskName>
374 $tsConfig = $this->getBackendUser()->getTSConfig('taskcenter.' . $extKey . '.' . $taskClass);
375 if (isset($tsConfig['value']) && (int)$tsConfig['value'] === 0) {
376 return FALSE;
377 }
378 // Admins are always allowed
379 if ($this->getBackendUser()->isAdmin()) {
380 return TRUE;
381 }
382 // Check if task is restricted to admins
383 if ((int)$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'][$extKey][$taskClass]['admin'] === 1) {
384 return FALSE;
385 }
386 return TRUE;
387 }
388
389 /**
390 * Returns HTML code to dislay an url in an iframe at the right side of the taskcenter
391 *
392 * @param string $url Url to display
393 * @param int $max
394 * @return string Code that inserts the iframe (HTML)
395 */
396 public function urlInIframe($url, $max = 0) {
397 return '<iframe scrolling="auto" width="100%" src="' . $url . '" name="list_frame" id="list_frame" frameborder="no"></iframe>';
398 }
399
400 /**
401 * Create a unique key from a string which can be used in JS for sorting
402 * Therefore '_' are replaced
403 *
404 * @param string $string string which is used to generate the identifier
405 * @return string Modified string
406 */
407 protected function getUniqueKey($string) {
408 $search = array('.', '_');
409 $replace = array('-', '');
410 return str_replace($search, $replace, $string);
411 }
412
413 /**
414 * This method prepares the link for opening the devlog in a new window
415 *
416 * @return string Hyperlink with icon and appropriate JavaScript
417 */
418 protected function openInNewWindow() {
419 $url = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
420 $onClick = 'devlogWin=window.open(' . GeneralUtility::quoteJSvalue($url) . ',\'taskcenter\',\'width=790,status=0,menubar=1,resizable=1,location=0,scrollbars=1,toolbar=0\');return false;';
421 $content = '<a href="#" onclick="' . htmlspecialchars($onClick) . '">' .
422 IconUtility::getSpriteIcon('actions-window-open', array('title' => $this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.openInNewWindow', TRUE))) .
423 '</a>';
424 return $content;
425 }
426
427 /**
428 * Returns the current BE user.
429 *
430 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
431 */
432 protected function getBackendUser() {
433 return $GLOBALS['BE_USER'];
434 }
435
436 /**
437 * Returns LanguageService
438 *
439 * @return \TYPO3\CMS\Lang\LanguageService
440 */
441 protected function getLanguageService() {
442 return $GLOBALS['LANG'];
443 }
444
445 }