Passed
Push — master ( 7b6eed...a3cd89 )
by Michael
02:46
created

class/Common/Blocksadmin.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Xoopsheadline\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\Xoopsheadline\{
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
    public $helper;
37
    public $moduleDirName;
38
    public $moduleDirNameUpper;
39
40
    /**
41
     * Blocksadmin constructor.
42
     */
43
    public function __construct(?\XoopsDatabase $db, Helper $helper)
44
    {
45
        if (null === $db) {
46
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
47
        }
48
        $this->db                 = $db;
49
        $this->helper             = $helper;
50
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
51
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
52
        \xoops_loadLanguage('admin', 'system');
53
        \xoops_loadLanguage('admin/blocksadmin', 'system');
54
        \xoops_loadLanguage('admin/groups', 'system');
55
        \xoops_loadLanguage('common', $this->moduleDirName);
56
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
57
    }
58
59
    public function listBlocks(): void
60
    {
61
        global $xoopsModule, $pathIcon16;
62
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
63
        //        xoops_loadLanguage('admin', 'system');
64
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
65
        //        xoops_loadLanguage('admin/groups', 'system');
66
        //        xoops_loadLanguage('common', $moduleDirName);
67
        //        xoops_loadLanguage('blocks', $moduleDirName);
68
69
        /** @var \XoopsModuleHandler $moduleHandler */
70
        $moduleHandler = \xoops_getHandler('module');
71
        /** @var \XoopsMemberHandler $memberHandler */
72
        $memberHandler = \xoops_getHandler('member');
73
        /** @var \XoopsGroupPermHandler $grouppermHandler */
74
        $grouppermHandler = \xoops_getHandler('groupperm');
75
        $groups           = $memberHandler->getGroups();
76
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', 1));
77
        $criteria->add(new \Criteria('isactive', 1));
78
        $moduleList     = $moduleHandler->getList($criteria);
79
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
80
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
81
        \ksort($moduleList);
82
        echo "
83
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
84
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
85
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
86
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
87
        <tr valign='middle'><th align='center'>"
88
             . \_AM_SYSTEM_BLOCKS_TITLE
89
             . "</th><th align='center' nowrap='nowrap'>"
90
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
91
             . '<br>'
92
             . _LEFT
93
             . '-'
94
             . _CENTER
95
             . '-'
96
             . _RIGHT
97
             . "</th><th align='center'>"
98
             . \constant(
99
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
100
             )
101
             . "</th><th align='center'>"
102
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE')
103
             . "</th><th align='center'>"
104
             . \_AM_SYSTEM_BLOCKS_VISIBLEIN
105
             . "</th><th align='center'>"
106
             . \_AM_SYSTEM_ADGS
107
             . "</th><th align='center'>"
108
             . \_AM_SYSTEM_BLOCKS_BCACHETIME
109
             . "</th><th align='center'>"
110
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION')
111
             . '</th></tr>
112
        ';
113
        $blockArray = \XoopsBlock::getByModule($xoopsModule->mid());
114
        $blockCount = \count($blockArray);
115
        $class      = 'even';
116
        $cachetimes = [
117
            0       => _NOCACHE,
118
            30      => \sprintf(_SECONDS, 30),
119
            60      => _MINUTE,
120
            300     => \sprintf(_MINUTES, 5),
121
            1800    => \sprintf(_MINUTES, 30),
122
            3600    => _HOUR,
123
            18000   => \sprintf(_HOURS, 5),
124
            86400   => _DAY,
125
            259200  => \sprintf(_DAYS, 3),
126
            604800  => _WEEK,
127
            2592000 => _MONTH,
128
        ];
129
        foreach ($blockArray as $i) {
130
            $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
131
            $sql               = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $i->getVar('bid');
132
            $result            = $this->db->query($sql);
133
            $modules           = [];
134
            if (!$result instanceof \mysqli_result) {
135
                \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
136
            }
137
            while (false !== ($row = $this->db->fetchArray($result))) {
138
                $modules[] = (int)$row['module_id'];
139
            }
140
141
            $cachetimeOptions = '';
142
            foreach ($cachetimes as $cachetime => $cachetimeName) {
143
                if ($i->getVar('bcachetime') == $cachetime) {
144
                    $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n";
145
                } else {
146
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
147
                }
148
            }
149
150
            $ssel7 = '';
151
            $ssel6 = $ssel7;
152
            $ssel5 = $ssel6;
153
            $ssel4 = $ssel5;
154
            $ssel3 = $ssel4;
155
            $ssel2 = $ssel3;
156
            $ssel1 = $ssel2;
157
            $ssel0 = $ssel1;
158
            $sel1  = $ssel0;
159
            $sel0  = $sel1;
160
            if (1 === $i->getVar('visible')) {
161
                $sel1 = ' checked';
162
            } else {
163
                $sel0 = ' checked';
164
            }
165
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
166
                $ssel0 = ' checked';
167
            } elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
168
                $ssel1 = ' checked';
169
            } elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
170
                $ssel2 = ' checked';
171
            } elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
