Passed
Push — master ( 0708c5...e4b65d )
by Michael
13:42 queued 13s
created

Blocksadmin::orderBlock()   C

Complexity

Conditions 15
Paths 38

Size

Total Lines 55
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 27
c 1
b 0
f 0
nc 38
nop 15
dl 0
loc 55
rs 5.9166

How to fix   Long Method    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\News\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.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 */
19
20
use Xmf\Request;
21
use XoopsModules\News\{
22
    Helper
23
};
24
25
//require __DIR__ . '/admin_header.php';
26
27
/**
28
 * class Blocksadmin
29
 */
30
class Blocksadmin
31
{
32
    /**
33
     * @var \XoopsMySQLDatabase|null
34
     */
35
    public $db;
36
    /**
37
     * @var \XoopsModules\Tag\Helper
38
     */
39
    public $helper;
40
    /**
41
     * @var string
42
     */
43
    public $moduleDirName;
44
    /**
45
     * @var string
46
     */
47
    public $moduleDirNameUpper;
48
49
    /**
50
     * Blocksadmin constructor.
51
     */
52
    public function __construct(?\XoopsDatabase $db, Helper $helper)
53
    {
54
        if (null === $db) {
55
            $db = \XoopsDatabaseFactory::getDatabaseConnection();
56
        }
57
        $this->db                 = $db;
58
        $this->helper             = $helper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $helper of type XoopsModules\News\Helper is incompatible with the declared type XoopsModules\Tag\Helper of property $helper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        $this->moduleDirName      = \basename(\dirname(__DIR__, 2));
60
        $this->moduleDirNameUpper = \mb_strtoupper($this->moduleDirName);
61
        \xoops_loadLanguage('admin', 'system');
62
        \xoops_loadLanguage('admin/blocksadmin', 'system');
63
        \xoops_loadLanguage('admin/groups', 'system');
64
        \xoops_loadLanguage('common', $this->moduleDirName);
65
        \xoops_loadLanguage('blocksadmin', $this->moduleDirName);
66
    }
67
68
    public function listBlocks()
69
    : void
70
    {
71
        global $xoopsModule, $pathIcon16;
72
        require_once XOOPS_ROOT_PATH . '/class/xoopslists.php';
73
        //        xoops_loadLanguage('admin', 'system');
74
        //        xoops_loadLanguage('admin/blocksadmin', 'system');
75
        //        xoops_loadLanguage('admin/groups', 'system');
76
        //        xoops_loadLanguage('common', $moduleDirName);
77
        //        xoops_loadLanguage('blocks', $moduleDirName);
78
79
        /** @var \XoopsModuleHandler $moduleHandler */
80
        $moduleHandler = \xoops_getHandler('module');
81
        /** @var \XoopsMemberHandler $memberHandler */
82
        $memberHandler = \xoops_getHandler('member');
83
        /** @var \XoopsGroupPermHandler $grouppermHandler */
84
        $grouppermHandler = \xoops_getHandler('groupperm');
85
        $groups           = $memberHandler->getGroups();
86
        $criteria         = new \CriteriaCompo(new \Criteria('hasmain', '1'));
87
        $criteria->add(new \Criteria('isactive', '1'));
88
        $moduleList     = $moduleHandler->getList($criteria);
89
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
90
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
91
        \ksort($moduleList);
92
        echo "
93
        <h4 style='text-align:left;'>" . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BADMIN') . '</h4>';
94
        echo "<form action='" . $_SERVER['SCRIPT_NAME'] . "' name='blockadmin' method='post'>";
95
        echo $GLOBALS['xoopsSecurity']->getTokenHTML();
96
        echo "<table width='100%' class='outer' cellpadding='4' cellspacing='1'>
97
        <tr valign='middle'><th align='center'>"
98
             . \_AM_SYSTEM_BLOCKS_TITLE
99
             . "</th><th align='center' nowrap='nowrap'>"
100
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'SIDE')
101
             . '<br>'
102
             . _LEFT
103
             . '-'
104
             . _CENTER
105
             . '-'
106
             . _RIGHT
107
             . "</th><th align='center'>"
108
             . \constant(
109
                 'CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'
110
             )
111
             . "</th><th align='center'>"
112
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE')
113
             . "</th><th align='center'>"
114
             . \_AM_SYSTEM_BLOCKS_VISIBLEIN
115
             . "</th><th align='center'>"
116
             . \_AM_SYSTEM_ADGS
117
             . "</th><th align='center'>"
118
             . \_AM_SYSTEM_BLOCKS_BCACHETIME
119
             . "</th><th align='center'>"
120
             . \constant('CO_' . $this->moduleDirNameUpper . '_' . 'ACTION')
121
             . '</th></tr>
122
        ';
123
        $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

123
        $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...
124
        $blockCount = \count($blockArray);
0 ignored issues
show
Unused Code introduced by
The assignment to $blockCount is dead and can be removed.
Loading history...
125
        $class      = 'even';
126
        $cachetimes = [
127
            0       => _NOCACHE,
128
            30      => \sprintf(_SECONDS, 30),
129
            60      => _MINUTE,
130
            300     => \sprintf(_MINUTES, 5),
131
            1800    => \sprintf(_MINUTES, 30),
132
            3600    => _HOUR,
133
            18000   => \sprintf(_HOURS, 5),
134
            86400   => _DAY,
135
            259200  => \sprintf(_DAYS, 3),
136
            604800  => _WEEK,
137
            2592000 => _MONTH,
138
        ];
139
        foreach ($blockArray as $i) {
140
            $groupsPermissions = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
141
            $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

141
            $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...
142
            $result            = $this->db->query($sql);
143
            $modules           = [];
144
            if (!$result instanceof \mysqli_result) {
145
                \trigger_error("Query Failed! SQL: $sql Error: " . $this->db->error(), \E_USER_ERROR);
146
            }
147
            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

147
            while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
148
                $modules[] = (int)$row['module_id'];
149
            }
150
151
            $cachetimeOptions = '';
152
            foreach ($cachetimes as $cachetime => $cachetimeName) {
153
                if ($i->getVar('bcachetime') == $cachetime) {
154
                    $cachetimeOptions .= "<option value='$cachetime' selected='selected'>$cachetimeName</option>\n";
155
                }
156
                else {
157
                    $cachetimeOptions .= "<option value='$cachetime'>$cachetimeName</option>\n";
158
                }
159
            }
160
161
            $ssel7 = '';
162
            $ssel6 = $ssel7;
163
            $ssel5 = $ssel6;
164
            $ssel4 = $ssel5;
165
            $ssel3 = $ssel4;
166
            $ssel2 = $ssel3;
167
            $ssel1 = $ssel2;
168
            $ssel0 = $ssel1;
169
            $sel1  = $ssel0;
170
            $sel0  = $sel1;
171
            if (1 === $i->getVar('visible')) {
172
                $sel1 = ' checked';
173
            }
174
            else {
175
                $sel0 = ' checked';
176
            }
177
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
178
                $ssel0 = ' checked';
179
            }
