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