172
                $ssel4 = ' checked';
173
            } elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
174
                $ssel3 = ' checked';
175
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
176
                $ssel5 = ' checked';
177
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
178
                $ssel6 = ' checked';
179
            } elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
180
                $ssel7 = ' checked';
181
            }
182
            if ('' === $i->getVar('title')) {
183
                $title = '&nbsp;';
184
            } else {
185
                $title = $i->getVar('title');
186
            }
187
            $name = $i->getVar('name');
188
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
189
                 . $i->getVar('bid')
190
                 . "]' value='"
191
                 . $title
192
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
193
                    <div align='center' >
194
                    <input type='radio' name='side["
195
                 . $i->getVar('bid')
196
                 . "]' value='"
197
                 . \XOOPS_CENTERBLOCK_LEFT
198
                 . "'$ssel2>
199
                        <input type='radio' name='side["
200
                 . $i->getVar('bid')
201
                 . "]' value='"
202
                 . \XOOPS_CENTERBLOCK_CENTER
203
                 . "'$ssel3>
204
                    <input type='radio' name='side["
205
                 . $i->getVar('bid')
206
                 . "]' value='"
207
                 . \XOOPS_CENTERBLOCK_RIGHT
208
                 . "'$ssel4>
209
                    </div>
210
                    <div>
211
                        <span style='float:right;'><input type='radio' name='side["
212
                 . $i->getVar('bid')
213
                 . "]' value='"
214
                 . \XOOPS_SIDEBLOCK_RIGHT
215
                 . "'$ssel1></span>
216
                    <div align='left'><input type='radio' name='side["
217
                 . $i->getVar('bid')
218
                 . "]' value='"
219
                 . \XOOPS_SIDEBLOCK_LEFT
220
                 . "'$ssel0></div>
221
                    </div>
222
                    <div align='center'>
223
                    <input type='radio' name='side["
224
                 . $i->getVar('bid')
225
                 . "]' value='"
226
                 . \XOOPS_CENTERBLOCK_BOTTOMLEFT
227
                 . "'$ssel5>
228
                        <input type='radio' name='side["
229
                 . $i->getVar('bid')
230
                 . "]' value='"
231
                 . \XOOPS_CENTERBLOCK_BOTTOM
232
                 . "'$ssel7>
233
                    <input type='radio' name='side["
234
                 . $i->getVar('bid')
235
                 . "]' value='"
236
                 . \XOOPS_CENTERBLOCK_BOTTOMRIGHT
237
                 . "'$ssel6>
238
                    </div>
239
                </td><td class='$class' align='center'><input type='text' name='weight["
240
                 . $i->getVar('bid')
241
                 . "]' value='"
242
                 . $i->getVar('weight')
243
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
244
                 . $i->getVar('bid')
245
                 . "]' value='1'$sel1>"
246
                 . _YES
247
                 . "&nbsp;<input type='radio' name='visible["
248
                 . $i->getVar('bid')
249
                 . "]' value='0'$sel0>"
250
                 . _NO
251
                 . '</td>';
252
253
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
254
            foreach ($moduleList as $k => $v) {
255
                echo "<option value='$k'" . (\in_array($k, $modules) ? " selected='selected'" : '') . ">$v</option>";
256
            }
257
            echo '</select></td>';
258
259
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
260
            foreach ($groups as $grp) {
261
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
262
            }
263
            echo '</select></td>';
264
265
            // Cache lifetime
266
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
267
                                    </td>';
268
269
            // Actions
270
271
            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 . "'>
272
                 </a> <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'>
