Passed
Push — master ( 34ef5d...2f53de )
by Michael
04:04 queued 19s
created

Blocksadmin::render()   C

Complexity

Conditions 9
Paths 72

Size

Total Lines 108
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 84
c 1
b 0
f 0
dl 0
loc 108
rs 6.7935
cc 9
nc 72
nop 1

How to fix   Long Method   

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

101
        $block_arr   = /** @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...
102
        $block_count = \count($block_arr);
0 ignored issues
show
Unused Code introduced by
The assignment to $block_count is dead and can be removed.
Loading history...
103
        $class       = 'even';
104
        $cachetimes  = [
105
            0       => _NOCACHE,
106
            30      => \sprintf(_SECONDS, 30),
107
            60      => _MINUTE,
108
            300     => \sprintf(_MINUTES, 5),
109
            1800    => \sprintf(_MINUTES, 30),
110
            3600    => _HOUR,
111
            18000   => \sprintf(_HOURS, 5),
112
            86400   => _DAY,
113
            259200  => \sprintf(_DAYS, 3),
114
            604800  => _WEEK,
115
            2592000 => _MONTH,
116
        ];
117
        foreach ($block_arr as $i) {
118
            $groups_perms = $grouppermHandler->getGroupIds('block_read', $i->getVar('bid'));
119
            $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

119
            $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...
120
            $result       = $this->db->query($sql);
121
            $modules      = [];
122
            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

122
            while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
123
                $modules[] = (int)$row['module_id'];
124
            }
125
126
            $cachetime_options = '';
127
            foreach ($cachetimes as $cachetime => $cachetime_name) {
128
                if ($i->getVar('bcachetime') == $cachetime) {
129
                    $cachetime_options .= "<option value='$cachetime' selected='selected'>$cachetime_name</option>\n";
130
                } else {
131
                    $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
132
                }
133
            }
134
135
            $sel0 = $sel1 = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = $ssel5 = $ssel6 = $ssel7 = '';
136
            if (1 === $i->getVar('visible')) {
137
                $sel1 = ' checked';
138
            } else {
139
                $sel0 = ' checked';
140
            }
141
            if (\XOOPS_SIDEBLOCK_LEFT === $i->getVar('side')) {
142
                $ssel0 = ' checked';
143
            } elseif (\XOOPS_SIDEBLOCK_RIGHT === $i->getVar('side')) {
144
                $ssel1 = ' checked';
145
            } elseif (\XOOPS_CENTERBLOCK_LEFT === $i->getVar('side')) {
146
                $ssel2 = ' checked';
147
            } elseif (\XOOPS_CENTERBLOCK_RIGHT === $i->getVar('side')) {
148
                $ssel4 = ' checked';
149
            } elseif (\XOOPS_CENTERBLOCK_CENTER === $i->getVar('side')) {
150
                $ssel3 = ' checked';
151
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMLEFT === $i->getVar('side')) {
152
                $ssel5 = ' checked';
153
            } elseif (\XOOPS_CENTERBLOCK_BOTTOMRIGHT === $i->getVar('side')) {
154
                $ssel6 = ' checked';
155
            } elseif (\XOOPS_CENTERBLOCK_BOTTOM === $i->getVar('side')) {
156
                $ssel7 = ' checked';
157
            }
158
            if ('' === $i->getVar('title')) {
159
                $title = '&nbsp;';
160
            } else {
161
                $title = $i->getVar('title');
162
            }
163
            $name = $i->getVar('name');
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
164
            echo "<tr valign='top'><td class='$class' align='center'><input type='text' name='title["
165
                 . $i->getVar('bid')
166
                 . "]' value='"
167
                 . $title
168
                 . "'></td><td class='$class' align='center' nowrap='nowrap'>
169
                    <div align='center' >
170
                    <input type='radio' name='side["
171
                 . $i->getVar('bid')
172
                 . "]' value='"
173
                 . \XOOPS_CENTERBLOCK_LEFT
174
                 . "'$ssel2>
175
                        <input type='radio' name='side["
176
                 . $i->getVar('bid')
177
                 . "]' value='"
178
                 . \XOOPS_CENTERBLOCK_CENTER
179
                 . "'$ssel3>
180
                    <input type='radio' name='side["
181
                 . $i->getVar('bid')
182
                 . "]' value='"
183
                 . \XOOPS_CENTERBLOCK_RIGHT
184
                 . "'$ssel4>
185
                    </div>
186
                    <div>
187
                        <span style='float:right;'><input type='radio' name='side["
188
                 . $i->getVar('bid')
189
                 . "]' value='"
190
                 . \XOOPS_SIDEBLOCK_RIGHT
191
                 . "'$ssel1></span>
192
                    <div align='left'><input type='radio' name='side["
193
                 . $i->getVar('bid')
194
                 . "]' value='"
195
                 . \XOOPS_SIDEBLOCK_LEFT
196
                 . "'$ssel0></div>
197
                    </div>
198
                    <div align='center'>
199
                    <input type='radio' name='side["
200
                 . $i->getVar('bid')
201
                 . "]' value='"
202
                 . \XOOPS_CENTERBLOCK_BOTTOMLEFT
203
                 . "'$ssel5>
204
                        <input type='radio' name='side["
205
                 . $i->getVar('bid')
206
                 . "]' value='"
207
                 . \XOOPS_CENTERBLOCK_BOTTOM
208
                 . "'$ssel7>
209
                    <input type='radio' name='side["
210
                 . $i->getVar('bid')
211
                 . "]' value='"
212
                 . \XOOPS_CENTERBLOCK_BOTTOMRIGHT
213
                 . "'$ssel6>
214
                    </div>
215
                </td><td class='$class' align='center'><input type='text' name='weight["
216
                 . $i->getVar('bid')
217
                 . "]' value='"
218
                 . $i->getVar('weight')
219
                 . "' size='5' maxlength='5'></td><td class='$class' align='center' nowrap><input type='radio' name='visible["
220
                 . $i->getVar('bid')
221
                 . "]' value='1'$sel1>"
222
                 . _YES
223
                 . "&nbsp;<input type='radio' name='visible["
224
                 . $i->getVar('bid')
225
                 . "]' value='0'$sel0>"
226
                 . _NO
227
                 . '</td>';
228
229
            echo "<td class='$class' align='center'><select size='5' name='bmodule[" . $i->getVar('bid') . "][]' id='bmodule[" . $i->getVar('bid') . "][]' multiple='multiple'>";
230
            foreach ($module_list as $k => $v) {
231
                echo "<option value='$k'" . (\in_array($k, $modules) ? " selected='selected'" : '') . ">$v</option>";
232
            }
233
            echo '</select></td>';
234
235
            echo "<td class='$class' align='center'><select size='5' name='groups[" . $i->getVar('bid') . "][]' id='groups[" . $i->getVar('bid') . "][]' multiple='multiple'>";
236
            foreach ($groups as $grp) {
237
                echo "<option value='" . $grp->getVar('groupid') . "' " . (\in_array($grp->getVar('groupid'), $groups_perms) ? " selected='selected'" : '') . '>' . $grp->getVar('name') . '</option>';
238
            }
239
            echo '</select></td>';
240
241
            // Cache lifetime
242
            echo '<td class="' . $class . '" align="center"> <select name="bcachetime[' . $i->getVar('bid') . ']" size="1">' . $cachetime_options . '</select>
243
                                    </td>';
244
245
            // Actions
246
247
            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 . "'>
248
                 </a> <a href='blocksadmin.php?op=clone&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/editcopy.png' . " alt='" . _CLONE . "' title='" . _CLONE . "'>
249
                 </a>";
250
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
251
            //                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 . "'>
252
            //                     </a>";
253
            //            }
254
255
            //            if ('S' !== $i->getVar('block_type') && 'M' !== $i->getVar('block_type')) {
256
            if (!in_array($i->getVar('block_type', ['M', 'S']))) {
0 ignored issues
show
Bug introduced by
The call to in_array() has too few arguments starting with haystack. ( Ignorable by Annotation )

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

256
            if (!/** @scrutinizer ignore-call */ in_array($i->getVar('block_type', ['M', 'S']))) {

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
257
                echo "&nbsp;
258
                <a href='blocksadmin.php?op=delete&amp;bid=" . $i->getVar('bid') . "'><img src=" . $pathIcon16 . '/delete.png' . " alt='" . _DELETE . "' title='" . _DELETE . "'>
259
                     </a>";
260
            }
