d7c590d498ab3205a33901604e0e35cbcb636058
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / View / ModuleMenuView.php
1 <?php
2 namespace TYPO3\CMS\Backend\View;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2007-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * class to render the TYPO3 backend menu for the modules
34 *
35 * @author Ingo Renner <ingo@typo3.org>
36 */
37 class ModuleMenuView {
38
39 /**
40 * Module loading object
41 *
42 * @var \TYPO3\CMS\Backend\Module\ModuleLoader
43 */
44 protected $moduleLoader;
45
46 protected $backPath;
47
48 protected $linkModules;
49
50 protected $loadedModules;
51
52 /**
53 * Constructor, initializes several variables
54 */
55 public function __construct() {
56 if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_AJAX) {
57 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_misc.xlf');
58 }
59 $this->backPath = '';
60 $this->linkModules = TRUE;
61 // Loads the backend modules available for the logged in user.
62 $this->moduleLoader = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Module\\ModuleLoader');
63 $this->moduleLoader->observeWorkspaces = TRUE;
64 $this->moduleLoader->load($GLOBALS['TBE_MODULES']);
65 $this->loadedModules = $this->moduleLoader->modules;
66 }
67
68 /**
69 * sets the path back to /typo3/
70 *
71 * @param string $backPath Path back to /typo3/
72 * @throws \InvalidArgumentException
73 * @return void
74 */
75 public function setBackPath($backPath) {
76 if (!is_string($backPath)) {
77 throw new \InvalidArgumentException('parameter $backPath must be of type string', 1193315266);
78 }
79 $this->backPath = $backPath;
80 }
81
82 /**
83 * loads the collapse states for the main modules from user's configuration (uc)
84 *
85 * @return array Collapse states
86 */
87 protected function getCollapsedStates() {
88 $collapsedStates = array();
89 if ($GLOBALS['BE_USER']->uc['moduleData']['moduleMenu']) {
90 $collapsedStates = $GLOBALS['BE_USER']->uc['moduleData']['moduleMenu'];
91 }
92 return $collapsedStates;
93 }
94
95 /**
96 * ModuleMenu Store loading data
97 *
98 * @param array $params
99 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj
100 * @return array
101 */
102 public function getModuleData($params, $ajaxObj) {
103 $data = array('success' => TRUE, 'root' => array());
104 $rawModuleData = $this->getRawModuleData();
105 $index = 0;
106 foreach ($rawModuleData as $moduleKey => $moduleData) {
107 $key = substr($moduleKey, 8);
108 $num = count($data['root']);
109 if ($moduleData['link'] != 'dummy.php' || $moduleData['link'] == 'dummy.php' && is_array($moduleData['subitems'])) {
110 $data['root'][$num]['key'] = $key;
111 $data['root'][$num]['menuState'] = $GLOBALS['BE_USER']->uc['moduleData']['menuState'][$moduleKey];
112 $data['root'][$num]['label'] = $moduleData['title'];
113 $data['root'][$num]['subitems'] = is_array($moduleData['subitems']) ? count($moduleData['subitems']) : 0;
114 if ($moduleData['link'] && $this->linkModules) {
115 $data['root'][$num]['link'] = 'top.goToModule(\'' . $moduleData['name'] . '\')';
116 }
117 // Traverse submodules
118 if (is_array($moduleData['subitems'])) {
119 foreach ($moduleData['subitems'] as $subKey => $subData) {
120 $data['root'][$num]['sub'][] = array(
121 'name' => $subData['name'],
122 'description' => $subData['description'],
123 'label' => $subData['title'],
124 'icon' => $subData['icon']['filename'],
125 'navframe' => $subData['parentNavigationFrameScript'],
126 'link' => $subData['link'],
127 'originalLink' => $subData['originalLink'],
128 'index' => $index++,
129 'navigationFrameScript' => $subData['navigationFrameScript'],
130 'navigationFrameScriptParam' => $subData['navigationFrameScriptParam'],
131 'navigationComponentId' => $subData['navigationComponentId']
132 );
133 }
134 }
135 }
136 }
137 if ($ajaxObj) {
138 $ajaxObj->setContent($data);
139 $ajaxObj->setContentFormat('jsonbody');
140 } else {
141 return $data;
142 }
143 }
144
145 /**
146 * Returns the loaded modules
147 *
148 * @return array Array of loaded modules
149 */
150 public function getLoadedModules() {
151 return $this->loadedModules;
152 }
153
154 /**
155 * saves the menu's toggle state in the backend user's uc
156 *
157 * @param array $params Array of parameters from the AJAX interface, currently unused
158 * @param \TYPO3\CMS\Core\Http\AjaxRequestHandler $ajaxObj Object of type AjaxRequestHandler
159 * @return void
160 */
161 public function saveMenuState($params, $ajaxObj) {
162 $menuItem = GeneralUtility::_POST('menuid');
163 $state = GeneralUtility::_POST('state') === 'true' ? 1 : 0;
164 $GLOBALS['BE_USER']->uc['moduleData']['menuState'][$menuItem] = $state;
165 $GLOBALS['BE_USER']->writeUC();
166 }
167
168 /**
169 * gets the raw module data
170 *
171 * @return array Multi dimension array with module data
172 */
173 public function getRawModuleData() {
174 $modules = array();
175 // Remove the 'doc' module?
176 if ($GLOBALS['BE_USER']->getTSConfigVal('options.disableDocModuleInAB')) {
177 unset($this->loadedModules['doc']);
178 }
179 foreach ($this->loadedModules as $moduleName => $moduleData) {
180 $moduleLink = '';
181 if (!is_array($moduleData['sub'])) {
182 $moduleLink = $moduleData['script'];
183 }
184 $moduleLink = GeneralUtility::resolveBackPath($moduleLink);
185 $moduleKey = 'modmenu_' . $moduleName;
186 $moduleIcon = $this->getModuleIcon($moduleKey);
187 $modules[$moduleKey] = array(
188 'name' => $moduleName,
189 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$moduleName . '_tab'],
190 'onclick' => 'top.goToModule(\'' . $moduleName . '\');',
191 'icon' => $moduleIcon,
192 'link' => $moduleLink,
193 'description' => $GLOBALS['LANG']->moduleLabels['labels'][$moduleKey . 'label']
194 );
195 if (!is_array($moduleData['sub']) && $moduleData['script'] != 'dummy.php') {
196 // Work around for modules with own main entry, but being self the only submodule
197 $modules[$moduleKey]['subitems'][$moduleKey] = array(
198 'name' => $moduleName,
199 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$moduleName . '_tab'],
200 'onclick' => 'top.goToModule(\'' . $moduleName . '\');',
201 'icon' => $this->getModuleIcon($moduleName . '_tab'),
202 'link' => $moduleLink,
203 'originalLink' => $moduleLink,
204 'description' => $GLOBALS['LANG']->moduleLabels['labels'][$moduleKey . 'label'],
205 'navigationFrameScript' => NULL,
206 'navigationFrameScriptParam' => NULL,
207 'navigationComponentId' => NULL
208 );
209 } elseif (is_array($moduleData['sub'])) {
210 foreach ($moduleData['sub'] as $submoduleName => $submoduleData) {
211 $submoduleLink = GeneralUtility::resolveBackPath($submoduleData['script']);
212 $submoduleKey = $moduleName . '_' . $submoduleName . '_tab';
213 $submoduleIcon = $this->getModuleIcon($submoduleKey);
214 $submoduleDescription = $GLOBALS['LANG']->moduleLabels['labels'][$submoduleKey . 'label'];
215 $originalLink = $submoduleLink;
216 $modules[$moduleKey]['subitems'][$submoduleKey] = array(
217 'name' => $moduleName . '_' . $submoduleName,
218 'title' => $GLOBALS['LANG']->moduleLabels['tabs'][$submoduleKey],
219 'onclick' => 'top.goToModule(\'' . $moduleName . '_' . $submoduleName . '\');',
220 'icon' => $submoduleIcon,
221 'link' => $submoduleLink,
222 'originalLink' => $originalLink,
223 'description' => $submoduleDescription,
224 'navigationFrameScript' => $submoduleData['navFrameScript'],
225 'navigationFrameScriptParam' => $submoduleData['navFrameScriptParam'],
226 'navigationComponentId' => $submoduleData['navigationComponentId']
227 );
228 // if the main module has a navframe script, inherit to the submodule,
229 // but only if it is not disabled explicitly (option is set to FALSE)
230 if ($moduleData['navFrameScript'] && $submoduleData['inheritNavigationComponentFromMainModule'] !== FALSE) {
231 $modules[$moduleKey]['subitems'][$submoduleKey]['parentNavigationFrameScript'] = $moduleData['navFrameScript'];
232 }
233 }
234 }
235 }
236 return $modules;
237 }
238
239 /**
240 * gets the module icon and its size
241 *
242 * @param string $moduleKey Module key
243 * @return array Icon data array with 'filename', 'size', and 'html'
244 */
245 protected function getModuleIcon($moduleKey) {
246 $icon = array(
247 'filename' => '',
248 'size' => '',
249 'title' => '',
250 'html' => ''
251 );
252 $iconFileRelative = $this->getModuleIconRelative($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleKey]);
253 $iconFileAbsolute = $this->getModuleIconAbsolute($GLOBALS['LANG']->moduleLabels['tabs_images'][$moduleKey]);
254 $iconSizes = @getimagesize($iconFileAbsolute);
255 $iconTitle = $GLOBALS['LANG']->moduleLabels['tabs'][$moduleKey];
256 if (!empty($iconFileRelative)) {
257 $icon['filename'] = $iconFileRelative;
258 $icon['size'] = $iconSizes[3];
259 $icon['title'] = htmlspecialchars($iconTitle);
260 $icon['html'] = '<img src="' . $iconFileRelative . '" ' . $iconSizes[3] . ' title="' . htmlspecialchars($iconTitle) . '" alt="' . htmlspecialchars($iconTitle) . '" />';
261 }
262 return $icon;
263 }
264
265 /**
266 * Returns the filename readable for the script from PATH_typo3.
267 * That means absolute names are just returned while relative names are
268 * prepended with the path pointing back to typo3/ dir
269 *
270 * @param string $iconFilename Icon filename
271 * @return string Icon filename with absolute path
272 * @see getModuleIconRelative()
273 */
274 protected function getModuleIconAbsolute($iconFilename) {
275 if (!GeneralUtility::isAbsPath($iconFilename)) {
276 $iconFilename = $this->backPath . $iconFilename;
277 }
278 return $iconFilename;
279 }
280
281 /**
282 * Returns relative path to the icon filename for use in img-tags
283 *
284 * @param string $iconFilename Icon filename
285 * @return string Icon filename with relative path
286 * @see getModuleIconAbsolute()
287 */
288 protected function getModuleIconRelative($iconFilename) {
289 if (GeneralUtility::isAbsPath($iconFilename)) {
290 $iconFilename = '../' . substr($iconFilename, strlen(PATH_site));
291 }
292 return $this->backPath . $iconFilename;
293 }
294
295 /**
296 * Appends a '?' if there is none in the string already
297 *
298 * @param string $link Link URL
299 * @return string Link URl appended with ? if there wasn't one
300 */
301 protected function appendQuestionmarkToLink($link) {
302 if (!strstr($link, '?')) {
303 $link .= '?';
304 }
305 return $link;
306 }
307
308 /**
309 * renders the logout button form
310 *
311 * @return string Html code snippet displaying the logout button
312 */
313 public function renderLogoutButton() {
314 $buttonLabel = $GLOBALS['BE_USER']->user['ses_backuserid'] ? 'LLL:EXT:lang/locallang_core.xlf:buttons.exit' : 'LLL:EXT:lang/locallang_core.xlf:buttons.logout';
315 $buttonForm = '
316 <form action="logout.php" target="_top">
317 <input type="submit" id="logout-submit-button" value="' . $GLOBALS['LANG']->sL($buttonLabel, TRUE) . '" />
318 </form>';
319 return $buttonForm;
320 }
321
322 /**
323 * turns linking of modules on or off
324 *
325 * @param boolean $linkModules Status for linking modules with a-tags, set to FALSE to turn lining off
326 * @throws \InvalidArgumentException
327 * @return void
328 */
329 public function setLinkModules($linkModules) {
330 if (!is_bool($linkModules)) {
331 throw new \InvalidArgumentException('parameter $linkModules must be of type bool', 1193326558);
332 }
333 $this->linkModules = $linkModules;
334 }
335
336 }