Completed
Push — master ( e8ce57...2ef872 )
by Craig
10:53 queued 04:32
created

SettingsModuleInstaller::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 26
nc 2
nop 0
dl 0
loc 37
rs 9.504
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - 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\Entity\ExtensionVarEntity;
26
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
27
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
28
29
/**
30
 * Installation and upgrade routines for the Settings module.
31
 */
32
class SettingsModuleInstaller extends AbstractExtensionInstaller
33
{
34
    /**
35
     * @var LocaleApiInterface
36
     */
37
    private $localeApi;
38
39
    /**
40
     * @var string
41
     */
42
    private $locale;
43
44
    public function __construct(
45
        LocaleApiInterface $localeApi,
46
        $locale,
47
        AbstractExtension $extension,
48
        ManagerRegistry $managerRegistry,
49
        SchemaHelper $schemaTool,
50
        RequestStack $requestStack,
51
        TranslatorInterface $translator,
52
        VariableApiInterface $variableApi
53
    ) {
54
        $this->localeApi = $localeApi;
55
        $this->locale = $locale;
56
        parent::__construct($extension, $managerRegistry, $schemaTool, $requestStack, $translator, $variableApi);
57
    }
58
59
    public function install(): bool
60
    {
61
        $this->setSystemVar('startdate', date('m/Y'));
62
        $this->setSystemVar('adminmail', '[email protected]');
63
        $this->setSystemVar('Default_Theme', 'ZikulaBootstrapTheme');
64
        $this->setSystemVar('timezone', date_default_timezone_get());
65
        $this->setSystemVar('Version_Num', ZikulaKernel::VERSION);
66
        $this->setSystemVar('multilingual', '1');
67
        $this->setSystemVar('theme_change', '0');
68
        $this->setSystemVar('UseCompression', '0');
69
        $this->setSystemVar('siteoff', 0);
70
        $this->setSystemVar('siteoffreason');
71
        $this->setSystemVar('startController');
72
        $this->setSystemVar('startargs');
73
        $this->setSystemVar('language_detect', 0);
74
75
        // Multilingual support
76
        foreach ($this->localeApi->getSupportedLocales() as $lang) {
77
            $this->setSystemVar('sitename_' . $lang, $this->trans('Site name'));
78
            $this->setSystemVar('slogan_' . $lang, $this->trans('Site description'));
79
            $this->setSystemVar('defaultpagetitle_' . $lang, $this->trans('Site name'));
80
            $this->setSystemVar('defaultmetadescription_' . $lang, $this->trans('Site description'));
81
        }
82
83
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_PROFILE_MODULE);
84
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_MESSAGE_MODULE);
85
        $this->setSystemVar('languageurl', 0);
86
        $this->setSystemVar('ajaxtimeout', 5000);
87
        //! this is a comma-separated list of special characters to search for in permalinks
88
        $this->setSystemVar('permasearch', $this->trans('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü'));
89
        //! this is a comma-separated list of special characters to replace in permalinks
90
        $this->setSystemVar('permareplace', $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'));
91
92
        $this->setSystemVar('locale', $this->locale);
93
94
        // Initialisation successful
95
        return true;
96
    }
97
98
    public function upgrade(string $oldVersion): bool
