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

UserconfigsConfigsForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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\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...
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
 * @author          Andricq Nicolas (AKA MusS)
18
 * @author          trabis <[email protected]>
19
 * @package         userconfigs
20
 * @version         $Id$
21
 */
22
23
class UserconfigsConfigsForm extends Xoops\Form\SimpleForm
24
{
25
    /**
26
     * __construct
27
     *
28
     * @param null $obj
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...
29
     */
30
    public function __construct($obj = null)
31
    {
32
    }
33
34
    /**
35
     * @param array       $obj
36
     * @param XoopsModule $mod
37
     */
38
    public function getForm(&$obj, XoopsModule $mod)
39
    {
40
        $xoops = Xoops::getInstance();
41
        $helper = Userconfigs::getInstance();
42
        $config_handler = $helper->getHandlerConfig();
43
        /* @var $plugin UserconfigsPluginInterface */
44
        if ($plugin = \Xoops\Module\Plugin::getPlugin($mod->getVar('dirname'), 'userconfigs')) {
0 ignored issues
show
Bug introduced by
It seems like $mod->getVar('dirname') can also be of type array; however, parameter $dirname of Xoops\Module\Plugin::getPlugin() 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

44
        if ($plugin = \Xoops\Module\Plugin::getPlugin(/** @scrutinizer ignore-type */ $mod->getVar('dirname'), 'userconfigs')) {
Loading history...
45
46
            parent::__construct('', 'pref_form', 'index.php', 'post', true);
47
            if ($mod->getVar('dirname') !== 'system') {
48
                $xoops->loadLanguage('modinfo', $mod->getVar('dirname'));
49
                $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

49
                $xoops->loadLocale(/** @scrutinizer ignore-type */ $mod->getVar('dirname'));
Loading history...
50
            }
51
            $configs = $plugin->configs();
52
            $configNames = array();
53
            foreach (array_keys($configs) as $i) {
54
                $configNames[$configs[$i]['name']] =& $configs[$i];
55
            }
56
            $configCats = $plugin->categories();
57
            if (!$configCats) {
58
                $configCats = array(
59
                    'default' => array(
60
                        'name'        => _MD_USERCONFIGS_CONFIGS,
61
                        'description' => ''
62
                    )
63
                );
64
            }
65
66
            if (!in_array('default', array_keys($configCats))) {
67
                $configCats['default'] = array(
68
                    'name'        => _MD_USERCONFIGS_CONFIGS,
69
                    'description' => ''
70
                );
71
            }
72
73
            foreach (array_keys($configNames) as $name) {
74
                if (!isset($configNames[$name]['category'])) {
75
                    $configNames[$name]['category'] = 'default';
76
                }
77
            }
78
79
            $tabTray = new Xoops\Form\TabTray('', 'pref_tabtay');
80
            $tabs = array();
81
            foreach ($configCats as $name => $info) {
82
                $tabs[$name] = new Xoops\Form\Tab($info['name'], 'pref_tab_' . $name);
83
                if (isset($info['description']) && $info['description'] != '') {
84
                    $tabs[$name]->addElement(new Xoops\Form\Label('', $info['description']));
85
                }
86
            }
87
            $count = count($obj);
88
            for ($i = 0; $i < $count; ++$i) {
89
                $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

89
                $title = \Xoops\Locale::translate($obj[$i]->getVar('conf_title'), /** @scrutinizer ignore-type */ $mod->getVar('dirname'));
Loading history...
90
                $desc = ($obj[$i]->getVar('conf_desc') != '') ? \Xoops\Locale::translate($obj[$i]->getVar('conf_desc'), $mod->getVar('dirname')) : '';
91
                switch ($obj[$i]->getVar('conf_formtype')) {
92
93
                    case 'textarea':
94
                        $myts = \Xoops\Core\Text\Sanitizer::getInstance();
95
                        if ($obj[$i]->getVar('conf_valuetype') === 'array') {
96
                            // this is exceptional.. only when value type is arrayneed a smarter way for this
97
                            $ele = ($obj[$i]->getVar('conf_value') != '') ? new Xoops\Form\TextArea($title, $obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars(implode('|', $obj[$i]->getConfValueForOutput())), 5, 5) : new Xoops\Form\TextArea($title, $obj[$i]->getVar('conf_name'), '', 5, 5);
98
                        } else {
99
                            $ele = new Xoops\Form\TextArea($title, $obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()), 5, 5);
100
                        }
101
                        break;
102
103
                    case 'select':
104
                        $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
105
                        $options = $config_handler->getConfigOptions(new Criteria('conf_id', $obj[$i]->getVar('conf_id')));
106
                        $opcount = count($options);
107
                        for ($j = 0; $j < $opcount; ++$j) {
108
                            $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), $mod->getVar('dirname'));
109
                            $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), $mod->getVar('dirname'));
110
                            $ele->addOption($optval, $optkey);
111
                        }
112
                        break;
113
114
                    case 'select_multi':
115
                        $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput(), 5, true);
