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