Passed
Push — master ( 08b589...961b46 )
by Michael
08:46
created

listBlocks()   F

Complexity

Conditions 22
Paths 3457

Size

Total Lines 223
Code Lines 176

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 22
eloc 176
nc 3457
nop 0
dl 0
loc 223
rs 0
c 1
b 0
f 0

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
 * PHP version 5
8
 *
9
 * @category        Module
10
 * @author          XOOPS Development Team
11
 * @copyright       XOOPS Project
12
 * @link            https://www.xoops.org
13
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14
 */
15
use Xmf\Request;
16
use XoopsModules\Wflinks\{
17
    Helper
18
};
19
/** @var Helper $helper */
20
21
require __DIR__ . '/admin_header.php';
22
23
$moduleDirName = basename(dirname(__DIR__));
24
$moduleDirNameUpper = mb_strtoupper($moduleDirName); //$capsDirName
25
26
$helper->loadLanguage('blocksadmin');
27
28
if (!is_object($GLOBALS['xoopsUser']) || !is_object($xoopsModule)
29
    || !$GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) {
30
    exit(constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR403'));
31
}
32
if ($GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) {
33
    require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
34
    $op = 'list';
35
    if (isset($_POST)) {
36
        foreach ($_POST as $k => $v) {
37
            ${$k} = $v;
38
        }
39
    }
40
    /*
41
    if (\Xmf\Request::hasVar('op', 'GET')) {
42
        if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op'] || 'clone' === $_GET['op']
43
            || 'edit' === $_GET['op']) {
44
            $op  = $_GET['op'];
45
            $bid = \Xmf\Request::getInt('bid', 0, 'GET');
46
        }
47
    */
48
49
    $op = Request::getString('op', $op);
50
    if (in_array($op, ['edit', 'delete', 'delete_ok', 'clone'])) {
51
        $bid = Request::getInt('bid', 0, 'GET');
52
    }
53
54
    function listBlocks()
55
    {
56
        global $xoopsModule, $pathIcon16;
57
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
58
        $moduleDirName = basename(dirname(__DIR__));
59
        $moduleDirNameUpper = mb_strtoupper($moduleDirName); //$capsDirName
60
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
61
        xoops_loadLanguage('admin', 'system');
62
        xoops_loadLanguage('admin/blocksadmin', 'system');
63
        xoops_loadLanguage('admin/groups', 'system');
64
65
        /** @var \XoopsModuleHandler $moduleHandler */
66
        $moduleHandler = xoops_getHandler('module');
67
        /** @var \XoopsMemberHandler $memberHandler */
68
        $memberHandler = xoops_getHandler('member');
69
        /** @var \XoopsGroupPermHandler $grouppermHandler */
70
        $grouppermHandler = xoops_getHandler('groupperm');
71
        $groups = $memberHandler->getGroups();
72
        $criteria = new \CriteriaCompo(new \Criteria('hasmain', 1));
73
        $criteria->add(new \Criteria('isactive', 1));
74
        $module_list = $moduleHandler->getList($criteria);
75
        $module_list[-1] = _AM_SYSTEM_BLOCKS_TOPPAGE;
76
        $module_list[0] = _AM_SYSTEM_BLOCKS_ALLPAGES;
77
        ksort($module_list);
78
        echo "
79
        <h4 style='text-align:left;'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
80
        $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleHandler is dead and can be removed.
Loading history...
81
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
82
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
83
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
84
        <tr valign='middle'><th align='center'>"
85
             . constant('CO_' . $moduleDirNameUpper . '_' . 'TITLE')
86
             . "</th><th align='center' nowrap='nowrap'>"
87
             . constant('CO_' . $moduleDirNameUpper . '_' . 'SIDE')
88
             . '<br>'
89
             . _LEFT
90
             . '-'
91
             . _CENTER
92
             . '-'
93
             . _RIGHT
94
             . "</th><th align='center'>"
95
             . constant('CO_' . $moduleDirNameUpper . '_' . 'WEIGHT')
96
             . "</th><th align='center'>"
97
             . constant('CO_' . $moduleDirNameUpper . '_' . 'VISIBLE')
98
             . "</th><th align='center'>"
99
             . _AM_SYSTEM_BLOCKS_VISIBLEIN
100
             . "</th><th align='center'>"
101
             . _AM_SYSTEM_ADGS
102
             . "</th><th align='center'>"
103
             . _AM_SYSTEM_BLOCKS_BCACHETIME
104
             . "</th><th align='center'>"
105
             . constant('CO_' . $moduleDirNameUpper . '_' . 'ACTION')
106
             . '</th></tr>
107
        ';
108
        $block_arr = \XoopsBlock::getByModule($xoopsModule->mid());
0 ignored issues
show
Bug introduced by
The method getByModule() does not exist on XoopsBlock. ( Ignorable by Annotation )

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

108
        /** @scrutinizer ignore-call */ 
109
        $block_arr = \XoopsBlock::getByModule($xoopsModule->mid());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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

360
        if (isset($options) && (count(/** @scrutinizer ignore-type */ $options) > 0)) {
Loading history...
361
            $options = implode('|', $options);
0 ignored issues
show
Bug introduced by
It seems like $options can also be of type 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

361
            $options = implode('|', /** @scrutinizer ignore-type */ $options);
Loading history...
362
            $clone->setVar('options', $options);
363
        }
364
        $clone->setVar('bid', 0);
365
        if ('C' === $block->getVar('block_type') || 'E' === $block->getVar('block_type')) {
366
            $clone->setVar('block_type', 'E');
367
        } else {
368
            $clone->setVar('block_type', 'D');
369
        }
370
        $newid = $clone->store();
371
        if (!$newid) {
372
            xoops_cp_header();
373
            $clone->getHtmlErrors();
374
            xoops_cp_footer();
375
            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...
376
        }
377
        if ('' !== $clone->getVar('template')) {
378
            /** @var \XoopsTplfileHandler $tplfileHandler */
379
            $tplfileHandler = xoops_getHandler('tplfile');
380
            $btemplate = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
381
            if (count($btemplate) > 0) {
382
                $tplclone = $btemplate[0]->xoopsClone();
383
                $tplclone->setVar('tpl_id', 0);
384
                $tplclone->setVar('tpl_refid', $newid);
385
                $tplfileHandler->insert($tplclone);
386
            }
387
        }
388
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
389
        foreach ($bmodule as $bmid) {
390
            $sql = 'INSERT INTO ' . $db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
391
            $db->query($sql);
392
        }
393
        $groups = &$GLOBALS['xoopsUser']->getGroups();
394
        $count = count($groups);
395
        for ($i = 0; $i < $count; ++$i) {
396
            $sql = 'INSERT INTO ' . $db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $groups[$i] . ', ' . $newid . ", 1, 'block_read')";
397
            $db->query($sql);
398
        }
399
        redirect_header('blocksadmin.php?op=listar', 1, _AM_DBUPDATED);
400
    }
401
402
    /**
403
     * @param int               $bid
404
     * @param string            $title
405
     * @param int               $weight
406
     * @param bool              $visible
407
     * @param string            $side
408
     * @param int               $bcachetime
409
     */
410
    function setOrder($bid, $title, $weight, $visible, $side, $bcachetime)
411
    {
412
        $myblock = new XoopsBlock($bid);
413
        $myblock->setVar('title', $title);
414
        $myblock->setVar('weight', $weight);
415
        $myblock->setVar('visible', $visible);
416
        $myblock->setVar('side', $side);
417
        $myblock->setVar('bcachetime', $bcachetime);
418
        $myblock->store();
0 ignored issues
show
Bug introduced by
The method store() does not exist on XoopsBlock. ( Ignorable by Annotation )

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

418
        $myblock->/** @scrutinizer ignore-call */ 
419
                  store();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
419
    }
420
421
    /**
422
     * @param int $bid
423
     */
424
    function editBlock($bid)
425
    {
426
        require_once __DIR__ . '/admin_header.php';
427
        //require_once __DIR__ . '/admin_header.php';
428
        xoops_cp_header();
429
        $moduleDirName = basename(dirname(__DIR__));
430
        $moduleDirNameUpper = mb_strtoupper($moduleDirName); //$capsDirName
431
        xoops_loadLanguage('admin', 'system');
432
        xoops_loadLanguage('admin/blocksadmin', 'system');
433
        xoops_loadLanguage('admin/groups', 'system');
434
        //        mpu_adm_menu();
435
        $myblock = new XoopsBlock($bid);
436
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
437
        $sql = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
438
        $result = $db->query($sql);
439
        $modules = [];
440
        while (false !== ($row = $db->fetchArray($result))) {
441
            $modules[] = (int)$row['module_id'];
442
        }
443
        $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type'));
444
        $block = [
0 ignored issues
show
Unused Code introduced by
The assignment to $block is dead and can be removed.
Loading history...
445
            'title' => $myblock->getVar('title'),
446
            'form_title' => constant('CO_' . $moduleDirNameUpper . '_' . 'BLOCKS_EDITBLOCK'),
447
            //        'name'       => $myblock->getVar('name'),
448
            'side' => $myblock->getVar('side'),
449
            'weight' => $myblock->getVar('weight'),
450
            'visible' => $myblock->getVar('visible'),
451
            'content' => $myblock->getVar('content', 'N'),
452
            'modules' => $modules,
453
            'is_custom' => $is_custom,
454
            'ctype' => $myblock->getVar('c_type'),
455
            'bcachetime' => $myblock->getVar('bcachetime'),
456
            'op' => 'edit_ok',
457
            'bid' => $myblock->getVar('bid'),
458
            'edit_form' => $myblock->getOptions(),
459
            'template' => $myblock->getVar('template'),
460
            'options' => $myblock->getVar('options'),
461
        ];
462
        echo '<a href="blocksadmin.php">' . _AM_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>';
0 ignored issues
show
Bug introduced by
The constant _AM_BADMIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
463
        require_once __DIR__ . '/blockform.php';
464
        $form->display();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $form seems to be never defined.
Loading history...
465
        //        xoops_cp_footer();
466
        require_once __DIR__ . '/admin_footer.php';
467
        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...
468
    }
469
470
    /**
471
     * @param int $bid
472
     * @param $btitle
473
     * @param $bside
474
     * @param $bweight
475
     * @param $bvisible
476
     * @param $bcachetime
477
     * @param $bmodule
478
     * @param $options
479
     * @param $groups
480
     */
481
    function updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups)
482
    {
483
        $myblock = new XoopsBlock($bid);
484
        $myblock->setVar('title', $btitle);
485
        $myblock->setVar('weight', $bweight);
486
        $myblock->setVar('visible', $bvisible);
487
        $myblock->setVar('side', $bside);
488
        $myblock->setVar('bcachetime', $bcachetime);
489
        //update block options
490
        if (isset($options)) {
491
            $options_count = count($options);
492
            if ($options_count > 0) {
493
                //Convert array values to comma-separated
494
                for ($i = 0; $i < $options_count; ++$i) {
495
                    if (is_array($options[$i])) {
496
                        $options[$i] = implode(',', $options[$i]);
497
                    }
498
                }
499
                $options = implode('|', $options);
500
                $myblock->setVar('options', $options);
501
            }
502
        }
503
        $myblock->store();
504
505
        global $xoopsDB;
506
507
        $moduleDirName = basename(dirname(__DIR__));
508
        $moduleDirNameUpper = mb_strtoupper($moduleDirName); //$capsDirName
509
510
        if (!empty($bmodule) && count($bmodule) > 0) {
511
            $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $xoopsDB->prefix('block_module_link'), $bid);
512
            $xoopsDB->query($sql);
513
            if (in_array(0, $bmodule)) {
514
                $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid, 0);
515
                $xoopsDB->query($sql);
516
            } else {
517
                foreach ($bmodule as $bmid) {
518
                    $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid, (int)$bmid);
519
                    $xoopsDB->query($sql);
520
                }
521
            }
