Blocksadmin   F
last analyzed

Complexity

Total Complexity 81

Size/Duplication

Total Lines 678
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 460
c 1
b 0
f 0
dl 0
loc 678
rs 2
wmc 81

10 Methods

Rating   Name   Duplication   Size   Complexity  
A cloneBlock() 0 42 3
A setOrder() 0 9 1
F listBlocks() 0 242 22
B isBlockCloned() 0 62 11
C updateBlock() 0 49 12
A editBlock() 0 39 3
A __construct() 0 14 2
C orderBlock() 0 53 15
A deleteBlock() 0 17 3
C render() 0 108 9

How to fix   Complexity   

Complex Class

Complex classes like Blocksadmin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Blocksadmin, and based on these observations, apply Extract Interface, too.

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Songlist\Common;
4
5
/**
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 *
11
 * @category        Module
12
 * @author          XOOPS Development Team
13
 * @copyright       XOOPS Project
14
 * @link            https://xoops.org
15
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
16
 */
17
18
use Xmf\Request;
19
use XoopsModules\Songlist\Helper;
20
21
//require __DIR__ . '/admin_header.php';
22
23
/**
24
 * class Blocksadmin
25
 */
26
class Blocksadmin
27
{
28
    /**
29
     * @var \XoopsMySQLDatabase|null
30
     */
31
    public $db;
32
    public $helper;
33
    public $moduleDirName;
34
    public $moduleDirNameUpper;
35
36
    /**
37
     * Blocksadmin constructor.
38
     */
39
    public function __construct(?\XoopsDatabase $db, Helper $helper)
40
    {
41
        if (null === $db) {
42
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
43
        }
44
        $this->db                 = $db;
45
        $this->helper             = $helper;
46
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
47
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
48
        \xoops_loadLanguage('admin', 'system');
49
        \xoops_loadLanguage('admin/blocksadmin', 'system');
50
        \xoops_loadLanguage('admin/groups', 'system');
51
        \xoops_loadLanguage('common', $this->moduleDirName);
52
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
53
    }
54
55
    public function listBlocks(): void
56
    {
57
        global $xoopsModule, $pathIcon16;
58
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
59
        //        xoops_loadLanguage('admin', 'system');
60
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
61
        //        xoops_loadLanguage('admin/groups', 'system');
62
        //        xoops_loadLanguage('common', $moduleDirName);
63
        //        xoops_loadLanguage('blocks', $moduleDirName);
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
        $moduleList     = $moduleHandler->getList($criteria);
75
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
76
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
77
        \ksort($moduleList);
78
        echo "
79
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
80
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
81
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
82
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
83
        <tr valign='middle'><th align='center'>"
84
             . \_AM_SYSTEM_BLOCKS_TITLE
85
             . "</th><th align='center' nowrap='nowrap'>"
86
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
87
             . '<br>'
88
             . _LEFT
89
             . '-'
90
             . _CENTER
91
             . '-'
92
             . _RIGHT
93
             . "</th><th align='center'>"
94
             . \constant(
95
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
96
             )
97
             . "</th><th align='center'>"
98
             . \constant('CO_' . $this->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_' . $this->moduleDirNameUpper . '_' . 'ACTION')
107
             . '</th></tr>
108
        ';
109
        $blockArray = \XoopsBlock::getByModule($xoopsModule->mid());
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::getByModule() has been deprecated: (This also appears, dead, in XoopsBlockHandler) ( Ignorable by Annotation )

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

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

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

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

Loading history...
110
        $blockCount = \count($blockArray);
0 ignored issues
show
Unused Code introduced by
The assignment to $blockCount 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 ($blockArray as $i) {
126
            $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
127
            $sql               = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $i->getVar('bid');
0 ignored issues
show
Bug introduced by
The method prefix() does not exist on null. ( Ignorable by Annotation )

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

127
            $sql               = 'SELECT module_id FROM ' . $this->db->/** @scrutinizer ignore-call */ prefix('block_module_link') . ' WHERE block_id=' . $i->getVar('bid');

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...
128
            $result            = $this->db->query($sql);
129
            $modules           = [];
130
            if (!$result instanceof \mysqli_result) {
131
                \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
132
            }
133
            while (false !== ($row = $this->db->fetchArray($result))) {
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type boolean; however, parameter $result of XoopsMySQLDatabase::fetchArray() does only seem to accept mysqli_result, maybe add an additional type check? ( Ignorable by Annotation )

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

133
            while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
134
                $modules[] = (int)$row['module_id'];
135
            }
136
137
            $cachetimeOptions = '';
138
            foreach ($cachetimes as $cachetime => $cachetimeName) {
139
                if ($i->getVar('bcachetime') == $cachetime) {
140
                    $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n";
141
                } else {
142
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
143
                }
144
            }
145
146
            $ssel7 = '';
147
            $ssel6 = $ssel7;
148
            $ssel5 = $ssel6;
149
            $ssel4 = $ssel5;
150
            $ssel3 = $ssel4;
151
            $ssel2 = $ssel3;
152
            $ssel1 = $ssel2;
153
            $ssel0 = $ssel1;
154
            $sel1  = $ssel0;
155
            $sel0  = $sel1;
156
            if (1 === $i->getVar('visible')) {
157
                $sel1 = ' checked';
158
            } else {
159
                $sel0 = ' checked';
160
            }
161
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
162
                $ssel0 = ' checked';
163
            } elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
164
                $ssel1 = ' checked';
165
            } elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
166
                $ssel2 = ' checked';
167
            } elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
168
                $ssel4 = ' checked';
169
            } elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
