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