Passed
Push — master ( 48749a...470881 )
by Michael
03:31
created

class/Common/Blocksadmin.php (2 issues)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Adslight\Common;
4
5
/**
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 *
11
 * @category        Module
12
 * @author          XOOPS Development Team
13
 * @copyright       XOOPS Project
14
 * @link            https://xoops.org
15
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
16
 */
17
18
use Xmf\Request;
19
use XoopsModules\Adslight\{
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 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());
121
        $blockCount = \count($blockArray);
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');
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))) {
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
                }
153
                else {
154
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
155
                }
156
            }
157
158
            $ssel7 = '';
159
            $ssel6 = $ssel7;
160
            $ssel5 = $ssel6;
161
            $ssel4 = $ssel5;
162
            $ssel3 = $ssel4;
163
            $ssel2 = $ssel3;
164
            $ssel1 = $ssel2;
165
            $ssel0 = $ssel1;
166
            $sel1  = $ssel0;
167
            $sel0  = $sel1;
168
            if (1 === $i->getVar('visible')) {
169
                $sel1 = ' checked';
170
            }
171
            else {
172
                $sel0 = ' checked';
173
            }
174
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
175
                $ssel0 = ' checked';
176
            }
177
            elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
178
                $ssel1 = ' checked';
179
            }
180
            elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
181
                $ssel2 = ' checked';
182
            }
183
            elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
184
                $ssel4 = ' checked';
185
            }
186
            elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
187
                $ssel3 = ' checked';
188
            }
189
            elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
190
                $ssel5 = ' checked';
191
            }
192
            elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
193
                $ssel6 = ' checked';
194
            }
195
            elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
196
                $ssel7 = ' checked';
197
            }
198
            if ('' === $i->getVar('title')) {
199
                $title = '&nbsp;';
200
            }
201
            else {
202
                $title = $i->getVar('title');
203
            }
204
            $name = $i->getVar('name');
205
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
206
                 . $i->getVar('bid')
207
                 . "]' value='"
208
                 . $title
209
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
210
                    <div align='center' >
211
                    <input type='radio' name='side["
212
                 . $i->getVar('bid')
213
                 . "]' value='"
214
                 . \XOOPS_CENTERBLOCK_LEFT
215
                 . "'$ssel2>
216
                        <input type='radio' name='side["
217
                 . $i->getVar('bid')
218
                 . "]' value='"
219
                 . \XOOPS_CENTERBLOCK_CENTER
220
                 . "'$ssel3>
221
                    <input type='radio' name='side["
222
                 . $i->getVar('bid')
223
                 . "]' value='"
224
                 . \XOOPS_CENTERBLOCK_RIGHT
225
                 . "'$ssel4>
226
                    </div>
227
                    <div>
228
                        <span style='float:right;'><input type='radio' name='side["
229
                 . $i->getVar('bid')
230
                 . "]' value='"
231
                 . \XOOPS_SIDEBLOCK_RIGHT
232
                 . "'$ssel1></span>
233
                    <div align='left'><input type='radio' name='side["
234
                 . $i->getVar('bid')
235
                 . "]' value='"
236
                 . \XOOPS_SIDEBLOCK_LEFT
237
                 . "'$ssel0></div>
238
                    </div>
239
                    <div align='center'>
240
                    <input type='radio' name='side["
241
                 . $i->getVar('bid')
242
                 . "]' value='"
243
                 . \XOOPS_CENTERBLOCK_BOTTOMLEFT
244
                 . "'$ssel5>
245
                        <input type='radio' name='side["
246
                 . $i->getVar('bid')
247
                 . "]' value='"
248
                 . \XOOPS_CENTERBLOCK_BOTTOM
249
                 . "'$ssel7>
250
                    <input type='radio' name='side["
251
                 . $i->getVar('bid')
252
                 . "]' value='"
253
                 . \XOOPS_CENTERBLOCK_BOTTOMRIGHT
254
                 . "'$ssel6>
255
                    </div>
