Completed
Pull Request — master (#5)
by Michael
02:20
created

blocksadmin.php ➔ listBlocks()   F

Complexity

Conditions 22
Paths 3457

Size

Total Lines 229

Duplication

Lines 3
Ratio 1.31 %

Importance

Changes 0
Metric Value
cc 22
nc 3457
nop 0
dl 3
loc 229
rs 0
c 0
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
16
use Xmf\Request;
17
18
require __DIR__ . '/admin_header.php';
19
20
$moduleDirName      = basename(dirname(__DIR__));
21
$moduleDirNameUpper = mb_strtoupper($moduleDirName); //$capsDirName
22
23
if (!is_object($GLOBALS['xoopsUser']) || !is_object($xoopsModule)
24
    || !$GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) {
25
    exit(constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR403'));
26
}
27
if ($GLOBALS['xoopsUser']->isAdmin($xoopsModule->mid())) {
28
    require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
29
    $op = 'list';
30
    if (isset($_POST)) {
31
        foreach ($_POST as $k => $v) {
32
            ${$k} = $v;
33
        }
34
    }
35
    /*
36
    if (\Xmf\Request::hasVar('op', 'GET')) {
37
        if ('edit' === $_GET['op'] || 'delete' === $_GET['op'] || 'delete_ok' === $_GET['op'] || 'clone' === $_GET['op']
38
            || 'edit' === $_GET['op']) {
39
            $op  = $_GET['op'];
40
            $bid = \Xmf\Request::getInt('bid', 0, 'GET');
41
        }
42
    */
43
44
    $op = Request::getString('op', $op);
45
    if (in_array($op, ['edit', 'delete', 'delete_ok', 'clone'])) {
46
        $bid = Request::getInt('bid', 0, 'GET');
47
    }
48
49
    function listBlocks()
50
    {
51
        global $xoopsModule, $pathIcon16;
52
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
53
        $moduleDirName      = basename(dirname(__DIR__));
54
        $moduleDirNameUpper = mb_strtoupper($moduleDirName); //$capsDirName
55
        $db                 = \XoopsDatabaseFactory::getDatabaseConnection();
56
        xoops_loadLanguage('admin', 'system');
57
        xoops_loadLanguage('admin/blocksadmin', 'system');
58
        xoops_loadLanguage('admin/groups', 'system');
59
60
        /** @var \XoopsModuleHandler $moduleHandler */
61
        $moduleHandler = xoops_getHandler('module');
62
        /** @var \XoopsMemberHandler $memberHandler */
63
        $memberHandler = xoops_getHandler('member');
64
        /** @var \XoopsGroupPermHandler $grouppermHandler */
65
        $grouppermHandler = xoops_getHandler('groupperm');
66
        $groups           = $memberHandler->getGroups();
67
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', 1));
68
        $criteria->add(new \Criteria('isactive', 1));
69
        $module_list     = $moduleHandler->getList($criteria);
70
        $module_list[-1] = _AM_SYSTEM_BLOCKS_TOPPAGE;
71
        $module_list[0]  = _AM_SYSTEM_BLOCKS_ALLPAGES;
72
        ksort($module_list);
73
        echo "
74
        <h4 style='text-align:left;'>" . constant('CO_' . $moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
75
        $moduleHandler = xoops_getHandler('module');
0 ignored issues
show
Unused Code introduced by
$moduleHandler is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
77
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
78
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
79
        <tr valign='middle'><th align='center'>"
80
             . constant('CO_' . $moduleDirNameUpper . '_' . 'TITLE')
81
             . "</th><th align='center' nowrap='nowrap'>"
82
             . constant('CO_' . $moduleDirNameUpper . '_' . 'SIDE')
83
             . '<br>'
84
             . _LEFT
85
             . '-'
86
             . _CENTER
87
             . '-'
88
             . _RIGHT
89
             . "</th><th align='center'>"
90
             . constant(
91
                 'CO_' . $moduleDirNameUpper . '_' . 'WEIGHT'
92
             )
93
             . "</th><th align='center'>"
94
             . constant('CO_' . $moduleDirNameUpper . '_' . 'VISIBLE')
95
             . "</th><th align='center'>"
96
             . _AM_SYSTEM_BLOCKS_VISIBLEIN
97
             . "</th><th align='center'>"
98
             . _AM_SYSTEM_ADGS
99
             . "</th><th align='center'>"
100
             . _AM_SYSTEM_BLOCKS_BCACHETIME
101
             . "</th><th align='center'>"
102
             . constant('CO_' . $moduleDirNameUpper . '_' . 'ACTION')
103
             . '</th></tr>
104
        ';
105
        $block_arr   = \XoopsBlock::getByModule($xoopsModule->mid());
106
        $block_count = count($block_arr);
0 ignored issues
show
Unused Code introduced by
$block_count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
        $class       = 'even';
108
        $cachetimes  = [
109
            '0'       => _NOCACHE,
110
            '30'      => sprintf(_SECONDS, 30),
111
            '60'      => _MINUTE,
112
            '300'     => sprintf(_MINUTES, 5),
113
            '1800'    => sprintf(_MINUTES, 30),
114
            '3600'    => _HOUR,
115
            '18000'   => sprintf(_HOURS, 5),
116
            '86400'   => _DAY,
117
            '259200'  => sprintf(_DAYS, 3),
118
            '604800'  => _WEEK,
119
            '2592000' => _MONTH,
120
        ];
121
        foreach ($block_arr as $i) {
122
            $groups_perms = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
123
            $sql          = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . $i->getVar('bid');
124
            $result       = $db->query($sql);
125
            $modules      = [];
126 View Code Duplication
            while (false !== ($row = $db->fetchArray($result))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
                $modules[] = (int)$row['module_id'];
128
            }
129
130
            $cachetime_options = '';
131
            foreach ($cachetimes as $cachetime => $cachetime_name) {
132
                if ($i->getVar('bcachetime') == $cachetime) {
133
                    $cachetime_options .= "<option value='$cachetime' selected>$cachetime_name</option>\n";
134
                } else {
135
                    $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
136
                }
137
            }
138
139
            $sel0 = $sel1 = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = $ssel5 = $ssel6 = $ssel7 = '';
140
            if (1 === $i->getVar('visible')) {
141
                $sel1 = ' checked';
142
            } else {
143
                $sel0 = ' checked';
144
            }
145
            if (XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
146
                $ssel0 = ' checked';
147
            } elseif (XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
148
                $ssel1 = ' checked';
149
            } elseif (XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
150
                $ssel2 = ' checked';
151
            } elseif (XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
152
                $ssel4 = ' checked';
153
            } elseif (XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
154
                $ssel3 = ' checked';
155
            } elseif (XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
156
                $ssel5 = ' checked';
157
            } elseif (XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
158
                $ssel6 = ' checked';
159
            } elseif (XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
160
                $ssel7 = ' checked';
161
            }
162
            if ('' === $i->getVar('title')) {
163
                $title = '&nbsp;';
164
            } else {
165
                $title = $i->getVar('title');
166
            }
167
            $name = $i->getVar('name');
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
168
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
169
                 . $i->getVar('bid')
170
                 . "]' value='"
171
                 . $title
172
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
173
                    <div align='center' >
174
                    <input type='radio' name='side["
175
                 . $i->getVar('bid')
176
                 . "]' value='"
177
                 . XOOPS_CENTERBLOCK_LEFT
178
                 . "'$ssel2>
179
                        <input type='radio' name='side["
180
                 . $i->getVar('bid')
181
                 . "]' value='"
182
                 . XOOPS_CENTERBLOCK_CENTER
183
                 . "'$ssel3>
184
                    <input type='radio' name='side["
185
                 . $i->getVar('bid')
186
                 . "]' value='"
187
                 . XOOPS_CENTERBLOCK_RIGHT
188
                 . "'$ssel4>
189
                    </div>
190
                    <div>
191
                        <span style='float:right;'><input type='radio' name='side["
192
                 . $i->getVar('bid')
193
                 . "]' value='"
194
                 . XOOPS_SIDEBLOCK_RIGHT
195
                 . "'$ssel1></span>
196
                    <div align='left'><input type='radio' name='side["
197
                 . $i->getVar('bid')
198
                 . "]' value='"
199
                 . XOOPS_SIDEBLOCK_LEFT
200
                 . "'$ssel0></div>
201
                    </div>
202
                    <div align='center'>
203
                    <input type='radio' name='side["
204
                 . $i->getVar('bid')
205
                 . "]' value='"
206
                 . XOOPS_CENTERBLOCK_BOTTOMLEFT
207
                 . "'$ssel5>
208
                        <input type='radio' name='side["
209
                 . $i->getVar('bid')
210
                 . "]' value='"
211
                 . XOOPS_CENTERBLOCK_BOTTOM
212
                 . "'$ssel7>
213
                    <input type='radio' name='side["
214
                 . $i->getVar('bid')
215
                 . "]' value='"
216
                 . XOOPS_CENTERBLOCK_BOTTOMRIGHT
217
                 . "'$ssel6>
218
                    </div>
219
                </td><td class='$class' align='center'><input type='text' name='weight["
220
                 . $i->getVar('bid')
221
                 . "]' value='"
222
                 . $i->getVar('weight')
223
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
224
                 . $i->getVar('bid')
225
                 . "]' value='1'$sel1>"
226
                 . _YES
227
                 . "&nbsp;<input type='radio' name='visible["
228
                 . $i->getVar('bid')
229
                 . "]' value='0'$sel0>"
230
                 . _NO
231
                 . '</td>';
232
233
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
234
            foreach ($module_list as $k => $v) {
235
                echo "<option value='$k'" . (in_array($k, $modules) ? 'selected' : '') . ">$v</option>";
236
            }
237
            echo '</select></td>';
238
239
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
240
            foreach ($groups as $grp) {
241
                echo "<option value='" . $grp->getVar('groupid') . "' " . (in_array($grp->getVar('groupid'), $groups_perms) ? 'selected' : '') . '>' . $grp->getVar('name') . '</option>';
242
            }
243
            echo '</select></td>';
244
245
            // Cache lifetime
246
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetime_options . '</select>
247
                                    </td>';
248
249
            // Actions
250
251
            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 . "'>
252
                 </a> <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'>
253
                 </a>";
254
            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
255
                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 . "'>
256
                     </a>";
257
            }
258
            echo "
259
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
260
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
261
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
262
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
263
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
264
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
265
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
266
            </td></tr>
267
            ";
268
            $class = ('even' === $class) ? 'odd' : 'even';
269
        }
270
        echo "<tr><td class='foot' align='center' colspan='8'>
271
        <input type='hidden' name='op' value='order'>
272
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
273
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
274
        </td></tr></table>
275
        </form>
276
        <br><br>";
277
    }
278
279
    /**
280
     * @param int $bid
281
     */
282
    function cloneBlock($bid)
283
    {
284
        require_once __DIR__ . '/admin_header.php';
285
        //require_once __DIR__ . '/admin_header.php';
286
        xoops_cp_header();
287
288
        xoops_loadLanguage('admin', 'system');
289
        xoops_loadLanguage('admin/blocksadmin', 'system');
290
        xoops_loadLanguage('admin/groups', 'system');
291
292
        //        mpu_adm_menu();
293
        $myblock = new \XoopsBlock($bid);
294
        $db      = \XoopsDatabaseFactory::getDatabaseConnection();
295
        $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
296
        $result  = $db->query($sql);
297
        $modules = [];
298 View Code Duplication
        while (false !== ($row = $db->fetchArray($result))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
            $modules[] = (int)$row['module_id'];
300
        }
301
        $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type'));
302
        $block     = [
0 ignored issues
show
Unused Code introduced by
$block is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
303
            'title'      => $myblock->getVar('title') . ' Clone',
304
            'form_title' => constant('CO_' . $moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
0 ignored issues
show
Bug introduced by
The variable $moduleDirNameUpper does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
305
            'name'       => $myblock->getVar('name'),
306
            'side'       => $myblock->getVar('side'),
307
            'weight'     => $myblock->getVar('weight'),
308
            'visible'    => $myblock->getVar('visible'),
309
            'content'    => $myblock->getVar('content', 'N'),
310
            'modules'    => $modules,
311
            'is_custom'  => $is_custom,
312
            'ctype'      => $myblock->getVar('c_type'),
313
            'bcachetime' => $myblock->getVar('bcachetime'),
314
            'op'         => 'clone_ok',
315
            'bid'        => $myblock->getVar('bid'),
316
            'edit_form'  => $myblock->getOptions(),
317
            'template'   => $myblock->getVar('template'),
318
            'options'    => $myblock->getVar('options'),
319
        ];
320
        echo '<a href="blocksadmin.php">' . _AM_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_SYSTEM_BLOCKS_CLONEBLOCK . '<br><br>';
321
        require_once __DIR__ . '/blockform.php';
322
        $form->display();
0 ignored issues
show
Bug introduced by
The variable $form does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
323
        //        xoops_cp_footer();
324
        require_once __DIR__ . '/admin_footer.php';
325
        exit();
326
    }
327
328
    /**
329
     * @param int               $bid
330
     * @param string            $bside
331
     * @param int               $bweight
332
     * @param bool              $bvisible
333
     * @param int               $bcachetime
334
     * @param array             $bmodule
335
     * @param null|array|string $options
336
     */
337
    function isBlockCloned($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options)
338
    {
339
        xoops_loadLanguage('admin', 'system');
340
        xoops_loadLanguage('admin/blocksadmin', 'system');
341
        xoops_loadLanguage('admin/groups', 'system');
342
343
        $block = new \XoopsBlock($bid);
344
        $clone = $block->xoopsClone();
345
        if (empty($bmodule)) {
346
            xoops_cp_header();
347
            xoops_error(sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
348
            xoops_cp_footer();
349
            exit();
350
        }
351
        $clone->setVar('side', $bside);
352
        $clone->setVar('weight', $bweight);
353
        $clone->setVar('visible', $bvisible);
354
        //$clone->setVar('content', $_POST['bcontent']);
355
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
356
        $clone->setVar('bcachetime', $bcachetime);
357
        if (isset($options) && (count($options) > 0)) {
358
            $options = implode('|', $options);
359
            $clone->setVar('options', $options);
360
        }
361
        $clone->setVar('bid', 0);
362
        if ('C' === $block->getVar('block_type') || 'E' === $block->getVar('block_type')) {
363
            $clone->setVar('block_type', 'E');
364
        } else {
365
            $clone->setVar('block_type', 'D');
366
        }
367
        $newid = $clone->store();
368
        if (!$newid) {
369
            xoops_cp_header();
370
            $clone->getHtmlErrors();
371
            xoops_cp_footer();
372
            exit();
373
        }
374
        if ('' !== $clone->getVar('template')) {
375
            /** @var \XoopsTplfileHandler $tplfileHandler */
376
            $tplfileHandler = xoops_getHandler('tplfile');
377
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
378
            if (count($btemplate) > 0) {
379
                $tplclone = $btemplate[0]->xoopsClone();
380
                $tplclone->setVar('tpl_id', 0);
381
                $tplclone->setVar('tpl_refid', $newid);
382
                $tplfileHandler->insert($tplclone);
383
            }
384
        }
385
        $db = \XoopsDatabaseFactory::getDatabaseConnection();
386
        foreach ($bmodule as $bmid) {
387
            $sql = 'INSERT INTO ' . $db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
388
            $db->query($sql);
389
        }
390
        $groups = &$GLOBALS['xoopsUser']->getGroups();
391
        $count  = count($groups);
392
        for ($i = 0; $i < $count; ++$i) {
393
            $sql = 'INSERT INTO ' . $db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $groups[$i] . ', ' . $newid . ", 1, 'block_read')";
394
            $db->query($sql);
395
        }
396
        redirect_header('blocksadmin.php?op=listar', 1, _AM_DBUPDATED);
397
    }
398
399
    /**
400
     * @param int    $bid
401
     * @param string $title
402
     * @param int    $weight
403
     * @param bool   $visible
404
     * @param string $side
405
     * @param int    $bcachetime
406
     */
407
    function setOrder($bid, $title, $weight, $visible, $side, $bcachetime)
408
    {
409
        $myblock = new \XoopsBlock($bid);
410
        $myblock->setVar('title', $title);
411
        $myblock->setVar('weight', $weight);
412
        $myblock->setVar('visible', $visible);
413
        $myblock->setVar('side', $side);
414
        $myblock->setVar('bcachetime', $bcachetime);
415
        $myblock->store();
416
    }
417
418
    /**
419
     * @param int $bid
420
     */
421
    function editBlock($bid)
422
    {
423
        require_once __DIR__ . '/admin_header.php';
424
        //require_once __DIR__ . '/admin_header.php';
425
        xoops_cp_header();
426
427
        xoops_loadLanguage('admin', 'system');
428
        xoops_loadLanguage('admin/blocksadmin', 'system');
429
        xoops_loadLanguage('admin/groups', 'system');
430
        //        mpu_adm_menu();
431
        $myblock = new \XoopsBlock($bid);
432
        $db      = \XoopsDatabaseFactory::getDatabaseConnection();
433
        $sql     = 'SELECT module_id FROM ' . $db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
434
        $result  = $db->query($sql);
435
        $modules = [];
436 View Code Duplication
        while (false !== ($row = $db->fetchArray($result))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
437
            $modules[] = (int)$row['module_id'];
438
        }
439
        $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type'));
440
        $block     = [
0 ignored issues
show
Unused Code introduced by
$block is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
441
            'title'      => $myblock->getVar('title'),
442
            'form_title' => constant('CO_' . $moduleDirNameUpper . '_' . 'BLOCKS_EDITBLOCK'),
0 ignored issues
show
Bug introduced by
The variable $moduleDirNameUpper does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
443
            //        'name'       => $myblock->getVar('name'),
444
            'side'       => $myblock->getVar('side'),
445
            'weight'     => $myblock->getVar('weight'),
446
            'visible'    => $myblock->getVar('visible'),
447
            'content'    => $myblock->getVar('content', 'N'),
448
            'modules'    => $modules,
449
            'is_custom'  => $is_custom,
450
            'ctype'      => $myblock->getVar('c_type'),
451
            'bcachetime' => $myblock->getVar('bcachetime'),
452
            'op'         => 'edit_ok',
453
            'bid'        => $myblock->getVar('bid'),
454
            'edit_form'  => $myblock->getOptions(),
455
            'template'   => $myblock->getVar('template'),
456
            'options'    => $myblock->getVar('options'),
457
        ];
458
        echo '<a href="blocksadmin.php">' . _AM_BADMIN . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . _AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>';
459
        require_once __DIR__ . '/blockform.php';
460
        $form->display();
0 ignored issues
show
Bug introduced by
The variable $form does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
461
        //        xoops_cp_footer();
462
        require_once __DIR__ . '/admin_footer.php';
463
        exit();
464
    }
465
466
    /**
467
     * @param int $bid
468
     * @param     $btitle
469
     * @param     $bside
470
     * @param     $bweight
471
     * @param     $bvisible
472
     * @param     $bcachetime
473
     * @param     $bmodule
474
     * @param     $options
475
     * @param     $groups
476
     */
477
    function updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

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

Loading history...
478
    {
479
        $myblock = new \XoopsBlock($bid);
480
        $myblock->setVar('title', $btitle);
481
        $myblock->setVar('weight', $bweight);
482
        $myblock->setVar('visible', $bvisible);
483
        $myblock->setVar('side', $bside);
484
        $myblock->setVar('bcachetime', $bcachetime);
485
        $myblock->store();
486
487
        global $xoopsDB;
488
489
        $moduleDirName      = basename(dirname(__DIR__));
490
        $moduleDirNameUpper = mb_strtoupper($moduleDirName); //$capsDirName
491
492
        if (!empty($bmodule) && count($bmodule) > 0) {
493
            $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $xoopsDB->prefix('block_module_link'), $bid);
494
            $xoopsDB->query($sql);
495
            if (in_array(0, $bmodule)) {
496
                $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid, 0);
497
                $xoopsDB->query($sql);
498 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
499
                foreach ($bmodule as $bmid) {
500
                    $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid, (int)$bmid);
501
                    $xoopsDB->query($sql);
502
                }
503
            }
504
        }
505
        $sql = sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $xoopsDB->prefix('group_permission'), $bid);
506
        $xoopsDB->query($sql);
507
        if (!empty($groups)) {
508 View Code Duplication
            foreach ($groups as $grp) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
509
                $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);
510
                $xoopsDB->query($sql);
511
            }
