Blocksadmin   F
last analyzed

Complexity

Total Complexity 80

Size/Duplication

Total Lines 667
Duplicated Lines 0 %

Importance

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

10 Methods

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

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\Myiframe\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
22
//require __DIR__ . '/admin_header.php';
23
24
/**
25
 * class Blocksadmin
26
 */
27
class Blocksadmin
28
{
29
    /**
30
     * @var \XoopsMySQLDatabase|null
31
     */
32
    public $db;
33
    /**
34
     * @var \Xmf\Module\Helper
35
     */
36
    public $helper;
37
    /**
38
     * @var string
39
     */
40
    public $moduleDirName;
41
    /**
42
     * @var string
43
     */
44
    public $moduleDirNameUpper;
45
46
    /**
47
     * Blocksadmin constructor.
48
     */
49
    public function __construct(?\XoopsDatabase $db, \Xmf\Module\Helper $helper)
50
    {
51
        if (null === $db) {
52
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
53
        }
54
        $this->db                 = $db;
55
        $this->helper             = $helper;
56
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
57
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
58
        \xoops_loadLanguage('admin', 'system');
59
        \xoops_loadLanguage('admin/blocksadmin', 'system');
60
        \xoops_loadLanguage('admin/groups', 'system');
61
        \xoops_loadLanguage('common', $this->moduleDirName);
62
        //        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
63
    }
64
65
    /**
66
     * @return void
67
     */
68
    public function listBlocks(): void
69
    {
70
        global $xoopsModule, $pathIcon16;
71
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
72
        //        xoops_loadLanguage('admin', 'system');
73
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
74
        //        xoops_loadLanguage('admin/groups', 'system');
75
        //        xoops_loadLanguage('common', $moduleDirName);
76
        //        xoops_loadLanguage('blocks', $moduleDirName);
77
78
        /** @var \XoopsModuleHandler $moduleHandler */
79
        $moduleHandler = \xoops_getHandler('module');
80
        /** @var \XoopsMemberHandler $memberHandler */
81
        $memberHandler = \xoops_getHandler('member');
82
        /** @var \XoopsGroupPermHandler $grouppermHandler */
83
        $grouppermHandler = \xoops_getHandler('groupperm');
84
        $groups           = $memberHandler->getGroups();
85
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', '1'));
86
        $criteria->add(new \Criteria('isactive', '1'));
87
        $moduleList     = $moduleHandler->getList($criteria);
88
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
89
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
90
        \ksort($moduleList);
91
        echo "<h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
92
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
93
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
94
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
95
        <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>
96
        <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT') . "</th>
97
        <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE') . "</th><th align='center'>" . \_AM_SYSTEM_BLOCKS_VISIBLEIN . "</th>
98
        <th align='center'>" . \_AM_SYSTEM_ADGS . "</th>
99
        <th align='center'>" . \_AM_SYSTEM_BLOCKS_BCACHETIME . "</th>
100
        <th align='center'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION') . '</th>
101
        </tr>';
102
        $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

102
        $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...
103
        $blockCount = \count($blockArray);
0 ignored issues
show
Unused Code introduced by
The assignment to $blockCount is dead and can be removed.
Loading history...
104
        $class      = 'even';
105
        $cachetimes = [
106
            0       => \_NOCACHE,
107
            30      => \sprintf(\_SECONDS, 30),
108
            60      => \_MINUTE,
109
            300     => \sprintf(\_MINUTES, 5),
110
            1800    => \sprintf(\_MINUTES, 30),
111
            3600    => \_HOUR,
112
            18000   => \sprintf(\_HOURS, 5),
113
            86400   => \_DAY,
114
            259200  => \sprintf(\_DAYS, 3),
115
            604800  => \_WEEK,
116
            2592000 => \_MONTH,
117
        ];
118
        foreach ($blockArray as $i) {
119
            $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
120
            $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

120
            $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...
121
            $result            = $this->db->query($sql);
122
            $modules           = [];
123
            if (!$result instanceof \mysqli_result) {
124
                \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
125
            }
126
            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

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

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

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

409
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
410
        //        /** @var \XoopsBlockHandler $blockHandler */
411
        //        $blockHandler = \xoops_getHandler('block');
412
        //        return $blockHandler->insert($myblock);
413
    }
414
415
    /**
416
     * @param int $bid
417
     * @return void
418
     */
419
    public function editBlock(int $bid): void
420
    {
421
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
422
        //        \xoops_cp_header();
423
        \xoops_loadLanguage('admin', 'system');
424
        \xoops_loadLanguage('admin/blocksadmin', 'system');
425
        \xoops_loadLanguage('admin/groups', 'system');
426
        //        mpu_adm_menu();
427
        $myblock = new \XoopsBlock($bid);
428
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
429
        $result  = $this->db->query($sql);
430
        $modules = [];
431
        if ($result instanceof \mysqli_result) {
432
            while (false !== ($row = $this->db->fetchArray($result))) {
433
                $modules[] = (int)$row['module_id'];
434
            }
435
        }
436
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
437
        $block    = [
438
            'title'      => $myblock->getVar('title'),
439
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
440
            //        'name'       => $myblock->getVar('name'),
441
            'side'       => $myblock->getVar('side'),
442
            'weight'     => $myblock->getVar('weight'),
443
            'visible'    => $myblock->getVar('visible'),
444
            'content'    => $myblock->getVar('content', 'N'),
445
            'modules'    => $modules,
446
            'is_custom'  => $isCustom,
447
            'ctype'      => $myblock->getVar('c_type'),
448
            'bcachetime' => $myblock->getVar('bcachetime'),
449
            'op'         => 'edit_ok',
450
            'bid'        => $myblock->getVar('bid'),
451
            'edit_form'  => $myblock->getOptions(),
452
            'template'   => $myblock->getVar('template'),
453
            'options'    => $myblock->getVar('options'),
454
        ];
455
        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>';
456
457
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Myiframe\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...
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

457
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
458
    }
459
460
    /**
461
     * @param int        $bid
462
     * @param string     $btitle
463
     * @param string     $bside
464
     * @param string     $bweight
465
     * @param string     $bvisible
466
     * @param string     $bcachetime
467
     * @param array|null $bmodule
468
     * @param array|null $options
469
     * @param array|null $groups
470
     * @return void
471
     */
472
    public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
473
    {
474
        $myblock = new \XoopsBlock($bid);
475
        $myblock->setVar('title', $btitle);
476
        $myblock->setVar('weight', $bweight);
477
        $myblock->setVar('visible', $bvisible);
478
        $myblock->setVar('side', $bside);
479
        $myblock->setVar('bcachetime', $bcachetime);
480
        //update block options
481
        if (isset($options)) {
482
            $optionsCount = \count($options);
483
            if ($optionsCount > 0) {
484
                //Convert array values to comma-separated
485
                foreach ($options as $i => $iValue) {
486
                    if (\is_array($iValue)) {
487
                        $options[$i] = \implode(',', $iValue);
488
                    }
489
                }
490
                $options = \implode('|', $options);
491
                $myblock->setVar('options', $options);
492
            }
493
        }
494
        $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

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

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