Fixed bug #15691: Javascript Error in taskcenter
[Packages/TYPO3.CMS.git] / typo3 / sysext / taskcenter / task / index.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Georg Ringer <typo3@ringerge.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * This class provides a taskcenter for BE users
28 *
29 * @author Georg Ringer <typo3@ringerge.org>
30 * @package TYPO3
31 * @subpackage taskcenter
32 *
33 */
34
35
36 $LANG->includeLLFile('EXT:taskcenter/task/locallang.xml');
37
38
39 $BE_USER->modAccess($MCONF, 1);
40
41
42 // ***************************
43 // Script Classes
44 // ***************************
45 class SC_mod_user_task_index extends t3lib_SCbase {
46
47 protected $pageinfo;
48
49 /**
50 * Initializes the Module
51 *
52 * @return void
53 */
54 public function __construct() {
55 parent::init();
56
57 // initialize document
58 $this->doc = t3lib_div::makeInstance('template');
59 $this->doc->setModuleTemplate(
60 t3lib_extMgm::extPath('taskcenter') . 'res/mod_template.html'
61 );
62 $this->doc->backPath = $GLOBALS['BACK_PATH'];
63 $this->doc->getPageRenderer()->loadScriptaculous('effects,dragdrop');
64 $this->doc->addStyleSheet(
65 'tx_taskcenter',
66 '../' . t3lib_extMgm::siteRelPath('taskcenter') . 'res/mod_styles.css'
67 );
68 }
69
70 /**
71 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
72 *
73 * @return void
74 */
75 public function menuConfig() {
76 $this->MOD_MENU = array('mode' => array());
77
78 $this->MOD_MENU['mode']['information'] = $GLOBALS['LANG']->sL('LLL:EXT:taskcenter/locallang.xml:task_overview');
79 $this->MOD_MENU['mode']['tasks'] = 'Tasks';
80
81 parent::menuConfig();
82 }
83
84 /**
85 * Creates the module's content. In this case it rather acts as a kind of #
86 * dispatcher redirecting requests to specific tasks.
87 *
88 * @return void
89 */
90 public function main() {
91 $docHeaderButtons = $this->getButtons();
92 $markers = array();
93
94 $this->doc->JScodeArray[] = '
95 script_ended = 0;
96 function jumpToUrl(URL) {
97 document.location = URL;
98 }
99 if ($("#task-list")) {
100 Event.observe(document, "dom:loaded", function(){
101 var changeEffect;
102 Sortable.create("task-list", { handles:$$("#task-list .drag"), tag: "li", ghosting:false, overlap:"vertical", constraint:false,
103 onChange: function(item) {
104 var list = Sortable.options(item).element;
105 // deactivate link
106 $$("#task-list a").each(function(link) {
107 link.writeAttribute("onclick","return false;");
108 });
109
110 },
111
112 onUpdate: function(list) {
113 new Ajax.Request("ajax.php", {
114 method: "post",
115 parameters: { ajaxID :"Taskcenter::saveSortingState", data: Sortable.serialize(list)}
116 });
117 // activate link
118 Event.observe(window,"mouseup",function(){
119 $$("#task-list a").each(function(link) {
120 link.writeAttribute("onclick","");
121 });
122 });
123
124 }
125 });
126
127 $$("#taskcenter-menu .down").invoke("observe", "click", function(event){
128 var item = Event.element(event);
129 var itemParent = item.up();
130 item = item.next("div").next("div").next("div").next("div");
131
132 if (itemParent.hasClassName("expanded")) {
133 itemParent.removeClassName("expanded").addClassName("collapsed");
134 Effect.BlindUp(item, {duration : 0.5});
135 state = 1;
136 } else {
137 itemParent.removeClassName("collapsed").addClassName("expanded");
138 Effect.BlindDown(item, {duration : 0.5});
139 state = 0;
140 }
141 new Ajax.Request("ajax.php", {
142 parameters : "ajaxID=Taskcenter::saveCollapseState&item=" + itemParent.id + "&state=" + state
143 });
144 });
145 });
146 }
147 ';
148 $this->doc->postCode='
149 <script language="javascript" type="text/javascript">
150 script_ended = 1;
151 if (top.fsMod) {
152 top.fsMod.recentIds["web"] = 0;
153 }
154 </script>
155 ';
156
157 // Render content depending on the mode
158 $mode = (string)$this->MOD_SETTINGS['mode'];
159 if ($mode == 'information') {
160 $this->renderInformationContent();
161 } else {
162 $this->renderModuleContent();
163 }
164
165 // compile document
166 $markers['FUNC_MENU'] = t3lib_BEfunc::getFuncMenu(
167 0,
168 'SET[mode]',
169 $this->MOD_SETTINGS['mode'],
170 $this->MOD_MENU['mode']
171 );
172 $markers['CONTENT'] = $this->content;
173
174 // Build the <body> for the module
175 $this->content = $this->doc->startPage($GLOBALS['LANG']->getLL('title'));
176 $this->content .= $this->doc->moduleBody($this->pageinfo, $docHeaderButtons, $markers);
177 $this->content .= $this->doc->endPage();
178 $this->content = $this->doc->insertStylesAndJS($this->content);
179 }
180
181 /**
182 * Prints out the module's HTML
183 *
184 * @return void
185 */
186 public function printContent() {
187 echo $this->content;
188 }
189
190 /**
191 * Generates the module content by calling the selected task
192 *
193 * @return void
194 */
195 protected function renderModuleContent() {
196 $title = $content = $actionContent = '';
197 $chosenTask = (string)$this->MOD_SETTINGS['function'];
198
199 // render the taskcenter task as default
200 if (empty($chosenTask) || $chosenTask == 'index') {
201 $chosenTask = 'taskcenter.tasks';
202 }
203
204 // remder the task
205 list($extKey, $taskClass) = explode('.', $chosenTask, 2);
206 $title = $GLOBALS['LANG']->sL($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'][$extKey][$taskClass]['title']);
207
208 if (class_exists($taskClass)) {
209 $taskInstance = t3lib_div::makeInstance($taskClass, $this);
210
211 if ($taskInstance instanceof tx_taskcenter_Task) {
212 // check if the task is restricted to admins only
213 if ($this->checkAccess($extKey, $taskClass)) {
214 $actionContent .= $taskInstance->getTask();
215 } else {
216 $flashMessage = t3lib_div::makeInstance(
217 't3lib_FlashMessage',
218 $GLOBALS['LANG']->getLL('error-access', TRUE),
219 $GLOBALS['LANG']->getLL('error_header'),
220 t3lib_FlashMessage::ERROR
221 );
222 $actionContent .= $flashMessage->render();
223 }
224 } else {
225 // error if the task is not an instance of tx_taskcenter_Task
226 $flashMessage = t3lib_div::makeInstance(
227 't3lib_FlashMessage',
228 sprintf($GLOBALS['LANG']->getLL('error_no-instance', TRUE), $taskClass, 'tx_taskcenter_Task'),
229 $GLOBALS['LANG']->getLL('error_header'),
230 t3lib_FlashMessage::ERROR
231 );
232 $actionContent .= $flashMessage->render();
233 }
234 } else {
235 $flashMessage = t3lib_div::makeInstance(
236 't3lib_FlashMessage',
237 $GLOBALS['LANG']->sL('LLL:EXT:taskcenter/task/locallang_mod.xml:mlang_labels_tabdescr'),
238 $GLOBALS['LANG']->sL('LLL:EXT:taskcenter/task/locallang_mod.xml:mlang_tabs_tab'),
239 t3lib_FlashMessage::INFO
240 );
241 $actionContent .= $flashMessage->render();
242 }
243
244 $content = '<div id="taskcenter-main">
245 <div id="taskcenter-menu">' . $this->indexAction() . '</div>
246 <div id="taskcenter-item" class="' . htmlspecialchars($extKey . '-' . $taskClass) . '">' .
247 $actionContent . '
248 </div>
249 </div>';
250
251 $this->content .= $content;
252 }
253
254 /**
255 * Generates the information content
256 *
257 * @return void
258 */
259 protected function renderInformationContent() {
260 $content = $this->description (
261 $GLOBALS['LANG']->getLL('mlang_tabs_tab'),
262 $GLOBALS['LANG']->sL('LLL:EXT:taskcenter/task/locallang_mod.xml:mlang_labels_tabdescr')
263 );
264
265 $content .= $GLOBALS['LANG']->getLL('taskcenter-about');
266
267 if ($GLOBALS['BE_USER']->isAdmin()) {
268 $content .= '<br /><br />' . $this->description (
269 $GLOBALS['LANG']->getLL('taskcenter-adminheader'),
270 $GLOBALS['LANG']->getLL('taskcenter-admin')
271 );
272 }
273
274 $this->content .= $content;
275 }
276
277 /**
278 * Render the headline of a task including a title and an optional description.
279 *
280 * @param string $title: Title
281 * @param string $description: Description
282 * @return string formatted title and description
283 */
284 public function description($title, $description='') {
285 if (!empty($description)) {
286 $description = '<p class="description">' . nl2br(htmlspecialchars($description)) . '</p><br />';
287 }
288 $content = $this->doc->section($title, $description, FALSE, TRUE);
289
290 return $content;
291 }
292
293 /**
294 * Render a list of items as a nicely formated definition list including a
295 * link, icon, title and description.
296 * The keys of a single item are:
297 * - title: Title of the item
298 * - link: Link to the task
299 * - icon: Path to the icon or Icon as HTML if it begins with <img
300 * - description: Description of the task, using htmlspecialchars()
301 * - descriptionHtml: Description allowing HTML tags which will override the
302 * description
303 *
304 * @param array $items: List of items to be displayed in the definition list.
305 * @param boolean $mainMenu: Set it to TRUE to render the main menu
306 * @return string definition list
307 */
308 public function renderListMenu($items, $mainMenu = FALSE) {
309 $content = $section = '';
310 $count = 0;
311
312 // change the sorting of items to the user's one
313 if ($mainMenu) {
314 $userSorting = unserialize($GLOBALS['BE_USER']->uc['taskcenter']['sorting']);
315 if (is_array($userSorting)) {
316 $newSorting = array();
317 foreach($userSorting as $item) {
318 if(isset($items[$item])) {
319 $newSorting[] = $items[$item];
320 unset($items[$item]);
321 }
322 }
323 $items = $newSorting + $items;
324 }
325 }
326
327 if (is_array($items) && count($items) > 0) {
328 foreach($items as $item) {
329 $title = htmlspecialchars($item['title']);
330
331 $icon = $additionalClass = $collapsedStyle = '';
332 // Check for custom icon
333 if (!empty($item['icon'])) {
334 if (strpos($item['icon'], '<img ') === FALSE) {
335 $absIconPath = t3lib_div::getFileAbsFilename($item['icon']);
336 // If the file indeed exists, assemble relative path to it
337 if (file_exists($absIconPath)) {
338 $icon = $GLOBALS['BACK_PATH'] . '../' . str_replace(PATH_site, '', $absIconPath);
339 $icon = '<img src="' . $icon . '" title="' . $title . '" alt="' . $title . '" />';
340 }
341 if (@is_file($icon)) {
342 $icon = '<img' . t3lib_iconworks::skinImg($GLOBALS['BACK_PATH'], $icon, 'width="16" height="16"') . ' title="' . $title . '" alt="' . $title . '" />';
343 }
344 } else {
345 $icon = $item['icon'];
346 }
347 }
348
349
350 $description = (!empty($item['descriptionHtml'])) ? $item['descriptionHtml'] : '<p>' . nl2br(htmlspecialchars($item['description'])) . '</p>';
351
352 $id = $this->getUniqueKey($item['uid']);
353
354 // collapsed & expanded menu items
355 if ($mainMenu && isset($GLOBALS['BE_USER']->uc['taskcenter']['states'][$id]) && $GLOBALS['BE_USER']->uc['taskcenter']['states'][$id]) {
356 $collapsedStyle = 'style="display:none"';
357 $additionalClass = 'collapsed';
358 } else {
359 $additionalClass = 'expanded';
360 }
361
362 // first & last menu item
363 if ($count == 0) {
364 $additionalClass .= ' first-item';
365 } elseif ($count + 1 === count($items)) {
366 $additionalClass .= ' last-item';
367 }
368
369 // active menu item
370 $active = ((string) $this->MOD_SETTINGS['function'] == $item['uid']) ? ' active-task' : '';
371
372 // Main menu: Render additional syntax to sort tasks
373 if ($mainMenu) {
374 $dragIcon = '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'], 'gfx/move.gif', 'width="16" height="16" hspace="2"') . ' title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.move', 1) . '" alt="" />';
375 $section = '<div class="down">&nbsp;</div>
376 <div class="drag">' . $dragIcon . '</div>';
377 $backgroundClass = 't3-row-header ';
378 }
379
380 $content .= '<li class="' . $additionalClass . $active . '" id="el_' .$id . '">
381 ' . $section . '
382 <div class="image">' . $icon . '</div>
383 <div class="' . $backgroundClass . 'link"><a href="' . $item['link'] . '">' . $title . '</a></div>
384 <div class="content " ' . $collapsedStyle . '>' . $description . '</div>
385 </li>';
386
387 $count++;
388 }
389
390 $navigationId = ($mainMenu) ? 'id="task-list"' : '';
391
392 $content = '<ul ' . $navigationId . ' class="task-list">' . $content . '</ul>';
393
394 }
395
396 return $content;
397 }
398
399 /**
400 * Shows an overview list of available reports.
401 *
402 * @return string list of available reports
403 */
404 protected function indexAction() {
405 $content = '';
406 $tasks = array();
407 $icon = t3lib_extMgm::extRelPath('taskcenter') . 'task/task.gif';
408
409 // render the tasks only if there are any available
410 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter']) && count($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter']) > 0) {
411 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'] as $extKey => $extensionReports) {
412 foreach ($extensionReports as $taskClass => $task) {
413 if (!$this->checkAccess($extKey, $taskClass)) {
414 continue;
415 }
416 $link = 'mod.php?M=user_task&SET[function]=' . $extKey . '.' . $taskClass;
417 $taskTitle = $GLOBALS['LANG']->sL($task['title']);
418 $taskDescriptionHtml = '';
419
420 // Check for custom icon
421 if (!empty($task['icon'])) {
422 $icon = t3lib_div::getFileAbsFilename($task['icon']);
423 }
424
425 if (class_exists($taskClass)) {
426 $taskInstance = t3lib_div::makeInstance($taskClass, $this);
427 if ($taskInstance instanceof tx_taskcenter_Task) {
428 $taskDescriptionHtml = $taskInstance->getOverview();
429 }
430 }
431
432 // generate an array of all tasks
433 $uniqueKey = $this->getUniqueKey($extKey . '.' . $taskClass);
434 $tasks[$uniqueKey] = array(
435 'title' => $taskTitle,
436 'descriptionHtml' => $taskDescriptionHtml,
437 'description' => $GLOBALS['LANG']->sL($task['description']),
438 'icon' => $icon,
439 'link' => $link,
440 'uid' => $extKey . '.' . $taskClass
441 );
442 }
443 }
444
445 $content .= $this->renderListMenu($tasks, TRUE);
446 } else {
447 $flashMessage = t3lib_div::makeInstance(
448 't3lib_FlashMessage',
449 $GLOBALS['LANG']->getLL('no-tasks', TRUE),
450 '',
451 t3lib_FlashMessage::INFO
452 );
453 $this->content .= $flashMessage->render();
454 }
455
456 return $content;
457 }
458
459 /**
460 * Create the panel of buttons for submitting the form or otherwise
461 * perform operations.
462 *
463 * @return array all available buttons as an assoc. array
464 */
465 protected function getButtons() {
466 $buttons = array(
467 'csh' => t3lib_BEfunc::cshItem('_MOD_web_func', '', $GLOBALS['BACK_PATH']),
468 'shortcut' => '',
469 'open_new_window' => $this->openInNewWindow()
470 );
471
472 // Shortcut
473 if ($GLOBALS['BE_USER']->mayMakeShortcut()) {
474 $buttons['shortcut'] = $this->doc->makeShortcutIcon('', 'function', $this->MCONF['name']);
475 }
476
477 return $buttons;
478 }
479
480 /**
481 * Check the access to a task. Considered are:
482 * - Admins are always allowed
483 * - Tasks can be restriced to admins only
484 * - Tasks can be blinded for Users with TsConfig taskcenter.<extensionkey>.<taskName> = 0
485 *
486 * @param string $extKey: Extension key
487 * @param string $taskClass: Name of the task
488 * @return boolean Access to the task allowed or not
489 */
490 protected function checkAccess($extKey, $taskClass) {
491 // check if task is blinded with TsConfig (taskcenter.<extkey>.<taskName>
492 $tsConfig = $GLOBALS['BE_USER']->getTSConfig('taskcenter.' . $extKey . '.' . $taskClass);
493 if (isset($tsConfig['value']) && intval($tsConfig['value']) == 0) {
494 return FALSE;
495 }
496
497 // admins are always allowed
498 if ($GLOBALS['BE_USER']->isAdmin()) {
499 return TRUE;
500 }
501
502 // check if task is restricted to admins
503 if (intval($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['taskcenter'][$extKey][$taskClass]['admin']) == 1) {
504 return FALSE;
505 }
506
507 return TRUE;
508 }
509
510 /**
511 * Returns HTML code to dislay an url in an iframe at the right side of the taskcenter
512 *
513 * @param string $url: url to display
514 * @param int $max:
515 * @return string code that inserts the iframe (HTML)
516 */
517 public function urlInIframe($url, $max=0) {
518 $this->doc->JScodeArray[] =
519 'function resizeIframe(frame,max) {
520 var parent = $("typo3-docbody");
521 var parentHeight = $(parent).getHeight() - 0;
522 var parentWidth = $(parent).getWidth() - $("taskcenter-menu").getWidth() - 50;
523 $("list_frame").setStyle({height: parentHeight+"px", width: parentWidth+"px"});
524
525 }
526 // event crashes IE6 so he is excluded first
527 var version = parseFloat(navigator.appVersion.split(";")[1].strip().split(" ")[1]);
528 if (!(Prototype.Browser.IE && version == 6)) {
529 Event.observe(window, "resize", resizeIframe, false);
530 }';
531
532 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>';
533 }
534
535 /**
536 * Create a unique key from a string which can be used in Prototype's Sortable
537 * Therefore '_' are replaced
538 *
539 * @param string $string: string which is used to generate the identifier
540 * @return string modified string
541 */
542 protected function getUniqueKey($string) {
543 $search = array('.', '_');
544 $replace = array('-', '');
545
546 return str_replace($search, $replace, $string);
547 }
548
549 /**
550 * This method prepares the link for opening the devlog in a new window
551 *
552 * @return string Hyperlink with icon and appropriate JavaScript
553 */
554 protected function openInNewWindow() {
555 $url = t3lib_div::getIndpEnv('TYPO3_REQUEST_SCRIPT');
556 $onClick = "devlogWin=window.open('" . $url . "','taskcenter','width=790,status=0,menubar=1,resizable=1,location=0,scrollbars=1,toolbar=0');return false;";
557 $content = '<a href="#" onclick="' . htmlspecialchars($onClick).'">' .
558 '<img' . t3lib_iconWorks::skinImg($GLOBALS['BACK_PATH'],'gfx/open_in_new_window.gif', 'width="19" height="14"') . ' title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.php:labels.openInNewWindow', 1) . '" class="absmiddle" alt="" />' .
559 '</a>';
560 return $content;
561 }
562
563
564 }
565
566
567 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/taskcenter/task/index.php']) {
568 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/taskcenter/task/index.php']);
569 }
570
571
572
573 // Make instance:
574 $SOBE = t3lib_div::makeInstance('SC_mod_user_task_index');
575 // Include files?
576 foreach($SOBE->include_once as $INC_FILE) {
577 include_once($INC_FILE);
578 }
579
580 $SOBE->main();
581 $SOBE->printContent();
582
583 ?>