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