Completed
Push — master ( e715c7...ffa9af )
by Craig
08:48 queued 02:34
created

SettingsModuleInstaller   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 175
rs 10
c 1
b 0
f 0
wmc 25
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
B install() 0 62 4
C upgrade() 0 56 14
A uninstall() 0 5 1
A setSystemVar() 0 4 1
A getSystemVar() 0 4 1
A setGuestTimeZone() 0 16 4
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\SettingsModule;
13
14
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
15
use Zikula\Core\AbstractExtensionInstaller;
16
use Zikula\ExtensionsModule\Api\VariableApi;
17
18
/**
19
 * Installation and upgrade routines for the settings module.
20
 *
21
 * PLEASE NOTE CAREFULLY.  The use of System::get/set/delVar() is deliberate
22
 * we cannot use $this->get/set/delVar() because the keys will be incorrectly
23
 * generated (System instead of ZConfig).
24
 */
25
class SettingsModuleInstaller extends AbstractExtensionInstaller
26
{
27
    /**
28
     * Initialise the settings module.
29
     *
30
     * @return boolean
31
     */
32
    public function install()
33
    {
34
        // Set up an initial value for a module variable. Note that all module
35
        // variables should be initialised with some value in this way rather
36
        // than just left blank, this helps the user-side code and means that
37
        // there doesn't need to be a check to see if the variable is set in
38
        // the rest of the code as it always will be.
39
        $this->setSystemVar('debug', '0');
40
        $this->setSystemVar('startdate', date('m/Y', time()));
41
        $this->setSystemVar('adminmail', '[email protected]');
42
        $this->setSystemVar('Default_Theme', 'ZikulaBootstrapTheme');
43
        $this->setSystemVar('timezone', date_default_timezone_get());
44
        $this->setSystemVar('funtext', '1');
45
        $this->setSystemVar('reportlevel', '0');
46
        $this->setSystemVar('startpage', '');
47
        $this->setSystemVar('Version_Num', ZikulaKernel::VERSION);
48
        $this->setSystemVar('Version_Sub', ZikulaKernel::VERSION_SUB);
49
        $this->setSystemVar('debug_sql', '0');
50
        $this->setSystemVar('multilingual', '1');
51
        $this->setSystemVar('useflags', '0');
52
        $this->setSystemVar('theme_change', '0');
53
        $this->setSystemVar('UseCompression', '0');
54
        $this->setSystemVar('siteoff', 0);
55
        $this->setSystemVar('siteoffreason', '');
56
        $this->setSystemVar('startargs', '');
57
        $this->setSystemVar('entrypoint', 'index.php');
58
        $this->setSystemVar('language_detect', 0);
59
        // Multilingual support
60
        foreach ($this->container->get('zikula_settings_module.locale_api')->getSupportedLocales() as $lang) {
61
            $this->setSystemVar('sitename_' . $lang, $this->__('Site name'));
62
            $this->setSystemVar('slogan_' . $lang, $this->__('Site description'));
63
            $this->setSystemVar('metakeywords_' . $lang, $this->__('zikula, portal, open source, web site, website, weblog, blog, content management system, cms, application framework'));
64
            $this->setSystemVar('defaultpagetitle_' . $lang, $this->__('Site name'));
65
            $this->setSystemVar('defaultmetadescription_' . $lang, $this->__('Site description'));
66
        }
67
68
        if (function_exists('apache_get_modules') && in_array('mod_rewrite', apache_get_modules())) {
69
            // Only strip entry point if "mod_rewrite" is available.
70
            $this->setSystemVar('shorturlsstripentrypoint', true);
71
        } else {
72
            $this->setSystemVar('shorturlsstripentrypoint', false);
73
        }
74
75
        $this->setSystemVar('shorturlsdefaultmodule', '');
76
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_PROFILE_MODULE, '');
77
        $this->setSystemVar(SettingsConstant::SYSTEM_VAR_MESSAGE_MODULE, '');
78
        $this->setSystemVar('languageurl', 0);
79
        $this->setSystemVar('ajaxtimeout', 5000);
80
        //! this is a comma-separated list of special characters to search for in permalinks
81
        $this->setSystemVar('permasearch', $this->__('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü'));
82
        //! this is a comma-separated list of special characters to replace in permalinks
83
        $this->setSystemVar('permareplace', $this->__('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'));
84
85
        $locale = $this->container->getParameter('locale');
86
        $this->setSystemVar('locale', $locale);
87
        $this->setSystemVar('language_i18n', $locale);
88
89
        $this->setSystemVar('idnnames', 1);
90
91
        // Initialisation successful
92
        return true;
93
    }
94
95
    /**
96
     * upgrade the module from an old version
97
     *
98
     * @param  string $oldversion version number string to upgrade from
99
     *
100
     * @return bool|string true on success, last valid version string or false if fails
101
     */