180
            elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
181
                $ssel1 = ' checked';
182
            }
183
            elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
184
                $ssel2 = ' checked';
185
            }
186
            elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
187
                $ssel4 = ' checked';
188
            }
189
            elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
190
                $ssel3 = ' checked';
191
            }
192
            elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
193
                $ssel5 = ' checked';
194
            }
195
            elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
196
                $ssel6 = ' checked';
197
            }
198
            elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
199
                $ssel7 = ' checked';
200
            }
201
            if ('' === $i->getVar('title')) {
202
                $title = '&nbsp;';
203
            }
204
            else {
205
                $title = $i->getVar('title');
206
            }
207
            $name = $i->getVar('name');
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
208
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
209
                 . $i->getVar('bid')
210
                 . "]' value='"
211
                 . $title
212
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
213
                    <div align='center' >
214
                    <input type='radio' name='side["
215
                 . $i->getVar('bid')
216
                 . "]' value='"
217
                 . \XOOPS_CENTERBLOCK_LEFT
218
                 . "'$ssel2>
219
                        <input type='radio' name='side["
220
                 . $i->getVar('bid')
221
                 . "]' value='"
222
                 . \XOOPS_CENTERBLOCK_CENTER
223
                 . "'$ssel3>
224
                    <input type='radio' name='side["
