Completed
Pull Request — master (#563)
by Richard
08:33
created

SystemPreferencesForm   B

Complexity

Total Complexity 53

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 308
rs 7.4757
c 0
b 0
f 0
wmc 53

2 Methods

Rating   Name   Duplication   Size   Complexity  
F getForm() 0 289 52
A __construct() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like SystemPreferencesForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SystemPreferencesForm, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xoops\Core\Kernel\Criteria;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Criteria. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Xoops\Core\Kernel\CriteriaCompo;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaCompo. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Xoops\Core\Kernel\Handlers\XoopsModule;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModule. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
16
/**
17
 * Preference Form Class
18
 *
19
 * @category  Modules/system/class/form
20
 * @package   SystemPreferencesForm
21
 * @author    Andricq Nicolas (AKA MusS)
22
 * @author    trabis <[email protected]>
23
 * @copyright 2000-2014 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 * @since     2.0
27
 */
28
class SystemPreferencesForm extends Xoops\Form\SimpleForm
29
{
30
    /**
31
     * __construct
32
     *
33
     * @param null $obj unused object
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $obj is correct as it would always require null to be passed?
Loading history...
34
     */
35
    public function __construct($obj = null)
36
    {
37
    }
38
39
    /**
40
     * getForm
41
     *
42
     * @param array       &$obj array of config objects
43
     * @param XoopsModule $mod module
44
     *
45
     * @return void
46
     */
47
    public function getForm(&$obj, XoopsModule $mod)
48
    {
49
        $xoops = Xoops::getInstance();
50
        $config_handler = $xoops->getHandlerConfig();
51
52
        parent::__construct('', 'pref_form', 'admin.php?fct=preferences', 'post', true);
53
        if ($mod->getVar('dirname') !== 'system') {
54
            $xoops->loadLanguage('modinfo', $mod->getVar('dirname'));
55
        }
56
        $xoops->loadLocale($mod->getVar('dirname'));
0 ignored issues
show
Bug introduced by
It seems like $mod->getVar('dirname') can also be of type array; however, parameter $domain of Xoops::loadLocale() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $xoops->loadLocale(/** @scrutinizer ignore-type */ $mod->getVar('dirname'));
Loading history...
57
        $configs = $mod->getInfo('config');
58
        $configNames = array();
59
        foreach (array_keys($configs) as $i) {
0 ignored issues
show
Bug introduced by
It seems like $configs can also be of type string; however, parameter $input of array_keys() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        foreach (array_keys(/** @scrutinizer ignore-type */ $configs) as $i) {
Loading history...
60
            $configNames[$configs[$i]['name']] =& $configs[$i];
61
        }
62
        $configCats = $mod->getInfo('configcat');
63
        if (!$configCats) {
64
            $configCats = array(
65
                'default' => array(
66
                    'name'        => SystemLocale::OTHER_SETTINGS,
67
                    'description' => ''
68
                )
69
            );
70
        }
71
72
        if (!in_array('default', array_keys($configCats))) {
73
            $configCats['default'] = array(
74
                'name'        => SystemLocale::OTHER_SETTINGS,
75
                'description' => ''
76
            );
77
        }
78
79
        foreach (array_keys($configNames) as $name) {
80
            if (!isset($configNames[$name]['category'])) {
81
                $configNames[$name]['category'] = 'default';
82
            }
83
        }
84
85
        $tabTray = new Xoops\Form\TabTray('', 'pref_tabtay');
86
        $tabs = array();
87
        foreach ($configCats as $name => $info) {
88
            $tabs[$name] = new Xoops\Form\Tab($info['name'], 'pref_tab_' . $name);
89
            if (isset($info['description']) && $info['description'] != '') {
90
                $tabs[$name]->addElement(new Xoops\Form\Label('', $info['description']));
91
            }
92
        }
93
94
        $xoops->events()->triggerEvent('system.preferences.form', $mod);
95
96
        if (!empty($_REQUEST["redirect"])) {
97
            $myts = \Xoops\Core\Text\Sanitizer::getInstance();
98
            $this->addElement(new Xoops\Form\Hidden('redirect', $myts->htmlSpecialChars($_REQUEST["redirect"])));
99
        } elseif ($mod->getInfo('adminindex')) {
100
            $this->addElement(new Xoops\Form\Hidden(
101
                'redirect',
102
                \XoopsBaseConfig::get('url') . '/modules/' . $mod->getVar('dirname') . '/' . $mod->getInfo('adminindex')
0 ignored issues
show
Bug introduced by
Are you sure $mod->getInfo('adminindex') of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
                \XoopsBaseConfig::get('url') . '/modules/' . $mod->getVar('dirname') . '/' . /** @scrutinizer ignore-type */ $mod->getInfo('adminindex')
Loading history...
103
            ));
104
        }
105
        $count = count($obj);
106
        for ($i = 0; $i < $count; ++$i) {
107
            $title = \Xoops\Locale::translate($obj[$i]->getVar('conf_title'), $mod->getVar('dirname'));
0 ignored issues
show
Bug introduced by
It seems like $mod->getVar('dirname') can also be of type array; however, parameter $dirname of Xoops\Locale::translate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
            $title = \Xoops\Locale::translate($obj[$i]->getVar('conf_title'), /** @scrutinizer ignore-type */ $mod->getVar('dirname'));
Loading history...
108
            $desc = ($obj[$i]->getVar('conf_desc') != '') ?
109
                \Xoops\Locale::translate($obj[$i]->getVar('conf_desc'), $mod->getVar('dirname')) : '';
110
            switch ($obj[$i]->getVar('conf_formtype')) {
111
112
                case 'textarea':
113
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
114
                    if ($obj[$i]->getVar('conf_valuetype') === 'array') {
115
                        // this is exceptional.. only when value type is arrayneed a smarter way for this
116
                        $ele = ($obj[$i]->getVar('conf_value') != '')
117
                            ? new Xoops\Form\TextArea(
118
                                $title,
119
                                $obj[$i]->getVar('conf_name'),
120
                                $myts->htmlSpecialChars(implode('|', $obj[$i]->getConfValueForOutput())),
121
                                5,
122
                                5
123
                            )
124
                            : new Xoops\Form\TextArea($title, $obj[$i]->getVar('conf_name'), '', 5, 5);
125
                    } else {
126
                        $ele = new Xoops\Form\TextArea(
127
                            $title,
128
                            $obj[$i]->getVar('conf_name'),
129
                            $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()),
130
                            5,
131
                            5
132
                        );
133
                    }
134
                    break;
135
136
                case 'select':
137
                    $ele = new Xoops\Form\Select(
138
                        $title,
139
                        $obj[$i]->getVar('conf_name'),
140
                        $obj[$i]->getConfValueForOutput()
141
                    );
142
                    $options = $config_handler->getConfigOptions(new Criteria('conf_id', $obj[$i]->getVar('conf_id')));
143
                    $opcount = count($options);
144
                    for ($j = 0; $j < $opcount; ++$j) {
145
                        $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), $mod->getVar('dirname'));
0 ignored issues
show
Bug introduced by
It seems like $options[$j]->getVar('confop_value') can also be of type array; however, parameter $key of Xoops\Locale::translate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

145
                        $optval = \Xoops\Locale::translate(/** @scrutinizer ignore-type */ $options[$j]->getVar('confop_value'), $mod->getVar('dirname'));
Loading history...
146
                        $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), $mod->getVar('dirname'));
147
                        $ele->addOption($optval, $optkey);
148
                    }
149
                    break;
150
151
                case 'select_editor':
152
                    $ele = new Xoops\Form\Select(
153
                        $title,
154
                        $obj[$i]->getVar('conf_name'),
155
                        $obj[$i]->getConfValueForOutput()
156
                    );
157
                    \Xoops\Core\Lists\Editor::setOptionsArray($ele);
158
                    break;
159
160
                case 'select_multi':
161
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput(), 5, true);
162
                    $options = $config_handler->getConfigOptions(new Criteria('conf_id', $obj[$i]->getVar('conf_id')));
