Completed
Pull Request — master (#4281)
by Craig
04:38
created

SettingsModuleInstaller::upgrade()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 29
c 0
b 0
f 0
nc 32
nop 1
dl 0
loc 37
rs 8.2114
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
        // Initialisation successful
90
        return true;
91
    }
92
93
    public function upgrade(string $oldVersion): bool
94
    {
95
        switch ($oldVersion) {
96
            case '2.9.11': // shipped with Core-1.4.3
97
                $this->setSystemVar('UseCompression', (bool)$this->getSystemVar('UseCompression'));
98
            case '2.9.12': // shipped with Core-1.4.4
99
                // reconfigure TZ settings
100
                $this->setGuestTimeZone();
101
            case '2.9.13':
102
                $this->getVariableApi()->del(VariableApi::CONFIG, 'entrypoint');
103
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturlsstripentrypoint');
104
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturls');
105
                $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...
106
            case '2.9.14': // shipped with Core-1.5.x + Core-2.0.15
107
                $this->getVariableApi()->del(VariableApi::CONFIG, 'Version_Sub');
108
                $this->setSystemVar('startController'); // reset to blank because of new format FQCN::method
109
            case '2.9.15':
110
                $varsToRemove = [
111
                    'funtext',
112
                    'reportlevel',
113
                    'idnnames',
114
                    'debug',
115
                    'debug_sql',
116
                    'useflags',
117
                    'language_i18n',
118
                    'startController',
119
                    'startargs'
120
                ];
121
                foreach ($varsToRemove as $varName) {
122
                    $this->getVariableApi()->del(VariableApi::CONFIG, $varName);
123
                }
124
                foreach ($this->localeApi->getSupportedLocales() as $lang) {
125
                    $this->setSystemVar('startController_' . $lang, $this->getDefaultValue('startController'));
126
                }
127
        }
128
129
        return true;
130
    }
131
132
    public function uninstall(): bool
133
    {
134
        // This module cannot be uninstalled.
135
        return false;
136
    }
137
138
    /**
139
     * @return string|array|null
140
     */
141
    private function getDefaultValue(string $name)
142
    {
143
        if ('permasearch' === $name) {
144
            return $this->trans('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü');
145
        }
146
        if ('permareplace' === $name) {
147
            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');
148
        }
149
        if ('startController' === $name) {
150
            return [
151
                'controller' => '',
152
                'query' => '',
153
                'request' => '',
154
                'attributes' => ''
155
            ];
156
        }
157
158
        return null;
159
    }
160
161
    private function setSystemVar(string $name, $value = ''): void
162
    {
163
        $this->getVariableApi()->set(VariableApi::CONFIG, $name, $value);
164
    }
165
166
    private function getSystemVar(string $name)
167
    {
168
        return $this->getVariableApi()->getSystemVar($name);
169
    }
170
171
    private function setGuestTimeZone(): void
172
    {
173
        $existingOffset = $this->getSystemVar('timezone_offset');
174
        $actualOffset = (float) $existingOffset * 60; // express in minutes
175
        $timezoneAbbreviations = DateTimeZone::listAbbreviations();
176
        $timeZone = date_default_timezone_get();
177
        foreach ($timezoneAbbreviations as $abbreviation => $zones) {
178
            foreach ($zones as $zone) {
179
                if ($zone['offset'] === $actualOffset) {
180
                    $timeZone = $zone['timezone_id'];
181
                    break 2;
182
                }
183
            }
184
        }
185
        $this->setSystemVar('timezone', $timeZone);
186
    }
187
}
188