Passed
Push — master ( 05ee30...e8b784 )
by Michael
05:15
created

updateBlock()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 55
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 38
c 1
b 1
f 0
dl 0
loc 55
rs 7.3166
cc 11
nc 12
nop 9

How to fix   Long Method    Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * PHP version 5
11
 *
12
 * @category        Module
13
 * @author          XOOPS Development Team
14
 * @copyright       XOOPS Project
15
 * @link            https://xoops.org
16
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 */
18
19
use Xmf\Module\Admin;
20
use Xmf\Request;
21
use XoopsModules\Adslight\{
22
    Common\Configurator,
23
    Helper
24
};
25
26
/** @var Admin $adminObject */
27
/** @var Helper $helper */
28
29
require __DIR__ . '/admin_header.php';
30
31
$moduleDirName      = \basename(\dirname(__DIR__));
32
$moduleDirNameUpper = \mb_strtoupper($moduleDirName);
33
34
if (!is_object($GLOBALS['xoopsUser']) || !is_object($xoopsModule)
35
    || !$GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) {
36
    exit(constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR403'));
37
}
38
if ($GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) {
39
    require_once XOOPS_ROOT_PATH . '/kernel/block.php';
40
    $op = 'list';
41
    if (isset($_POST)) {
42
        foreach ($_POST as $k => $v) {
43
            ${$k} = $v;
44
        }
45
    }
46
    /*
47
    if (Request::hasVar('op', 'GET')) {
48
        if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op'] || 'clone' === $_GET['op']
49
            || 'edit' === $_GET['op']) {
50
            $op  = $_GET['op'];
51
            $bid = Request::getInt('bid', 0, 'GET');
52
        }
53
    */
54
55
    $op = Request::getString('op', $op);
56
    if (in_array($op, ['edit', 'delete', 'delete_ok', 'clone'])) {
57
        $bid = Request::getInt('bid', 0, 'GET');
58
    }
59
60
    function listBlocks()
61
    {
62
        global $xoopsModule, $pathIcon16;
63
        //        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
64
        xoops_load('xoopslist');
65
        $moduleDirName      = \basename(\dirname(__DIR__));
66
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
67
        /** @var \XoopsMySQLDatabase $db */
68
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
69
70
        $adminObject = Admin::getInstance();
71
        $adminObject->displayNavigation(basename(__FILE__));
72
73
        xoops_loadLanguage('admin', 'system');
74
        xoops_loadLanguage('admin/blocksadmin', 'system');
75
        xoops_loadLanguage('admin/groups', 'system');
76
77
        $configurator = new Configurator();
78
        $icons        = $configurator->icons;
79
80
        /** @var \XoopsModuleHandler $moduleHandler */
81
        $moduleHandler = xoops_getHandler('module');
82
        /** @var \XoopsMemberHandler $memberHandler */
83
        $memberHandler = xoops_getHandler('member');
84
        /** @var \XoopsGroupPermHandler $grouppermHandler */
85
        $grouppermHandler = xoops_getHandler('groupperm');
86
        $groups           = $memberHandler->getGroups();
87
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', 1));
88
        $criteria->add(new \Criteria('isactive', 1));
89
        $moduleList     = $moduleHandler->getList($criteria);
90
        $moduleList[-1] = _AM_SYSTEM_BLOCKS_TOPPAGE;
91
        $moduleList[0]  = _AM_SYSTEM_BLOCKS_ALLPAGES;
92
        ksort($moduleList);
93
        echo "
94
        <h4 style='text-align:left;'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
95
        echo "<form action='" . Request::getString('SCRIPT_NAME', '', 'SERVER') . "' name='blockadmin' method='post'>";
96
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
97
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
98
        <tr valign='middle'><th align='center'>"
99
             . constant('CO_' . $moduleDirNameUpper . '_' . 'TITLE')
100
             . "</th><th align='center' nowrap='nowrap'>"
101
             . constant('CO_' . $moduleDirNameUpper . '_' . 'SIDE')
102
             . '<br>'
103
             . _LEFT
104
             . '-'
105
             . _CENTER
106
             . '-'
107
             . _RIGHT
108
             . "</th><th align='center'>"
109
             . constant(
110
                 'CO_' . $moduleDirNameUpper . '_' . 'WEIGHT'
111
             )
112
             . "</th><th align='center'>"
113
             . constant('CO_' . $moduleDirNameUpper . '_' . 'VISIBLE')
114
             . "</th><th align='center'>"
115
             . _AM_SYSTEM_BLOCKS_VISIBLEIN
116
             . "</th><th align='center'>"
117
             . _AM_SYSTEM_ADGS
118
             . "</th><th align='center'>"
119
             . _AM_SYSTEM_BLOCKS_BCACHETIME
120
             . "</th><th align='center'>"
121
             . constant('CO_' . $moduleDirNameUpper . '_' . 'ACTION')
122
             . '</th></tr>
123
        ';
124
        $blockArray = \XoopsBlock::getByModule($xoopsModule->mid());
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::getByModule() has been deprecated: (This also appears, dead, in XoopsBlockHandler) ( Ignorable by Annotation )

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

124
        $blockArray = /** @scrutinizer ignore-deprecated */ \XoopsBlock::getByModule($xoopsModule->mid());

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

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

Loading history...
125
        $blockCount = count($blockArray);
0 ignored issues
show
Unused Code introduced by
The assignment to $blockCount is dead and can be removed.
Loading history...
126
        $class      = 'even';
127
        $cachetimes = [
128
            0       => _NOCACHE,
129
            30      => sprintf(_SECONDS, 30),
130
            60      => _MINUTE,
131
            300     => sprintf(_MINUTES, 5),
132
            1800    => sprintf(_MINUTES, 30),
133
            3600    => _HOUR,
134
            18000   => sprintf(_HOURS, 5),
135
            86400   => _DAY,
136
            259200  => sprintf(_DAYS, 3),
137
            604800  => _WEEK,
138
            2592000 => _MONTH,
139
        ];
140
        foreach ($blockArray as $i) {
141
            $groupsPerms = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
142
            $sql         = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . $i->getVar('bid');
143
            $result      = $db->query($sql);
144
            $modules     = [];
145
            while (false !== ($row = $db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, 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
            while (false !== ($row = $db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
146
                $modules[] = (int)$row['module_id'];
147
            }
148
149
            $cachetimeOptions = '';
150
            foreach ($cachetimes as $cachetime => $cachetimeName) {
151
                if ($i->getVar('bcachetime') == $cachetime) {
152
                    $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n";
153
                } else {
154
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
155
                }
156
            }
157
158
            $sel0 = $sel1 = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = $ssel5 = $ssel6 = $ssel7 = '';
159
            if (1 === $i->getVar('visible')) {
160
                $sel1 = ' checked';
161
            } else {
162
                $sel0 = ' checked';
163
            }
164
            if (XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
165
                $ssel0 = ' checked';
166
            } elseif (XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
167
                $ssel1 = ' checked';
168
            } elseif (XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
169
                $ssel2 = ' checked';
170
            } elseif (XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
171
                $ssel4 = ' checked';
172
            } elseif (XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
173
                $ssel3 = ' checked';
174
            } elseif (XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
175
                $ssel5 = ' checked';
176
            } elseif (XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
177
                $ssel6 = ' checked';
178
            } elseif (XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
179
                $ssel7 = ' checked';
180
            }
181
            if ('' === $i->getVar('title')) {
182
                $title = '&nbsp;';
183
            } else {
184
                $title = $i->getVar('title');
185
            }
186
            $name = $i->getVar('name');
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
187
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
188
                 . $i->getVar('bid')
189
                 . "]' value='"
190
                 . $title
191
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
192
                    <div align='center' >
193
                    <input type='radio' name='side["
194
                 . $i->getVar('bid')
195
                 . "]' value='"
196
                 . XOOPS_CENTERBLOCK_LEFT
197
                 . "'$ssel2>
198
                        <input type='radio' name='side["
199
                 . $i->getVar('bid')
200
                 . "]' value='"
201
                 . XOOPS_CENTERBLOCK_CENTER
202
                 . "'$ssel3>
203
                    <input type='radio' name='side["
204
                 . $i->getVar('bid')
205
                 . "]' value='"
206
                 . XOOPS_CENTERBLOCK_RIGHT
207
                 . "'$ssel4>
208
                    </div>
209
                    <div>
210
                        <span style='float:right;'><input type='radio' name='side["
211
                 . $i->getVar('bid')
212
                 . "]' value='"
213
                 . XOOPS_SIDEBLOCK_RIGHT
214
                 . "'$ssel1></span>
215
                    <div align='left'><input type='radio' name='side["
216
                 . $i->getVar('bid')
217
                 . "]' value='"
218
                 . XOOPS_SIDEBLOCK_LEFT
219
                 . "'$ssel0></div>
220
                    </div>
221
                    <div align='center'>
222
                    <input type='radio' name='side["
223
                 . $i->getVar('bid')
224
                 . "]' value='"
225
                 . XOOPS_CENTERBLOCK_BOTTOMLEFT
226
                 . "'$ssel5>
227
                        <input type='radio' name='side["
228
                 . $i->getVar('bid')
229
                 . "]' value='"
230
                 . XOOPS_CENTERBLOCK_BOTTOM
231
                 . "'$ssel7>
232
                    <input type='radio' name='side["
233
                 . $i->getVar('bid')
234
                 . "]' value='"
235
                 . XOOPS_CENTERBLOCK_BOTTOMRIGHT
236
                 . "'$ssel6>
237
                    </div>
238
                </td><td class='$class' align='center'><input type='text' name='weight["
239
                 . $i->getVar('bid')
240
                 . "]' value='"
241
                 . $i->getVar('weight')
242
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
243
                 . $i->getVar('bid')
244
                 . "]' value='1'$sel1>"
245
                 . _YES
246
                 . "&nbsp;<input type='radio' name='visible["
247
                 . $i->getVar('bid')
248
                 . "]' value='0'$sel0>"
249
                 . _NO
250
                 . '</td>';
251
252
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
253
            foreach ($moduleList as $k => $v) {
254
                echo "<option value='$k'" . (in_array($k, $modules) ? 'selected' : '') . ">$v</option>";
255
            }
256
            echo '</select></td>';
257
258
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
259
            foreach ($groups as $grp) {
260
                echo "<option value='" . $grp->getVar('groupid') . "' " . (in_array($grp->getVar('groupid'), $groupsPerms) ? 'selected' : '') . '>' . $grp->getVar('name') . '</option>';
261
            }
262
            echo '</select></td>';
263
264
            // Cache lifetime
265
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
266
                                    </td>';
267
268
            // Actions
269
270
            echo "<td class='$class' align='center'><a href='blocksadmin.php?op=edit&amp;bid="
271
                 . $i->getVar('bid')
272
                 . "'>"
273
                 . $icons->edit
274
                 . "</a> <a href='blocksadmin.php?op=clone&amp;bid="
275
                 . $i->getVar('bid')
276
                 . "'><img src="
277
                 . $pathIcon16
278
                 . '/editcopy.png'
279
                 . " alt='"
280
                 . _CLONE
281
                 . "' title='"
282
                 . _CLONE
283
                 . "'>
284
                 </a>";
285
            if (!in_array($i->getVar('block_type'), ['S', 'M'])) {
286
                echo "&nbsp;<a href='" . XOOPS_URL . '/modules/system/admin.php?fct=blocksadmin&amp;op=delete&amp;bid=' . $i->getVar('bid') . "'>" . $icons->delete . '</a>';
287
            }
288
            echo "
289
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
290
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
291
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
292
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
293
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
294
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
295
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
296
            </td></tr>
297
            ";
298
            $class = ('even' === $class) ? 'odd' : 'even';
299
        }
300
        echo "<tr><td class='foot' align='center' colspan='8'>
301
        <input type='hidden' name='op' value='order'>
302
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
303
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
304
        </td></tr></table>
305
        </form>
306
        <br><br>";
307
    }
308
309
    /**
310
     * @param int $bid
311
     */
312
    function cloneBlock($bid)
313
    {
314
        xoops_cp_header();
315
316
        $adminObject = Admin::getInstance();
317
        $adminObject->displayNavigation(basename(__FILE__));
318
        $moduleDirName      = \basename(\dirname(__DIR__));
319
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
320
        xoops_loadLanguage('admin', 'system');
321
        xoops_loadLanguage('admin/blocksadmin', 'system');
322
        xoops_loadLanguage('admin/groups', 'system');
323
324
        //        mpu_adm_menu();
325
        $myblock = new \XoopsBlock($bid);
326
        $db      = \XoopsDatabaseFactory::getDatabaseConnection();
327
        $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
328
        $result  = $db->query($sql);
329
        $modules = [];
330
        while (false !== ($row = $db->fetchArray($result))) {
331
            $modules[] = (int)$row['module_id'];
332
        }
333
        $isCustom = (in_array($myblock->getVar('block_type'), ['C', 'E']));
334
        $block    = [
0 ignored issues
show
Unused Code introduced by
The assignment to $block is dead and can be removed.
Loading history...
335
            'title'      => $myblock->getVar('title') . ' Clone',
336
            'form_title' => constant('CO_' . $moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
337
            'name'       => $myblock->getVar('name'),
338
            'side'       => $myblock->getVar('side'),
339
            'weight'     => $myblock->getVar('weight'),
340
            'visible'    => $myblock->getVar('visible'),
341
            'content'    => $myblock->getVar('content', 'N'),
342
            'modules'    => $modules,
343
            'is_custom'  => $isCustom,
344
            'ctype'      => $myblock->getVar('c_type'),
345
            'bcachetime' => $myblock->getVar('bcachetime'),
346
            'op'         => 'clone_ok',
347
            'bid'        => $myblock->getVar('bid'),
348
            'edit_form'  => $myblock->getOptions(),
349
            'template'   => $myblock->getVar('template'),
350
            'options'    => $myblock->getVar('options'),
351
        ];
352
        echo '<a href="blocksadmin.php">' . constant('CO_' . $moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_SYSTEM_BLOCKS_CLONEBLOCK . '<br><br>';
353
        require_once __DIR__ . '/blockform.php';
354
        /** @var XoopsThemeForm $form */
355
        $form->display();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $form seems to be never defined.
Loading history...
356
        //        xoops_cp_footer();
357
        require_once __DIR__ . '/admin_footer.php';
358
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
359
    }
360
361
    /**
362
     * @param int               $bid
363
     * @param string            $bside
364
     * @param int               $bweight
365
     * @param bool              $bvisible
366
     * @param int               $bcachetime
367
     * @param array             $bmodule
368
     * @param null|array|string $options
369
     */
370
    function isBlockCloned($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options = null)
371
    {
372
        xoops_loadLanguage('admin', 'system');
373
        xoops_loadLanguage('admin/blocksadmin', 'system');
374
        xoops_loadLanguage('admin/groups', 'system');
375
376
        $moduleDirName      = \basename(\dirname(__DIR__));
377
        $moduleDirNameUpper = mb_strtoupper($moduleDirName);
378
379
        $block = new \XoopsBlock($bid);
380
        $clone = $block->xoopsClone();
381
        if (empty($bmodule)) {
382
            xoops_cp_header();
383
            xoops_error(sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'NOTSELNG'), constant('CO_' . $moduleDirNameUpper . '_' . 'VISIBLEIN')));
384
            xoops_cp_footer();
385
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
386
        }
387
        $clone->setVar('side', $bside);
388
        $clone->setVar('weight', $bweight);
389
        $clone->setVar('visible', $bvisible);
390
        //$clone->setVar('content', $_POST['bcontent']);
391
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
392
        $clone->setVar('bcachetime', $bcachetime);
393
        if ($options && is_array($options)) {
394
            $options = implode('|', $options);
395
            $clone->setVar('options', $options);
396
        }
397
        $clone->setVar('bid', 0);
398
        if (in_array($block->getVar('block_type'), ['C', 'E'])) {
399
            $clone->setVar('block_type', 'E');
400
        } else {
401
            $clone->setVar('block_type', 'D');
402
        }
403
        $newid = $clone->store();
404
        if (!$newid) {
405
            xoops_cp_header();
406
            $clone->getHtmlErrors();
407
            xoops_cp_footer();
408
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
409
        }
410
        if ('' !== $clone->getVar('template')) {
411
            /** @var \XoopsTplfileHandler $tplfileHandler */
412
            $tplfileHandler = xoops_getHandler('tplfile');
413
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
414
            if (count($btemplate) > 0) {
415
                $tplclone = $btemplate[0]->xoopsClone();
416
                $tplclone->setVar('tpl_id', 0);
417
                $tplclone->setVar('tpl_refid', $newid);
418
                $tplfileHandler->insert($tplclone);
419
            }
420
        }
421
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
422
        foreach ($bmodule as $bmid) {
423
            $sql = 'INSERT INTO ' . $db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
424
            $db->query($sql);
425
        }
426
        $groups = &$GLOBALS['xoopsUser']->getGroups();
427
        foreach ($groups as $iValue) {
428
            $sql = 'INSERT INTO ' . $db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
429
            $db->query($sql);
430
        }
431
        redirect_header('blocksadmin.php?op=listar', 1, _AM_SYSTEM_BLOCKS_DBUPDATED);
432
    }
433
434
    /**
435
     * @param int      $bid
436
     * @param string   $title
437
     * @param int      $weight
438
     * @param bool     $visible
439
     * @param string   $side
440
     * @param int      $bcachetime
441
     * @param null|int $bmodule
442
     */
443
    function setOrder($bid, $title, $weight, $visible, $side, $bcachetime, $bmodule = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $bmodule is not used and could be removed. ( Ignorable by Annotation )

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

443
    function setOrder($bid, $title, $weight, $visible, $side, $bcachetime, /** @scrutinizer ignore-unused */ $bmodule = null): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
444
    {
445
        $myblock = new \XoopsBlock($bid);
446
        $myblock->setVar('title', $title);
447
        $myblock->setVar('weight', $weight);
448
        $myblock->setVar('visible', $visible);
449
        $myblock->setVar('side', $side);
450
        $myblock->setVar('bcachetime', $bcachetime);
451
        //        $myblock->store();
452
        /** @var \XoopsBlockHandler $blockHandler */
453
        $blockHandler = xoops_getHandler('block');
454
        return $blockHandler->insert($myblock);
455
    }
456
457
    /**
458
     * @param int $bid
459
     */
460
    function editBlock($bid)
461
    {
462
        xoops_cp_header();
463
        /** @var \Xmf\Module\Admin $adminObject */
464
        $adminObject = Admin::getInstance();
465
        $adminObject->displayNavigation(basename(__FILE__));
466
        $moduleDirName      = \basename(\dirname(__DIR__));
467
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
468
469
        xoops_loadLanguage('admin', 'system');
470
        xoops_loadLanguage('admin/blocksadmin', 'system');
471
        xoops_loadLanguage('admin/groups', 'system');
472
        //        mpu_adm_menu();
473
        $myblock = new \XoopsBlock($bid);
474
        $db      = \XoopsDatabaseFactory::getDatabaseConnection();
475
        $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
476
        $result  = $db->query($sql);
477
        $modules = [];
478
        while (false !== ($row = $db->fetchArray($result))) {
479
            $modules[] = (int)$row['module_id'];
480
        }
481
        $isCustom = (in_array($myblock->getVar('block_type'), ['C', 'E']));
482
        $block    = [
0 ignored issues
show
Unused Code introduced by
The assignment to $block is dead and can be removed.
Loading history...
483
            'title'      => $myblock->getVar('title'),
484
            'form_title' => constant('CO_' . $moduleDirNameUpper . '_' . 'BLOCKS_EDITBLOCK'),
485
            //        'name'       => $myblock->getVar('name'),
486
            'side'       => $myblock->getVar('side'),
487
            'weight'     => $myblock->getVar('weight'),
488
            'visible'    => $myblock->getVar('visible'),
489
            'content'    => $myblock->getVar('content', 'N'),
490
            'modules'    => $modules,
491
            'is_custom'  => $isCustom,
492
            'ctype'      => $myblock->getVar('c_type'),
493
            'bcachetime' => $myblock->getVar('bcachetime'),
494
            'op'         => 'edit_ok',
495
            'bid'        => $myblock->getVar('bid'),
496
            'edit_form'  => $myblock->getOptions(),
497
            'template'   => $myblock->getVar('template'),
498
            'options'    => $myblock->getVar('options'),
499
        ];
500
        echo '<a href="blocksadmin.php">' . constant('CO_' . $moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>';
501
        require_once __DIR__ . '/blockform.php';
502
        /** @var XoopsThemeForm $form */
503
        $form->display();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $form seems to be never defined.
Loading history...
504
        //        xoops_cp_footer();
505
        require_once __DIR__ . '/admin_footer.php';
506
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
507
    }
508
509
    /**
510
     * @param int               $bid
511
     * @param string            $btitle
512
     * @param string            $bside
513
     * @param int               $bweight
514
     * @param bool              $bvisible
515
     * @param int               $bcachetime
516
     * @param null|array        $bmodule
517
     * @param null|array|string $options
518
     * @param null|array        $groups
519
     */
520
    function updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups)
521
    {
522
        $myblock = new \XoopsBlock($bid);
523
        $myblock->setVar('title', $btitle);
524
        $myblock->setVar('weight', $bweight);
525
        $myblock->setVar('visible', $bvisible);
526
        $myblock->setVar('side', $bside);
527
        $myblock->setVar('bcachetime', $bcachetime);
528
        $myblock->setVar('module', $bmodule);
529
        $myblock->setVar('groups', $groups);
530
        $helper = Helper::getInstance();
531
        $helper->loadLanguage('common');
532
        //update block options
533
        if (is_iterable($options) && count($options) > 0) {
0 ignored issues
show
Bug introduced by
It seems like $options can also be of type null and string; however, parameter $value of count() does only seem to accept Countable|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

533
        if (is_iterable($options) && count(/** @scrutinizer ignore-type */ $options) > 0) {
Loading history...
534
            //Convert array values to comma-separated
535
            foreach ($options as $i => $iValue) {
536
                if (is_array($iValue)) {
537
                    $options[$i] = implode(',', $iValue);
538
                }
539
            }
540
            $options = implode('|', $options);
0 ignored issues
show
Bug introduced by
It seems like $options can also be of type null and string; however, parameter $pieces of implode() 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

540
            $options = implode('|', /** @scrutinizer ignore-type */ $options);
Loading history...
541
            $myblock->setVar('options', $options);
542
        }
543
        //        $myblock->store();
544
        /** @var \XoopsBlockHandler $blockHandler */
545
        $blockHandler = xoops_getHandler('block');
546
        $blockHandler->insert($myblock);
547
548
        global $xoopsDB;
549
550
        $moduleDirName      = \basename(\dirname(__DIR__));
551
        $moduleDirNameUpper = \mb_strtoupper($moduleDirName);
552
553
        if (!empty($bmodule) && count($bmodule) > 0) {
554
            $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $xoopsDB->prefix('block_module_link'), $bid);
555
            $xoopsDB->query($sql);
556
            if (in_array(0, $bmodule)) {
557
                $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid, 0);
558
                $xoopsDB->query($sql);
559
            } else {
560
                foreach ($bmodule as $bmid) {
561
                    $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid, (int)$bmid);
562
                    $xoopsDB->query($sql);
563
                }
564
            }
565
        }
566
        $sql = sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $xoopsDB->prefix('group_permission'), $bid);
567
        $xoopsDB->query($sql);
568
        if (!empty($groups)) {
569
            foreach ($groups as $grp) {
570
                $sql = sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $xoopsDB->prefix('group_permission'), $grp, $bid);
571
                $xoopsDB->query($sql);
572
            }
573
        }
574
        redirect_header($_SERVER['SCRIPT_NAME'], 1, constant('CO_' . $moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
575
    }
576
577
    if ('list' === $op) {
578
        xoops_cp_header();
579
        //$adminObject->displayNavigation(basename(__FILE__));
580
        listBlocks();
581
        require_once __DIR__ . '/admin_footer.php';
582
        exit();
583
    }
584
585
    if ('order' === $op) {
586
        if (!$GLOBALS['xoopsSecurity']->check()) {
587
            redirect_header($_SERVER['SCRIPT_NAME'], 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
588
        }
589
        foreach (array_keys($bid) as $i) {
590
            if ($oldtitle[$i] !== $title[$i] || $oldweight[$i] !== $weight[$i] || $oldvisible[$i] !== $visible[$i]
591
                || $oldside[$i] !== $side[$i]
592
                || $oldbcachetime[$i] !== $bcachetime[$i]) {
593
                setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);
594
            }
595
            if (!empty($bmodule[$i]) && count($bmodule[$i]) > 0) {
596
                $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $xoopsDB->prefix('block_module_link'), $bid[$i]);
597
                $xoopsDB->query($sql);
598
                if (in_array(0, $bmodule[$i])) {
599
                    $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid[$i], 0);
600
                    $xoopsDB->query($sql);
601
                } else {
602
                    foreach ($bmodule[$i] as $bmid) {
603
                        $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid[$i], (int)$bmid);
604
                        $xoopsDB->query($sql);
605
                    }
606
                }
607
            }
608
            $sql = sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $xoopsDB->prefix('group_permission'), $bid[$i]);
609
            $xoopsDB->query($sql);
610
            if (!empty($groups[$i])) {
611
                foreach ($groups[$i] as $grp) {
612
                    $sql = sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $xoopsDB->prefix('group_permission'), $grp, $bid[$i]);
613
                    $xoopsDB->query($sql);
614
                }
615
            }
616
        }
617
        redirect_header($_SERVER['SCRIPT_NAME'], 1, constant('CO_' . $moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
618
    }
619
    if ('clone' === $op) {
620
        cloneBlock($bid);
621
    }
622
623
    if ('edit' === $op) {
624
        editBlock($bid);
625
    }
626
627
    if ('edit_ok' === $op) {
628
        updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups);
629
    }
630
631
    if ('clone_ok' === $op) {
632
        isBlockCloned($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options);
633
    }
634
} else {
635
    echo constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR403');
636
}
637