d6d368d7342faea6a76fbc4f58eec70c2064352b
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Module / ModuleController.php
1 <?php
2 namespace TYPO3\CMS\Backend\Module;
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 /**
18 * Class with utility functions for module menu
19 *
20 * @author Susanne Moog <typo3@susannemoog.de>
21 */
22 class ModuleController {
23
24 /**
25 * @var \TYPO3\CMS\Backend\Module\ModuleStorage
26 */
27 protected $moduleMenu;
28
29 /**
30 * @var \TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository
31 */
32 protected $moduleMenuRepository;
33
34 /**
35 * Constructor
36 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, not in use, as everything can be done via the ModuleMenuRepository directly
37 */
38 public function __construct() {
39 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
40 $this->moduleMenu = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Module\ModuleStorage::class);
41 $this->moduleMenuRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Domain\Repository\Module\BackendModuleRepository::class);
42 }
43
44 /**
45 * This method creates the module menu if necessary
46 * afterwards you only need an instance of \TYPO3\CMS\Backend\Module\ModuleStorage
47 * to get the menu
48 *
49 * @return void
50 */
51 public function createModuleMenu() {
52 if (count($this->moduleMenu->getEntries()) === 0) {
53 /** @var $moduleMenu \TYPO3\CMS\Backend\View\ModuleMenuView */
54 $moduleMenu = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\View\ModuleMenuView::class);
55 $rawData = $moduleMenu->getRawModuleData();
56 $this->convertRawModuleDataToModuleMenuObject($rawData);
57 $this->createMenuEntriesForTbeModulesExt();
58 }
59 }
60
61 /**
62 * Creates the module menu object structure from the raw data array
63 *
64 * @param array $rawModuleData
65 * @see class.modulemenu.php getRawModuleData()
66 * @return void
67 */
68 protected function convertRawModuleDataToModuleMenuObject(array $rawModuleData) {
69 foreach ($rawModuleData as $module) {
70 $entry = $this->createEntryFromRawData($module);
71 if (isset($module['subitems']) && count($module['subitems']) > 0) {
72 foreach ($module['subitems'] as $subitem) {
73 $subEntry = $this->createEntryFromRawData($subitem);
74 $entry->addChild($subEntry);
75 }
76 }
77 $this->moduleMenu->attachEntry($entry);
78 }
79 }
80
81 /**
82 * Creates a menu entry object from an array
83 *
84 * @param array $module
85 * @return \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule
86 */
87 protected function createEntryFromRawData(array $module) {
88 /** @var $entry \TYPO3\CMS\Backend\Domain\Model\Module\BackendModule */
89 $entry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Domain\Model\Module\BackendModule::class);
90 if (!empty($module['name']) && is_string($module['name'])) {
91 $entry->setName($module['name']);
92 }
93 if (!empty($module['title']) && is_string($module['title'])) {
94 $entry->setTitle($this->getLanguageService()->sL($module['title']));
95 }
96 if (!empty($module['onclick']) && is_string($module['onclick'])) {
97 $entry->setOnClick($module['onclick']);
98 }
99 if (!empty($module['link']) && is_string($module['link'])) {
100 $entry->setLink($module['link']);
101 }
102 if (empty($module['link']) && !empty($module['path']) && is_string($module['path'])) {
103 $entry->setLink($module['path']);
104 }
105 if (!empty($module['description']) && is_string($module['description'])) {
106 $entry->setDescription($module['description']);
107 }
108 if (!empty($module['icon']) && is_array($module['icon'])) {
109 $entry->setIcon($module['icon']);
110 }
111 if (!empty($module['navigationComponentId']) && is_string($module['navigationComponentId'])) {
112 $entry->setNavigationComponentId($module['navigationComponentId']);
113 }
114 return $entry;
115 }
116
117 /**
118 * Creates the "third level" menu entries (submodules for the info module for
119 * example) from the TBE_MODULES_EXT array
120 *
121 * @return void
122 */
123 protected function createMenuEntriesForTbeModulesExt() {
124 foreach ($GLOBALS['TBE_MODULES_EXT'] as $mainModule => $tbeModuleExt) {
125 list($main) = explode('_', $mainModule);
126 $mainEntry = $this->moduleMenuRepository->findByModuleName($main);
127 if ($mainEntry !== FALSE) {
128 $subEntries = $mainEntry->getChildren();
129 if (count($subEntries) > 0) {
130 $matchingSubEntry = $this->moduleMenuRepository->findByModuleName($mainModule);
131 if ($matchingSubEntry !== FALSE) {
132 if (array_key_exists('MOD_MENU', $tbeModuleExt) && array_key_exists('function', $tbeModuleExt['MOD_MENU'])) {
133 foreach ($tbeModuleExt['MOD_MENU']['function'] as $subModule) {
134 $entry = $this->createEntryFromRawData($subModule);
135 $matchingSubEntry->addChild($entry);
136 }
137 }
138 }
139 }
140 }
141 }
142 }
143
144 /**
145 * Return language service instance
146 *
147 * @return \TYPO3\CMS\Lang\LanguageService
148 */
149 protected function getLanguageService() {
150 return $GLOBALS['LANG'];
151 }
152
153 }