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