256
                </td><td class='$class' align='center'><input type='text' name='weight["
257
                 . $i->getVar('bid')
258
                 . "]' value='"
259
                 . $i->getVar('weight')
260
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
261
                 . $i->getVar('bid')
262
                 . "]' value='1'$sel1>"
263
                 . \_YES
264
                 . "&nbsp;<input type='radio' name='visible["
265
                 . $i->getVar('bid')
266
                 . "]' value='0'$sel0>"
267
                 . \_NO
268
                 . '</td>';
269
270
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
271
            foreach ($moduleList as $k => $v) {
272
                echo "<option value='$k'" . (\in_array($k, $modules) ? " selected='selected'" : '') . ">$v</option>";
273
            }
274
            echo '</select></td>';
275
276
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
277
            foreach ($groups as $grp) {
278
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
279
            }
280
            echo '</select></td>';
281
282
            // Cache lifetime
283
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
284
                                    </td>';
285
286
            // Actions
287
288
            echo "<td class='$class' align='center'>
289
                <a href='blocksadmin.php?op=edit&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/edit.png' . " alt='" . \_EDIT . "' title='" . \_EDIT . "'></a> 
290
                <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . \_CLONE . "' title='" . \_CLONE . "'></a>";
291
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
292
            //                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 . "'>
293
            //                     </a>";
294
            //            }
295
296
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
297
            if (!\in_array($i->getVar('block_type'), ['M', 'S'])) {
298
                echo "&nbsp;
299
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . \_DELETE . "' title='" . \_DELETE . "'>
300
                     </a>";
301
            }
302
            echo "
303
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
304
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
305
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
306
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
307
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
308
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
309
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
310
            </td></tr>
311
            ";
312
            $class = ('even' === $class) ? 'odd' : 'even';
313
        }
314
        echo "<tr><td class='foot' align='center' colspan='8'>
315
        <input type='hidden' name='op' value='order'>
316
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
317
        <input type='submit' name='submit' value='" . \_SUBMIT . "'>
318
        </td></tr></table>
319
        </form>
320
        <br><br>";
321
    }
322
323
    /**
324
     * @param int $bid
325
     */
326
    public function deleteBlock(int $bid): void 
327
    {
328
        //        \xoops_cp_header();
329
330
        \xoops_loadLanguage('admin', 'system');
331
        \xoops_loadLanguage('admin/blocksadmin', 'system');
332
        \xoops_loadLanguage('admin/groups', 'system');
333
334
        $myblock = new \XoopsBlock($bid);
335
336
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
337
        $result = $this->db->queryF($sql);
338
        if (!$result) {
339
            \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
340
        }
341
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
342
        $result = $this->db->queryF($sql);
343
        if (!$result) {
344
            \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
345
        }
346
347
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
348
    }
349
350
    /**
351
     * @param int $bid
352
     */
353
    public function cloneBlock(int $bid): void 
354
    {
355
        //require __DIR__ . '/admin_header.php';
356
        //        \xoops_cp_header();
357
358
        \xoops_loadLanguage('admin', 'system');
359
        \xoops_loadLanguage('admin/blocksadmin', 'system');
360
        \xoops_loadLanguage('admin/groups', 'system');
361
362
        $myblock = new \XoopsBlock($bid);
363
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
364
        $result  = $this->db->query($sql);
365
        $modules = [];
366
        if ($this->db->isResultSet($result)) {
367
            while (false !== ($row = $this->db->fetchArray($result))) {
368
                $modules[] = (int)$row['module_id'];
369
            }
370
        }
371
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
372
        $block    = [
373
            'title'      => $myblock->getVar('title') . ' Clone',
374
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
375
            'name'       => $myblock->getVar('name'),
376
            'side'       => $myblock->getVar('side'),
377
            'weight'     => $myblock->getVar('weight'),
378
            'visible'    => $myblock->getVar('visible'),
379
            'content'    => $myblock->getVar('content', 'N'),
380
            'modules'    => $modules,
381
            'is_custom'  => $isCustom,
382
            'ctype'      => $myblock->getVar('c_type'),
383
            'bcachetime' => $myblock->getVar('bcachetime'),
384
            'op'         => 'clone_ok',
385
            'bid'        => $myblock->getVar('bid'),
386
            'edit_form'  => $myblock->getOptions(),
387
            'template'   => $myblock->getVar('template'),
388
            'options'    => $myblock->getVar('options'),
389
        ];
390
        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>';
391
        //        $form = new Blockform();
392
        //        $form->render();
393
394
        echo $this->render($block);
0 ignored issues
show
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

394
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
395
        //        xoops_cp_footer();
396
        //        require_once __DIR__ . '/admin_footer.php';
397
        //        exit();
398
    }
