Blocksadmin   F
last analyzed

Complexity

Total Complexity 80

Size/Duplication

Total Lines 667
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 403
dl 0
loc 667
rs 2
c 1
b 0
f 0
wmc 80

10 Methods

Rating   Name   Duplication   Size   Complexity  
C render() 0 108 9
A editBlock() 0 41 3
B isBlockCloned() 0 63 11
F listBlocks() 0 173 22
B updateBlock() 0 49 11
A __construct() 0 14 2
C orderBlock() 0 39 15
A deleteBlock() 0 16 3
A setOrder() 0 9 1
A cloneBlock() 0 44 3

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
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico\Common;
6
7
/**
8
 * You may not change or alter any portion of this comment or credits
9
 * of supporting developers from this source code or any supporting source code
10
 * which is considered copyrighted (c) material of the original comment or credit authors.
11
 *
12
 *
13
 * @category        Module
14
 * @author          XOOPS Development Team
15
 * @copyright       XOOPS Project
16
 * @link            https://xoops.org
17
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 */
19
20
use Xmf\Request;
21
use XoopsModules\Suico\{
22
    Helper
23
};
24
25
//require __DIR__ . '/admin_header.php';
26
27
/**
28
 * class Blocksadmin
29
 */
30
class Blocksadmin
31
{
32
    /**
33
     * @var \XoopsMySQLDatabase|null
34
     */
35
    public $db;
36
    /**
37
     * @var Helper
38
     */
39
    public $helper;
40
    /**
41
     * @var string
42
     */
43
    public $moduleDirName;
44
    /**
45
     * @var string
46
     */
47
    public $moduleDirNameUpper;
48
49
    /**
50
     * Blocksadmin constructor.
51
     * @param \XoopsDatabase|null $db
52
     * @param Helper|null         $helper
53
     */
54
    public function __construct(?\XoopsDatabase $db, Helper $helper)
55
    {
56
        if (null === $db) {
57
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
58
        }
59
        $this->db                 = $db;
60
        $this->helper             = $helper;
61
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
62
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
63
        \xoops_loadLanguage('admin', 'system');
64
        \xoops_loadLanguage('admin/blocksadmin', 'system');
65
        \xoops_loadLanguage('admin/groups', 'system');
66
        \xoops_loadLanguage('common', $this->moduleDirName);
67
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
68
    }
69
70
    /**
71
     * @return void
72
     */
73
    public function listBlocks(): void
74
    {
75
        global $xoopsModule, $pathIcon16;
76
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
77
        //        xoops_loadLanguage('admin', 'system');
78
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
79
        //        xoops_loadLanguage('admin/groups', 'system');
80
        //        xoops_loadLanguage('common', $moduleDirName);
81
        //        xoops_loadLanguage('blocks', $moduleDirName);
82
83
        /** @var \XoopsModuleHandler $moduleHandler */
84
        $moduleHandler = \xoops_getHandler('module');
85
        /** @var \XoopsMemberHandler $memberHandler */
86
        $memberHandler = \xoops_getHandler('member');
87
        /** @var \XoopsGroupPermHandler $grouppermHandler */
88
        $grouppermHandler = \xoops_getHandler('groupperm');
89
        $groups           = $memberHandler->getGroups();
90
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', '1'));
91
        $criteria->add(new \Criteria('isactive', '1'));
92
        $moduleList     = $moduleHandler->getList($criteria);
93
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
94
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
95
        \ksort($moduleList);
96
        echo "<h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
97
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
98
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
99
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
100
        <tr valign='middle'><th align='center'>" . \_AM_SYSTEM_BLOCKS_TITLE . "</th><th align='center' nowrap='nowrap'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE') . '<br>' . _LEFT . '-' . _CENTER . '-' . _RIGHT . "</th>
101
        <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT') . "</th>
102
        <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE') . "</th><th align='center'>" . \_AM_SYSTEM_BLOCKS_VISIBLEIN . "</th>
103
        <th align='center'>" . \_AM_SYSTEM_ADGS . "</th>
104
        <th align='center'>" . \_AM_SYSTEM_BLOCKS_BCACHETIME . "</th>
105
        <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION') . '</th>
106
        </tr>';
107
        $blockArray = \XoopsBlock::getByModule($xoopsModule->mid());
108
//        $blockCount = \count($blockArray);
109
        $class      = 'even';
110
        $cachetimes = [
111
            0       => _NOCACHE,
112
            30      => \sprintf(_SECONDS, 30),
113
            60      => _MINUTE,
114
            300     => \sprintf(_MINUTES, 5),
115
            1800    => \sprintf(_MINUTES, 30),
116
            3600    => _HOUR,
117
            18000   => \sprintf(_HOURS, 5),
118
            86400   => _DAY,
119
            259200  => \sprintf(_DAYS, 3),
120
            604800  => _WEEK,
121
            2592000 => _MONTH,
122
        ];
123
        foreach ($blockArray as $i) {
124
            $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
125
            $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

125
            $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...
126
            $result            = $this->db->query($sql);
127
            if (!$this->db->isResultSet($result)) {
128
                \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
129
            }
130
            $modules           = [];
131
            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

131
            while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
132
                $modules[] = (int)$row['module_id'];
133
            }
134
135
            $cachetimeOptions = '';
136
            foreach ($cachetimes as $cachetime => $cachetimeName) {
137
                if ($i->getVar('bcachetime') == $cachetime) {
138
                    $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n";
139
                } else {
140
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
141
                }
142
            }
143
144
            $ssel7 = '';
145
            $ssel6 = $ssel7;
146
            $ssel5 = $ssel6;
147
            $ssel4 = $ssel5;
148
            $ssel3 = $ssel4;
149
            $ssel2 = $ssel3;
150
            $ssel1 = $ssel2;
151
            $ssel0 = $ssel1;
152
            $sel1  = $ssel0;
153
            $sel0  = $sel1;
154
            if (1 === $i->getVar('visible')) {
155
                $sel1 = ' checked';
156
            } else {
157
                $sel0 = ' checked';
158
            }
159
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
160
                $ssel0 = ' checked';
161
            } elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
162
                $ssel1 = ' checked';
163
            } elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
