Blocksadmin::listBlocks()   F
last analyzed

Complexity

Conditions 22
Paths 6913

Size

Total Lines 242
Code Lines 183

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 22
eloc 183
nc 6913
nop 0
dl 0
loc 242
rs 0
c 3
b 0
f 0

How to fix   Long Method    Complexity   

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:

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xoopsheadline\Common;
4
5
/**
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 *
11
 * @category        Module
12
 * @author          XOOPS Development Team
13
 * @copyright       XOOPS Project
14
 * @link            https://xoops.org
15
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
16
 */
17
18
use Xmf\Request;
19
use XoopsModules\Xoopsheadline\{
20
    Helper
21
};
22
23
//require __DIR__ . '/admin_header.php';
24
25
/**
26
 * class Blocksadmin
27
 */
28
class Blocksadmin
29
{
30
    /**
31
     * @var \XoopsMySQLDatabase|null
32
     */
33
    public $db;
34
    public $helper;
35
    public $moduleDirName;
36
    public $moduleDirNameUpper;
37
38
    /**
39
     * Blocksadmin constructor.
40
     */
41
    public function __construct(?\XoopsDatabase $db, Helper $helper)
42
    {
43
        if (null === $db) {
44
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
45
        }
46
        $this->db                 = $db;
47
        $this->helper             = $helper;
48
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
49
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
50
        \xoops_loadLanguage('admin', 'system');
51
        \xoops_loadLanguage('admin/blocksadmin', 'system');
52
        \xoops_loadLanguage('admin/groups', 'system');
53
        \xoops_loadLanguage('common', $this->moduleDirName);
54
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
55
    }
56
57
    public function listBlocks(): void
58
    {
59
        global $xoopsModule, $pathIcon16;
60
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
61
        //        xoops_loadLanguage('admin', 'system');
62
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
63
        //        xoops_loadLanguage('admin/groups', 'system');
64
        //        xoops_loadLanguage('common', $moduleDirName);
65
        //        xoops_loadLanguage('blocks', $moduleDirName);
66
67
        /** @var \XoopsModuleHandler $moduleHandler */
68
        $moduleHandler = \xoops_getHandler('module');
69
        /** @var \XoopsMemberHandler $memberHandler */
70
        $memberHandler = \xoops_getHandler('member');
71
        /** @var \XoopsGroupPermHandler $grouppermHandler */
72
        $grouppermHandler = \xoops_getHandler('groupperm');
73
        $groups           = $memberHandler->getGroups();
74
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', 1));
75
        $criteria->add(new \Criteria('isactive', 1));
76
        $moduleList     = $moduleHandler->getList($criteria);
77
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
78
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
79
        \ksort($moduleList);
80
        echo "
81
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
82
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
83
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
84
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
85
        <tr valign='middle'><th align='center'>"
86
             . \_AM_SYSTEM_BLOCKS_TITLE
87
             . "</th><th align='center' nowrap='nowrap'>"
88
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
89
             . '<br>'
90
             . _LEFT
91
             . '-'
92
             . _CENTER
93
             . '-'
94
             . _RIGHT
95
             . "</th><th align='center'>"
96
             . \constant(
97
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
98
             )
99
             . "</th><th align='center'>"
100
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE')
101
             . "</th><th align='center'>"
102
             . \_AM_SYSTEM_BLOCKS_VISIBLEIN
103
             . "</th><th align='center'>"
104
             . \_AM_SYSTEM_ADGS
105
             . "</th><th align='center'>"
106
             . \_AM_SYSTEM_BLOCKS_BCACHETIME
107
             . "</th><th align='center'>"
108
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION')
109
             . '</th></tr>
110
        ';
111
        $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

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

129
            $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...
130
            $result            = $this->db->query($sql);
131
            $modules           = [];
132
            if (!$result instanceof \mysqli_result) {
133
                \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
134
            }
135
            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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

364
        echo /** @scrutinizer ignore-type */ $this->render($block);
Loading history...
365
        //        xoops_cp_footer();
366
        //        require_once __DIR__ . '/admin_footer.php';
367
        //        exit();
368
    }
369
370
    /**
371
     * @param null|array|string $options
372
     */
373
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $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 (\is_array($options) && (\count($options) > 0)) {
394
            $options = \implode('|', $options);
395
            $clone->setVar('options', $options);
396
        }
397
        $clone->setVar('bid', 0);
398
        if (\in_array($block->getVar('block_type'), ['C', 'E'], true)) {
399
            $clone->setVar('block_type', 'E');
400
        } else {
401
            $clone->setVar('block_type', 'D');
402
        }
403
        //        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
404
        if ($clone->store()) {
405
            $newid = $clone->id();  //get the id of the cloned block
406
        }
407
        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...
408
            //            \xoops_cp_header();
409
            $clone->getHtmlErrors();
410
            \xoops_cp_footer();
411
            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...
412
        }
413
        if ('' !== $clone->getVar('template')) {
414
            /** @var \XoopsTplfileHandler $tplfileHandler */
415
            $tplfileHandler = \xoops_getHandler('tplfile');
416
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', (string)$bid);
417
            if (\count($btemplate) > 0) {
418
                $tplclone = $btemplate[0]->xoopsClone();
419
                $tplclone->setVar('tpl_id', 0);
420
                $tplclone->setVar('tpl_refid', $newid);
421
                $tplfileHandler->insert($tplclone);
422
            }
423
        }
424
425
        foreach ($bmodule as $bmid) {
426
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
427
            $this->db->query($sql);
428
        }
429
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
430
        foreach ($groups as $iValue) {
431
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
432
            $this->db->query($sql);
433
        }
434
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
435
    }
436
437
    public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, ?array $bmodule = null): void
0 ignored issues
show
Unused Code introduced by
The parameter $bmodule is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

437
    public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, /** @scrutinizer ignore-unused */ ?array $bmodule = null): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
438
    {
439
        $myblock = new \XoopsBlock($bid);
440
        $myblock->setVar('title', $title);
441
        $myblock->setVar('weight', $weight);
442
        $myblock->setVar('visible', $visible);
443
        $myblock->setVar('side', $side);
444
        $myblock->setVar('bcachetime', $bcachetime);
445
        $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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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

514
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
515
        //        /** @var \XoopsBlockHandler $blockHandler */
516
        //        $blockHandler = \xoops_getHandler('block');
517
        //        $blockHandler->insert($myblock);
518
519
        if (!empty($bmodule) && \count($bmodule) > 0) {
520
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
521
            $this->db->query($sql);
522
            if (\in_array(0, $bmodule, true)) {
523
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
524
                $this->db->query($sql);
525
            } else {
526
                foreach ($bmodule as $bmid) {
527
                    $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, (int)$bmid);
528
                    $this->db->query($sql);
529
                }
530
            }
531
        }
532
        $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid);
533
        $this->db->query($sql);
534
        if (!empty($groups)) {
535
            foreach ($groups as $grp) {
536
                $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);
537
                $this->db->query($sql);
538
            }
539
        }
540
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
541
    }
542
543
    public function orderBlock(
544
        array $bid,
545
        array $oldtitle,
546
        array $oldside,
547
        array $oldweight,
548
        array $oldvisible,
549
        array $oldgroups,
0 ignored issues
show
Unused Code introduced by
The parameter $oldgroups is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

549
        /** @scrutinizer ignore-unused */ array $oldgroups,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

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