Blocksadmin::isBlockCloned()   B
last analyzed

Complexity

Conditions 11
Paths 105

Size

Total Lines 62
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 43
c 1
b 0
f 0
nc 105
nop 8
dl 0
loc 62
rs 7.275

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Tag\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 or later (https://www.gnu.org/licenses/gpl-2.0.html)
16
 */
17
18
use Xmf\Request;
19
use XoopsModules\Tag\{
20
    Helper
21
};
22
23
//require __DIR__ . '/admin_header.php';
24
25
/**
26
 * class Blocksadmin
27
 */
28
class Blocksadmin
29
{
30
    /**
31
     * @var \XoopsMySQLDatabase|null
32
     */
33
    public $db;
34
    /**
35
     * @var \XoopsModules\Tag\Helper
36
     */
37
    public $helper;
38
    /**
39
     * @var string
40
     */
41
    public $moduleDirName;
42
    /**
43
     * @var string
44
     */
45
    public $moduleDirNameUpper;
46
47
    /**
48
     * Blocksadmin constructor.
49
     */
50
    public function __construct(?\XoopsDatabase $db, Helper $helper)
51
    {
52
        if (null === $db) {
53
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
54
        }
55
        $this->db                 = $db;
56
        $this->helper             = $helper;
57
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
58
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
59
        \xoops_loadLanguage('admin', 'system');
60
        \xoops_loadLanguage('admin/blocksadmin', 'system');
61
        \xoops_loadLanguage('admin/groups', 'system');
62
        \xoops_loadLanguage('common', $this->moduleDirName);
63
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
64
    }
65
66
    public function listBlocks(): void
67
    {
68
        global $xoopsModule, $pathIcon16;
69
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
70
        //        xoops_loadLanguage('admin', 'system');
71
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
72
        //        xoops_loadLanguage('admin/groups', 'system');
73
        //        xoops_loadLanguage('common', $moduleDirName);
74
        //        xoops_loadLanguage('blocks', $moduleDirName);
75
76
        /** @var \XoopsModuleHandler $moduleHandler */
77
        $moduleHandler = \xoops_getHandler('module');
78
        /** @var \XoopsMemberHandler $memberHandler */
79
        $memberHandler = \xoops_getHandler('member');
80
        /** @var \XoopsGroupPermHandler $grouppermHandler */
81
        $grouppermHandler = \xoops_getHandler('groupperm');
82
        $groups           = $memberHandler->getGroups();
83
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', '1'));
84
        $criteria->add(new \Criteria('isactive', '1'));
85
        $moduleList     = $moduleHandler->getList($criteria);
86
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
87
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
88
        \ksort($moduleList);
89
        echo "
90
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
91
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
92
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
93
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
94
        <tr valign='middle'><th align='center'>"
95
             . \_AM_SYSTEM_BLOCKS_TITLE
96
             . "</th><th align='center' nowrap='nowrap'>"
97
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
98
             . '<br>'
99
             . _LEFT
100
             . '-'
101
             . _CENTER
102
             . '-'
103
             . _RIGHT
104
             . "</th><th align='center'>"
105
             . \constant(
106
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
107
             )
108
             . "</th><th align='center'>"
109
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE')
110
             . "</th><th align='center'>"
111
             . \_AM_SYSTEM_BLOCKS_VISIBLEIN
112
             . "</th><th align='center'>"
113
             . \_AM_SYSTEM_ADGS
114
             . "</th><th align='center'>"
115
             . \_AM_SYSTEM_BLOCKS_BCACHETIME
116
             . "</th><th align='center'>"
117
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION')
118
             . '</th></tr>
119
        ';
120
        $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

120
        $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...
121
        $blockCount = \count($blockArray);
0 ignored issues
show
Unused Code introduced by
The assignment to $blockCount is dead and can be removed.
Loading history...
122
        $class      = 'even';
123
        $cachetimes = [
124
            0       => _NOCACHE,
125
            30      => \sprintf(_SECONDS, 30),
126
            60      => _MINUTE,
127
            300     => \sprintf(_MINUTES, 5),
128
            1800    => \sprintf(_MINUTES, 30),
129
            3600    => _HOUR,
130
            18000   => \sprintf(_HOURS, 5),
131
            86400   => _DAY,
132
            259200  => \sprintf(_DAYS, 3),
133
            604800  => _WEEK,
134
            2592000 => _MONTH,
135
        ];
136
        foreach ($blockArray as $i) {
137
            $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
138
            $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

138
            $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...
139
            $result            = $this->db->query($sql);
140
            $modules           = [];
141
            if (!$result instanceof \mysqli_result) {
142
                \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
143
            }
144
            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

144
            while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
145
                $modules[] = (int)$row['module_id'];
146
            }
147
148
            $cachetimeOptions = '';
149
            foreach ($cachetimes as $cachetime => $cachetimeName) {
150
                if ($i->getVar('bcachetime') == $cachetime) {
151
                    $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n";
152
                } else {
153
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
154
                }
155
            }
156
157
            $ssel7 = '';
158
            $ssel6 = $ssel7;
159
            $ssel5 = $ssel6;
160
            $ssel4 = $ssel5;
161
            $ssel3 = $ssel4;
162
            $ssel2 = $ssel3;
163
            $ssel1 = $ssel2;
164
            $ssel0 = $ssel1;
165
            $sel1  = $ssel0;
166
            $sel0  = $sel1;
167
            if (1 === $i->getVar('visible')) {
168
                $sel1 = ' checked';
169
            } else {
170
                $sel0 = ' checked';
171
            }
172
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
173
                $ssel0 = ' checked';
174
            } elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
175
                $ssel1 = ' checked';
176
            } elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
177
                $ssel2 = ' checked';
178
            } elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