164
                $ssel2 = ' checked';
165
            } elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
166
                $ssel4 = ' checked';
167
            } elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
168
                $ssel3 = ' checked';
169
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
170
                $ssel5 = ' checked';
171
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
172
                $ssel6 = ' checked';
173
            } elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
174
                $ssel7 = ' checked';
175
            }
176
            if ('' === $i->getVar('title')) {
177
                $title = '&nbsp;';
178
            } else {
179
                $title = $i->getVar('title');
180
            }
181
            $name = $i->getVar('name');
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
182
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title[" . $i->getVar('bid') . "]' value='" . $title . "'></td>
183
            <td class='$class' align='center' nowrap='nowrap'><div align='center' >
184
                    <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_LEFT . "'$ssel2>
185
                    <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_CENTER . "'$ssel3>
186
                    <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_RIGHT . "'$ssel4>
187
                    </div>
188
                    <div>
189
                        <span style='float:right;'><input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_SIDEBLOCK_RIGHT . "'$ssel1></span>
190
                    <div align='left'><input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_SIDEBLOCK_LEFT . "'$ssel0></div>
191
                    </div>
192
                    <div align='center'>
193
                    <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_BOTTOMLEFT . "'$ssel5>
194
                        <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_BOTTOM . "'$ssel7>
195
                    <input type='radio' name='side[" . $i->getVar('bid') . "]' value='" . \XOOPS_CENTERBLOCK_BOTTOMRIGHT . "'$ssel6>
196
                    </div>
197
                </td>
198
                <td class='$class' align='center'><input type='text' name='weight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "' size='5' maxlength='5'></td>
199
                <td class='$class' align='center' nowrap><input type='radio' name='visible[" . $i->getVar('bid') . "]' value='1'$sel1>" . _YES . "&nbsp;<input type='radio' name='visible[" . $i->getVar('bid') . "]' value='0'$sel0>" . _NO . '</td>';
200
201
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
202
            foreach ($moduleList as $k => $v) {
203
                echo "<option value='$k'" . (\in_array($k, $modules) ? " selected='selected'" : '') . ">$v</option>";
204
            }
205
            echo '</select></td>';
206
207
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
208
            foreach ($groups as $grp) {
209
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
210
            }
211
            echo '</select></td>';
212
213
            // Cache lifetime
214
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
215
                                    </td>';
216
217
            // Actions
218
219
            echo "<td class='$class' align='center'>
220
                <a href='blocksadmin.php?op=edit&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/edit.png' . " alt='" . _EDIT . "' title='" . _EDIT . "'></a> 
221
                <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'></a>";
222
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
223
            //                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 . "'>
224
            //                     </a>";
225
            //            }
226
227
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
228
            if (!\in_array($i->getVar('block_type'), ['M', 'S'])) {
229
                echo "&nbsp;
230
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
231
                     </a>";
232
            }
233
            echo "
234
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
235
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
236
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
237
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
238
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
239
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
240
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
241
            </td></tr>
242
            ";
243
            $class = ('even' === $class) ? 'odd' : 'even';
244
        }
245
        echo "<tr><td class='foot' align='center' colspan='8'> <input type='hidden' name='op' value='order'>" . $GLOBALS['xoopsSecurity']->getTokenHTML() . "<input type='submit' name='submit' value='" . _SUBMIT . "'></td></tr></table></form><br><br>";
