a140075f9968d7cffe8d5481ecaea6e38cb8c034
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Modules / AbstractModule.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Modules;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Adminpanel\Service\ConfigurationService;
21 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Localization\LanguageService;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26 /**
27 * Abstract base class for Admin Panel Modules containing helper methods and default interface implementations
28 *
29 * Extend this class when writing own admin panel modules (or implement the Interface directly)
30 */
31 abstract class AbstractModule implements AdminPanelModuleInterface
32 {
33
34 /**
35 * @var AdminPanelSubModuleInterface[]
36 */
37 protected $subModules = [];
38
39 /**
40 * Main Configuration (from UserTSConfig, admPanel)
41 *
42 * @var array
43 */
44 protected $mainConfiguration;
45
46 /**
47 * @var ConfigurationService
48 */
49 protected $configurationService;
50
51 public function __construct()
52 {
53 $this->configurationService = GeneralUtility::makeInstance(ConfigurationService::class);
54 $this->mainConfiguration = $this->configurationService->getMainConfiguration();
55 }
56
57 /**
58 * @inheritdoc
59 */
60 public function getSettings(): string
61 {
62 return '';
63 }
64
65 /**
66 * @inheritdoc
67 */
68 public function getIconIdentifier(): string
69 {
70 return '';
71 }
72
73 /**
74 * @inheritdoc
75 */
76 public function initializeModule(ServerRequestInterface $request): void
77 {
78 }
79
80 /**
81 * Returns true if the module is
82 * -> either enabled via TSConfig admPanel.enable
83 * -> or any setting is overridden
84 * override is a way to use functionality of the admin panel without displaying the admin panel to users
85 * for example: hidden records or pages can be displayed by default
86 *
87 * @return bool
88 */
89 public function isEnabled(): bool
90 {
91 $identifier = $this->getIdentifier();
92 $result = $this->isEnabledViaTsConfig();
93 if ($this->mainConfiguration['override.'][$identifier] ?? false) {
94 $result = (bool)$this->mainConfiguration['override.'][$identifier];
95 }
96 return $result;
97 }
98
99 /**
100 * @inheritdoc
101 */
102 public function onSubmit(array $input, ServerRequestInterface $request): void
103 {
104 }
105
106 /**
107 * Returns the current BE user.
108 *
109 * @return BackendUserAuthentication|FrontendBackendUserAuthentication
110 */
111 protected function getBackendUser(): BackendUserAuthentication
112 {
113 return $GLOBALS['BE_USER'];
114 }
115
116 /**
117 * Returns LanguageService
118 *
119 * @return LanguageService
120 */
121 protected function getLanguageService(): LanguageService
122 {
123 return $GLOBALS['LANG'];
124 }
125
126 /**
127 * Returns true if TSConfig admPanel.enable is set for this module (or all modules)
128 *
129 * @return bool
130 */
131 protected function isEnabledViaTsConfig(): bool
132 {
133 $result = false;
134 $identifier = $this->getIdentifier();
135 if (!empty($this->mainConfiguration['enable.']['all'])) {
136 $result = true;
137 } elseif (!empty($this->mainConfiguration['enable.'][$identifier])) {
138 $result = true;
139 }
140 return $result;
141 }
142
143 /**
144 * @return array
145 */
146 public function getJavaScriptFiles(): array
147 {
148 return [];
149 }
150
151 /**
152 * @inheritdoc
153 */
154 public function getCssFiles(): array
155 {
156 return [];
157 }
158
159 /**
160 * @inheritdoc
161 */
162 public function getShortInfo(): string
163 {
164 return '';
165 }
166
167 /**
168 * @inheritdoc
169 */
170 public function setSubModules(array $subModules): void
171 {
172 $this->subModules = $subModules;
173 }
174
175 /**
176 * @inheritdoc
177 */
178 public function getSubModules(): array
179 {
180 return $this->subModules;
181 }
182
183 /**
184 * @inheritdoc
185 */
186 public function getHasSubmoduleSettings(): bool
187 {
188 foreach ($this->subModules as $subModule) {
189 if (!empty($subModule->getSettings())) {
190 return true;
191 }
192 }
193 return false;
194 }
195 }