261
            echo "
262
            <input type='hidden' name='oldtitle[" . $i->getVar('bid') . "]' value='" . $i->getVar('title') . "'>
263
            <input type='hidden' name='oldside[" . $i->getVar('bid') . "]' value='" . $i->getVar('side') . "'>
264
            <input type='hidden' name='oldweight[" . $i->getVar('bid') . "]' value='" . $i->getVar('weight') . "'>
265
            <input type='hidden' name='oldvisible[" . $i->getVar('bid') . "]' value='" . $i->getVar('visible') . "'>
266
            <input type='hidden' name='oldgroups[" . $i->getVar('groups') . "]' value='" . $i->getVar('groups') . "'>
267
            <input type='hidden' name='oldbcachetime[" . $i->getVar('bid') . "]' value='" . $i->getVar('bcachetime') . "'>
268
            <input type='hidden' name='bid[" . $i->getVar('bid') . "]' value='" . $i->getVar('bid') . "'>
269
            </td></tr>
270
            ";
271
            $class = ('even' === $class) ? 'odd' : 'even';
272
        }
273
        echo "<tr><td class='foot' align='center' colspan='8'>
274
        <input type='hidden' name='op' value='order'>
275
        " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
276
        <input type='submit' name='submit' value='" . _SUBMIT . "'>