273
                 </a>";
274
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
275
            //                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 . "'>
276
            //                     </a>";
277
            //            }
278
279
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
280
            if (!\in_array($i->getVar('block_type'), ['M', 'S'])) {
281
                echo "&nbsp;
282
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
283
                     </a>";
284
            }
285
            echo "
286
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
287
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
288
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
289
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
290
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
291
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
292
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
293
            </td></tr>
294
            ";
295
            $class = ('even' === $class) ? 'odd' : 'even';
296
        }
297
        echo "<tr><td class='foot' align='center' colspan='8'>
298
        <input type='hidden' name='op' value='order'>
299
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
300
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
301
        </td></tr></table>
302
        </form>
303
        <br><br>";
304
    }
305
306
    public function deleteBlock(int $bid)
307
    {
308
        //        \xoops_cp_header();
309
310
        \xoops_loadLanguage('admin', 'system');
311
        \xoops_loadLanguage('admin/blocksadmin', 'system');
312
        \xoops_loadLanguage('admin/groups', 'system');
313
314
        $myblock = new \XoopsBlock($bid);
315
316
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
317
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
318
319
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
320
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
321
322
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
323
    }
324
325
    public function cloneBlock(int $bid)
326
    {
327
        //require __DIR__ . '/admin_header.php';
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
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
336
        $result  = $this->db->query($sql);
337
        $modules = [];
338
        if ($result instanceof \mysqli_result) {
339
            while (false !== ($row = $this->db->fetchArray($result))) {
340
                $modules[] = (int)$row['module_id'];
341
            }
342
        }
343
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
344
        $block    = [
345
            'title'      => $myblock->getVar('title') . ' Clone',
346
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
347
            'name'       => $myblock->getVar('name'),
348
            'side'       => $myblock->getVar('side'),
349
            'weight'     => $myblock->getVar('weight'),
350
            'visible'    => $myblock->getVar('visible'),
351
            'content'    => $myblock->getVar('content', 'N'),
352
            'modules'    => $modules,
353
            'is_custom'  => $isCustom,
354
            'ctype'      => $myblock->getVar('c_type'),
355
            'bcachetime' => $myblock->getVar('bcachetime'),
356
            'op'         => 'clone_ok',
357
            'bid'        => $myblock->getVar('bid'),
358
            'edit_form'  => $myblock->getOptions(),
359
            'template'   => $myblock->getVar('template'),
360
            'options'    => $myblock->getVar('options'),
361
        ];
362
        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>';
363
        //        $form = new Blockform();
364
        //        $form->render();
365
366
        echo $this->render($block);
367
        //        xoops_cp_footer();
368
        //        require_once __DIR__ . '/admin_footer.php';
369
        //        exit();
370
    }
371
372
    /**
373
     * @param null|array|string $options
374
     */
375
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array$options, ?array$groups): void
376
    {
377
        \xoops_loadLanguage('admin', 'system');
378
        \xoops_loadLanguage('admin/blocksadmin', 'system');
379
        \xoops_loadLanguage('admin/groups', 'system');
380
381
        $block = new \XoopsBlock($bid);
382
        $clone = $block->xoopsClone();
383
        if (empty($bmodule)) {
384
            //            \xoops_cp_header();
385
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
386
            \xoops_cp_footer();
387
            exit();
388
        }
389
        $clone->setVar('side', $bside);
390
        $clone->setVar('weight', $bweight);
391
        $clone->setVar('visible', $bvisible);
392
        //$clone->setVar('content', $_POST['bcontent']);
393
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
394
        $clone->setVar('bcachetime', $bcachetime);
395
        if (\is_array($options) && (\count($options) > 0)) {
396
            $options = \implode('|', $options);
397
            $clone->setVar('options', $options);
398
        }
399
        $clone->setVar('bid', 0);
400
        if (\in_array($block->getVar('block_type'), ['C', 'E'])) {
401
            $clone->setVar('block_type', 'E');
402
        } else {
403
            $clone->setVar('block_type', 'D');
404
        }
405
//        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
406
        if ($clone->store()) {
407
            $newid = $clone->id();  //get the id of the cloned block
408
        }
409
        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...
410
            //            \xoops_cp_header();
411
            $clone->getHtmlErrors();
412
            \xoops_cp_footer();
413
            exit();
414
        }