225
                 . $i->getVar('bid')
226
                 . "]' value='"
227
                 . \XOOPS_CENTERBLOCK_RIGHT
228
                 . "'$ssel4>
229
                    </div>
230
                    <div>
231
                        <span style='float:right;'><input type='radio' name='side["
232
                 . $i->getVar('bid')
233
                 . "]' value='"
234
                 . \XOOPS_SIDEBLOCK_RIGHT
235
                 . "'$ssel1></span>
236
                    <div align='left'><input type='radio' name='side["
237
                 . $i->getVar('bid')
238
                 . "]' value='"
239
                 . \XOOPS_SIDEBLOCK_LEFT
240
                 . "'$ssel0></div>
241
                    </div>
242
                    <div align='center'>
243
                    <input type='radio' name='side["
244
                 . $i->getVar('bid')
245
                 . "]' value='"
246
                 . \XOOPS_CENTERBLOCK_BOTTOMLEFT
247
                 . "'$ssel5>
248
                        <input type='radio' name='side["
249
                 . $i->getVar('bid')
250
                 . "]' value='"
251
                 . \XOOPS_CENTERBLOCK_BOTTOM
252
                 . "'$ssel7>
253
                    <input type='radio' name='side["
254
                 . $i->getVar('bid')
255
                 . "]' value='"
256
                 . \XOOPS_CENTERBLOCK_BOTTOMRIGHT
257
                 . "'$ssel6>
258
                    </div>
259
                </td><td class='$class' align='center'><input type='text' name='weight["
260
                 . $i->getVar('bid')
261
                 . "]' value='"
262
                 . $i->getVar('weight')
263
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
264
                 . $i->getVar('bid')
265
                 . "]' value='1'$sel1>"
266
                 . _YES
267
                 . "&nbsp;<input type='radio' name='visible["
268
                 . $i->getVar('bid')
269
                 . "]' value='0'$sel0>"
270
                 . _NO
271
                 . '</td>';
272
273
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
274
            foreach ($moduleList as $k => $v) {
275
                echo "<option value='$k'" . (\in_array($k, $modules) ? " selected='selected'" : '') . ">$v</option>";
276
            }
277
            echo '</select></td>';
278
279
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
280
            foreach ($groups as $grp) {
281
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groupsPermissions) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
282
            }
283
            echo '</select></td>';
284
285
            // Cache lifetime
286
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetimeOptions . '</select>
287
                                    </td>';
288
289
            // Actions
290
291
            echo "<td class='$class' align='center'>
292
                <a href='blocksadmin.php?op=edit&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/edit.png' . " alt='" . _EDIT . "' title='" . _EDIT . "'></a> 
293
                <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'></a>";
294
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
295
            //                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 . "'>
296
            //                     </a>";
297
            //            }
298
299
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
300
            if (!\in_array($i->getVar('block_type'), ['M', 'S'])) {
301
                echo "&nbsp;
302
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
303
                     </a>";
304
            }
305
            echo "
306
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
307
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
308
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
309
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
310
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
311
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
312
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
313
            </td></tr>
314
            ";
315
            $class = ('even' === $class) ? 'odd' : 'even';
316
        }
317
        echo "<tr><td class='foot' align='center' colspan='8'>
318
        <input type='hidden' name='op' value='order'>
319
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
320
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
321
        </td></tr></table>
322
        </form>
323
        <br><br>";
324
    }
325
326
    public function deleteBlock(int $bid)