170
                $ssel3 = ' checked';
171
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
172
                $ssel5 = ' checked';
173
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
174
                $ssel6 = ' checked';
175
            } elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
176
                $ssel7 = ' checked';
177
            }
178
            if ('' === $i->getVar('title')) {
179
                $title = '&nbsp;';
180
            } else {
181
                $title = $i->getVar('title');
182
            }
183
            $name = $i->getVar('name');
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
184
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
185
                 . $i->getVar('bid')
186
                 . "]' value='"
187
                 . $title
188
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
189
                    <div align='center' >
190
                    <input type='radio' name='side["
191
                 . $i->getVar('bid')
192
                 . "]' value='"
193
                 . \XOOPS_CENTERBLOCK_LEFT
194
                 . "'$ssel2>
195
                        <input type='radio' name='side["
196
                 . $i->getVar('bid')
197
                 . "]' value='"
198
                 . \XOOPS_CENTERBLOCK_CENTER
199
                 . "'$ssel3>
200
                    <input type='radio' name='side["
201
                 . $i->getVar('bid')
202
                 . "]' value='"
203
                 . \XOOPS_CENTERBLOCK_RIGHT
204
                 . "'$ssel4>
205
                    </div>
206
                    <div>
207
                        <span style='float:right;'><input type='radio' name='side["
208
                 . $i->getVar('bid')
209
                 . "]' value='"
210
                 . \XOOPS_SIDEBLOCK_RIGHT
211
                 . "'$ssel1></span>
212
                    <div align='left'><input type='radio' name='side["
213
                 . $i->getVar('bid')
214
                 . "]' value='"
215
                 . \XOOPS_SIDEBLOCK_LEFT
216
                 . "'$ssel0></div>
217
                    </div>
218
                    <div align='center'>
219
                    <input type='radio' name='side["
220
                 . $i->getVar('bid')
221
                 . "]' value='"
222
                 . \XOOPS_CENTERBLOCK_BOTTOMLEFT
223
                 . "'$ssel5>
224
                        <input type='radio' name='side["
225
                 . $i->getVar('bid')
226
                 . "]' value='"
227
                 . \XOOPS_CENTERBLOCK_BOTTOM
228
                 . "'$ssel7>
229
                    <input type='radio' name='side["
230
                 . $i->getVar('bid')
231
                 . "]' value='"
232
                 . \XOOPS_CENTERBLOCK_BOTTOMRIGHT
233
                 . "'$ssel6>
234
                    </div>
235
                </td><td class='$class' align='center'><input type='text' name='weight["
236
                 . $i->getVar('bid')
