Completed
Pull Request — master (#489)
by Richard
10:59
created

createconfigform.php ➔ createConfigform()   D

Complexity

Conditions 35
Paths 125

Size

Total Lines 197
Code Lines 151

Duplication

Lines 112
Ratio 56.85 %
Metric Value
cc 35
eloc 151
nc 125
nop 1
dl 112
loc 197
rs 4.0963

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Handlers\XoopsConfigItem;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsConfigItem.

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
14
/**
15
 * @copyright   XOOPS Project (http://xoops.org)
16
 * @license     GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package     installer
18
 * @since       2.3.0
19
 * @author      Haruki Setoyama  <[email protected]>
20
 * @author      Kazumi Ono <[email protected]>
21
 * @author      Skalpa Keo <[email protected]>
22
 * @author      Taiwen Jiang <[email protected]>
23
 * @author      DuGris (aka L. JEN) <[email protected]>
24
 * @version     $Id$
25
 **/
26
27
defined('XOOPS_INSTALL') or die('XOOPS Custom Installation die');
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
28
29
/**
30
 * @param $config
31
 *
32
 * @return array
33
 */
34
function createConfigform($config)
35
{
36
    $xoops = Xoops::getInstance();
37
    $config_handler = $xoops->getHandlerConfig();
38
    //$xoops->config = $config_handler->getConfigsByCat(XOOPS_CONF);
39
    //$config =& $xoops->config;
40
41
    $ret = array();
42
    $confcount = count($config);
43
44
    for ($i = 0; $i < $confcount; ++$i) {
45
        $conf_catid = $config[$i]->getVar('conf_catid');
46
        if (!isset($ret[$conf_catid])) {
47
            $ret[$conf_catid] = new Xoops\Form\ThemeForm('', 'configs', 'index.php', 'post');
48
        }
49
50
        $title = \Xoops\Locale::translate($config[$i]->getVar('conf_title'), 'system');
51
52
        switch ($config[$i]->getVar('conf_formtype')) {
53
54
            case 'textarea':
55
                if ($config[$i]->getVar('conf_valuetype') === 'array') {
56
                    // this is exceptional.. only when value type is array need a smarter way for this
57
                    $ele = ($config[$i]->getVar('conf_value') != '')
58
                        ? new Xoops\Form\TextArea($title, $config[$i]->getVar('conf_name'), installHtmlSpecialCharacters(implode('|', $config[$i]->getConfValueForOutput())), 5, 50)
59
                        : new Xoops\Form\TextArea($title, $config[$i]->getVar('conf_name'), '', 5, 50);
60
                } else {
61
                    $ele = new Xoops\Form\TextArea($title, $config[$i]->getVar('conf_name'), installHtmlSpecialCharacters($config[$i]->getConfValueForOutput()), 5, 100);
62
                }
63
                break;
64
65 View Code Duplication
            case 'select':
66
                $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
67
                $options =& $config_handler->getConfigOptions(new Criteria('conf_id', $config[$i]->getVar('conf_id')));
68
                $opcount = count($options);
69
                for ($j = 0; $j < $opcount; ++$j) {
70
                    $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), 'system');
71
                    $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), 'system');
72
                    $ele->addOption($optval, $optkey);
73
                }
74
                break;
75
76 View Code Duplication
            case 'select_multi':
77
                $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput(), 5, true);
78
                $options =& $config_handler->getConfigOptions(new Criteria('conf_id', $config[$i]->getVar('conf_id')));
79
                $opcount = count($options);
80
                for ($j = 0; $j < $opcount; ++$j) {
81
                    $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), 'system');
82
                    $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), 'system');
83
                    $ele->addOption($optval, $optkey);
84
                }
85
                break;
86
87 View Code Duplication
            case 'yesno':
88
                $ele = new Xoops\Form\RadioYesNo($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput(), XoopsLocale::YES, XoopsLocale::NO);
89
                break;
90
91
            case 'theme':
92 View Code Duplication
            case 'theme_multi':
93
                $ele = ($config[$i]->getVar('conf_formtype') !== 'theme_multi')
94
                    ? new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput())
95
                    : new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput(), 5, true);
96
                $dirlist = XoopsLists::getThemesList();
97
                if (!empty($dirlist)) {
98
                    asort($dirlist);
99
                    $ele->addOptionArray($dirlist);
100
                }
101
                break;
102
103 View Code Duplication
            case 'tplset':
104
                $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
105
                $tplset_handler = $xoops->getHandlerTplSet();
106
                $tplsetlist = $tplset_handler->getNameList();
107
                asort($tplsetlist);
