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

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

359
        if (isset($options) && (count(/** @scrutinizer ignore-type */ $options) > 0)) {
Loading history...
360
            $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

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

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

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