Passed
Push — master ( f22b21...1a00ed )
by Michael
02:58
created

Blocksadmin::editBlock()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 43
rs 9.456
cc 3
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Adslight\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
22
//require __DIR__ . '/admin_header.php';
23
24
class Blocksadmin
25
{
26
    /**
27
     * @var \XoopsMySQLDatabase|null
28
     */
29
    public $db;
30
    public $modHelper;
31
    public $moduleDirName;
32
    public $moduleDirNameUpper;
33
34
    /**
35
     * @param $db
36
     * @param $modHelper
37
     */
38
    public function __construct($db, $modHelper)
39
    {
40
        if (null === $db){
41
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
42
        }
43
        $this->db                 = $db;
44
        $this->modHelper          = $modHelper;
45
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
46
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
47
        \xoops_loadLanguage('admin', 'system');
48
        \xoops_loadLanguage('admin/blocksadmin', 'system');
49
        \xoops_loadLanguage('admin/groups', 'system');
50
        \xoops_loadLanguage('common', $this->moduleDirName);
51
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
52
    }
53
54
    public function listBlocks(): void
55
    {
56
        global $xoopsModule, $pathIcon16;
57
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
58
        //        xoops_loadLanguage('admin', 'system');
59
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
60
        //        xoops_loadLanguage('admin/groups', 'system');
61
        //        xoops_loadLanguage('common', $moduleDirName);
62
        //        xoops_loadLanguage('blocks', $moduleDirName);
63
64
        /** @var \XoopsModuleHandler $moduleHandler */
65
        $moduleHandler = \xoops_getHandler('module');
66
        /** @var \XoopsMemberHandler $memberHandler */
67
        $memberHandler = \xoops_getHandler('member');
68
        /** @var \XoopsGroupPermHandler $grouppermHandler */
69
        $grouppermHandler = \xoops_getHandler('groupperm');
70
        $groups           = $memberHandler->getGroups();
71
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', 1));
72
        $criteria->add(new \Criteria('isactive', 1));
73
        $moduleList     = $moduleHandler->getList($criteria);
74
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
75
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
76
        \ksort($moduleList);
77
        echo "
78
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
79
        /** @var \XoopsModuleHandler $moduleHandler */
80
        $moduleHandler = \xoops_getHandler('module');
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleHandler is dead and can be removed.
Loading history...
81
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
82
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
83
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
84
        <tr valign='middle'><th align='center'>"
85
             . \_AM_SYSTEM_BLOCKS_TITLE
86
             . "</th><th align='center' nowrap='nowrap'>"
87
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
88
             . '<br>'
89
             . _LEFT
90
             . '-'
91
             . _CENTER
92
             . '-'
93
             . _RIGHT
94
             . "</th><th align='center'>"
95
             . \constant(
96
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
97
             )
98
             . "</th><th align='center'>"
99
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE')
100
             . "</th><th align='center'>"
101
             . \_AM_SYSTEM_BLOCKS_VISIBLEIN
102
             . "</th><th align='center'>"
103
             . \_AM_SYSTEM_ADGS
104
             . "</th><th align='center'>"
105
             . \_AM_SYSTEM_BLOCKS_BCACHETIME
106
             . "</th><th align='center'>"
107
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION')
108
             . '</th></tr>
109
        ';
110
        $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

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

128
            $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...
129
            $result            = $this->db->query($sql);
130
            $modules           = [];
131
            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

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

393
        if (isset($options) && (\count(/** @scrutinizer ignore-type */ $options) > 0)) {
Loading history...
394
            $options = \implode('|', $options);
0 ignored issues
show
Bug introduced by
It seems like $options can also be of type string; however, parameter $pieces of implode() does only seem to accept array, 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

394
            $options = \implode('|', /** @scrutinizer ignore-type */ $options);
Loading history...
395
            $clone->setVar('options', $options);
396
        }
397
        $clone->setVar('bid', 0);
398
        if (\in_array($block->getVar('block_type'), ['C', 'E'])) {
399
            $clone->setVar('block_type', 'E');
400
        } else {
401
            $clone->setVar('block_type', 'D');
402
        }
403
        $newid = $clone->store();
404
        if (!$newid) {
405
            //            \xoops_cp_header();
406
            $clone->getHtmlErrors();
407
            \xoops_cp_footer();
408
            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...
409
        }
410
        if ('' !== $clone->getVar('template')) {
411
            /** @var \XoopsTplfileHandler $tplfileHandler */
412
            $tplfileHandler = \xoops_getHandler('tplfile');
413
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
414
            if (\count($btemplate) > 0) {
415
                $tplclone = $btemplate[0]->xoopsClone();
416
                $tplclone->setVar('tpl_id', 0);
417
                $tplclone->setVar('tpl_refid', $newid);
418
                $tplfileHandler->insert($tplclone);
419
            }
420
        }
421
422
        foreach ($bmodule as $bmid) {
423
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
424
            $this->db->query($sql);
425
        }
426
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
427
        foreach ($groups as $iValue) {
428
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
429
            $this->db->query($sql);
430
        }
431
        $this->modHelper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
432
    }
433
434
    /**
435
     * @param int    $bid
436
     * @param string $title
437
     * @param int    $weight
438
     * @param bool   $visible
439
     * @param string $side
440
     * @param int    $bcachetime
441
     */
442
    public function setOrder($bid, $title, $weight, $visible, $side, $bcachetime, $bmodule): 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

442
    public function setOrder($bid, $title, $weight, $visible, $side, $bcachetime, /** @scrutinizer ignore-unused */ $bmodule): 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...
443
    {
444
        $myblock = new \XoopsBlock($bid);
445
        $myblock->setVar('title', $title);
446
        $myblock->setVar('weight', $weight);
447
        $myblock->setVar('visible', $visible);
448
        $myblock->setVar('side', $side);
449
        $myblock->setVar('bcachetime', $bcachetime);
450
        $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

450
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
451
        //        /** @var \XoopsBlockHandler $blockHandler */
452
        //        $blockHandler = \xoops_getHandler('block');
453
        //        return $blockHandler->insert($myblock);
454
    }
455
456
    /**
457
     * @param int $bid
458
     */
459
    public function editBlock($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=' . (int)$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
        /** @var \XoopsThemeForm $form */ //                $form = new Blockform();
498
        //        $form->render($block);
499
        //        $form = new \XoopsThemeForm();
500
501
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Adslight\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...
502
    }
503
504
    /**
505
     * @param int               $bid
506
     * @param string            $btitle
507
     * @param string            $bside
508
     * @param string            $bweight
509
     * @param string            $bvisible
510
     * @param string            $bcachetime
511
     * @param array             $bmodule
512
     * @param null|array|string $options
513
     * @param null|array        $groups
514
     */
515
    public function updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups): void