116
                        $options = $config_handler->getConfigOptions(new Criteria('conf_id', $obj[$i]->getVar('conf_id')));
117
                        $opcount = count($options);
118
                        for ($j = 0; $j < $opcount; ++$j) {
119
                            $optval = \Xoops\Locale::translate($options[$j]->getVar('confop_value'), $mod->getVar('dirname'));
120
                            $optkey = \Xoops\Locale::translate($options[$j]->getVar('confop_name'), $mod->getVar('dirname'));
121
                            $ele->addOption($optval, $optkey);
122
                        }
123
                        break;
124
125
                    case 'yesno':
126
                        $ele = new Xoops\Form\RadioYesNo($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
127
                        break;
128
129
                    case 'theme':
130
                    case 'theme_multi':
131
                        $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);
132
                        $dirlist = XoopsLists::getThemesList();
133
                        if (!empty($dirlist)) {
134
                            asort($dirlist);
135
                            $ele->addOptionArray($dirlist);
136
                        }
137
                        break;
138
                    case 'tplset':
139
                        $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
140
                        $tplset_handler = $xoops->getHandlerTplSet();
141
                        $tplsetlist = $tplset_handler->getNameList();
142
                        asort($tplsetlist);
143
                        foreach ($tplsetlist as $key => $name) {
144
                            $ele->addOption($key, $name);
145
                        }
146
                        break;
147
148
                    case 'cpanel':