237
                 . "]' value='"
238
                 . $i->getVar('weight')
239
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
240
                 . $i->getVar('bid')
241
                 . "]' value='1'$sel1>"
242
                 . _YES
243
                 . "&nbsp;<input type='radio' name='visible["
244
                 . $i->getVar('bid')
245
                 . "]' value='0'$sel0>"
246
                 . _NO
247
                 . '</td>';
248
249
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
250
            foreach ($moduleList as $k => $v) {
251
                echo "<option value='$k'" . (\in_array($k, $modules, true) ? " selected='selected'" : '') . ">$v</option>";
252
            }
253
            echo '</select></td>';
254
255
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
256
            foreach ($groups as $grp) {
257
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions, true) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
258
            }
259
            echo '</select></td>';
260
261
            // Cache lifetime
262
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
263
                                    </td>';
264
265
            // Actions
266
267
            echo "<td class='$class' align='center'>
268
                <a href='blocksadmin.php?op=edit&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/edit.png' . " alt='" . _EDIT . "' title='" . _EDIT . "'></a> 
269
                <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'></a>";
270
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
271
            //                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 . "'>
272
            //                     </a>";
273
            //            }
274
275
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
276
            if (!\in_array($i->getVar('block_type'), ['M', 'S'], true)) {
277
                echo "&nbsp;
278
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
279
                     </a>";
280
            }
281
            echo "
282
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
283
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
284
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
285
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
286
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
287
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
288
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
289
            </td></tr>
290
            ";
291
            $class = ('even' === $class) ? 'odd' : 'even';
292
        }
293
        echo "<tr><td class='foot' align='center' colspan='8'>
294
        <input type='hidden' name='op' value='order'>
295
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
296
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
297
        </td></tr></table>
298
        </form>
299
        <br><br>";
300
    }
301
302
    public function deleteBlock(int $bid): void
303
    {
304
        //        \xoops_cp_header();
305
306
        \xoops_loadLanguage('admin', 'system');
307
        \xoops_loadLanguage('admin/blocksadmin', 'system');
308
        \xoops_loadLanguage('admin/groups', 'system');
309
310
        $myblock = new \XoopsBlock($bid);
0 ignored issues
show
Unused Code introduced by
The assignment to $myblock is dead and can be removed.
Loading history...
311
312
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
313
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
314
315
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
316
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
317
318
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
319
    }
320
321
    public function cloneBlock(int $bid): void
322
    {
323
        //require __DIR__ . '/admin_header.php';
324
        //        \xoops_cp_header();
325
326
        \xoops_loadLanguage('admin', 'system');
327
        \xoops_loadLanguage('admin/blocksadmin', 'system');
328
        \xoops_loadLanguage('admin/groups', 'system');
329
330
        $myblock = new \XoopsBlock($bid);
331
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
332
        $result  = $this->db->query($sql);
333
        $modules = [];
334
        if ($result instanceof \mysqli_result) {
335
            while (false !== ($row = $this->db->fetchArray($result))) {
336
                $modules[] = (int)$row['module_id'];
337
            }
338
        }
339
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E'], true);
340
        $block    = [
341
            'title'      => $myblock->getVar('title') . ' Clone',
342
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
343
            'name'       => $myblock->getVar('name'),
344
            'side'       => $myblock->getVar('side'),
345
            'weight'     => $myblock->getVar('weight'),
346
            'visible'    => $myblock->getVar('visible'),
347
            'content'    => $myblock->getVar('content', 'N'),
348
            'modules'    => $modules,
349
            'is_custom'  => $isCustom,
350
            'ctype'      => $myblock->getVar('c_type'),
351
            'bcachetime' => $myblock->getVar('bcachetime'),
352
            'op'         => 'clone_ok',
353
            'bid'        => $myblock->getVar('bid'),
354
            'edit_form'  => $myblock->getOptions(),
355
            'template'   => $myblock->getVar('template'),
356
            'options'    => $myblock->getVar('options'),
357
        ];
358
        echo '<a href="blocksadmin.php">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . \_AM_SYSTEM_BLOCKS_CLONEBLOCK . '<br><br>';
359
        //        $form = new Blockform();
360
        //        $form->render();
361
362
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure $this->render($block) of type void can be used in echo? ( Ignorable by Annotation )

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

362
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Songlist\Common\Blocksadmin::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
363
        //        xoops_cp_footer();
364
        //        require_once __DIR__ . '/admin_footer.php';
365
        //        exit();
366
    }
