Passed
Pull Request — master (#29)
by Michael
05:27 queued 02:44
created

Blocksadmin::deleteBlock()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Tag\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 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 */
19
20
use Xmf\Request;
21
use XoopsModules\Tag\{
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 \XoopsModules\Tag\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
     */
52
    public function __construct(?\XoopsDatabase $db, Helper $helper)
53
    {
54
        if (null === $db) {
55
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
56
        }
57
        $this->db                 = $db;
58
        $this->helper             = $helper;
59
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
60
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
61
        \xoops_loadLanguage('admin', 'system');
62
        \xoops_loadLanguage('admin/blocksadmin', 'system');
63
        \xoops_loadLanguage('admin/groups', 'system');
64
        \xoops_loadLanguage('common', $this->moduleDirName);
65
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
66
    }
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 "
92
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
93
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
94
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
95
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
96
        <tr valign='middle'><th align='center'>"
97
             . \_AM_SYSTEM_BLOCKS_TITLE
98
             . "</th><th align='center' nowrap='nowrap'>"
99
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
100
             . '<br>'
101
             . _LEFT
102
             . '-'
103
             . _CENTER
104
             . '-'
105
             . _RIGHT
106
             . "</th><th align='center'>"
107
             . \constant(
108
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
109
             )
110
             . "</th><th align='center'>"
111
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE')
112
             . "</th><th align='center'>"
113
             . \_AM_SYSTEM_BLOCKS_VISIBLEIN
114
             . "</th><th align='center'>"
115
             . \_AM_SYSTEM_ADGS
116
             . "</th><th align='center'>"
117
             . \_AM_SYSTEM_BLOCKS_BCACHETIME
118
             . "</th><th align='center'>"
119
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION')
120
             . '</th></tr>
121
        ';
122
        $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

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

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

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

375
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
376
        //        xoops_cp_footer();
377
        //        require_once __DIR__ . '/admin_footer.php';
378
        //        exit();
379
    }
380
381
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
382
    {
383
        \xoops_loadLanguage('admin', 'system');
384
        \xoops_loadLanguage('admin/blocksadmin', 'system');
385
        \xoops_loadLanguage('admin/groups', 'system');
386
387
        $block = new \XoopsBlock($bid);
388
        $clone = $block->xoopsClone();
389
        if (empty($bmodule)) {
390
            //            \xoops_cp_header();
391
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
392
            \xoops_cp_footer();
393
            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...
394
        }
395
        $clone->setVar('side', $bside);
396
        $clone->setVar('weight', $bweight);
397
        $clone->setVar('visible', $bvisible);
398
        //$clone->setVar('content', $_POST['bcontent']);
399
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
400
        $clone->setVar('bcachetime', $bcachetime);
401
        if (\is_array($options) && (\count($options) > 0)) {
402
            $options = \implode('|', $options);
403
            $clone->setVar('options', $options);
404
        }
405
        $clone->setVar('bid', 0);
406
        if (\in_array($block->getVar('block_type'), ['C', 'E'])) {
407
            $clone->setVar('block_type', 'E');
408
        } else {
409
            $clone->setVar('block_type', 'D');
410
        }
411
        //        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
412
        if ($clone->store()) {
413
            $newid = $clone->id();  //get the id of the cloned block
414
        }
415
        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...
416
            //            \xoops_cp_header();
417
            $clone->getHtmlErrors();
418
            \xoops_cp_footer();
419
            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...
420
        }
421
        if ('' !== $clone->getVar('template')) {
422
            /** @var \XoopsTplfileHandler $tplfileHandler */
423
            $tplfileHandler = \xoops_getHandler('tplfile');
424
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
425
            if (\count($btemplate) > 0) {
426
                $tplclone = $btemplate[0]->xoopsClone();
427
                $tplclone->setVar('tpl_id', 0);
428
                $tplclone->setVar('tpl_refid', $newid);
429
                $tplfileHandler->insert($tplclone);
430
            }
431
        }
432
433
        foreach ($bmodule as $bmid) {
434
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
435
            $this->db->query($sql);
436
        }
437
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
438
        foreach ($groups as $iValue) {
439
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
440
            $this->db->query($sql);
441
        }
442
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
443
    }
444
445
    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

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

453
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
454
        //        /** @var \XoopsBlockHandler $blockHandler */
455
        //        $blockHandler = \xoops_getHandler('block');
456
        //        return $blockHandler->insert($myblock);
457
    }
458
459
    public function editBlock(int $bid): void
460
    {
461
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
462
        //        \xoops_cp_header();
463
        \xoops_loadLanguage('admin', 'system');
464
        \xoops_loadLanguage('admin/blocksadmin', 'system');
465
        \xoops_loadLanguage('admin/groups', 'system');
466
        //        mpu_adm_menu();
467
        $myblock = new \XoopsBlock($bid);
468
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
469
        $result  = $this->db->query($sql);
470
        $modules = [];
471
        if ($result instanceof \mysqli_result) {
472
            while (false !== ($row = $this->db->fetchArray($result))) {
473
                $modules[] = (int)$row['module_id'];
474
            }
475
        }
476
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
477
        $block    = [
478
            'title'      => $myblock->getVar('title'),
479
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
480
            //        'name'       => $myblock->getVar('name'),
481
            'side'       => $myblock->getVar('side'),
482
            'weight'     => $myblock->getVar('weight'),
483
            'visible'    => $myblock->getVar('visible'),
484
            'content'    => $myblock->getVar('content', 'N'),
485
            'modules'    => $modules,
486
            'is_custom'  => $isCustom,
487
            'ctype'      => $myblock->getVar('c_type'),
488
            'bcachetime' => $myblock->getVar('bcachetime'),
489
            'op'         => 'edit_ok',
490
            'bid'        => $myblock->getVar('bid'),
491
            'edit_form'  => $myblock->getOptions(),
492
            'template'   => $myblock->getVar('template'),
493
            'options'    => $myblock->getVar('options'),
494
        ];
495
        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>';
496
497
        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...
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

497
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
498
    }
499
500
    public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
501
    {
502
        $myblock = new \XoopsBlock($bid);
503
        $myblock->setVar('title', $btitle);
504
        $myblock->setVar('weight', $bweight);
505
        $myblock->setVar('visible', $bvisible);
506
        $myblock->setVar('side', $bside);
507
        $myblock->setVar('bcachetime', $bcachetime);
508
        //update block options
509
        if (isset($options)) {
510
            $optionsCount = \count($options);
511
            if ($optionsCount > 0) {
512
                //Convert array values to comma-separated
513
                foreach ($options as $i => $iValue) {
514
                    if (\is_array($iValue)) {
515
                        $options[$i] = \implode(',', $iValue);
516
                    }
517
                }
518
                $options = \implode('|', $options);
519
                $myblock->setVar('options', $options);
520
            }
521
        }
522
        $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

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

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