415
        if ('' !== $clone->getVar('template')) {
416
            /** @var \XoopsTplfileHandler $tplfileHandler */
417
            $tplfileHandler = \xoops_getHandler('tplfile');
418
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
419
            if (\count($btemplate) > 0) {
420
                $tplclone = $btemplate[0]->xoopsClone();
421
                $tplclone->setVar('tpl_id', 0);
422
                $tplclone->setVar('tpl_refid', $newid);
423
                $tplfileHandler->insert($tplclone);
424
            }
425
        }
426
427
        foreach ($bmodule as $bmid) {
428
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
429
            $this->db->query($sql);
430
        }
431
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
432
        foreach ($groups as $iValue) {
433
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
434
            $this->db->query($sql);
435
        }
436
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
437
    }
438
439
440
    public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, ?array $bmodule = null)
441
    {
442
        $myblock = new \XoopsBlock($bid);
443
        $myblock->setVar('title', $title);
444
        $myblock->setVar('weight', $weight);
445
        $myblock->setVar('visible', $visible);
446
        $myblock->setVar('side', $side);
447
        $myblock->setVar('bcachetime', $bcachetime);
448
        $myblock->store();
449
//        /** @var \XoopsBlockHandler $blockHandler */
450
//        $blockHandler = \xoops_getHandler('block');
451
//        return $blockHandler->insert($myblock);
452
    }
453
454
    public function editBlock(int $bid)
455
    {
456
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
457
        //        \xoops_cp_header();
458
        \xoops_loadLanguage('admin', 'system');
459
        \xoops_loadLanguage('admin/blocksadmin', 'system');
460
        \xoops_loadLanguage('admin/groups', 'system');
461
        //        mpu_adm_menu();
462
        $myblock = new \XoopsBlock($bid);
463
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
464
        $result  = $this->db->query($sql);
465
        $modules = [];
466
        if ($result instanceof \mysqli_result) {
467
            while (false !== ($row = $this->db->fetchArray($result))) {
468
                $modules[] = (int)$row['module_id'];
469
            }
470
        }
471
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
472
        $block    = [
473
            'title'      => $myblock->getVar('title'),
474
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
475
            //        'name'       => $myblock->getVar('name'),
476
            'side'       => $myblock->getVar('side'),
477
            'weight'     => $myblock->getVar('weight'),
478
            'visible'    => $myblock->getVar('visible'),
479
            'content'    => $myblock->getVar('content', 'N'),
480
            'modules'    => $modules,
481
            'is_custom'  => $isCustom,
482
            'ctype'      => $myblock->getVar('c_type'),
483
            'bcachetime' => $myblock->getVar('bcachetime'),
484
            'op'         => 'edit_ok',
485
            'bid'        => $myblock->getVar('bid'),
486
            'edit_form'  => $myblock->getOptions(),
487
            'template'   => $myblock->getVar('template'),
488
            'options'    => $myblock->getVar('options'),
489
        ];
490
        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>';
491
492
        echo $this->render($block);
493
    }
494
495
    public function updateBlock(int $bid, string $btitle, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array$options, ?array$groups): void
496
    {
497
        $myblock = new \XoopsBlock($bid);
498
        $myblock->setVar('title', $btitle);
499
        $myblock->setVar('weight', $bweight);
500
        $myblock->setVar('visible', $bvisible);
501
        $myblock->setVar('side', $bside);
502
        $myblock->setVar('bcachetime', $bcachetime);
503
        //update block options
504
        if (isset($options) && \is_array($options)) {
505
            $optionsCount = \count($options);
506
            if ($optionsCount > 0) {
507
                //Convert array values to comma-separated
508
                foreach ($options as $i => $iValue) {
509
                    if (\is_array($iValue)) {
510
                        $options[$i] = \implode(',', $iValue);
511
                    }
512
                }
513
                $options = \implode('|', $options);
514
                $myblock->setVar('options', $options);
515
            }
516
        }
517
        $myblock->store();
518
        //        /** @var \XoopsBlockHandler $blockHandler */
519
        //        $blockHandler = \xoops_getHandler('block');
520
        //        $blockHandler->insert($myblock);
521
522
        if (!empty($bmodule) && \count($bmodule) > 0) {
523
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
524
            $this->db->query($sql);
525
            if (\in_array(0, $bmodule)) {
526
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
527
                $this->db->query($sql);
528
            } else {
529
                foreach ($bmodule as $bmid) {
530
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
531
                    $this->db->query($sql);
532
                }
533
            }
534
        }
535
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
536
        $this->db->query($sql);
537
        if (!empty($groups)) {
538
            foreach ($groups as $grp) {
539
                $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);
540
                $this->db->query($sql);
541
            }
542
        }