327
    : void {
328
        //        \xoops_cp_header();
329
330
        \xoops_loadLanguage('admin', 'system');
331
        \xoops_loadLanguage('admin/blocksadmin', 'system');
332
        \xoops_loadLanguage('admin/groups', 'system');
333
334
        $myblock = new \XoopsBlock($bid);
0 ignored issues
show
Unused Code introduced by
The assignment to $myblock is dead and can be removed.
Loading history...
335
336
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
337
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
338
339
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
340
        $this->db->queryF($sql) or \trigger_error($GLOBALS['xoopsDB']->error());
341
342
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
343
    }
344
345
    public function cloneBlock(int $bid)
346
    : void {
347
        //require __DIR__ . '/admin_header.php';
348
        //        \xoops_cp_header();
349
350
        \xoops_loadLanguage('admin', 'system');
351
        \xoops_loadLanguage('admin/blocksadmin', 'system');
352
        \xoops_loadLanguage('admin/groups', 'system');
353
354
        $myblock = new \XoopsBlock($bid);
355
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . $bid;
356
        $result  = $this->db->query($sql);
357
        $modules = [];
358
        if ($result instanceof \mysqli_result) {
359
            while (false !== ($row = $this->db->fetchArray($result))) {
360
                $modules[] = (int)$row['module_id'];
361
            }
362
        }
363
        $isCustom = \in_array($myblock->getVar('block_type'), ['C', 'E']);
364
        $block    = [
365
            'title'      => $myblock->getVar('title') . ' Clone',
366
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
367
            'name'       => $myblock->getVar('name'),
368
            'side'       => $myblock->getVar('side'),
369
            'weight'     => $myblock->getVar('weight'),
370
            'visible'    => $myblock->getVar('visible'),
371
            'content'    => $myblock->getVar('content', 'N'),
372
            'modules'    => $modules,
373
            'is_custom'  => $isCustom,
374
            'ctype'      => $myblock->getVar('c_type'),
375
            'bcachetime' => $myblock->getVar('bcachetime'),
376
            'op'         => 'clone_ok',
377
            'bid'        => $myblock->getVar('bid'),
378
            'edit_form'  => $myblock->getOptions(),
379
            'template'   => $myblock->getVar('template'),
380
            'options'    => $myblock->getVar('options'),
381
        ];
382
        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>';
383
        //        $form = new Blockform();
384
        //        $form->render();
385
386
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\News\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...
387
        //        xoops_cp_footer();
388
        //        require_once __DIR__ . '/admin_footer.php';
389
        //        exit();
390
    }
391
392
    public function isBlockCloned(int $bid, string $bside, string $bweight, string $bvisible, string $bcachetime, ?array $bmodule, ?array $options, ?array $groups): void
393
    {
394
        \xoops_loadLanguage('admin', 'system');
395
        \xoops_loadLanguage('admin/blocksadmin', 'system');
396
        \xoops_loadLanguage('admin/groups', 'system');
397
398
        $block = new \XoopsBlock($bid);
399
        $clone = $block->xoopsClone();
400
        if (empty($bmodule)) {
401
            //            \xoops_cp_header();
402
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
403
            \xoops_cp_footer();
404
            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...
405
        }
406
        $clone->setVar('side', $bside);
407
        $clone->setVar('weight', $bweight);
408
        $clone->setVar('visible', $bvisible);
409
        //$clone->setVar('content', $_POST['bcontent']);
410
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
411
        $clone->setVar('bcachetime', $bcachetime);
412
        if (\is_array($options) && (\count($options) > 0)) {
413
            $options = \implode('|', $options);
414
            $clone->setVar('options', $options);
415
        }
416
        $clone->setVar('bid', 0);
417
        if (\in_array($block->getVar('block_type'), ['C', 'E'])) {
418
            $clone->setVar('block_type', 'E');
419
        }
420
        else {
421
            $clone->setVar('block_type', 'D');
422
        }
423
        //        $newid = $clone->store(); //see https://github.com/XOOPS/XoopsCore25/issues/1105
424
        if ($clone->store()) {
425
            $newid = $clone->id();  //get the id of the cloned block
426
        }
427
        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...
428
            //            \xoops_cp_header();
429
            $clone->getHtmlErrors();
430
            \xoops_cp_footer();
431
            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...
432
        }