399
400
    /**
401
     * @param int               $bid
402
     * @param string            $bside
403
     * @param string            $bweight
404
     * @param string            $bvisible
405
     * @param string            $bcachetime
406
     * @param array             $bmodule
407
     * @param null|array|string $options
408
     */
409
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
410
    {
411
        \xoops_loadLanguage('admin', 'system');
412
        \xoops_loadLanguage('admin/blocksadmin', 'system');
413
        \xoops_loadLanguage('admin/groups', 'system');
414
415
        $block = new \XoopsBlock($bid);
416
        $clone = $block->xoopsClone();
417
        if (empty($bmodule)) {
418
            //            \xoops_cp_header();
419
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
420
            \xoops_cp_footer();
421
            exit();
422
        }
423
        $clone->setVar('side', $bside);
424
        $clone->setVar('weight', $bweight);
425
        $clone->setVar('visible', $bvisible);
426
        //$clone->setVar('content', $_POST['bcontent']);
427
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
428
        $clone->setVar('bcachetime', $bcachetime);
429
        if (\is_array($options) && (\count($options) > 0)) {
430
            $options = \implode('|', $options);
431
            $clone->setVar('options', $options);
432
        }
433
        $clone->setVar('bid', 0);
434
        if (\in_array($block->getVar('block_type'), ['C', 'E'])) {
435
            $clone->setVar('block_type', 'E');
436
        }
437
        else {
438
            $clone->setVar('block_type', 'D');
439
        }
440
        //        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
441
        if ($clone->store()) {
442
            $newid = $clone->id();  //get the id of the cloned block
443
        }
444
        if (!$newid) {
445
            //            \xoops_cp_header();
446
            $clone->getHtmlErrors();
447
            \xoops_cp_footer();
448
            exit();
449
        }
450
        if ('' !== $clone->getVar('template')) {
451
            /** @var \XoopsTplfileHandler $tplfileHandler */
452
            $tplfileHandler = \xoops_getHandler('tplfile');
453
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', (string)$bid);
454
            if (\count($btemplate) > 0) {
455
                $tplclone = $btemplate[0]->xoopsClone();
456
                $tplclone->setVar('tpl_id', 0);
457
                $tplclone->setVar('tpl_refid', $newid);
458
                $tplfileHandler->insert($tplclone);
459
            }
460
        }
461
462
        foreach ($bmodule as $bmid) {
463
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
464
            $this->db->query($sql);
465
        }
466
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
467
        foreach ($groups as $iValue) {
468
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
469
            $this->db->query($sql);
470
        }
471
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
472
    }
473
474
    /**
475
     * @param int    $bid
476
     * @param string $title
477
     * @param int    $weight
478
     * @param bool   $visible
479
     * @param string $side
480
     * @param int    $bcachetime
481
     */
482
    public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, ?array $bmodule = null)
483
    : void {
484
        $myblock = new \XoopsBlock($bid);
485
        $myblock->setVar('title', $title);
486
        $myblock->setVar('weight', $weight);
487
        $myblock->setVar('visible', $visible);
488
        $myblock->setVar('side', $side);
489
        $myblock->setVar('bcachetime', $bcachetime);
490
        $myblock->store();
491
        //        /** @var \XoopsBlockHandler $blockHandler */
492
        //        $blockHandler = \xoops_getHandler('block');
493
        //        return $blockHandler->insert($myblock);
494
    }