149
                        $ele = new Xoops\Form\Hidden($obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
150
                        /*
151
                        $ele = new Xoops\Form\Select($title, $config[$i]->getVar('conf_name'), $config[$i]->getConfValueForOutput());
152
                        XoopsLoad::load("cpanel", "system");
153
                        $list = XoopsSystemCpanel::getGuis();
154
                        $ele->addOptionArray($list);  */
155
                        break;
156
157
                    case 'timezone':
158
                        $ele = new Xoops\Form\SelectTimeZone($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
159
                        break;
160
161
                    case 'language':
162
                        $ele = new Xoops\Form\SelectLanguage($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
163
                        break;
164
165
                    case 'locale':
166
                        $ele = new Xoops\Form\SelectLocale($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
167
                        break;
168
169
                    case 'startpage':
170
                        $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
171
172
                        $module_handler = $xoops->getHandlerModule();
173
                        $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
174
                        $criteria->add(new Criteria('isactive', 1));
175
                        $moduleslist = $module_handler->getNameList($criteria, true);
176
                        $moduleslist['--'] = XoopsLocale::NONE;
177
                        $ele->addOptionArray($moduleslist);
178
                        break;
179
180
                    case 'group':
181
                        $ele = new Xoops\Form\SelectGroup($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 1, false);
182
                        break;
183
184
                    case 'group_multi':
185
                        $ele = new Xoops\Form\SelectGroup($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 5, true);
186
                        break;
187
188
                    // RMV-NOTIFY: added 'user' and 'user_multi'
189
                    case 'user':
190
                        $ele = new Xoops\Form\SelectUser($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 1, false);
191
                        break;
192
193
                    case 'user_multi':
194
                        $ele = new Xoops\Form\SelectUser($title, $obj[$i]->getVar('conf_name'), false, $obj[$i]->getConfValueForOutput(), 5, true);
195
                        break;
196
                    case 'module_cache':
197
                        $module_handler = $xoops->getHandlerModule();
198
                        $modules = $module_handler->getObjectsArray(new Criteria('hasmain', 1), true);
199
                        $currrent_val = $obj[$i]->getConfValueForOutput();
200
                        $cache_options = array(
201
                            '0'       => XoopsLocale::NO_CACHE,
202
                            '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
203
                            '60'      => XoopsLocale::ONE_MINUTE,
204
                            '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
205
                            '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
206
                            '3600'    => XoopsLocale::ONE_HOUR,
207
                            '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
208
                            '86400'   => XoopsLocale::ONE_DAY,
209
                            '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
210
                            '604800'  => XoopsLocale::ONE_WEEK,
211
                            '2592000' => XoopsLocale::ONE_MONTH
212
                        );
213
                        if (count($modules) > 0) {
214
                            $ele = new Xoops\Form\ElementTray($title, '<br />');
215
                            foreach (array_keys($modules) as $mid) {
216
                                $c_val = isset($currrent_val[$mid]) ? (int)($currrent_val[$mid]) : null;
217
                                $selform = new Xoops\Form\Select($modules[$mid]->getVar('name'), $obj[$i]->getVar('conf_name') . "[$mid]", $c_val);
218
                                $selform->addOptionArray($cache_options);
219
                                $ele->addElement($selform);
220
                                unset($selform);
221
                            }
222
                        } else {
223
                            $ele = new Xoops\Form\Label($title, SystemLocale::NO_MODULE_TO_CACHE);
224
                        }
225
                        break;
226
227
                    case 'site_cache':
228
                        $ele = new Xoops\Form\Select($title, $obj[$i]->getVar('conf_name'), $obj[$i]->getConfValueForOutput());
229
                        $ele->addOptionArray(array(
230
                            '0'       => XoopsLocale::NO_CACHE,
231
                            '30'      => sprintf(XoopsLocale::F_SECONDS, 30),
232
                            '60'      => XoopsLocale::ONE_MINUTE,
233
                            '300'     => sprintf(XoopsLocale::F_MINUTES, 5),
234
                            '1800'    => sprintf(XoopsLocale::F_MINUTES, 30),
235
                            '3600'    => XoopsLocale::ONE_HOUR,
236
                            '18000'   => sprintf(XoopsLocale::F_HOURS, 5),
237
                            '86400'   => XoopsLocale::ONE_DAY,
238
                            '259200'  => sprintf(XoopsLocale::F_DAYS, 3),
239
                            '604800'  => XoopsLocale::ONE_WEEK,
240
                            '2592000' => XoopsLocale::ONE_MONTH
241
                        ));
242
                        break;
243
244
                    case 'password':
245
                        $myts = \Xoops\Core\Text\Sanitizer::getInstance();
246
                        $ele = new Xoops\Form\Password($title, $obj[$i]->getVar('conf_name'), 32, 255, $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
247
                        break;
248
249
                    case 'color':
250
                        $myts = \Xoops\Core\Text\Sanitizer::getInstance();
251
                        $ele = new Xoops\Form\ColorPicker($title, $obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
252
                        break;
253
254
                    case 'hidden':
255
                        $myts = \Xoops\Core\Text\Sanitizer::getInstance();
256
                        $ele = new Xoops\Form\Hidden($obj[$i]->getVar('conf_name'), $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
257
                        break;
258
259
                    case 'textbox':
260
                    default:
261
                        $myts = \Xoops\Core\Text\Sanitizer::getInstance();
262
                        $ele = new Xoops\Form\Text($title, $obj[$i]->getVar('conf_name'), 5, 255, $myts->htmlSpecialChars($obj[$i]->getConfValueForOutput()));
263
                        break;
264
                }
265
                $hidden = new Xoops\Form\Hidden('conf_ids[]', $obj[$i]->getVar('conf_id'));
266
                if (isset($ele)) {
267
                    $ele->setDescription($desc);
268
                    if ($obj[$i]->getVar('conf_formtype') !== 'hidden') {
269
                        $name = 'default';
270
                        if (isset($configNames[$obj[$i]->getVar('conf_name')]['category'])) {
271
                            $name = $configNames[$obj[$i]->getVar('conf_name')]['category'];
272
                        }
273
                        $tabs[$name]->addElement($ele);
274
                    } else {
275
                        $this->addElement($ele);
276
                    }
277
                    $this->addElement($hidden);
278
                    unset($ele);
279
                    unset($hidden);
280
                }
281
            }
282
            foreach (array_keys($tabs) as $name) {
283
                if ($tabs[$name]->getElements()) {
284
                    $tabTray->addElement($tabs[$name]);
285
                }
286
            }
287
            $this->addElement($tabTray);
288
            $this->addElement(new Xoops\Form\Hidden('op', 'save'));
289
            $this->addElement(new Xoops\Form\Hidden('mid', $mod->getVar('mid')));
0 ignored issues
show
Bug introduced by
It seems like $mod->getVar('mid') can also be of type array; however, parameter $value of Xoops\Form\Hidden::__construct() 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

289
            $this->addElement(new Xoops\Form\Hidden('mid', /** @scrutinizer ignore-type */ $mod->getVar('mid')));
Loading history...
290
            $this->addElement(new Xoops\Form\Button('', 'button', XoopsLocale::A_SUBMIT, 'submit'));
291
        }
292
    }
293
}
294