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