179
                $ssel4 = ' checked';
180
            } elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
181
                $ssel3 = ' checked';
182
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
183
                $ssel5 = ' checked';
184
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
185
                $ssel6 = ' checked';
186
            } elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
187
                $ssel7 = ' checked';
188
            }
189
            if ('' === $i->getVar('title')) {
190
                $title = '&nbsp;';
191
            } else {
192
                $title = $i->getVar('title');
193
            }
194
            $name = $i->getVar('name');
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
195
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
196
                 . $i->getVar('bid')
197
                 . "]' value='"
198
                 . $title
199
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
200
                    <div align='center' >
201
                    <input type='radio' name='side["
202
                 . $i->getVar('bid')
203
                 . "]' value='"
204
                 . \XOOPS_CENTERBLOCK_LEFT
205
                 . "'$ssel2>
206
                        <input type='radio' name='side["
207
                 . $i->getVar('bid')
208
                 . "]' value='"
209
                 . \XOOPS_CENTERBLOCK_CENTER
210
                 . "'$ssel3>
211
                    <input type='radio' name='side["
212
                 . $i->getVar('bid')
213
                 . "]' value='"
214
                 . \XOOPS_CENTERBLOCK_RIGHT
215
                 . "'$ssel4>
216
                    </div>
217
                    <div>
218
                        <span style='float:right;'><input type='radio' name='side["
219
                 . $i->getVar('bid')
220
                 . "]' value='"
221
                 . \XOOPS_SIDEBLOCK_RIGHT
222
                 . "'$ssel1></span>
223
                    <div align='left'><input type='radio' name='side["
224
                 . $i->getVar('bid')
225
                 . "]' value='"
226
                 . \XOOPS_SIDEBLOCK_LEFT
227
                 . "'$ssel0></div>
228
                    </div>
229
                    <div align='center'>
230
                    <input type='radio' name='side["
231
                 . $i->getVar('bid')
232
                 . "]' value='"
233
                 . \XOOPS_CENTERBLOCK_BOTTOMLEFT
234
                 . "'$ssel5>
235
                        <input type='radio' name='side["
236
                 . $i->getVar('bid')
237
                 . "]' value='"
238
                 . \XOOPS_CENTERBLOCK_BOTTOM
239
                 . "'$ssel7>
240
                    <input type='radio' name='side["
241
                 . $i->getVar('bid')
242
                 . "]' value='"
243
                 . \XOOPS_CENTERBLOCK_BOTTOMRIGHT
244
                 . "'$ssel6>
245
                    </div>
246
                </td><td class='$class' align='center'><input type='text' name='weight["
247
                 . $i->getVar('bid')
248
                 . "]' value='"
249
                 . $i->getVar('weight')
250
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
251
                 . $i->getVar('bid')
252
                 . "]' value='1'$sel1>"
253
                 . _YES
254
                 . "&nbsp;<input type='radio' name='visible["
255
                 . $i->getVar('bid')
256
                 . "]' value='0'$sel0>"
257
                 . _NO
258
                 . '</td>';
259
260
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
261
            foreach ($moduleList as $k => $v) {
262
                echo "<option value='$k'" . (\in_array($k, $modules) ? " selected='selected'" : '') . ">$v</option>";
263
            }
264
            echo '</select></td>';
265
266
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
267
            foreach ($groups as $grp) {
268
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
269
            }
270
            echo '</select></td>';
271
272
            // Cache lifetime
273
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
274
                                    </td>';
275
276
            // Actions
277
278
            echo "<td class='$class' align='center'>
279
                <a href='blocksadmin.php?op=edit&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/edit.png' . " alt='" . _EDIT . "' title='" . _EDIT . "'></a> 