108
                foreach ($tplsetlist as $key => $name) {
109
                    $ele->addOption($key, $name);
110
                }
111
                break;
112
113 View Code Duplication
            case 'timezone':
114
                $ele = new Xoops\Form\SelectTimeZone($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
115
                break;
116
117 View Code Duplication
            case 'language':
118
                $ele = new Xoops\Form\SelectLanguage($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
119
                break;
120
121 View Code Duplication
            case 'locale':
122
                $ele = new Xoops\Form\SelectLocale($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
123
                break;
124
125 View Code Duplication
            case 'startpage':
126
                $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
127
                $module_handler = $xoops->getHandlerModule();
128
                $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
129
                $criteria->add(new Criteria('isactive', 1));
130
                $moduleslist =& $module_handler->getNameList($criteria, true);
131
                $moduleslist['--'] = XoopsLocale::NONE;
132
                $ele->addOptionArray($moduleslist);
133
                break;
134
135
            case 'group':
136
                $ele = new Xoops\Form\SelectGroup($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 1, false);
137
                break;
138
139
            case 'group_multi':
140
                $ele = new Xoops\Form\SelectGroup($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 5, true);
141
                break;
142
143
            // RMV-NOTIFY - added 'user' and 'user_multi'
144 View Code Duplication
            case 'user':
145
                $ele = new Xoops\Form\SelectUser($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 1, false);
146
                break;
147
148 View Code Duplication
            case 'user_multi':
149
                $ele = new Xoops\Form\SelectUser($title, $config[$i]->getVar('conf_name'), false, $config[$i]->getConfValueForOutput(), 5, true);
150
                break;
151
152 View Code Duplication
            case 'module_cache':
153
                $module_handler = $xoops->getHandlerModule();
154
                $modules = $module_handler->getObjectsArray(new Criteria('hasmain', 1), true);
155
                $currrent_val = $config[$i]->getConfValueForOutput();
156
                $cache_options = array(
157
                    '0'       => XoopsLocale::NO_CACHE,
158
                    '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
159
                    '60'      => XoopsLocale::ONE_MINUTE,
160
                    '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
161
                    '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
162
                    '3600'    => XoopsLocale::ONE_HOUR,
163
                    '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
164
                    '86400'   => XoopsLocale::ONE_DAY,
165
                    '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
166
                    '604800'  => XoopsLocale::ONE_WEEK,
167
                    '2592000' => XoopsLocale::ONE_MONTH
168
                );
169
                if (count($modules) > 0) {
170
                    $ele = new Xoops\Form\ElementTray($title, '<br />');
171
                    foreach (array_keys($modules) as $mid) {
172
                        $c_val = isset($currrent_val[$mid]) ? (int)($currrent_val[$mid]) : null;
173
                        $selform = new Xoops\Form\Select($modules[$mid]->getVar('name'), $config[$i]->getVar('conf_name') . "[$mid]", $c_val);
174
                        $selform->addOptionArray($cache_options);
175
                        $ele->addElement($selform);
176
                        unset($selform);
177
                    }
178
                } else {
179
                    $ele = new Xoops\Form\Label($title, SystemLocale::NO_MODULE_TO_CACHE);
180
                }
181
                break;
182
183 View Code Duplication
            case 'site_cache':
184
                $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
185
                $ele->addOptionArray(array(
186
                    '0'       => XoopsLocale::NO_CACHE,
187
                    '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
188
                    '60'      => XoopsLocale::ONE_MINUTE,
189
                    '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
190
                    '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
191
                    '3600'    => XoopsLocale::ONE_HOUR,
192
                    '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
193
                    '86400'   => XoopsLocale::ONE_DAY,
194
                    '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
195
                    '604800'  => XoopsLocale::ONE_WEEK,
196
                    '2592000' => XoopsLocale::ONE_MONTH
197
                ));
198
                break;
199
200
            case 'password':
201
                $ele = new Xoops\Form\Password($title, $config[$i]->getVar('conf_name'), 50, 255, installHtmlSpecialCharacters($config[$i]->getConfValueForOutput()));
202
                break;
203
204
            case 'color':
205
                $ele = new Xoops\Form\ColorPicker($title, $config[$i]->getVar('conf_name'), installHtmlSpecialCharacters($config[$i]->getConfValueForOutput()));
206
                break;
207
208
            case 'hidden':
209
                $ele = new Xoops\Form\Hidden($config[$i]->getVar('conf_name'), installHtmlSpecialCharacters($config[$i]->getConfValueForOutput()));
210
                break;
211
212
            case 'textbox':
213
            default:
214
                $ele = new Xoops\Form\Text($title, $config[$i]->getVar('conf_name'), 50, 255, installHtmlSpecialCharacters($config[$i]->getConfValueForOutput()));
215
                break;
216
        }
217
218
        if ($config[$i]->getVar('conf_desc') != '') {
219
            $ele->setDescription(\Xoops\Locale::translate($config[$i]->getVar('conf_desc'), 'system'));
220
        }
221
        $ret[$conf_catid]->addElement($ele);
222
223
        $hidden = new Xoops\Form\Hidden('conf_ids[]', $config[$i]->getVar('conf_id'));
224
        $ret[$conf_catid]->addElement($hidden);
225
226
        unset($ele);
227
        unset($hidden);
228
    }
229
    return $ret;
230
}
231
232
/**
233
 * @param XoopsConfigItem $config
234
 *
235
 * @return Xoops\Form\ThemeForm[]
236
 */