163
                    $opcount = count($options);
164
                    for ($j = 0; $j < $opcount; ++$j) {
165
                        $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), $mod->getVar('dirname'));
166
                        $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), $mod->getVar('dirname'));
167
                        $ele->addOption($optval, $optkey);
168
                    }
169
                    break;
170
171
                case 'yesno':
172
                    $ele = new Xoops\Form\RadioYesNo($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
173
                    break;
174
175
                case 'theme':
176
                case 'theme_multi':
177
                    $ele = ($obj[$i]->getVar('conf_formtype') !== 'theme_multi') ? new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput()) : new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput(), 5, true);
178
                    $dirlist = XoopsLists::getThemesList();
179
                    if (!empty($dirlist)) {
180
                        asort($dirlist);
181
                        $ele->addOptionArray($dirlist);
182
                    }
183
                    break;
184
                case 'tplset':
185
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
186
                    $tplset_handler = $xoops->getHandlerTplSet();
187
                    $tplsetlist = $tplset_handler->getNameList();
188
                    asort($tplsetlist);
189
                    foreach ($tplsetlist as $key => $name) {
190
                        $ele->addOption($key, $name);
191
                    }
192
                    break;
193
194
                case 'cpanel':
195
                    $ele = new Xoops\Form\Hidden($obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
196
                    /*
197
                    $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
198
                    XoopsLoad::load("cpanel", "system");
199
                    $list = XoopsSystemCpanel::getGuis();
200
                    $ele->addOptionArray($list);  */
201
                    break;
202
203
                case 'timezone':
204
                    $ele = new Xoops\Form\SelectTimeZone($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
205
                    break;
206
207
                case 'language':
208
                    $ele = new Xoops\Form\SelectLanguage($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
209
                    break;
210
211
                case 'locale':
212
                    $ele = new Xoops\Form\SelectLocale($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
213
                    break;
214
215
                case 'startpage':
216
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
217
218
                    $module_handler = $xoops->getHandlerModule();
219
                    $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
220
                    $criteria->add(new Criteria('isactive', 1));
221
                    $moduleslist = $module_handler->getNameList($criteria, true);
222
                    $moduleslist['--'] = XoopsLocale::NONE;
223
                    $ele->addOptionArray($moduleslist);
224
                    break;
225
226
                case 'group':
227
                    $ele = new Xoops\Form\SelectGroup($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 1, false);
228
                    break;
229
230
                case 'group_multi':
231
                    $ele = new Xoops\Form\SelectGroup($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 5, true);
232
                    break;
233
234
                // RMV-NOTIFY: added 'user' and 'user_multi'
235
                case 'user':
236
                    $ele = new Xoops\Form\SelectUser($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 1, false);
237
                    break;
238
239
                case 'user_multi':
240
                    $ele = new Xoops\Form\SelectUser($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 5, true);
241
                    break;
242
                case 'module_cache':
243
                    $module_handler = $xoops->getHandlerModule();
244
                    $modules = $module_handler->getObjectsArray(new Criteria('hasmain', 1), true);
245
                    $currrent_val = $obj[$i]->getConfValueForOutput();
246
                    $cache_options = array(
247
                        '0'       => XoopsLocale::NO_CACHE,
248
                        '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
249
                        '60'      => XoopsLocale::ONE_MINUTE,
250
                        '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
251
                        '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
252
                        '3600'    => XoopsLocale::ONE_HOUR,
253
                        '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
254
                        '86400'   => XoopsLocale::ONE_DAY,
255
                        '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
256
                        '604800'  => XoopsLocale::ONE_WEEK,
257
                        '2592000' => XoopsLocale::ONE_MONTH
258
                    );
259
                    if (count($modules) > 0) {
260
                        $ele = new Xoops\Form\ElementTray($title, '<br />');
261
                        foreach (array_keys($modules) as $mid) {
262
                            $c_val = isset($currrent_val[$mid]) ? (int)($currrent_val[$mid]) : null;
263
                            $selform = new Xoops\Form\Select($modules[$mid]->getVar('name'), $obj[$i]->getVar('conf_name') . "[$mid]", $c_val);
264
                            $selform->addOptionArray($cache_options);
265
                            $ele->addElement($selform);
266
                            unset($selform);
267
                        }
268
                    } else {
269
                        $ele = new Xoops\Form\Label($title, SystemLocale::NO_MODULE_TO_CACHE);
270
                    }
271
                    break;
272
273
                case 'site_cache':
274
                    $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
275
                    $ele->addOptionArray(array(
276
                        '0'       => XoopsLocale::NO_CACHE,
277
                        '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
278
                        '60'      => XoopsLocale::ONE_MINUTE,
279
                        '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
280
                        '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
281
                        '3600'    => XoopsLocale::ONE_HOUR,
282
                        '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
283
                        '86400'   => XoopsLocale::ONE_DAY,
284
                        '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
285
                        '604800'  => XoopsLocale::ONE_WEEK,
286
                        '2592000' => XoopsLocale::ONE_MONTH
287
                    ));
288
                    break;
289
290
                case 'password':
291
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
292
                    $ele = new Xoops\Form\Password($title, $obj[$i]->getVar('conf_name'), 32, 255, $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
293
                    break;
294
295
                case 'color':
296
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
297
                    $ele = new Xoops\Form\ColorPicker($title, $obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
298
                    break;
299
300
                case 'hidden':
301
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
302
                    $ele = new Xoops\Form\Hidden($obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
303
                    break;
304
305
                case 'textbox':
306
                default:
307
                    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
308
                    $ele = new Xoops\Form\Text($title, $obj[$i]->getVar('conf_name'), 5, 255, $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
309
                    break;
310
            }
311
            $hidden = new Xoops\Form\Hidden('conf_ids[]', $obj[$i]->getVar('conf_id'));
312
            if (isset($ele)) {
313
                $ele->setDescription($desc);
314
                if ($obj[$i]->getVar('conf_formtype') !== 'hidden') {
315
                    $name = 'default';
316
                    if (isset($configNames[$obj[$i]->getVar('conf_name')]['category'])) {
317
                        $name = $configNames[$obj[$i]->getVar('conf_name')]['category'];
318
                    }
319
                    $tabs[$name]->addElement($ele);
320
                } else {
321
                    $this->addElement($ele);
322
                }
323
                $this->addElement($hidden);
324
                unset($ele);
325
                unset($hidden);
326
            }
327
        }
328
        foreach (array_keys($tabs) as $name) {
329
            if ($tabs[$name]->getElements()) {
330
                $tabTray->addElement($tabs[$name]);
331
            }
332
        }
333
        $this->addElement($tabTray);
334
        $this->addElement(new Xoops\Form\Hidden('op', 'save'));
335
        $this->addElement(new Xoops\Form\Button('', 'button', XoopsLocale::A_SUBMIT, 'submit'));
336
    }
337
}
338