280
                <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'></a>";
281
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
282
            //                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 . "'>
283
            //                     </a>";
284
            //            }
285
286
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
287
            if (!\in_array($i->getVar('block_type'), ['M', 'S'])) {
288
                echo "&nbsp;
289
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
290
                     </a>";
291
            }
292
            echo "
293
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
294
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
295
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
296
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
297
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
298
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
299
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
300
            </td></tr>
301
            ";
302
            $class = ('even' === $class) ? 'odd' : 'even';
303
        }
304
        echo "<tr><td class='foot' align='center' colspan='8'>
305
        <input type='hidden' name='op' value='order'>
306
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
307
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
308
        </td></tr></table>
309
        </form>
310
        <br><br>";
311
    }
312
313
    public function deleteBlock(int $bid): void
314
    {
315
        //        \xoops_cp_header();
316
317
        \xoops_loadLanguage('admin', 'system');
318
        \xoops_loadLanguage('admin/blocksadmin', 'system');
319
        \xoops_loadLanguage('admin/groups', 'system');
320
321
        $myblock = new \XoopsBlock($bid);
0 ignored issues
show
Unused Code introduced by
The assignment to $myblock is dead and can be removed.
Loading history...
322
323
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
324
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
325
326
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
327
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
328
329
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
330
    }
331
332
    public function cloneBlock(int $bid): void
333
    {
334
        //require __DIR__ . '/admin_header.php';
335
        //        \xoops_cp_header();
336
337
        \xoops_loadLanguage('admin', 'system');
338
        \xoops_loadLanguage('admin/blocksadmin', 'system');
339
        \xoops_loadLanguage('admin/groups', 'system');
340
341
        $myblock = new \XoopsBlock($bid);
342
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
343
        $result  = $this->db->query($sql);
344
        $modules = [];
345
        if ($result instanceof \mysqli_result) {
346
            while (false !== ($row = $this->db->fetchArray($result))) {
347
                $modules[] = (int)$row['module_id'];
348
            }
349
        }
350
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
351
        $block    = [
352
            'title'      => $myblock->getVar('title') . ' Clone',
353
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
354
            'name'       => $myblock->getVar('name'),
355
            'side'       => $myblock->getVar('side'),
356
            'weight'     => $myblock->getVar('weight'),
357
            'visible'    => $myblock->getVar('visible'),
358
            'content'    => $myblock->getVar('content', 'N'),
359
            'modules'    => $modules,
360
            'is_custom'  => $isCustom,
361
            'ctype'      => $myblock->getVar('c_type'),
362
            'bcachetime' => $myblock->getVar('bcachetime'),
363
            'op'         => 'clone_ok',
364
            'bid'        => $myblock->getVar('bid'),
365
            'edit_form'  => $myblock->getOptions(),
366
            'template'   => $myblock->getVar('template'),
367
            'options'    => $myblock->getVar('options'),
368
        ];
369
        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>';
370
        //        $form = new Blockform();
371
        //        $form->render();
372
373
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Tag\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...
374
        //        xoops_cp_footer();
375
        //        require_once __DIR__ . '/admin_footer.php';
376
        //        exit();
377
    }
378
379
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
380
    {
381
        \xoops_loadLanguage('admin', 'system');
382
        \xoops_loadLanguage('admin/blocksadmin', 'system');
383
        \xoops_loadLanguage('admin/groups', 'system');
384
385
        $block = new \XoopsBlock($bid);
386
        $clone = $block->xoopsClone();
387
        if (empty($bmodule)) {
388
            //            \xoops_cp_header();
389
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
390
            \xoops_cp_footer();
391
            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...
392
        }
393
        $clone->setVar('side', $bside);
394
        $clone->setVar('weight', $bweight);
395
        $clone->setVar('visible', $bvisible);
396
        //$clone->setVar('content', $_POST['bcontent']);
397
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
398
        $clone->setVar('bcachetime', $bcachetime);
399
        if (\is_array($options) && (\count($options) > 0)) {
400
            $options = \implode('|', $options);
401
            $clone->setVar('options', $options);
402
        }
403
        $clone->setVar('bid', 0);
404
        if (\in_array($block->getVar('block_type'), ['C', 'E'])) {
405
            $clone->setVar('block_type', 'E');
406
        } else {
407
            $clone->setVar('block_type', 'D');
408
        }
409
        //        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
410
        if ($clone->store()) {
411
            $newid = $clone->id();  //get the id of the cloned block
412
        }
413
        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...
414
            //            \xoops_cp_header();
415
            $clone->getHtmlErrors();
416
            \xoops_cp_footer();
417
            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...
418
        }