237
function createThemeform(XoopsConfigItem $config)
238
{
239
    $title = $config->getVar('conf_desc') == '' ? \Xoops\Locale::translate($config->getVar('conf_title'), 'system') : \Xoops\Locale::translate($config->getVar('conf_title'), 'system') . '<br /><br /><span>' . \Xoops\Locale::translate($config->getVar('conf_desc'), 'system') . '</span>';
240
    $form_theme_set = new Xoops\Form\Select('', $config->getVar('conf_name'), $config->getConfValueForOutput(), 1, false);
241
    $dirlist = XoopsLists::getThemesList();
242
    if (!empty($dirlist)) {
243
        asort($dirlist);
244
        $form_theme_set->addOptionArray($dirlist);
245
    }
246
247
    $label_content = "";
248
249
    // read ini file for each theme
250
    foreach ($dirlist as $theme) {
251
        // set default value
252
        $theme_ini = array(
253
            'Name'        => $theme,
254
            'Description' => '',
255
            'Version'     => '',
256
            'Format'      => '',
257
            'Author'      => '',
258
            'Demo'        => '',
259
            'Url'         => '',
260
            'Download'    => '',
261
            'W3C'         => '',
262
            'Licence'     => '',
263
            'thumbnail'   => 'screenshot.gif',
264
            'screenshot'  => 'screenshot.png',
265
        );
266
267
        if ($theme == $config->getConfValueForOutput()) {
268
            $label_content .= "<div id='$theme' rel='theme' style='display:block;'>";
269
        } else {
270
            $label_content .= "<div id='$theme' rel='theme' style='display:none;'>";
271
        }
272
        if (file_exists(XOOPS_ROOT_PATH . "/themes/$theme/theme.ini")) {
273
            $theme_ini = parse_ini_file(XOOPS_ROOT_PATH . "/themes/$theme/theme.ini");
274
            if ($theme_ini['screenshot'] == '') {
275
                $theme_ini['screenshot'] = 'screenshot.png';
276
                $theme_ini['thumbnail'] = 'thumbnail.png';
277
            }
278
        }
279
280
        if ($theme_ini['screenshot'] != '' && file_exists(XOOPS_ROOT_PATH . "/themes/$theme/" . $theme_ini['screenshot'])) {
281
            $label_content .= "<img src='" . XOOPS_URL . "/themes/" . $theme . "/" . $theme_ini['screenshot'] . "' alt='Screenshot' />";
282
        } elseif ($theme_ini['thumbnail'] != '' && file_exists(XOOPS_ROOT_PATH . "/themes/$theme/" . $theme_ini['thumbnail'])) {
283
            $label_content .= "<img src='" . XOOPS_URL . "/themes/" . $theme . "/" . $theme_ini['thumbnail'] . "' alt='$theme' />";
284
        } else {
285
            $label_content .= THEME_NO_SCREENSHOT;
286
        }
287
        $label_content .= "</div>";
288
    }
289
    // read ini file for each theme
290
291
    $form_theme_set->setExtra("onchange='showThemeSelected(this)'");
0 ignored issues
show
Deprecated Code introduced by
The method Xoops\Form\Element::setExtra() has been deprecated with message: please use attributes for event scripting

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
292
293
    $form = new Xoops\Form\ThemeForm($title, 'themes', 'index.php', 'post');
294
    $form->addElement($form_theme_set);
295
    $form->addElement(new Xoops\Form\Label('', "<div id='screenshot'>" . $label_content . "</div>"));
296
297
    $form->addElement(new Xoops\Form\Hidden('conf_ids[]', $config->getVar('conf_id')));
298
    return array($form);
299
}
300