277
        </td></tr></table>
278
        </form>
279
        <br><br>";
280
    }
281
282
    /**
283
     * @param int $bid
284
     */
285
    public function deleteBlock($bid)
286
    {
287
        //        \xoops_cp_header();
288
289
        \xoops_loadLanguage('admin', 'system');
290
        \xoops_loadLanguage('admin/blocksadmin', 'system');
291
        \xoops_loadLanguage('admin/groups', 'system');
292
293
        $myblock = new \XoopsBlock($bid);
0 ignored issues
show
Unused Code introduced by
The assignment to $myblock is dead and can be removed.
Loading history...
294
295
        $sql = \sprintf('DELETE FROM %s WHERE bid = %u', $this->db->prefix('newblocks'), $bid);
296
        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...
297
            return false;
298
        }
299
        $sql = \sprintf('DELETE FROM %s WHERE block_id = %u', $this->db->prefix('block_module_link'), $bid);
300
        $this->db->queryF($sql);
301
302
        $this->modHelper->redirect('blocksadmin.php?op=list', 1, _AM_DBUPDATED);
303
    }
304
305
    /**
306
     * @param int $bid
307
     */
308
    public function cloneBlock($bid)
309
    {
310
        //require __DIR__ . '/admin_header.php';
311
        //        \xoops_cp_header();
312
313
        \xoops_loadLanguage('admin', 'system');
314
        \xoops_loadLanguage('admin/blocksadmin', 'system');
315
        \xoops_loadLanguage('admin/groups', 'system');
316
317
        $myblock = new \XoopsBlock($bid);
318
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
319
        $result  = $this->db->query($sql);
320
        $modules = [];
321
        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

321
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
322
            $modules[] = (int)$row['module_id'];
323
        }
324
        $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type'));
325
        $block     = [
326
            'title'      => $myblock->getVar('title') . ' Clone',
327
            'form_title' => \constant('CO_' . $this->moduleDirNameUpper . '_' . 'BLOCKS_CLONEBLOCK'),
328
            'name'       => $myblock->getVar('name'),
329
            'side'       => $myblock->getVar('side'),
330
            'weight'     => $myblock->getVar('weight'),
331
            'visible'    => $myblock->getVar('visible'),
332
            'content'    => $myblock->getVar('content', 'N'),
333
            'modules'    => $modules,
334
            'is_custom'  => $is_custom,
335
            'ctype'      => $myblock->getVar('c_type'),
336
            'bcachetime' => $myblock->getVar('bcachetime'),
337
            'op'         => 'clone_ok',
338
            'bid'        => $myblock->getVar('bid'),
339
            'edit_form'  => $myblock->getOptions(),
340
            'template'   => $myblock->getVar('template'),
341
            'options'    => $myblock->getVar('options'),
342
        ];
343
        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>';
344
        //        $form = new Blockform();
345
        //        $form->render();
346
347
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Adslight\Common\Blocksadmin::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
348
        //        xoops_cp_footer();
349
        //        require_once __DIR__ . '/admin_footer.php';
350
        //        exit();
351
    }