367
368
    /**
369
     * @param null|array|string $options
370
     */
371
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
372
    {
373
        \xoops_loadLanguage('admin', 'system');
374
        \xoops_loadLanguage('admin/blocksadmin', 'system');
375
        \xoops_loadLanguage('admin/groups', 'system');
376
377
        $block = new \XoopsBlock($bid);
378
        $clone = $block->xoopsClone();
379
        if (empty($bmodule)) {
380
            //            \xoops_cp_header();
381
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
382
            \xoops_cp_footer();
383
            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...
384
        }
385
        $clone->setVar('side', $bside);
386
        $clone->setVar('weight', $bweight);
387
        $clone->setVar('visible', $bvisible);
388
        //$clone->setVar('content', $_POST['bcontent']);
389
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
390
        $clone->setVar('bcachetime', $bcachetime);
391
        if (\is_array($options) && (\count($options) > 0)) {
392
            $options = \implode('|', $options);
393
            $clone->setVar('options', $options);
394
        }
395
        $clone->setVar('bid', 0);
396
        if (\in_array($block->getVar('block_type'), ['C', 'E'], true)) {
397
            $clone->setVar('block_type', 'E');
398
        } else {
399
            $clone->setVar('block_type', 'D');
400
        }
401
        //        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
402
        if ($clone->store()) {
403
            $newid = $clone->id();  //get the id of the cloned block
404
        }
405
        if (!$newid) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $newid does not seem to be defined for all execution paths leading up to this point.
Loading history...
406
            //            \xoops_cp_header();
407
            $clone->getHtmlErrors();
408
            \xoops_cp_footer();
409
            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...
410
        }
411
        if ('' !== $clone->getVar('template')) {
412
            /** @var \XoopsTplfileHandler $tplfileHandler */
413
            $tplfileHandler = \xoops_getHandler('tplfile');
414
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', (string)$bid);
415
            if (\count($btemplate) > 0) {
416
                $tplclone = $btemplate[0]->xoopsClone();
417
                $tplclone->setVar('tpl_id', 0);
418
                $tplclone->setVar('tpl_refid', $newid);
419
                $tplfileHandler->insert($tplclone);
420
            }
421
        }
422
423
        foreach ($bmodule as $bmid) {
424
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
425
            $this->db->query($sql);
426
        }
427
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
428
        foreach ($groups as $iValue) {
429
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
430
            $this->db->query($sql);
431
        }
432
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
433
    }
434
435
    public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, ?array $bmodule = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $bmodule is not used and could be removed. ( Ignorable by Annotation )

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

435
    public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, /** @scrutinizer ignore-unused */ ?array $bmodule = null): void

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

Loading history...
436
    {
437
        $myblock = new \XoopsBlock($bid);
438
        $myblock->setVar('title', $title);
439
        $myblock->setVar('weight', $weight);
440
        $myblock->setVar('visible', $visible);
441
        $myblock->setVar('side', $side);
442
        $myblock->setVar('bcachetime', $bcachetime);
443
        $myblock->store();
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::store() has been deprecated. ( Ignorable by Annotation )

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

443
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
444
        //        /** @var \XoopsBlockHandler $blockHandler */
445
        //        $blockHandler = \xoops_getHandler('block');
446
        //        return $blockHandler->insert($myblock);
447
    }
448
449
    public function editBlock(int $bid): void