246
    }
247
248
    /**
249
     * @param int $bid
250
     */
251
    public function deleteBlock(int $bid): void
252
    {
253
        //        \xoops_cp_header();
254
255
        \xoops_loadLanguage('admin', 'system');
256
        \xoops_loadLanguage('admin/blocksadmin', 'system');
257
        \xoops_loadLanguage('admin/groups', 'system');
258
259
        $myblock = new \XoopsBlock($bid);
0 ignored issues
show
Unused Code introduced by
The assignment to $myblock is dead and can be removed.
Loading history...
260
261
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
262
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
263
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
264
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
265
266
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
267
    }
268
269
    /**
270
     * @param int $bid
271
     */
272
    public function cloneBlock(int $bid): void
273
    {
274
        //require __DIR__ . '/admin_header.php';
275
        //        \xoops_cp_header();
276
277
        \xoops_loadLanguage('admin', 'system');
278
        \xoops_loadLanguage('admin/blocksadmin', 'system');
279
        \xoops_loadLanguage('admin/groups', 'system');
280
281
        $myblock = new \XoopsBlock($bid);
282
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
283
        $result  = $this->db->query($sql);
284
        if (!$this->db->isResultSet($result)) {
285
            \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
286
        }
287
        $modules = [];
288
        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

288
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
289
            $modules[] = (int)$row['module_id'];
290
        }
291
292
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
293
        $block    = [
294
            'title'      => $myblock->getVar('title') . ' Clone',
295
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
296
            'name'       => $myblock->getVar('name'),
297
            'side'       => $myblock->getVar('side'),
298
            'weight'     => $myblock->getVar('weight'),
299
            'visible'    => $myblock->getVar('visible'),
300
            'content'    => $myblock->getVar('content', 'N'),
301
            'modules'    => $modules,
302
            'is_custom'  => $isCustom,
303
            'ctype'      => $myblock->getVar('c_type'),
304
            'bcachetime' => $myblock->getVar('bcachetime'),
305
            'op'         => 'clone_ok',
306
            'bid'        => $myblock->getVar('bid'),
307
            'edit_form'  => $myblock->getOptions(),
308
            'template'   => $myblock->getVar('template'),
309
            'options'    => $myblock->getVar('options'),
310
        ];
311
        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>';
312
        //        $form = new Blockform();
313
        //        $form->render();
314
315
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Suico\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...
316
        //        xoops_cp_footer();
317
        //        require_once __DIR__ . '/admin_footer.php';
318
        //        exit();
319
    }
320
321
    /**
322
     * @param int        $bid
323
     * @param string     $bside
324
     * @param string     $bweight
325
     * @param string     $bvisible
326
     * @param string     $bcachetime
327
     * @param array|null $bmodule
328
     * @param array|null $options
329
     * @param array|null $groups
330
     */
331
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
332
    {
333
        \xoops_loadLanguage('admin', 'system');
334
        \xoops_loadLanguage('admin/blocksadmin', 'system');
335
        \xoops_loadLanguage('admin/groups', 'system');
336
337
        $block = new \XoopsBlock($bid);
338
        $clone = $block->xoopsClone();
339
        if (empty($bmodule)) {
340
            //            \xoops_cp_header();
341
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
342
            \xoops_cp_footer();
343
            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...
344
        }
345
        $newid = 0;
346
        $clone->setVar('side', $bside);
347
        $clone->setVar('weight', $bweight);
348
        $clone->setVar('visible', $bvisible);
349
        //$clone->setVar('content', $_POST['bcontent']);
350
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
351
        $clone->setVar('bcachetime', $bcachetime);
352
        if (\is_array($options) && (\count($options) > 0)) {
353
            $options = \implode('|', $options);
354
            $clone->setVar('options', $options);
355
        }
356
        $clone->setVar('bid', 0);
357
        if (\in_array($block->getVar('block_type'), ['C', 'E'])) {
358
            $clone->setVar('block_type', 'E');
359
        } else {
360
            $clone->setVar('block_type', 'D');
361
        }
362
        //        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
363
        if ($clone->store()) {
364
            $newid = $clone->id();  //get the id of the cloned block
365
        }
366
        if (0 !== $newid) {
367
            //            \xoops_cp_header();
368
            $clone->getHtmlErrors();
369
            \xoops_cp_footer();
370
            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...
371
        }
372
        if ('' !== $clone->getVar('template')) {
373
            /** @var \XoopsTplfileHandler $tplfileHandler */
374
            $tplfileHandler = \xoops_getHandler('tplfile');
375
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', (string)$bid);
376
            if (\count($btemplate) > 0) {
377
                $tplclone = $btemplate[0]->xoopsClone();
378
                $tplclone->setVar('tpl_id', 0);
379
                $tplclone->setVar('tpl_refid', $newid);
380
                $tplfileHandler->insert($tplclone);
381
            }
382
        }
383
384
        foreach ($bmodule as $bmid) {
385
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
386
            $this->db->query($sql);
387
        }
388
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
389
        foreach ($groups as $iValue) {
390
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
391
            $this->db->query($sql);
392
        }
393
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
394
    }