99
    {
100
        $request = $this->requestStack->getMasterRequest();
101
        // Upgrade dependent on old version number
102
        switch ($oldVersion) {
103
            case '2.9.7':
104
            case '2.9.8':
105
                $permasearch = $this->getSystemVar('permasearch');
106
                if (empty($permasearch)) {
107
                    $this->setSystemVar('permasearch', $this->trans('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü'));
108
                }
109
                $permareplace = $this->getSystemVar('permareplace');
110
                if (empty($permareplace)) {
111
                    $this->setSystemVar('permareplace', $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'));
112
                }
113
                $locale = $this->getSystemVar('locale');
114
                if (empty($locale)) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
115
                    $this->setSystemVar('locale', $request->getLocale());
116
                }
117
118
            case '2.9.9':
119
                // update certain System vars to multilingual. provide default values for all locales using current value.
120
                // must directly manipulate System vars at DB level because using $this->getSystemVar() returns empty values
121
                $varsToChange = ['sitename', 'slogan', 'defaultpagetitle', 'defaultmetadescription'];
122
                $systemVars = $this->managerRegistry->getRepository(ExtensionVarEntity::class)->findBy(['modname' => VariableApi::CONFIG]);
123
                /** @var ExtensionVarEntity $modVar */
124
                foreach ($systemVars as $modVar) {
125
                    if (in_array($modVar->getName(), $varsToChange, true)) {
126
                        foreach ($this->localeApi->getSupportedLocales() as $langcode) {
127
                            $newModVar = clone $modVar;
128
                            $newModVar->setName($modVar->getName() . '_' . $langcode);
129
                            $this->entityManager->persist($newModVar);
130
                        }
131
                        $this->entityManager->remove($modVar);
132
                    }
133
                }
134
                $this->entityManager->flush();
135
136
            case '2.9.10':
137
                $this->setSystemVar('startController');
138
                $newStargArgs = str_replace(',', '&', $this->getSystemVar('startargs')); // replace comma with `&`
139
                $this->setSystemVar('startargs', $newStargArgs);
140
            case '2.9.11':
141
                $this->setSystemVar('UseCompression', (bool)$this->getSystemVar('UseCompression'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
142
            case '2.9.12': // ship with Core-1.4.4
143
                // reconfigure TZ settings
144
                $this->setGuestTimeZone();
145
            case '2.9.13':
146
                $this->getVariableApi()->del(VariableApi::CONFIG, 'entrypoint');
147
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturlsstripentrypoint');
148
                $this->getVariableApi()->del(VariableApi::CONFIG, 'shorturls');
149
                $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...
150
            case '2.9.14': // ship with Core-1.5.0 + Core-2.x
151
                $this->getVariableApi()->del(VariableApi::CONFIG, 'Version_Sub');
152
                $this->setSystemVar('startController'); // reset to blank because of new format FQCN::method
153
            case '2.9.15':
154
                $varsToRemove = [
155
                    'funtext',
156
                    'reportlevel',
157
                    'idnnames',
158
                    'debug',
159
                    'debug_sql',
160
                    'useflags',
161
                    'language_i18n'
162
                ];
163
                foreach ($varsToRemove as $varName) {
164
                    $this->getVariableApi()->del(VariableApi::CONFIG, $varName);
165
                }
166
            case '2.9.16': // ship with Core-3.0.0
167
                // current version
168
        }
169
170
        // Update successful
171
        return true;
172
    }
173
174
    public function uninstall(): bool
175
    {
176
        // This module cannot be uninstalled.
177
        return false;
178
    }
179
180
    private function setSystemVar(string $name, $value = ''): void
181
    {
182
        $this->getVariableApi()->set(VariableApi::CONFIG, $name, $value);
183
    }
184
185
    private function getSystemVar(string $name)
186
    {
187
        return $this->getVariableApi()->getSystemVar($name);
188
    }
189
190
    private function setGuestTimeZone(): void
191
    {
192
        $existingOffset = $this->getSystemVar('timezone_offset');
193
        $actualOffset = (float) $existingOffset * 60; // express in minutes
194
        $timezoneAbbreviations = DateTimeZone::listAbbreviations();
195
        $timeZone = date_default_timezone_get();
196
        foreach ($timezoneAbbreviations as $abbreviation => $zones) {
197
            foreach ($zones as $zone) {
198
                if ($zone['offset'] === $actualOffset) {
199
                    $timeZone = $zone['timezone_id'];
200
                    break 2;
201
                }
202
            }
203
        }
204
        $this->setSystemVar('timezone', $timeZone);
205
    }
206
}
207