Completed
Push — master ( a12ac5...2d0854 )
by Craig
06:25
created

SettingsModuleInstaller   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 157
rs 10
c 0
b 0
f 0
wmc 22

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A uninstall() 0 4 1
A setGuestTimeZone() 0 15 4
A setSystemVar() 0 3 1
B upgrade() 0 37 8
A install() 0 35 2
A getDefaultValue() 0 18 4
A getSystemVar() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\SettingsModule;
15
16
use DateTimeZone;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use Symfony\Contracts\Translation\TranslatorInterface;
20
use Zikula\Bundle\CoreBundle\Doctrine\Helper\SchemaHelper;
21
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
22
use Zikula\ExtensionsModule\AbstractExtension;
23
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
24
use Zikula\ExtensionsModule\Api\VariableApi;
25
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
26
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
27
28
class SettingsModuleInstaller extends AbstractExtensionInstaller
29
{
30
    /**
31
     * @var LocaleApiInterface
32
     */
33
    private $localeApi;
34
35
    /**
36
     * @var string
37
     */
38
    private $locale;
39
40
    public function __construct(
41
        LocaleApiInterface $localeApi,
42
        $locale,
43
        AbstractExtension $extension,
44
        ManagerRegistry $managerRegistry,
45
        SchemaHelper $schemaTool,
46
        RequestStack $requestStack,
47
        TranslatorInterface $translator,
48
        VariableApiInterface $variableApi
49
    ) {
50
        $this->localeApi = $localeApi;
51
        $this->locale = $locale;
52
        parent::__construct($extension, $managerRegistry, $schemaTool, $requestStack, $translator, $variableApi);
53
    }
54
55
    public function install(): bool
56
    {
57
        $this->setSystemVar('startdate', date('m/Y'));
58
        $this->setSystemVar('adminmail', '[email protected]');
59
        $this->setSystemVar('Default_Theme', 'ZikulaBootstrapTheme');
60
        $this->setSystemVar('timezone', date_default_timezone_get());
61
        $this->setSystemVar('Version_Num', ZikulaKernel::VERSION);
62
        $this->setSystemVar('multilingual', '1');
63
        $this->setSystemVar('theme_change', '0');
64
        $this->setSystemVar('UseCompression', '0');
65
        $this->setSystemVar('siteoff', 0);
66
        $this->setSystemVar('siteoffreason');
67
        $this->setSystemVar('language_detect', 0);
68
69
        // Multilingual support
70
        foreach ($this->localeApi->getSupportedLocales() as $lang) {
71
            $this->setSystemVar('sitename_' . $lang, $this->trans('Site name'));
72
            $this->setSystemVar('slogan_' . $lang, $this->trans('Site description'));
73
            $this->setSystemVar('defaultpagetitle_' . $lang, $this->trans('Site name'));
74
            $this->setSystemVar('defaultmetadescription_' . $lang, $this->trans('Site description'));
75
            $this->setSystemVar('startController_' . $lang, $this->getDefaultValue('startController'));
76
        }
77
78
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_PROFILE_MODULE);
79
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_MESSAGE_MODULE);
80
        $this->setSystemVar('languageurl', 0);
81
        $this->setSystemVar('ajaxtimeout', 5000);
82
        //! this is a comma-separated list of special characters to search for in permalinks
83
        $this->setSystemVar('permasearch', $this->getDefaultValue('permasearch'));
84
        //! this is a comma-separated list of special characters to replace in permalinks
85
        $this->setSystemVar('permareplace', $this->getDefaultValue('permareplace'));
86
87
        $this->setSystemVar('locale', $this->locale);
88
89
        return true;
90
    }
91
92
    public function upgrade(string $oldVersion): bool
93
    {
94
        switch ($oldVersion) {
95
            case '2.9.11': // shipped with Core-1.4.3
96
                $this->setSystemVar('UseCompression', (bool)$this->getSystemVar('UseCompression'));
97
            case '2.9.12': // shipped with Core-1.4.4
98
                // reconfigure TZ settings
99
                $this->setGuestTimeZone();
100
            case '2.9.13':
101
                $this->getVariableApi()->del(VariableApi::CONFIG, 'entrypoint');
102
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturlsstripentrypoint');
103
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturls');
104
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturlsdefaultmodule');
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
105
            case '2.9.14': // shipped with Core-1.5.x + Core-2.0.15
106
                $this->getVariableApi()->del(VariableApi::CONFIG, 'Version_Sub');
107
                $this->setSystemVar('startController'); // reset to blank because of new format FQCN::method
108
            case '2.9.15':
109
                $varsToRemove = [
110
                    'funtext',
111
                    'reportlevel',
112
                    'idnnames',
113
                    'debug',
114
                    'debug_sql',
115
                    'useflags',
116
                    'language_i18n',
117
                    'startController',
118
                    'startargs'
119
                ];
120
                foreach ($varsToRemove as $varName) {
121
                    $this->getVariableApi()->del(VariableApi::CONFIG, $varName);
122
                }
123
                foreach ($this->localeApi->getSupportedLocales() as $lang) {
124
                    $this->setSystemVar('startController_' . $lang, $this->getDefaultValue('startController'));
125
                }
126
        }
127
128
        return true;
129
    }
130
131
    public function uninstall(): bool
132
    {
133
        // This module cannot be uninstalled.
134
        return false;
135
    }
136
137
    /**
138
     * @return string|array|null
139
     */
140
    private function getDefaultValue(string $name)
141
    {
142
        if ('permasearch' === $name) {
143
            return $this->trans('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü');
144
        }
145
        if ('permareplace' === $name) {
146
            return $this->trans('A,A,A,A,A,a,a,a,a,a,O,O,O,O,O,o,o,o,o,o,E,E,E,E,e,e,e,e,C,c,I,I,I,I,i,i,i,i,U,U,U,u,u,u,y,N,n,ss,ae,Ae,oe,Oe,ue,Ue');
147
        }
148
        if ('startController' === $name) {
149
            return [
150
                'controller' => '',
151
                'query' => '',
152
                'request' => '',
153
                'attributes' => ''
154
            ];
155
        }
156
157
        return null;
158
    }
159
160
    private function setSystemVar(string $name, $value = ''): void
161
    {
162
        $this->getVariableApi()->set(VariableApi::CONFIG, $name, $value);
163
    }
164
165
    private function getSystemVar(string $name)
166
    {
167
        return $this->getVariableApi()->getSystemVar($name);
168
    }
169
170
    private function setGuestTimeZone(): void
171
    {
172
        $existingOffset = $this->getSystemVar('timezone_offset');
173
        $actualOffset = (float) $existingOffset * 60; // express in minutes
174
        $timezoneAbbreviations = DateTimeZone::listAbbreviations();
175
        $timeZone = date_default_timezone_get();
176
        foreach ($timezoneAbbreviations as $abbreviation => $zones) {
177
            foreach ($zones as $zone) {
178
                if ($zone['offset'] === $actualOffset) {
179
                    $timeZone = $zone['timezone_id'];
180
                    break 2;
181
                }
182
            }
183
        }
184
        $this->setSystemVar('timezone', $timeZone);
185
    }
186
}
187