543
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
544
    }
545
546
    public function orderBlock(
547
        array $bid,
548
        array $oldtitle,
549
        array $oldside,
550
        array $oldweight,
551
        array $oldvisible,
552
        array $oldgroups,
553
        array $oldbcachetime,
554
        array $oldbmodule ,
555
        array $title,
556
        array $weight,
557
        array $visible,
558
        array $side,
559
        array $bcachetime,
560
        array $groups,
561
        array $bmodule
562
    ): void {
563
        if (!$GLOBALS['xoopsSecurity']->check()) {
564
            \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
565
        }
566
        foreach (\array_keys($bid) as $i) {
567
            if ($oldtitle[$i] !== $title[$i]
568
                || $oldweight[$i] !== $weight[$i]
569
                || $oldvisible[$i] !== $visible[$i]
570
                || $oldside[$i] !== $side[$i]
571
                || $oldbcachetime[$i] !== $bcachetime[$i]
572
                || $oldbmodule[$i] !== $bmodule[$i]){
573
                $this->setOrder($bid[$i], $title[$i], $weight[$i], $visible[$i], $side[$i], $bcachetime[$i], $bmodule[$i]);
574
            }
575
            if (!empty($bmodule[$i]) && \count($bmodule[$i]) > 0) {
576
                $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid[$i]);
577
                $this->db->query($sql);
578
                if (\in_array(0, $bmodule[$i], true)) {
579
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], 0);
580
                    $this->db->query($sql);
581
                } else {
582
                    foreach ($bmodule[$i] as $bmid) {
583
                        $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], (int)$bmid);
584
                        $this->db->query($sql);
585
                    }
586
                }
587
            }
588
            $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid[$i]);
589
            $this->db->query($sql);
590
            if (!empty($groups[$i])) {
591
                foreach ($groups[$i] as $grp) {
592
                    $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]);
593
                    $this->db->query($sql);
594
                }
595
            }
596
        }
597
598
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
599
    }
600
601
    public function render(?array $block = null)
602
    {
603
        \xoops_load('XoopsFormLoader');
604
        \xoops_loadLanguage('common', $this->moduleDirNameUpper);
605
606
        $form = new \XoopsThemeForm($block['form_title'], 'blockform', 'blocksadmin.php', 'post', true);
607
        if (isset($block['name'])) {
608
            $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_NAME, $block['name']));
609
        }
610
        $sideSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_TYPE, 'bside', $block['side']);
611
        $sideSelect->addOptionArray([
612
                                        0 => \_AM_SYSTEM_BLOCKS_SBLEFT,
613
                                        1 => \_AM_SYSTEM_BLOCKS_SBRIGHT,
614
                                        3 => \_AM_SYSTEM_BLOCKS_CBLEFT,
615
                                        4 => \_AM_SYSTEM_BLOCKS_CBRIGHT,
616
                                        5 => \_AM_SYSTEM_BLOCKS_CBCENTER,
617
                                        7 => \_AM_SYSTEM_BLOCKS_CBBOTTOMLEFT,
618
                                        8 => \_AM_SYSTEM_BLOCKS_CBBOTTOMRIGHT,
619
                                        9 => \_AM_SYSTEM_BLOCKS_CBBOTTOM,
620
                                    ]);
621
        $form->addElement($sideSelect);