516
    {
517
        $myblock = new \XoopsBlock($bid);
518
        $myblock->setVar('title', $btitle);
519
        $myblock->setVar('weight', $bweight);
520
        $myblock->setVar('visible', $bvisible);
521
        $myblock->setVar('side', $bside);
522
        $myblock->setVar('bcachetime', $bcachetime);
523
        //update block options
524
        if (isset($options) && is_array($options)) {
525
            $optionsCount = \count($options);
526
            if ($optionsCount > 0) {
527
                //Convert array values to comma-separated
528
                foreach ($options as $i => $iValue) {
529
                    if (\is_array($iValue)) {
530
                        $options[$i] = \implode(',', $iValue);
531
                    }
532
                }
533
                $options = \implode('|', $options);
534
                $myblock->setVar('options', $options);
535
            }
536
        }
537
        $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

537
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
538
        //        /** @var \XoopsBlockHandler $blockHandler */
539
        //        $blockHandler = \xoops_getHandler('block');
540
        //        $blockHandler->insert($myblock);
541
542
        if (!empty($bmodule) && \count($bmodule) > 0) {
543
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
544
            $this->db->query($sql);
545
            if (\in_array(0, $bmodule)) {
546
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
547
                $this->db->query($sql);
548
            } else {
549
                foreach ($bmodule as $bmid) {
550
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
551
                    $this->db->query($sql);
552
                }
553
            }
554
        }
555
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
556
        $this->db->query($sql);
557
        if (!empty($groups)) {
558
            foreach ($groups as $grp) {
559
                $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);
560
                $this->db->query($sql);
561
            }
562
        }
563
        $this->modHelper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
564
    }
565
566
    /**
567
     * @param array $bid
568
     */
569
    public function orderBlock(
570
        $bid,
571
        $oldtitle,
572
        $oldside,
573
        $oldweight,
574
        $oldvisible,
575
        $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

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