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