450
    {
451
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
452
        //        \xoops_cp_header();
453
        \xoops_loadLanguage('admin', 'system');
454
        \xoops_loadLanguage('admin/blocksadmin', 'system');
455
        \xoops_loadLanguage('admin/groups', 'system');
456
        //        mpu_adm_menu();
457
        $myblock = new \XoopsBlock($bid);
458
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
459
        $result  = $this->db->query($sql);
460
        $modules = [];
461
        if ($result instanceof \mysqli_result) {
462
            while (false !== ($row = $this->db->fetchArray($result))) {
463
                $modules[] = (int)$row['module_id'];
464
            }
465
        }
466
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E'], true);
467
        $block    = [
468
            'title'      => $myblock->getVar('title'),
469
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
470
            //        'name'       => $myblock->getVar('name'),
471
            'side'       => $myblock->getVar('side'),
472
            'weight'     => $myblock->getVar('weight'),
473
            'visible'    => $myblock->getVar('visible'),
474
            'content'    => $myblock->getVar('content', 'N'),
475
            'modules'    => $modules,
476
            'is_custom'  => $isCustom,
477
            'ctype'      => $myblock->getVar('c_type'),
478
            'bcachetime' => $myblock->getVar('bcachetime'),
479
            'op'         => 'edit_ok',
480
            'bid'        => $myblock->getVar('bid'),
481
            'edit_form'  => $myblock->getOptions(),
482
            'template'   => $myblock->getVar('template'),
483
            'options'    => $myblock->getVar('options'),
484
        ];
485
        echo '<a href="blocksadmin.php">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . \_AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>';
486
487
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure $this->render($block) of type void can be used in echo? ( Ignorable by Annotation )

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

487
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Songlist\Common\Blocksadmin::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
488
    }
489
490
    public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
491
    {
492
        $myblock = new \XoopsBlock($bid);
493
        $myblock->setVar('title', $btitle);
494
        $myblock->setVar('weight', $bweight);
495
        $myblock->setVar('visible', $bvisible);
496
        $myblock->setVar('side', $bside);
497
        $myblock->setVar('bcachetime', $bcachetime);
498
        //update block options
499
        if (isset($options) && \is_array($options)) {
500
            $optionsCount = \count($options);
501
            if ($optionsCount > 0) {
502
                //Convert array values to comma-separated
503
                foreach ($options as $i => $iValue) {
504
                    if (\is_array($iValue)) {
505
                        $options[$i] = \implode(',', $iValue);
506
                    }
507
                }
508
                $options = \implode('|', $options);
509
                $myblock->setVar('options', $options);
510
            }
511
        }
512
        $myblock->store();
0 ignored issues
show
Deprecated Code introduced by
The function XoopsBlock::store() has been deprecated. ( Ignorable by Annotation )

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

512
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
513
        //        /** @var \XoopsBlockHandler $blockHandler */
514
        //        $blockHandler = \xoops_getHandler('block');
515
        //        $blockHandler->insert($myblock);
516
517
        if (!empty($bmodule) && \count($bmodule) > 0) {
518
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
519
            $this->db->query($sql);
520
            if (\in_array(0, $bmodule, true)) {
521
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
522
                $this->db->query($sql);
523
            } else {
524
                foreach ($bmodule as $bmid) {
525
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
526
                    $this->db->query($sql);
527
                }
528
            }
529
        }
530
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
531
        $this->db->query($sql);
532
        if (!empty($groups)) {
533
            foreach ($groups as $grp) {
534
                $sql = \sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $this->db->prefix('group_permission'), $grp, $bid);
535
                $this->db->query($sql);
536
            }
537
        }
538
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
539
    }
540
541
    public function orderBlock(
542
        array $bid,
543
        array $oldtitle,
544
        array $oldside,
545
        array $oldweight,
546
        array $oldvisible,
547
        array $oldgroups,
0 ignored issues
show
Unused Code introduced by
The parameter $oldgroups is not used and could be removed. ( Ignorable by Annotation )

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

547
        /** @scrutinizer ignore-unused */ array $oldgroups,

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

Loading history...
548
        array $oldbcachetime,
549
        array $oldbmodule,
550
        array $title,
551
        array $weight,
552
        array $visible,
553
        array $side,
554
        array $bcachetime,
555
        array $groups,
556
        array $bmodule
557
    ): void {
558
        if (!$GLOBALS['xoopsSecurity']->check()) {
559
            \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
560
        }
561
        foreach (\array_keys($bid) as $i) {
562
            if ($oldtitle[$i] !== $title[$i]
563
                || $oldweight[$i] !== $weight[$i]
564
                || $oldvisible[$i] !== $visible[$i]
565
                || $oldside[$i] !== $side[$i]
566
                || $oldbcachetime[$i] !== $bcachetime[$i]
567
                || $oldbmodule[$i] !== $bmodule[$i]) {
568
                $this->setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);
569
            }
570
            if (!empty($bmodule[$i]) && \count($bmodule[$i]) > 0) {
571
                $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid[$i]);
572
                $this->db->query($sql);
573
                if (\in_array(0, $bmodule[$i], true)) {
574
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], 0);
575
                    $this->db->query($sql);
