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