395
396
    /**
397
     * @param string     $bid
398
     * @param string     $title
399
     * @param string     $weight
400
     * @param string     $visible
401
     * @param string     $side
402
     * @param string     $bcachetime
403
     * @param array|null $bmodule
404
     */
405
    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

405
    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...
406
    {
407
        $myblock = new \XoopsBlock($bid);
408
        $myblock->setVar('title', $title);
409
        $myblock->setVar('weight', $weight);
410
        $myblock->setVar('visible', $visible);
411
        $myblock->setVar('side', $side);
412
        $myblock->setVar('bcachetime', $bcachetime);
413
        $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

413
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
414
        //        /** @var \XoopsBlockHandler $blockHandler */
415
        //        $blockHandler = \xoops_getHandler('block');
416
        //        return $blockHandler->insert($myblock);
417
    }
418
419
    /**
420
     * @param int $bid
421
     * @return void
422
     */
423
    public function editBlock(int $bid): void
424
    {
425
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
426
        //        \xoops_cp_header();
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
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
433
        $result  = $this->db->query($sql);
434
        if (!$this->db->isResultSet($result)) {
435
            \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
436
        }
437
        $modules = [];
438
            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

438
            while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
439
                $modules[] = (int)$row['module_id'];
440
            }
441
442
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
443
        $block    = [
444
            'title'      => $myblock->getVar('title'),
445
            'form_title' => \_AM_SYSTEM_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'  => $isCustom,
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">' . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</a>&nbsp;<span style="font-weight:bold;">&raquo;&raquo;</span>&nbsp;' . \_AM_SYSTEM_BLOCKS_EDITBLOCK . '<br><br>';
462
463
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Suico\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...
464
    }
465
466
    /**
467
     * @param int        $bid
468
     * @param string     $btitle
469
     * @param string     $bside
470
     * @param string     $bweight
471
     * @param string     $bvisible
472
     * @param string     $bcachetime
473
     * @param array|null $bmodule
474
     * @param array|null $options
475
     * @param array|null $groups
476
     */
477
    public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
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
        //update block options
486
        if (isset($options)) {
487
            $optionsCount = \count($options);
488
            if ($optionsCount > 0) {
489
                //Convert array values to comma-separated
490
                foreach ($options as $i => $iValue) {
491
                    if (\is_array($iValue)) {
492
                        $options[$i] = \implode(',', $iValue);
493
                    }
494
                }
495
                $options = \implode('|', $options);
496
                $myblock->setVar('options', $options);
497
            }
498
        }
499
        $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

499
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
500
        //        /** @var \XoopsBlockHandler $blockHandler */
501
        //        $blockHandler = \xoops_getHandler('block');
502
        //        $blockHandler->insert($myblock);
503
504
        if (!empty($bmodule) && \count($bmodule) > 0) {
505
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
506
            $this->db->query($sql);
507
            if (\in_array(0, $bmodule)) {
508
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
509
                $this->db->query($sql);
510
            } else {
511
                foreach ($bmodule as $bmid) {
512
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
513
                    $this->db->query($sql);
514
                }
515
            }
516
        }
517
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
518
        $this->db->query($sql);
519
        if (!empty($groups)) {
520
            foreach ($groups as $grp) {
521
                $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);
522
                $this->db->query($sql);
523
            }
524
        }
525
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
526
    }
527
528
    /**
529
     * @param array $bid
530
     * @param array $oldtitle
531
     * @param array $oldside
532
     * @param array $oldweight
533
     * @param array $oldvisible
534
     * @param array $oldgroups
535
     * @param array $oldbcachetime
536
     * @param array $oldbmodule
537
     * @param array $title
538
     * @param array $weight
539
     * @param array $visible
540
     * @param array $side
541
     * @param array $bcachetime
542
     * @param array $groups
543
     * @param array $bmodule
544
     */
545
    public function orderBlock(
546
        array $bid, array $oldtitle, array $oldside, array $oldweight, array $oldvisible, array $oldgroups, array $oldbcachetime, array $oldbmodule, array $title, array $weight, array $visible, array $side, array $bcachetime, array $groups, array $bmodule
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

546
        array $bid, array $oldtitle, array $oldside, array $oldweight, array $oldvisible, /** @scrutinizer ignore-unused */ array $oldgroups, array $oldbcachetime, array $oldbmodule, array $title, array $weight, array $visible, array $side, array $bcachetime, array $groups, array $bmodule

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