576
                } else {
577
                    foreach ($bmodule[$i] as $bmid) {
578
                        $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], (int)$bmid);
579
                        $this->db->query($sql);
580
                    }
581
                }
582
            }
583
            $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid[$i]);
584
            $this->db->query($sql);
585
            if (!empty($groups[$i])) {
586
                foreach ($groups[$i] as $grp) {
587
                    $sql = \sprintf("INSERT INTO `%s` (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, 1, 'block_read')", $this->db->prefix('group_permission'), $grp, $bid[$i]);
588
                    $this->db->query($sql);
589
                }
590
            }
591
        }
592
593
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
594
    }
595
596
    public function render(?array $block = null): void
597
    {
598
        \xoops_load('XoopsFormLoader');
599
        \xoops_loadLanguage('common', $this->moduleDirNameUpper);
600
601
        $form = new \XoopsThemeForm($block['form_title'], 'blockform', 'blocksadmin.php', 'post', true);
602
        if (isset($block['name'])) {
603
            $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_NAME, $block['name']));
604
        }
605
        $sideSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_TYPE, 'bside', $block['side']);
606
        $sideSelect->addOptionArray([
607
                                        0 => \_AM_SYSTEM_BLOCKS_SBLEFT,
608
                                        1 => \_AM_SYSTEM_BLOCKS_SBRIGHT,
609
                                        3 => \_AM_SYSTEM_BLOCKS_CBLEFT,
610
                                        4 => \_AM_SYSTEM_BLOCKS_CBRIGHT,
611
                                        5 => \_AM_SYSTEM_BLOCKS_CBCENTER,
612
                                        7 => \_AM_SYSTEM_BLOCKS_CBBOTTOMLEFT,
613
                                        8 => \_AM_SYSTEM_BLOCKS_CBBOTTOMRIGHT,
614
                                        9 => \_AM_SYSTEM_BLOCKS_CBBOTTOM,
615
                                    ]);
616
        $form->addElement($sideSelect);
