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