495
496
    /**
497
     * @param int $bid
498
     */
499
    public function editBlock(int $bid): void
500
    {
501
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
502
        //        \xoops_cp_header();
503
        \xoops_loadLanguage('admin', 'system');
504
        \xoops_loadLanguage('admin/blocksadmin', 'system');
505
        \xoops_loadLanguage('admin/groups', 'system');
506
        //        mpu_adm_menu();
507
        $myblock = new \XoopsBlock($bid);
508
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
509
        $result  = $this->db->query($sql);
510
        $modules = [];
511
        if ($this->db->isResultSet($result)) {
512
            while (false !== ($row = $this->db->fetchArray($result))) {
513
                $modules[] = (int)$row['module_id'];
514
            }
515
        }
516
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
517
        $block    = [
518
            'title'      => $myblock->getVar('title'),
519
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
520
            //        'name'       => $myblock->getVar('name'),
521
            'side'       => $myblock->getVar('side'),
522
            'weight'     => $myblock->getVar('weight'),
523
            'visible'    => $myblock->getVar('visible'),
524
            'content'    => $myblock->getVar('content', 'N'),
525
            'modules'    => $modules,
526
            'is_custom'  => $isCustom,
527
            'ctype'      => $myblock->getVar('c_type'),
528
            'bcachetime' => $myblock->getVar('bcachetime'),
529
            'op'         => 'edit_ok',
530
            'bid'        => $myblock->getVar('bid'),
531
            'edit_form'  => $myblock->getOptions(),
532
            'template'   => $myblock->getVar('template'),
533
            'options'    => $myblock->getVar('options'),
534
        ];
535
        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>';
536
537
        echo $this->render($block);
0 ignored issues
show
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

537
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
538
    }
539
540
    /**
541
     * @param int               $bid
542
     * @param string            $btitle
543
     * @param string            $bside
544
     * @param string            $bweight
545
     * @param string            $bvisible
546
     * @param string            $bcachetime
547
     * @param array             $bmodule
548
     * @param null|array|string $options
549
     * @param null|array        $groups
550
     */
551
    public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void 
552
    {
553
        $myblock = new \XoopsBlock($bid);
554
        $myblock->setVar('title', $btitle);
555
        $myblock->setVar('weight', $bweight);
556
        $myblock->setVar('visible', $bvisible);
557
        $myblock->setVar('side', $bside);
558
        $myblock->setVar('bcachetime', $bcachetime);
559
        //update block options
560
        if (isset($options)) {
561
            $optionsCount = \count($options);
562
            if ($optionsCount > 0) {
563
                //Convert array values to comma-separated
564
                foreach ($options as $i => $iValue) {
565
                    if (\is_array($iValue)) {
566
                        $options[$i] = \implode(',', $iValue);
567
                    }
568
                }
569
                $options = \implode('|', $options);
570
                $myblock->setVar('options', $options);
571
            }
572
        }
573
        $myblock->store();
574
        //        /** @var \XoopsBlockHandler $blockHandler */
575
        //        $blockHandler = \xoops_getHandler('block');
576
        //        $blockHandler->insert($myblock);
577
578
        if (!empty($bmodule) && \count($bmodule) > 0) {
579
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
580
            $this->db->query($sql);
581
            if (\in_array(0, $bmodule)) {
582
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
583
                $this->db->query($sql);
584
            }
585
            else {
586
                foreach ($bmodule as $bmid) {
587
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
588
                    $this->db->query($sql);
589
                }
590
            }
591
        }
592
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
593
        $this->db->query($sql);
594
        if (!empty($groups)) {
595
            foreach ($groups as $grp) {
596
                $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);
597
                $this->db->query($sql);
598
            }
599
        }
600
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
601
    }
602
603
    /**
604
     * @param array $bid
605
     */