433
        if ('' !== $clone->getVar('template')) {
434
            /** @var \XoopsTplfileHandler $tplfileHandler */
435
            $tplfileHandler = \xoops_getHandler('tplfile');
436
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', (string)$bid);
437
            if (\count($btemplate) > 0) {
438
                $tplclone = $btemplate[0]->xoopsClone();
439
                $tplclone->setVar('tpl_id', 0);
440
                $tplclone->setVar('tpl_refid', $newid);
441
                $tplfileHandler->insert($tplclone);
442
            }
443
        }
444
445
        foreach ($bmodule as $bmid) {
446
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
447
            $this->db->query($sql);
448
        }
449
        //$groups = &$GLOBALS['xoopsUser']->getGroups();
450
        foreach ($groups as $iValue) {
451
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
452
            $this->db->query($sql);
453
        }
454
        $this->helper->redirect('admin/blocksadmin.php?op=list', 1, _AM_DBUPDATED);
455
    }
456
457
    public function setOrder(string $bid, string $title, string $weight, string $visible, string $side, string $bcachetime, ?array $bmodule = null)
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

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

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...
458
    : void {
459
        $myblock = new \XoopsBlock($bid);
460
        $myblock->setVar('title', $title);
461
        $myblock->setVar('weight', $weight);
462
        $myblock->setVar('visible', $visible);
463
        $myblock->setVar('side', $side);
464
        $myblock->setVar('bcachetime', $bcachetime);
465
        $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

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

534
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
535
        //        /** @var \XoopsBlockHandler $blockHandler */
536
        //        $blockHandler = \xoops_getHandler('block');
537
        //        $blockHandler->insert($myblock);
538
539
        if (!empty($bmodule) && \count($bmodule) > 0) {
540
            $sql = \sprintf('DELETE FROM `%s` WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
541
            $this->db->query($sql);
542
            if (\in_array(0, $bmodule)) {
543
                $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid, 0);
544
                $this->db->query($sql);
545
            }
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->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
562
    }
563
564
    public function orderBlock(
565
        array $bid,
566
        array $oldtitle,
567
        array $oldside,
568
        array $oldweight,
569
        array $oldvisible,
570
        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

570
        /** @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...
571
        array $oldbcachetime,
572
        array $oldbmodule,
573
        array $title,
574
        array $weight,
575
        array $visible,
576
        array $side,
577
        array $bcachetime,
578
        array $groups,
579
        array $bmodule
580
    )
581
    : void {
582
        if (!$GLOBALS['xoopsSecurity']->check()) {
583
            \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
584
        }
585
        foreach (\array_keys($bid) as $i) {
586
            if ($oldtitle[$i] !== $title[$i]
587
                || $oldweight[$i] !== $weight[$i]
588
                || $oldvisible[$i] !== $visible[$i]
589
                || $oldside[$i] !== $side[$i]
590
                || $oldbcachetime[$i] !== $bcachetime[$i]
591
                || $oldbmodule[$i] !== $bmodule[$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
                }
601
                else {
602
                    foreach ($bmodule[$i] as $bmid) {
603
                        $sql = \sprintf('INSERT INTO `%s` (block_id, module_id) VALUES (%u, %d)', $this->db->prefix('block_module_link'), $bid[$i], (int)$bmid);
604
                        $this->db->query($sql);
605
                    }
606
                }
607
            }
608
            $sql = \sprintf('DELETE FROM `%s` WHERE gperm_itemid = %u', $this->db->prefix('group_permission'), $bid[$i]);
609
            $this->db->query($sql);
610
            if (!empty($groups[$i])) {
611
                foreach ($groups[$i] as $grp) {
612
                    $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]);
613
                    $this->db->query($sql);
614
                }
615
            }
616
        }
617
618
        $this->helper->redirect('admin/blocksadmin.php', 1, \constant('CO_' . $this->moduleDirNameUpper . '_' . 'UPDATE_SUCCESS'));
619
    }
620
621
    public function render(?array $block = null)
622
    {
623
        \xoops_load('XoopsFormLoader');
624
        \xoops_loadLanguage('common', $this->moduleDirNameUpper);
625
626
        $form = new \XoopsThemeForm($block['form_title'], 'blockform', 'blocksadmin.php', 'post', true);
627
        if (isset($block['name'])) {
628
            $form->addElement(new \XoopsFormLabel(\_AM_SYSTEM_BLOCKS_NAME, $block['name']));
629
        }
630
        $sideSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_TYPE, 'bside', $block['side']);
631
        $sideSelect->addOptionArray([
632
                                        0 => \_AM_SYSTEM_BLOCKS_SBLEFT,
633
                                        1 => \_AM_SYSTEM_BLOCKS_SBRIGHT,
634
                                        3 => \_AM_SYSTEM_BLOCKS_CBLEFT,
635
                                        4 => \_AM_SYSTEM_BLOCKS_CBRIGHT,
636
                                        5 => \_AM_SYSTEM_BLOCKS_CBCENTER,
637
                                        7 => \_AM_SYSTEM_BLOCKS_CBBOTTOMLEFT,
638
                                        8 => \_AM_SYSTEM_BLOCKS_CBBOTTOMRIGHT,
639
                                        9 => \_AM_SYSTEM_BLOCKS_CBBOTTOM,
640
                                    ]);
641
        $form->addElement($sideSelect);
642
        $form->addElement(new \XoopsFormText(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'WEIGHT'), 'bweight', 2, 5, $block['weight']));
643
        $form->addElement(new \XoopsFormRadioYN(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLE'), 'bvisible', $block['visible']));
644
        $modSelect = new \XoopsFormSelect(\constant('CO_' . $this->moduleDirNameUpper . '_' . 'VISIBLEIN'), 'bmodule', $block['modules'], 5, true);
645
        /** @var \XoopsModuleHandler $moduleHandler */
646
        $moduleHandler = \xoops_getHandler('module');
647
        $criteria      = new \CriteriaCompo(new \Criteria('hasmain', '1'));
648
        $criteria->add(new \Criteria('isactive', '1'));
649
        $moduleList     = $moduleHandler->getList($criteria);
650
        $moduleList[-1] = \_AM_SYSTEM_BLOCKS_TOPPAGE;
651
        $moduleList[0]  = \_AM_SYSTEM_BLOCKS_ALLPAGES;
652
        \ksort($moduleList);
653
        $modSelect->addOptionArray($moduleList);
654
        $form->addElement($modSelect);
655
        $form->addElement(new \XoopsFormText(\_AM_SYSTEM_BLOCKS_TITLE, 'btitle', 50, 255, $block['title']), false);
656
        if ($block['is_custom']) {
657
            $textarea = new \XoopsFormDhtmlTextArea(\_AM_SYSTEM_BLOCKS_CONTENT, 'bcontent', $block['content'], 15, 70);
658
            $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>');
659
            $form->addElement($textarea, true);
660
            $ctypeSelect = new \XoopsFormSelect(\_AM_SYSTEM_BLOCKS_CTYPE, 'bctype', $block['ctype']);
661
            $ctypeSelect->addOptionArray([
662
                                             'H' => \_AM_SYSTEM_BLOCKS_HTML,
663
                                             'P' => \_AM_SYSTEM_BLOCKS_PHP,
664
                                             'S' => \_AM_SYSTEM_BLOCKS_AFWSMILE,
665
                                             'T' => \_AM_SYSTEM_BLOCKS_AFNOSMILE,
666
                                         ]);
667
            $form->addElement($ctypeSelect);
668
        }
669
        else {
670
            if ('' !== $block['template']) {
671
                /** @var \XoopsTplfileHandler $tplfileHandler */
672
                $tplfileHandler = \xoops_getHandler('tplfile');
673
                $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $block['bid']);
674
                if (\count($btemplate) > 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=' . $btemplate[0]->getVar('tpl_id') . '">' . \_AM_SYSTEM_BLOCKS_EDITTPL . '</a>'));
676
                }
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