Passed
Push — master ( 2647c4...7ed9e9 )
by Carsten
02:02
created

Main::getShortContextName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace CarstenWindler\ContextBanner;
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
18
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Context Banner Renderer
23
 *
24
 * @author Carsten Windler <[email protected]>
25
 */
26
class Main
27
{
28
    const EXT_KEY = 'context_banner';
29
30
    /**
31
     * Extension key
32
     * @var string
33
     */
34
    private $extKey = self::EXT_KEY;
35
36
    /**
37
     * Extension configuration
38
     * @var array
39
     */
40
    private $conf = [];
41
42
    /**
43
     * Application context name
44
     * @var string
45
     */
46
    private $contextName;
47
48
    /**
49
     * @var array
50
     */
51
    private $toolbarIconBackgroundColors = [
52
        'Development' => '#00FF00',
53
        'Testing' => '#FFFF00',
54
        'Production' => '#FF0000'
55
    ];
56
57
    /**
58
     * @var array
59
     */
60
    private $shortContextNames = [
61
        'Development' => 'DEV',
62
        'Testing' => 'TEST',
63
        'Production' => 'PROD'
64
    ];
65
66 6
    public function __construct()
67
    {
68 6
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey])) {
69
            $this->setConf(
70
                unserialize(
71
                    $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$this->extKey],
72
                    [ 'allowed_classes' => false ]
73
                )
74
            );
75
        }
76
77 6
        $this->contextName = (string) GeneralUtility::getApplicationContext();
78 6
    }
79
80 6
    public function setConf(array $conf): self
81
    {
82 6
        $this->conf = array_merge($this->conf, $conf);
83
84 6
        return $this;
85
    }
86
87 2
    private function shouldFrontendBannerBeShownOnProduction(): bool
88
    {
89 2
        return (isset($this->conf['showBannerOnProduction']) &&
90 2
            $this->conf['showBannerOnProduction'] === 1);
91
    }
92
93 5
    public function isFrontendBannerShown(): bool
94
    {
95 5
        if ($this->contextName === 'Production' && !$this->shouldFrontendBannerBeShownOnProduction()) {
96 1
            return false;
97
        }
98
99 4
        return true;
100
    }
101
102
    protected function getBackendUser(): BackendUserAuthentication
103
    {
104
        return $GLOBALS['BE_USER'];
105
    }
106
107
    public function isToolbarItemShown(): bool
108
    {
109
        return $this->getBackendUser()->isAdmin();
110
    }
111
112
    /**
113
     * Hooked into contentPostProc_output
114
     */
115 5
    public function contentPostProcOutputHook(array &$params)
116
    {
117 5
        if (!$this->isFrontendBannerShown()) {
118 1
            return;
119
        }
120
121 4
        $feobj = &$params['pObj'];
122 4
        $outputArray = array();
123 4
        preg_match('/<body[^<]*>/', $feobj->content, $outputArray);
124
125
        // We expect the first occurence of <body> to be the correct one
126
        // there should be only one anyway
127 4
        $bodyTag = array_shift($outputArray);
128
129 4
        $feobj->content = str_replace($bodyTag, $bodyTag . $this->renderFrontendBanner(), $feobj->content);
130
131 4
        $outputArray = array();
132 4
        preg_match('/<title[^<]*>/', $feobj->content, $outputArray);
133
134
        // We expect the first occurence of <title> to be the correct one
135
        // there should be only one anyway
136 4
        $titleTag = array_shift($outputArray);
137
138 4
        $feobj->content = str_replace($titleTag, $titleTag . $this->getBannerText() . ' - ', $feobj->content);
139 4
    }
140
141
    /**
142
     * Hooked into backendRenderPreProcess
143
     */
144 1
    public function backendRenderPreProcessHook()
145
    {
146 1
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] = $this->getPageTitle();
147 1
    }
148
149 4
    private function getInlineCss(): string
150
    {
151 4
        $style = '';
152
153 4
        if ($this->conf['bannerStyle'] === 'auto') {
154 3
            $styleConfigName = 'bannerCss' . $this->contextName;
155
156 3
            if (!isset($this->conf[$styleConfigName])) {
157
                // should only happen if a new application context is introduced
158
                return '';
159
            }
160
161 3
            $style = $this->conf[$styleConfigName];
162
        }
163
164 4
        if ($this->conf['bannerStyle'] === 'custom' && isset($this->conf['bannerCssCustom'])) {
165 1
            $style = $this->conf['bannerCssCustom'];
166
        }
167
168 4
        return $style;
169
    }
170
171 4
    private function getBannerText(): string
172
    {
173 4
        $siteName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
174
175 4
        return str_replace(
176 4
            [ '###sitename###', '###context###' ],
177 4
            [ $siteName, $this->contextName ],
178 4
            $this->conf['bannerTemplate']
179
        );
180
    }
181
182 1
    private function getShortContextName(): string
183
    {
184 1
        if (!isset($this->shortContextNames[$this->contextName])) {
185
            return strtoupper(substr($this->contextName, 0, 3));
186
        }
187
188 1
        return $this->shortContextNames[$this->contextName];
189
    }
190
191 1
    private function getPageTitle(): string
192
    {
193 1
        $siteName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
194
195 1
        return str_replace(
196 1
            [ '###sitename###', '###context###' ],
197 1
            [ $siteName, $this->getShortContextName() ],
198 1
            $this->conf['pageTitleTemplate']
199
        );
200
    }
201
202 4
    public function renderFrontendBanner(): string
203
    {
204 4
        return '<div class="contextbanner" style="' . $this->getInlineCss() . '">' . $this->getBannerText() . '</div>';
205
    }
206
207
    public function renderToolbarItem(): string
208
    {
209
        $backgroundColor  = $this->toolbarIconBackgroundColors[$this->contextName] ?? '';
210
211
        return '<span style="color: #000000; background-color: ' . $backgroundColor .
212
            '" class="toolbar-item-link" title="Application context">' . strtoupper($this->contextName) . '</span>';
213
    }
214
}
215