606
    public function orderBlock(
607
        array $bid,
608
        array $oldtitle,
609
        array $oldside,
610
        array $oldweight,
611
        array $oldvisible,
612
        array $oldgroups,
613
        array $oldbcachetime,
614
        array $oldbmodule,
615
        array $title,
616
        array $weight,
617
        array $visible,
618
        array $side,
619
        array $bcachetime,
620
        array $groups,
621
        array $bmodule
622
    ): void {
623
        if (!$GLOBALS['xoopsSecurity']->check()) {
624
            \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
625
        }
626
        foreach (\array_keys($bid) as $i) {
627
            if ($oldtitle[$i] !== $title[$i]
628
                || $oldweight[$i] !== $weight[$i]
629
                || $oldvisible[$i] !== $visible[$i]
630
                || $oldside[$i] !== $side[$i]
631
                || $oldbcachetime[$i] !== $bcachetime[$i]
632
                || $oldbmodule[$i] !== $bmodule[$i]) {
633
                $this->setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);
634
            }
635
            if (!empty($bmodule[$i]) && \count($bmodule[$i]) > 0) {
636
                $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid[$i]);
637
                $this->db->query($sql);
638
                if (\in_array(0, $bmodule[$i], true)) {
639
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], 0);
640
                    $this->db->query($sql);
641
                }
642
                else {
643
                    foreach ($bmodule[$i] as $bmid) {
644
                        $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], (int)$bmid);
645
                        $this->db->query($sql);
646
                    }
647
                }
648
            }
649
            $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid[$i]);
650
            $this->db->query($sql);
651
            if (!empty($groups[$i])) {
652
                foreach ($groups[$i] as $grp) {
653
                    $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]);
654
                    $this->db->query($sql);
655
                }
656
            }
657
        }
658
659
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
660
    }
661
662
    /**
663
     * @param null|\array $block
664
     */
665
    public function render(?array $block = null): void
666
    {
667
        \xoops_load('XoopsFormLoader');
668
        \xoops_loadLanguage('common', $this->moduleDirNameUpper);
669
670
        $form = new \XoopsThemeForm($block['form_title'], 'blockform', 'blocksadmin.php', 'post', true);
671
        if (isset($block['name'])) {
672
            $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_NAME, $block['name']));
673
        }
674
        $sideSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_TYPE, 'bside', $block['side']);
675
        $sideSelect->addOptionArray([
676
                                        0 => \_AM_SYSTEM_BLOCKS_SBLEFT,
677
                                        1 => \_AM_SYSTEM_BLOCKS_SBRIGHT,
678
                                        3 => \_AM_SYSTEM_BLOCKS_CBLEFT,
679
                                        4 => \_AM_SYSTEM_BLOCKS_CBRIGHT,
680
                                        5 => \_AM_SYSTEM_BLOCKS_CBCENTER,
681
                                        7 => \_AM_SYSTEM_BLOCKS_CBBOTTOMLEFT,
682
                                        8 => \_AM_SYSTEM_BLOCKS_CBBOTTOMRIGHT,
683
                                        9 => \_AM_SYSTEM_BLOCKS_CBBOTTOM,
684
                                    ]);
685
        $form->addElement($sideSelect);
