[TASK] Decouple adminPanel from frontend
[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 TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
20 use TYPO3\CMS\Core\Http\ServerRequest;
21 use TYPO3\CMS\Core\Localization\LanguageService;
22
23 /**
24 * Abstract base class for Core Admin Panel Modules containing helper methods
25 *
26 * @internal
27 */
28 abstract class AbstractModule implements AdminPanelModuleInterface
29 {
30 /**
31 * @var string
32 */
33 protected $extResources = 'EXT:adminpanel/Resources/Private';
34
35 /**
36 * @var array
37 */
38 protected $mainConfiguration;
39
40 public function __construct()
41 {
42 $this->mainConfiguration = $this->getBackendUser()->getTSConfigProp('admPanel');
43 }
44
45 /**
46 * @inheritdoc
47 */
48 public function getAdditionalJavaScriptCode(): string
49 {
50 return '';
51 }
52
53 /**
54 * @inheritdoc
55 */
56 public function initializeModule(ServerRequest $request): void
57 {
58 }
59
60 /**
61 * Returns true if the module is
62 * -> either enabled via tsconfig admPanel.enable
63 * -> or any setting is overridden
64 * override is a way to use functionality of the admin panel without displaying the admin panel to users
65 * for example: hidden records or pages can be displayed by default
66 *
67 * @return bool
68 */
69 public function isEnabled(): bool
70 {
71 $identifier = $this->getIdentifier();
72 $result = $this->isEnabledViaTsConfig();
73 if ($this->mainConfiguration['override.'][$identifier] ?? false) {
74 $result = (bool)$this->mainConfiguration['override.'][$identifier];
75 }
76 return $result;
77 }
78
79 /**
80 * Uses the backend user session to determine if the module is open
81 *
82 * @return bool
83 */
84 public function isOpen(): bool
85 {
86 $option = 'display_' . $this->getIdentifier();
87 return isset($this->getBackendUser()->uc['TSFE_adminConfig'][$option])
88 ? (bool)$this->getBackendUser()->uc['TSFE_adminConfig'][$option]
89 : false;
90 }
91
92 /**
93 * Determines if the panel for this module is shown
94 * -> returns true if panel is enabled in TSConfig
95 *
96 * @see isEnabled()
97 * @return bool
98 */
99 public function isShown(): bool
100 {
101 return $this->isEnabledViaTsConfig();
102 }
103
104 /**
105 * @inheritdoc
106 */
107 public function onSubmit(array $input): void
108 {
109 }
110
111 /**
112 * @inheritdoc
113 */
114 public function showFormSubmitButton(): bool
115 {
116 return false;
117 }
118
119 /**
120 * Translate given key
121 *
122 * @param string $key Key for a label in the $LOCAL_LANG array of "sysext/lang/Resources/Private/Language/locallang_tsfe.xlf
123 * @param bool $convertWithHtmlspecialchars If TRUE the language-label will be sent through htmlspecialchars
124 * @return string The value for the $key
125 */
126 protected function extGetLL($key, $convertWithHtmlspecialchars = true): string
127 {
128 $labelStr = $this->getLanguageService()->getLL($key);
129 if ($convertWithHtmlspecialchars) {
130 $labelStr = htmlspecialchars($labelStr, ENT_QUOTES | ENT_HTML5);
131 }
132 return $labelStr;
133 }
134
135 /**
136 * Returns the current BE user.
137 *
138 * @return \TYPO3\CMS\Backend\FrontendBackendUserAuthentication
139 */
140 protected function getBackendUser(): FrontendBackendUserAuthentication
141 {
142 return $GLOBALS['BE_USER'];
143 }
144
145 /**
146 * Helper method to return configuration options
147 * Checks User TSConfig overrides and current backend user session
148 *
149 * @param string $option
150 * @return string
151 */
152 protected function getConfigurationOption(string $option): string
153 {
154 $beUser = $this->getBackendUser();
155 $identifier = $this->getIdentifier();
156
157 if ($option && isset($this->mainConfiguration['override.'][$identifier . '.'][$option])) {
158 $returnValue = $this->mainConfiguration['override.'][$identifier . '.'][$option];
159 } else {
160 $returnValue = $beUser->uc['TSFE_adminConfig'][$identifier . '_' . $option] ?? '';
161 }
162
163 return (string)$returnValue;
164 }
165
166 /**
167 * Returns LanguageService
168 *
169 * @return \TYPO3\CMS\Core\Localization\LanguageService
170 */
171 protected function getLanguageService(): LanguageService
172 {
173 return $GLOBALS['LANG'];
174 }
175
176 /**
177 * Returns true if TSConfig admPanel.enable is set for this module (or all modules)
178 *
179 * @return bool
180 */
181 protected function isEnabledViaTsConfig(): bool
182 {
183 $result = false;
184 $identifier = $this->getIdentifier();
185 if (!empty($this->mainConfiguration['enable.']['all'])) {
186 $result = true;
187 } elseif (!empty($this->mainConfiguration['enable.'][$identifier])) {
188 $result = true;
189 }
190 return $result;
191 }
192
193 /**
194 * @return array
195 */
196 public function getJavaScriptFiles(): array
197 {
198 return [];
199 }
200 }