352
353
    /**
354
     * @param int $bid
355
     * @param     $bside
356
     * @param     $bweight
357
     * @param     $bvisible
358
     * @param     $bcachetime
359
     * @param     $bmodule
360
     * @param     $options
361
     */
362
    public function isBlockCloned($bid, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options)
363
    {
364
        \xoops_loadLanguage('admin', 'system');
365
        \xoops_loadLanguage('admin/blocksadmin', 'system');
366
        \xoops_loadLanguage('admin/groups', 'system');
367
368
        /** @var \XoopsBlock $block */
369
        $block = new \XoopsBlock($bid);
370
        $clone = $block->xoopsClone();
371
        if (empty($bmodule)) {
372
            //            \xoops_cp_header();
373
            \xoops_error(\sprintf(_AM_NOTSELNG, _AM_VISIBLEIN));
374
            \xoops_cp_footer();
375
            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...
376
        }
377
        $clone->setVar('side', $bside);
378
        $clone->setVar('weight', $bweight);
379
        $clone->setVar('visible', $bvisible);
380
        //$clone->setVar('content', $_POST['bcontent']);
381
        $clone->setVar('title', Request::getString('btitle', '', 'POST'));
382
        $clone->setVar('bcachetime', $bcachetime);
383
        if (isset($options) && (\count($options) > 0)) {
384
            $options = \implode('|', $options);
385
            $clone->setVar('options', $options);
386
        }
387
        $clone->setVar('bid', 0);
388
        if ('C' === $block->getVar('block_type') || 'E' === $block->getVar('block_type')) {
389
            $clone->setVar('block_type', 'E');
390
        } else {
391
            $clone->setVar('block_type', 'D');
392
        }
393
        $newid = $clone->store();
394
        if (!$newid) {
395
            //            \xoops_cp_header();
396
            $clone->getHtmlErrors();
397
            \xoops_cp_footer();
398
            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...
399
        }
400
        if ('' !== $clone->getVar('template')) {
401
            /** @var \XoopsTplfileHandler $tplfileHandler */
402
            $tplfileHandler = \xoops_getHandler('tplfile');
403
            $btemplate      = $tplfileHandler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $bid);
404
            if (\count($btemplate) > 0) {
405
                $tplclone = $btemplate[0]->xoopsClone();
406
                $tplclone->setVar('tpl_id', 0);
407
                $tplclone->setVar('tpl_refid', $newid);
408
                $tplfileHandler->insert($tplclone);
409
            }
410
        }
411
412
        foreach ($bmodule as $bmid) {
413
            $sql = 'INSERT INTO ' . $this->db->prefix('block_module_link') . ' (block_id, module_id) VALUES (' . $newid . ', ' . $bmid . ')';
414
            $this->db->query($sql);
415
        }
416
        $groups = &$GLOBALS['xoopsUser']->getGroups();
417
        foreach ($groups as $iValue) {
418
            $sql = 'INSERT INTO ' . $this->db->prefix('group_permission') . ' (gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (' . $iValue . ', ' . $newid . ", 1, 'block_read')";
419
            $this->db->query($sql);
420
        }
421
        $this->modHelper->redirect('blocksadmin.php?op=list', 1, _AM_DBUPDATED);
422
    }
423
424
    /**
425
     * @param int    $bid
426
     * @param string $title
427
     * @param int    $weight
428
     * @param bool   $visible
429
     * @param string $side
430
     * @param int    $bcachetime
431
     */
432
    public function setOrder($bid, $title, $weight, $visible, $side, $bcachetime, $bmodule)
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

432
    public function setOrder($bid, $title, $weight, $visible, $side, $bcachetime, /** @scrutinizer ignore-unused */ $bmodule)

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...
433
    {
434
        $myblock = new \XoopsBlock($bid);
435
        $myblock->setVar('title', $title);
436
        $myblock->setVar('weight', $weight);
437
        $myblock->setVar('visible', $visible);
438
        $myblock->setVar('side', $side);
439
        $myblock->setVar('bcachetime', $bcachetime);
440
        $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

440
        /** @scrutinizer ignore-deprecated */ $myblock->store();
Loading history...
441
        //        /** @var \XoopsBlockHandler $blockHandler */
442
        //        $blockHandler = \xoops_getHandler('block');
443
        //        return $blockHandler->insert($myblock);
444
    }
445
446
    /**
447
     * @param int $bid
448
     */
449
    public function editBlock($bid)