622
        $form->addElement(new \XoopsFormText(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'), 'bweight', 2, 5, $block['weight']));
623
        $form->addElement(new \XoopsFormRadioYN(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE'), 'bvisible', $block['visible']));
624
        $modSelect = new \XoopsFormSelect(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLEIN'), 'bmodule', $block['modules'], 5, true);
625
        /** @var \XoopsModuleHandler $moduleHandler */
626
        $moduleHandler = \xoops_getHandler('module');
627
        $criteria      = new \CriteriaCompo(new \Criteria('hasmain', 1));
628
        $criteria->add(new \Criteria('isactive', 1));
629
        $moduleList     = $moduleHandler->getList($criteria);
630
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
631
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
632
        \ksort($moduleList);
633
        $modSelect->addOptionArray($moduleList);
634
        $form->addElement($modSelect);
635
        $form->addElement(new \XoopsFormText(\_AM_SYSTEM_BLOCKS_TITLE, 'btitle', 50, 255, $block['title']), false);
636
        if ($block['is_custom']) {
637
            $textarea = new \XoopsFormDhtmlTextArea(\_AM_SYSTEM_BLOCKS_CONTENT, 'bcontent', $block['content'], 15, 70);
638
            $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>');
639
            $form->addElement($textarea, true);
640
            $ctypeSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_CTYPE, 'bctype', $block['ctype']);
641
            $ctypeSelect->addOptionArray([
642
                                             'H' => \_AM_SYSTEM_BLOCKS_HTML,
643
                                             'P' => \_AM_SYSTEM_BLOCKS_PHP,
644
                                             'S' => \_AM_SYSTEM_BLOCKS_AFWSMILE,
645
                                             'T' => \_AM_SYSTEM_BLOCKS_AFNOSMILE,
646
                                         ]);
647
            $form->addElement($ctypeSelect);
648
        } else {
649
            if ('' !== $block['template']) {
650
                /** @var \XoopsTplfileHandler $tplfileHandler */
651
                $tplfileHandler = \xoops_getHandler('tplfile');
652
                $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $block['bid']);
653
                if (\count($btemplate) > 0) {
654
                    $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>'));
655
                } else {
656
                    $btemplate2 = $tplfileHandler->find('default', 'block', $block['bid']);
657
                    if (\count($btemplate2) > 0) {
658
                        $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>'));
659
                    }
660
                }
661
            }
662
            if (false !== $block['edit_form']) {
663
                $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_OPTIONS, $block['edit_form']));
664
            }
665
        }
666
        $cache_select = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_BCACHETIME, 'bcachetime', $block['bcachetime']);
667
        $cache_select->addOptionArray([
668
                                          0       => _NOCACHE,
669
                                          30      => \sprintf(_SECONDS, 30),
670
                                          60      => _MINUTE,
671
                                          300     => \sprintf(_MINUTES, 5),
672
                                          1800    => \sprintf(_MINUTES, 30),
673
                                          3600    => _HOUR,
674
                                          18000   => \sprintf(_HOURS, 5),
675
                                          86400   => _DAY,
676
                                          259200  => \sprintf(_DAYS, 3),
677
                                          604800  => _WEEK,
678
                                          2592000 => _MONTH,
679
                                      ]);
680
        $form->addElement($cache_select);
681
682
        /** @var \XoopsGroupPermHandler $grouppermHandler */
683
        $grouppermHandler = \xoops_getHandler('groupperm');
684
        $groups           = $grouppermHandler->getGroupIds('block_read', $block['bid']);
685
686
        $form->addElement(new \XoopsFormSelectGroup(\_AM_SYSTEM_BLOCKS_GROUP, 'groups', true, $groups, 5, true));
687
688
        if (isset($block['bid'])) {
689
            $form->addElement(new \XoopsFormHidden('bid', $block['bid']));
690
        }
691
        $form->addElement(new \XoopsFormHidden('op', $block['op']));
692
        $form->addElement(new \XoopsFormHidden('fct', 'blocksadmin'));
693
        $buttonTray = new \XoopsFormElementTray('', '&nbsp;');
694
        if ($block['is_custom']) {
695
            $buttonTray->addElement(new \XoopsFormButton('', 'previewblock', _PREVIEW, 'submit'));
696
        }
697
698
        //Submit buttons
699
        $buttonTray   = new \XoopsFormElementTray('', '');
700
        $submitButton = new \XoopsFormButton('', 'submitblock', _SUBMIT, 'submit');
701
        $buttonTray->addElement($submitButton);
702
703
        $cancelButton = new \XoopsFormButton('', '', _CANCEL, 'button');
704
        $cancelButton->setExtra('onclick="history.go(-1)"');
705
        $buttonTray->addElement($cancelButton);
706
707
        $form->addElement($buttonTray);
708
        $form->display();
709
    }
710
}
711
712
713