522
        }
523
        $sql = sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $xoopsDB->prefix('group_permission'), $bid);
524
        $xoopsDB->query($sql);
525
        if (!empty($groups)) {
526
            foreach ($groups as $grp) {
527
                $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);
528
                $xoopsDB->query($sql);
529
            }
530
        }
531
        redirect_header($_SERVER['PHP_SELF'], 1, constant('CO_' . $moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
532
    }
533
534
    if ('list' === $op) {
535
        xoops_cp_header();
536
        //        mpu_adm_menu();
537
        listBlocks();
538
        require_once __DIR__ . '/admin_footer.php';
539
        exit();
540
    }
541
542
    if ('order' === $op) {
543
        if (!$GLOBALS['xoopsSecurity']->check()) {
544
            redirect_header($_SERVER['PHP_SELF'], 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
545
        }
546
        foreach (array_keys($bid) as $i) {
547
            if ($oldtitle[$i] != $title[$i] || $oldweight[$i] != $weight[$i] || $oldvisible[$i] != $visible[$i]
548
                || $oldside[$i] != $side[$i]
549
                || $oldbcachetime[$i] != $bcachetime[$i]) {
550
                setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);
0 ignored issues
show
Unused Code introduced by
The call to setOrder() has too many arguments starting with $bmodule[$i]. ( Ignorable by Annotation )

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

550
                /** @scrutinizer ignore-call */ 
551
                setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
551
            }
552
            if (!empty($bmodule[$i]) && count($bmodule[$i]) > 0) {
553
                $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $xoopsDB->prefix('block_module_link'), $bid[$i]);
554
                $xoopsDB->query($sql);
555
                if (in_array(0, $bmodule[$i])) {
556
                    $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid[$i], 0);
557
                    $xoopsDB->query($sql);
558
                } else {
559
                    foreach ($bmodule[$i] as $bmid) {
560
                        $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid[$i], (int)$bmid);
561
                        $xoopsDB->query($sql);
562
                    }
563
                }
564
            }
565
            $sql = sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $xoopsDB->prefix('group_permission'), $bid[$i]);
566
            $xoopsDB->query($sql);
567
            if (!empty($groups[$i])) {
568
                foreach ($groups[$i] as $grp) {
569
                    $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]);
570
                    $xoopsDB->query($sql);
571
                }
572
            }
573
        }
574
        redirect_header($_SERVER['PHP_SELF'], 1, constant('CO_' . $moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
575
    }
576
    if ('clone' === $op) {
577
        cloneBlock($bid);
578
    }
579
580
    if ('edit' === $op) {
581
        editBlock($bid);
582
    }
583
584
    if ('edit_ok' === $op) {
585
        updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups);
586
    }
587
588
    if ('clone_ok' === $op) {
589
        isBlockCloned($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options);
590
    }
591
} else {
592
    echo constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR403');
593
}
594