Passed
Push — master ( eec3d3...7b6eed )
by Michael
02:45
created

Blocksadmin::orderBlock()   C

Complexity

Conditions 14
Paths 38

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 24
nc 38
nop 14
dl 0
loc 49
rs 6.2666
c 1
b 0
f 0

How to fix   Complexity    Many Parameters   

Long Method

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

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

Commonly applied refactorings include:

Many Parameters

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

There are several approaches to avoid long parameter lists:

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

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

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

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

395
        if (isset($options) && (\count(/** @scrutinizer ignore-type */ $options) > 0)) {
Loading history...
396
            $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

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

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

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

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

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