686
        $form->addElement(new \XoopsFormText(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'), 'bweight', 2, 5, $block['weight']));
687
        $form->addElement(new \XoopsFormRadioYN(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE'), 'bvisible', $block['visible']));
688
        $modSelect = new \XoopsFormSelect(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLEIN'), 'bmodule', $block['modules'], 5, true);
689
        /** @var \XoopsModuleHandler $moduleHandler */
690
        $moduleHandler = \xoops_getHandler('module');
691
        $criteria      = new \CriteriaCompo(new \Criteria('hasmain', '1'));
692
        $criteria->add(new \Criteria('isactive', '1'));
693
        $moduleList     = $moduleHandler->getList($criteria);
694
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
695
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
696
        \ksort($moduleList);
697
        $modSelect->addOptionArray($moduleList);
698
        $form->addElement($modSelect);
699
        $form->addElement(new \XoopsFormText(\_AM_SYSTEM_BLOCKS_TITLE, 'btitle', 50, 255, $block['title']), false);
700
        if ($block['is_custom']) {
701
            $textarea = new \XoopsFormDhtmlTextArea(\_AM_SYSTEM_BLOCKS_CONTENT, 'bcontent', $block['content'], 15, 70);
702
            $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>');
703
            $form->addElement($textarea, true);
704
            $ctypeSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_CTYPE, 'bctype', $block['ctype']);
705
            $ctypeSelect->addOptionArray([
706
                                             'H' => \_AM_SYSTEM_BLOCKS_HTML,
707
                                             'P' => \_AM_SYSTEM_BLOCKS_PHP,
708
                                             'S' => \_AM_SYSTEM_BLOCKS_AFWSMILE,
709
                                             'T' => \_AM_SYSTEM_BLOCKS_AFNOSMILE,
710
                                         ]);
711
            $form->addElement($ctypeSelect);
712
        }
713
        else {
714
            if ('' !== $block['template']) {
715
                /** @var \XoopsTplfileHandler $tplfileHandler */
716
                $tplfileHandler = \xoops_getHandler('tplfile');
717
                $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $block['bid']);
718
                if (\count($btemplate) > 0) {
719
                    $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>'));
720
                }
721
                else {
722
                    $btemplate2 = $tplfileHandler->find('default', 'block', $block['bid']);
723
                    if (\count($btemplate2) > 0) {
724
                        $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>'));
725
                    }
726
                }
727
            }
728
            if (false !== $block['edit_form']) {
729
                $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_OPTIONS, $block['edit_form']));
730
            }
731
        }
732
        $cache_select = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_BCACHETIME, 'bcachetime', $block['bcachetime']);
733
        $cache_select->addOptionArray([
734
                                          0       => \_NOCACHE,
735
                                          30      => \sprintf(\_SECONDS, 30),
736
                                          60      => \_MINUTE,
737
                                          300     => \sprintf(\_MINUTES, 5),
738
                                          1800    => \sprintf(\_MINUTES, 30),
739
                                          3600    => \_HOUR,
740
                                          18000   => \sprintf(\_HOURS, 5),
741
                                          86400   => \_DAY,
742
                                          259200  => \sprintf(\_DAYS, 3),
743
                                          604800  => \_WEEK,
744
                                          2592000 => \_MONTH,
745
                                      ]);
746
        $form->addElement($cache_select);
747
748
        /** @var \XoopsGroupPermHandler $grouppermHandler */
749
        $grouppermHandler = \xoops_getHandler('groupperm');
750
        $groups           = $grouppermHandler->getGroupIds('block_read', $block['bid']);
751
752
        $form->addElement(new \XoopsFormSelectGroup(\_AM_SYSTEM_BLOCKS_GROUP, 'groups', true, $groups, 5, true));
753
754
        if (isset($block['bid'])) {
755
            $form->addElement(new \XoopsFormHidden('bid', $block['bid']));
756
        }
757
        $form->addElement(new \XoopsFormHidden('op', $block['op']));
758
        $form->addElement(new \XoopsFormHidden('fct', 'blocksadmin'));
759
        $buttonTray = new \XoopsFormElementTray('', '&nbsp;');
760
        if ($block['is_custom']) {
761
            $buttonTray->addElement(new \XoopsFormButton('', 'previewblock', \_PREVIEW, 'submit'));
762
        }
763
764
        //Submit buttons
765
        $buttonTray   = new \XoopsFormElementTray('', '');
766
        $submitButton = new \XoopsFormButton('', 'submitblock', \_SUBMIT, 'submit');
767
        $buttonTray->addElement($submitButton);
768
769
        $cancelButton = new \XoopsFormButton('', '', \_CANCEL, 'button');
770
        $cancelButton->setExtra('onclick="history.go(-1)"');
771
        $buttonTray->addElement($cancelButton);
772
773
        $form->addElement($buttonTray);
774
        $form->display();
775
    }
776
}
777