419
        if ('' !== $clone->getVar('template')) {
420
            /** @var \XoopsTplfileHandler $tplfileHandler */
421
            $tplfileHandler = \xoops_getHandler('tplfile');
422
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', (string)$bid);
423
            if (\count($btemplate) > 0) {
424
                $tplclone = $btemplate[0]->xoopsClone();
425
                $tplclone->setVar('tpl_id', 0);
426
                $tplclone->setVar('tpl_refid', $newid);
427
                $tplfileHandler->insert($tplclone);
428
            }
429
        }
430
431
        foreach ($bmodule as $bmid) {
432
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
433
            $this->db->query($sql);
434
        }
435
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
436
        foreach ($groups as $iValue) {
437
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
438
            $this->db->query($sql);
439
        }
440
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
441
    }
442
443
    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

443
    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...
444
    {
445
        $myblock = new \XoopsBlock($bid);
446
        $myblock->setVar('title', $title);
447
        $myblock->setVar('weight', $weight);
448
        $myblock->setVar('visible', $visible);
449
        $myblock->setVar('side', $side);
450
        $myblock->setVar('bcachetime', $bcachetime);
451
        $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

451
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
452
        //        /** @var \XoopsBlockHandler $blockHandler */
453
        //        $blockHandler = \xoops_getHandler('block');
454
        //        return $blockHandler->insert($myblock);
455
    }
456
457
    public function editBlock(int $bid): void
458
    {
459
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
460
        //        \xoops_cp_header();
461
        \xoops_loadLanguage('admin', 'system');
462
        \xoops_loadLanguage('admin/blocksadmin', 'system');
463
        \xoops_loadLanguage('admin/groups', 'system');
464
        //        mpu_adm_menu();
465
        $myblock = new \XoopsBlock($bid);
466
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
467
        $result  = $this->db->query($sql);
468
        $modules = [];
469
        if ($result instanceof \mysqli_result) {
470
            while (false !== ($row = $this->db->fetchArray($result))) {
471
                $modules[] = (int)$row['module_id'];
472
            }
473
        }
474
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
475
        $block    = [
476
            'title'      => $myblock->getVar('title'),
477
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
478
            //        'name'       => $myblock->getVar('name'),
479
            'side'       => $myblock->getVar('side'),
480
            'weight'     => $myblock->getVar('weight'),
481
            'visible'    => $myblock->getVar('visible'),
482
            'content'    => $myblock->getVar('content', 'N'),
483
            'modules'    => $modules,
484
            'is_custom'  => $isCustom,
485
            'ctype'      => $myblock->getVar('c_type'),
486
            'bcachetime' => $myblock->getVar('bcachetime'),
487
            'op'         => 'edit_ok',
488
            'bid'        => $myblock->getVar('bid'),
489
            'edit_form'  => $myblock->getOptions(),
490
            'template'   => $myblock->getVar('template'),
491
            'options'    => $myblock->getVar('options'),
492
        ];
493
        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>';
494
495
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Tag\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...
496
    }
497
498
    public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
499
    {
500
        $myblock = new \XoopsBlock($bid);
501
        $myblock->setVar('title', $btitle);
502
        $myblock->setVar('weight', $bweight);
503
        $myblock->setVar('visible', $bvisible);
504
        $myblock->setVar('side', $bside);
505
        $myblock->setVar('bcachetime', $bcachetime);
506
        //update block options
507
        if (isset($options)) {
508
            $optionsCount = \count($options);
509
            if ($optionsCount > 0) {
510
                //Convert array values to comma-separated
511
                foreach ($options as $i => $iValue) {
512
                    if (\is_array($iValue)) {
513
                        $options[$i] = \implode(',', $iValue);
514
                    }
515
                }
516
                $options = \implode('|', $options);
517
                $myblock->setVar('options', $options);
518
            }
519
        }
520
        $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

520
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
521
        //        /** @var \XoopsBlockHandler $blockHandler */
522
        //        $blockHandler = \xoops_getHandler('block');
523
        //        $blockHandler->insert($myblock);
524
525
        if (!empty($bmodule) && \count($bmodule) > 0) {
526
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
527
            $this->db->query($sql);
528
            if (\in_array(0, $bmodule)) {
529
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
530
                $this->db->query($sql);
531
            } else {
532
                foreach ($bmodule as $bmid) {
533
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
534
                    $this->db->query($sql);
535
                }
536
            }
537
        }
538
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
539
        $this->db->query($sql);
540
        if (!empty($groups)) {
541
            foreach ($groups as $grp) {
542
                $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);
543
                $this->db->query($sql);
544
            }
545
        }
546
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
547
    }
548
549
    public function orderBlock(
550
        array $bid,
551
        array $oldtitle,
552
        array $oldside,
553
        array $oldweight,
554
        array $oldvisible,
555
        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

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