450
    {
451
        //        require_once \dirname(__DIR__,2) . '/admin/admin_header.php';
452
        //        \xoops_cp_header();
453
        \xoops_loadLanguage('admin', 'system');
454
        \xoops_loadLanguage('admin/blocksadmin', 'system');
455
        \xoops_loadLanguage('admin/groups', 'system');
456
        //        mpu_adm_menu();
457
        $myblock = new \XoopsBlock($bid);
458
        $sql     = 'SELECT module_id FROM ' . $this->db->prefix('block_module_link') . ' WHERE block_id=' . (int)$bid;
459
        $result  = $this->db->query($sql);
460
        $modules = [];
461
        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

461
        while (false !== ($row = $this->db->fetchArray(/** @scrutinizer ignore-type */ $result))) {
Loading history...
462
            $modules[] = (int)$row['module_id'];
463
        }
464
        $is_custom = ('C' === $myblock->getVar('block_type') || 'E' === $myblock->getVar('block_type'));
465
        $block     = [
466
            'title'      => $myblock->getVar('title'),
467
            'form_title' => \_AM_SYSTEM_BLOCKS_EDITBLOCK,
468
            //        'name'       => $myblock->getVar('name'),
469
            'side'       => $myblock->getVar('side'),
470
            'weight'     => $myblock->getVar('weight'),
471
            'visible'    => $myblock->getVar('visible'),
472
            'content'    => $myblock->getVar('content', 'N'),
473
            'modules'    => $modules,
474
            'is_custom'  => $is_custom,
475
            'ctype'      => $myblock->getVar('c_type'),
476
            'bcachetime' => $myblock->getVar('bcachetime'),
477
            'op'         => 'edit_ok',
478
            'bid'        => $myblock->getVar('bid'),
479
            'edit_form'  => $myblock->getOptions(),
480
            'template'   => $myblock->getVar('template'),
481
            'options'    => $myblock->getVar('options'),
482
        ];
483
        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>';
484
485
        /** @var \XoopsThemeForm $form */ //                $form = new Blockform();
486
        //        $form->render($block);
487
        //        $form = new \XoopsThemeForm();
488
489
        echo $this->render($block);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render($block) targeting XoopsModules\Adslight\Common\Blocksadmin::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
490
    }
491
492
    /**
493
     * @param int               $bid
494
     * @param string            $btitle
495
     * @param string            $bside
496
     * @param int               $bweight
497
     * @param bool              $bvisible
498
     * @param int               $bcachetime
499
     * @param array             $bmodule
500
     * @param null|array|string $options
501
     * @param null|array        $groups
502
     */
503
    public function updateBlock($bid, $btitle, $bside, $bweight, $bvisible, $bcachetime, $bmodule, $options, $groups)
504
    {
505
        $myblock = new \XoopsBlock($bid);
506
        $myblock->setVar('title', $btitle);
507
        $myblock->setVar('weight', $bweight);
508
        $myblock->setVar('visible', $bvisible);
509
        $myblock->setVar('side', $bside);
510
        $myblock->setVar('bcachetime', $bcachetime);
511
        //update block options
512
        if (isset($options)) {
513
            $options_count = \count($options);
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

513
            $options_count = \count(/** @scrutinizer ignore-type */ $options);
Loading history...
514
            if ($options_count > 0) {
515
                //Convert array values to comma-separated
516
                foreach ($options as $i => $iValue) {
517
                    if (\is_array($iValue)) {
518
                        $options[$i] = \implode(',', $iValue);
519
                    }
520
                }
521
                $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

521
                $options = \implode('|', /** @scrutinizer ignore-type */ $options);
Loading history...
522
                $myblock->setVar('options', $options);
523
            }
524
        }
525
        $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

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

563
        /** @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...
564
        $oldbcachetime,
565
        $title,
566
        $weight,
567
        $visible,
568
        $side,
569
        $bcachetime,
570
        $groups,
571
        $bmodule = null
572
    ) {
573
        if (!$GLOBALS['xoopsSecurity']->check()) {
574
            \redirect_header($_SERVER['SCRIPT_NAME'], 3, \implode('<br>', $GLOBALS['xoopsSecurity']->getErrors()));
575
        }
576
        foreach (\array_keys($bid) as $i) {
0 ignored issues
show
Bug introduced by
$bid of type integer is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

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

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