512
        }
513
        redirect_header($_SERVER['PHP_SELF'], 1, constant('CO_' . $moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
514
    }
515
516
    if ('list' === $op) {
517
        xoops_cp_header();
518
        //        mpu_adm_menu();
519
        listBlocks();
520
        require_once __DIR__ . '/admin_footer.php';
521
        exit();
522
    }
523
524
    if ('order' === $op) {
525 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
526
            redirect_header($_SERVER['PHP_SELF'], 3, implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
527
        }
528
        foreach (array_keys($bid) as $i) {
529
            if ($oldtitle[$i] != $title[$i] || $oldweight[$i] != $weight[$i] || $oldvisible[$i] != $visible[$i]
530
                || $oldside[$i] != $side[$i]
531
                || $oldbcachetime[$i] != $bcachetime[$i]) {
532
                xtubeSetOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);
533
            }
534
            if (!empty($bmodule[$i]) && count($bmodule[$i]) > 0) {
535
                $sql = sprintf('DELETE FROM `%s` WHERE block_id = %u', $xoopsDB->prefix('block_module_link'), $bid[$i]);
536
                $xoopsDB->query($sql);
537
                if (in_array(0, $bmodule[$i])) {
538
                    $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid[$i], 0);
539
                    $xoopsDB->query($sql);
540 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
541
                    foreach ($bmodule[$i] as $bmid) {
542
                        $sql = sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $xoopsDB->prefix('block_module_link'), $bid[$i], (int)$bmid);
543
                        $xoopsDB->query($sql);
544
                    }
545
                }
546
            }
547
            $sql = sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $xoopsDB->prefix('group_permission'), $bid[$i]);
548
            $xoopsDB->query($sql);
549
            if (!empty($groups[$i])) {
550 View Code Duplication
                foreach ($groups[$i] as $grp) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
551
                    $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]);
552
                    $xoopsDB->query($sql);
553
                }
554
            }
555
        }
556
        redirect_header($_SERVER['PHP_SELF'], 1, constant('CO_' . $moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
557
    }
558
    if ('clone' === $op) {
559
        cloneBlock($bid);
560
    }
561
562
    if ('edit' === $op) {
563
        editBlock($bid);
564
    }
565
566
    if ('edit_ok' === $op) {
567
        updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups);
568
    }
569
570
    if ('clone_ok' === $op) {
571
        isBlockCloned($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options);
572
    }
573
} else {
574
    echo constant('CO_' . $moduleDirNameUpper . '_' . 'ERROR403');
575
}
576