102
    public function upgrade($oldversion)
103
    {
104
        $request = $this->container->get('request_stack')->getMasterRequest();
105
        // Upgrade dependent on old version number
106
        switch ($oldversion) {
107
            case '2.9.7':
108
            case '2.9.8':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
109
                $permasearch = $this->getSystemVar('permasearch');
110
                if (empty($permasearch)) {
111
                    $this->setSystemVar('permasearch', $this->__('À,Á,Â,Ã,Å,à,á,â,ã,å,Ò,Ó,Ô,Õ,Ø,ò,ó,ô,õ,ø,È,É,Ê,Ë,è,é,ê,ë,Ç,ç,Ì,Í,Î,Ï,ì,í,î,ï,Ù,Ú,Û,ù,ú,û,ÿ,Ñ,ñ,ß,ä,Ä,ö,Ö,ü,Ü'));
112
                }
113
                $permareplace = $this->getSystemVar('permareplace');
114
                if (empty($permareplace)) {
115
                    $this->setSystemVar('permareplace', $this->__('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'));
116
                }
117
                $locale = $this->getSystemVar('locale');
118
                if (empty($locale)) {
119
                    $this->setSystemVar('locale', $request->getLocale());
120
                }
121
122
            case '2.9.9':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
123
                // update certain System vars to multilingual. provide default values for all locales using current value.
124
                // must directly manipulate System vars at DB level because using $this->getSystemVar() returns empty values
125
                $varsToChange = ['sitename', 'slogan', 'metakeywords', 'defaultpagetitle', 'defaultmetadescription'];
126
                $SystemVars = $this->entityManager->getRepository('Zikula\ExtensionsModule\Entity\ExtensionVarEntity')->findBy(['modname' => VariableApi::CONFIG]);
127
                /** @var \Zikula\ExtensionsModule\Entity\ExtensionVarEntity $modVar */
128
                foreach ($SystemVars as $modVar) {
129
                    if (in_array($modVar->getName(), $varsToChange)) {
130
                        foreach ($this->container->get('zikula_settings_module.locale_api')->getSupportedLocales() as $langcode) {
131
                            $newModVar = clone $modVar;
132
                            $newModVar->setName($modVar->getName() . '_' . $langcode);
133
                            $this->entityManager->persist($newModVar);
134
                        }
135
                        $this->entityManager->remove($modVar);
136
                    }
137
                }
138
                $this->entityManager->flush();
139
140
            case '2.9.10':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
141
                $this->setSystemVar('startController', '');
142
                $newStargArgs = str_replace(',', '&', $this->getSystemVar('startargs')); // replace comma with `&`
143
                $this->setSystemVar('startargs', $newStargArgs);
144
            case '2.9.11':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
145
                $this->setSystemVar('shorturls', (bool) $this->getSystemVar('shorturls'));
146
                $this->setSystemVar('shorturlsstripentrypoint', (bool) $this->getSystemVar('shorturlsstripentrypoint'));
147
                $this->setSystemVar('useCompression', (bool) $this->getSystemVar('useCompression'));
148
            case '2.9.12': // ship with Core-1.4.4
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
149
                // reconfigure TZ settings
150
                $this->setGuestTimeZone();
151
            case '2.9.13': // ship with Core-1.5.0
152
                // current version
153
        }
154
155
        // Update successful
156
        return true;
157
    }
158
159
    /**
160
     * Delete the settings module.
161
     *
162
     * @return boolean false
163
     */
164
    public function uninstall()
165
    {
166
        // This module cannot be uninstalled.
167
        return false;
168
    }
169
170
    private function setSystemVar($name, $value = '')
171
    {
172
        return $this->container->get('zikula_extensions_module.api.variable')->set(VariableApi::CONFIG, $name, $value);
173
    }
174
175
    private function getSystemVar($name)
176
    {
177
        return $this->container->get('zikula_extensions_module.api.variable')->getSystemVar($name);
178
    }
179
180
    /**
181
     * upgrade helper method
182
     */
183
    private function setGuestTimeZone()
184
    {
185
        $existingOffset = $this->getSystemVar('timezone_offset');
186
        $actualOffset = floatval($existingOffset) * 60; // express in minutes
187
        $timezoneAbbreviations = \DateTimeZone::listAbbreviations();
188
        $timeZone = date_default_timezone_get();
189
        foreach ($timezoneAbbreviations as $abbreviation => $zones) {
190
            foreach ($zones as $zone) {
191
                if ($zone['offset'] == $actualOffset) {
192
                    $timeZone = $zone['timezone_id'];
193
                    break 2;
194
                }
195
            }
196
        }
197
        $this->setSystemVar('timezone', $timeZone);
198
    }
199
}
200