617
        $form->addElement(new \XoopsFormText(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'), 'bweight', 2, 5, $block['weight']));
618
        $form->addElement(new \XoopsFormRadioYN(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE'), 'bvisible', $block['visible']));
619
        $modSelect = new \XoopsFormSelect(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLEIN'), 'bmodule', $block['modules'], 5, true);
620
        /** @var \XoopsModuleHandler $moduleHandler */
621
        $moduleHandler = \xoops_getHandler('module');
622
        $criteria      = new \CriteriaCompo(new \Criteria('hasmain', 1));
623
        $criteria->add(new \Criteria('isactive', 1));
624
        $moduleList     = $moduleHandler->getList($criteria);
625
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
626
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
627
        \ksort($moduleList);
628
        $modSelect->addOptionArray($moduleList);
629
        $form->addElement($modSelect);
630
        $form->addElement(new \XoopsFormText(\_AM_SYSTEM_BLOCKS_TITLE, 'btitle', 50, 255, $block['title']), false);
631
        if ($block['is_custom']) {
632
            $textarea = new \XoopsFormDhtmlTextArea(\_AM_SYSTEM_BLOCKS_CONTENT, 'bcontent', $block['content'], 15, 70);
633
            $textarea->setDescription('<span style="font-size:x-small;font-weight:bold;">' . \_AM_SYSTEM_BLOCKS_USEFULTAGS . '</span><br><span style="font-size:x-small;font-weight:normal;">' . \sprintf(_AM_BLOCKTAG1, '{X_SITEURL}', XOOPS_URL . '/') . '</span>');
634
            $form->addElement($textarea, true);
635
            $ctypeSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_CTYPE, 'bctype', $block['ctype']);
636
            $ctypeSelect->addOptionArray([
637
                                             'H' => \_AM_SYSTEM_BLOCKS_HTML,
638
                                             'P' => \_AM_SYSTEM_BLOCKS_PHP,
639
                                             'S' => \_AM_SYSTEM_BLOCKS_AFWSMILE,
640
                                             'T' => \_AM_SYSTEM_BLOCKS_AFNOSMILE,
641
                                         ]);
642
            $form->addElement($ctypeSelect);
643
        } else {
644
            if ('' !== $block['template']) {
645
                /** @var \XoopsTplfileHandler $tplfileHandler */
646
                $tplfileHandler = \xoops_getHandler('tplfile');
647
                $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $block['bid']);
648
                if (\count($btemplate) > 0) {
649
                    $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_CONTENT, '<a href="' . XOOPS_URL . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate[0]->getVar('tpl_id') . '">' . \_AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
650
                } else {
651
                    $btemplate2 = $tplfileHandler->find('default', 'block', $block['bid']);
652
                    if (\count($btemplate2) > 0) {
653
                        $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_CONTENT, '<a href="' . XOOPS_URL . '/modules/system/admin.php?fct=tplsets&amp;op=edittpl&amp;id=' . $btemplate2[0]->getVar('tpl_id') . '" target="_blank">' . \_AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
654
                    }
655
                }
656
            }
657
            if (false !== $block['edit_form']) {
658
                $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_OPTIONS, $block['edit_form']));
659
            }
660
        }
661
        $cache_select = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_BCACHETIME, 'bcachetime', $block['bcachetime']);
662
        $cache_select->addOptionArray([
663
                                          0       => _NOCACHE,
664
                                          30      => \sprintf(_SECONDS, 30),
665
                                          60      => _MINUTE,
666
                                          300     => \sprintf(_MINUTES, 5),
667
                                          1800    => \sprintf(_MINUTES, 30),
668
                                          3600    => _HOUR,
669
                                          18000   => \sprintf(_HOURS, 5),
670
                                          86400   => _DAY,
671
                                          259200  => \sprintf(_DAYS, 3),
672
                                          604800  => _WEEK,
673
                                          2592000 => _MONTH,
674
                                      ]);
675
        $form->addElement($cache_select);
676
677
        /** @var \XoopsGroupPermHandler $grouppermHandler */
678
        $grouppermHandler = \xoops_getHandler('groupperm');
679
        $groups           = $grouppermHandler->getGroupIds('block_read', $block['bid']);
680
681
        $form->addElement(new \XoopsFormSelectGroup(\_AM_SYSTEM_BLOCKS_GROUP, 'groups', true, $groups, 5, true));
682
683
        if (isset($block['bid'])) {
684
            $form->addElement(new \XoopsFormHidden('bid', $block['bid']));
685
        }
686
        $form->addElement(new \XoopsFormHidden('op', $block['op']));
687
        $form->addElement(new \XoopsFormHidden('fct', 'blocksadmin'));
688
        $buttonTray = new \XoopsFormElementTray('', '&nbsp;');
689
        if ($block['is_custom']) {
690
            $buttonTray->addElement(new \XoopsFormButton('', 'previewblock', _PREVIEW, 'submit'));
691
        }
692
693
        //Submit buttons
694
        $buttonTray   = new \XoopsFormElementTray('', '');
695
        $submitButton = new \XoopsFormButton('', 'submitblock', _SUBMIT, 'submit');
696
        $buttonTray->addElement($submitButton);
697
698
        $cancelButton = new \XoopsFormButton('', '', _CANCEL, 'button');
699
        $cancelButton->setExtra('onclick="history.go(-1)"');
700
        $buttonTray->addElement($cancelButton);
701
702
        $form->addElement($buttonTray);
703
        $form->display();
704
    }
705
}
706