Passed
Push — master ( 3a2152...658ebb )
by Goffy
04:30 queued 12s
created

list_blockinstances()   F

Complexity

Conditions 31
Paths 4338

Size

Total Lines 240
Code Lines 156

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 31
eloc 156
nc 4338
nop 0
dl 0
loc 240
rs 0
c 2
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
2
// ------------------------------------------------------------------------- //
3
//                     myblocksadmin_for_2.2.php                             //
4
//                - XOOPS block admin for each modules -                     //
5
//                          GIJOE <http://www.peak.ne.jp>                   //
6
// ------------------------------------------------------------------------- //
7
8
use Xmf\Request;
9
use XoopsModules\Lexikon\{
10
    GroupPermForm,
11
    Helper,
12
    Utility
13
};
14
/** @var Helper $helper */
15
16
require dirname(__DIR__, 3) . '/include/cp_header.php';
17
18
require_once __DIR__ . '/mygrouppermform.php';
19
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
20
21
$xoops_system_path = XOOPS_ROOT_PATH . '/modules/system';
22
23
// language files
24
$language = $xoopsConfig['language'];
25
if (!file_exists("$xoops_system_path/language/$language/admin/blocksadmin.php")) {
26
    $language = 'english';
27
}
28
29
// to prevent from notice that constants already defined
30
$error_reporting_level = error_reporting(0);
31
require_once "$xoops_system_path/constants.php";
32
require_once "$xoops_system_path/language/$language/admin.php";
33
require_once "$xoops_system_path/language/$language/admin/blocksadmin.php";
34
error_reporting($error_reporting_level);
35
36
$group_defs = file("$xoops_system_path/language/$language/admin/groups.php");
37
foreach ($group_defs as $def) {
38
    if (false !== mb_strpos($def, '_AM_ACCESSRIGHTS') || false !== mb_strpos($def, '_AM_ACTIVERIGHTS')) {
39
        eval($def);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
40
    }
41
}
42
43
// check $xoopsModule
44
if (!is_object($xoopsModule)) {
45
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
46
}
47
48
// set target_module if specified by $_GET['dirname']
49
/** @var \XoopsModuleHandler $moduleHandler */
50
$moduleHandler = xoops_getHandler('module');
51
if (!empty($_GET['dirname'])) {
52
    $target_module = $moduleHandler->getByDirname($_GET['dirname']);
53
}/* else if ( ! empty( $_GET['mid'] ) ) {
54
    $target_module = $moduleHandler->get( (int)( $_GET['mid'] ) );
55
}*/
56
57
if (!empty($target_module) && is_object($target_module)) {
58
    // specified by dirname
59
    $target_mid     = $target_module->getVar('mid');
60
    $target_mname   = $target_module->getVar('name') . '&nbsp;' . sprintf('(%2.2f)', $target_module->getVar('version') / 100.0);
61
    $query4redirect = '?dirname=' . urlencode(strip_tags($_GET['dirname']));
62
} elseif ((Request::hasVar('mid', 'GET') && 0 == $_GET['mid']) || 'blocksadmin' === $xoopsModule->getVar('dirname')) {
63
    $target_mid     = 0;
64
    $target_mname   = '';
65
    $query4redirect = '?mid=0';
66
} else {
67
    $target_mid     = $xoopsModule->getVar('mid');
68
    $target_mname   = $xoopsModule->getVar('name');
69
    $query4redirect = '';
70
}
71
72
// check access right (needs system_admin of BLOCK)
73
/** @var \XoopsGroupPermHandler $grouppermHandler */
74
$grouppermHandler = xoops_getHandler('groupperm');
75
if (!$grouppermHandler->checkRight('system_admin', XOOPS_SYSTEM_BLOCK, $xoopsUser->getGroups())) {
76
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
77
}
78
79
// get blocks owned by the module (Imported from xoopsblock.php then modified)
80
$db        = \XoopsDatabaseFactory::getDatabaseConnection();
81
$sql       = 'SELECT bid,name,show_func,func_file,template FROM ' . $db->prefix('newblocks') . " WHERE mid='$target_mid'";
82
$result    = $db->query($sql);
83
$block_arr = [];
84
while (list($bid, $bname, $show_func, $func_file, $template) = $db->fetchRow($result)) {
85
    $block_arr[$bid] = [
86
        'name'      => $bname,
87
        'show_func' => $show_func,
88
        'func_file' => $func_file,
89
        'template'  => $template,
90
    ];
91
}
92
93
// for 2.2
94
/**
95
 *
96
 */
97
function list_blockinstances()
98
{
99
    global $query4redirect, $block_arr;
100
101
    $myts = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $myts is dead and can be removed.
Loading history...
102
103
    // cachetime options
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
118
    // displaying TH
119
    echo "
120
    <form action='admin.php' name='blockadmin' method='post'>
121
        <table width='95%' class='outer' cellpadding='4' cellspacing='1'>
122
        <tr valign='middle'>
123
            <th>" . _AM_TITLE . "</th>
124
            <th align='center' nowrap='nowrap'>" . _AM_SIDE . "</th>
125
            <th align='center'>" . _AM_WEIGHT . "</th>
126
            <th align='center'>" . _AM_VISIBLEIN . "</th>
127
            <th align='center'>" . _AM_BCACHETIME . "</th>
128
            <th align='right'>" . _AM_ACTION . "</th>
129
        </tr>\n";
130
131
    // get block instances
132
    $crit     = new \Criteria('bid', '(' . implode(',', array_keys($block_arr)) . ')', 'IN');
133
    $criteria = new \CriteriaCompo($crit);
134
    $criteria->setSort('visible DESC, side ASC, weight');
135
    $instanceHandler = xoops_getHandler('blockinstance');
136
    $instances       = $instanceHandler->getObjects($criteria, true, true);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

136
    /** @scrutinizer ignore-call */ 
137
    $instances       = $instanceHandler->getObjects($criteria, true, true);
Loading history...
137
138
    //Get modules and pages for visible in
139
    $module_list[_AM_SYSTEMLEVEL]['0-2'] = _AM_ADMINBLOCK;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$module_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $module_list = array(); before regardless.
Loading history...
140
    $module_list[_AM_SYSTEMLEVEL]['0-1'] = _AM_TOPPAGE;
141
    $module_list[_AM_SYSTEMLEVEL]['0-0'] = _AM_ALLPAGES;
142
    $criteria                            = new \CriteriaCompo(new \Criteria('hasmain', 1));
143
    $criteria->add(new \Criteria('isactive', 1));
144
    /** @var \XoopsModuleHandler $moduleHandler */
145
    $moduleHandler = xoops_getHandler('module');
146
    $module_main   = $moduleHandler->getObjects($criteria, true, true);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModuleHandler::getObjects() has too many arguments starting with true. ( Ignorable by Annotation )

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

146
    /** @scrutinizer ignore-call */ 
147
    $module_main   = $moduleHandler->getObjects($criteria, true, true);

This check compares calls to functions or methods with their respective definitions. If the call has more 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...
147
    if (count($module_main) > 0) {
148
        foreach (array_keys($module_main) as $mid) {
149
            $module_list[$module_main[$mid]->getVar('name')][$mid . '-0'] = _AM_ALLMODULEPAGES;
150
            $pages                                                        = $module_main[$mid]->getInfo('pages');
151
            if (false === $pages) {
152
                $pages = $module_main[$mid]->getInfo('sub');
153
            }
154
            if (is_array($pages) && $pages != []) {
155
                foreach ($pages as $id => $pageinfo) {
156
                    $module_list[$module_main[$mid]->getVar('name')][$mid . '-' . $id] = $pageinfo['name'];
157
                }
158
            }
159
        }
160
    }
161
162
    // blocks displaying loop
163
    $class         = 'even';
164
    $block_configs = get_block_configs();
165
    foreach (array_keys($block_arr) as $i) {
166
        $sseln = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = $ssel5 = $ssel6 = $ssel7 = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $ssel5 is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $ssel7 is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $ssel6 is dead and can be removed.
Loading history...
167
        $scoln = $scol0 = $scol1 = $scol2 = $scol3 = $scol4 = $ssel5 = $ssel6 = $ssel7 = '';
168
169
        $weight     = $instances[$i]->getVar('weight');
170
        $title      = $instances[$i]->getVar('title');
171
        $bcachetime = $instances[$i]->getVar('bcachetime');
172
        $bid        = $instances[$i]->getVar('bid');
173
        $name       = htmlspecialchars($block_arr[$bid]['name'], ENT_QUOTES | ENT_HTML5);
174
175
        $visiblein = $instances[$i]->getVisibleIn();
176
177
        // visible and side
178
        if (1 != $instances[$i]->getVar('visible')) {
179
            $sseln = ' checked';
180
            $scoln = '#FF0000';
181
        } else {
182
            switch ($instances[$i]->getVar('side')) {
183
                default:
184
                case XOOPS_SIDEBLOCK_LEFT:
185
                    $ssel0 = ' checked';
186
                    $scol0 = '#00FF00';
187
                    break;
188
                case XOOPS_SIDEBLOCK_RIGHT:
189
                    $ssel1 = ' checked';
190
                    $scol1 = '#00FF00';
191
                    break;
192
                case XOOPS_CENTERBLOCK_LEFT:
193
                    $ssel2 = ' checked';
194
                    $scol2 = '#00FF00';
195
                    break;
196
                case XOOPS_CENTERBLOCK_RIGHT:
197
                    $ssel4 = ' checked';
198
                    $scol4 = '#00FF00';
199
                    break;
200
                case XOOPS_CENTERBLOCK_CENTER:
201
                    $ssel3 = ' checked';
202
                    $scol3 = '#00FF00';
203
                    break;
204
                case XOOPS_CENTERBLOCK_BOTTOMLEFT:
205
                    $ssel5 = ' checked';
206
                    $scol5 = '#00FF00';
0 ignored issues
show
Unused Code introduced by
The assignment to $scol5 is dead and can be removed.
Loading history...
207
                    break;
208
                case XOOPS_CENTERBLOCK_BOTTOMRIGHT:
209
                    $ssel6 = ' checked';
210
                    $scol6 = '#00FF00';
0 ignored issues
show
Unused Code introduced by
The assignment to $scol6 is dead and can be removed.
Loading history...
211
                    break;
212
                case XOOPS_CENTERBLOCK_BOTTOM:
213
                    $ssel7 = ' checked';
214
                    $scol7 = '#00FF00';
0 ignored issues
show
Unused Code introduced by
The assignment to $scol7 is dead and can be removed.
Loading history...
215
                    break;
216
            }
217
        }
218
219
        // bcachetime
220
        $cachetime_options = '';
221
        foreach ($cachetimes as $cachetime => $cachetime_name) {
222
            if ($bcachetime == $cachetime) {
223
                $cachetime_options .= "<option value='$cachetime' selected>$cachetime_name</option>\n";
224
            } else {
225
                $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
226
            }
227
        }
228
229
        $module_options = '';
230
        foreach ($module_list as $mname => $module) {
231
            $module_options .= "<optgroup label='$mname'>\n";
232
            foreach ($module as $mkey => $mval) {
233
                if (in_array($mkey, $visiblein)) {
234
                    $module_options .= "<option value='$mkey' selected>$mval</option>\n";
235
                } else {
236
                    $module_options .= "<option label='$mval' value='$mkey'>$mval</option>\n";
237
                }
238
            }
239
            $module_options .= "</optgroup>\n";
240
        }
241
242
        // delete link if it is cloned block
243
        $delete_link = "<br><a href='" . XOOPS_URL . "/modules/system/admin.php?fct=blocksadmin&amp;op=delete&amp;id=$i&amp;selmod=$mid'>" . _DELETE . '</a>';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $mid does not seem to be defined for all execution paths leading up to this point.
Loading history...
244
245
        // displaying part
246
        echo "
247
        <tr valign='middle'>
248
            <td class='$class'>
249
                $name
250
                <br>
251
                <input type='text' name='title[$i]' value='$title' size='20'>
252
            </td>
253
            <td class='$class' align='center' nowrap='nowrap' width='125px'>
254
                <div style='float:left;background-color:$scol0;'>
255
                    <input type='radio' name='side[$i]' value='" . XOOPS_SIDEBLOCK_LEFT . "' style='background-color:$scol0;' $ssel0>
256
                </div>
257
                <div style='float:left;'>-</div>
258
                <div style='float:left;background-color:$scol2;'>
259
                    <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_LEFT . "' style='background-color:$scol2;' $ssel2>
260
                </div>
261
                <div style='float:left;background-color:$scol3;'>
262
                    <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_CENTER . "' style='background-color:$scol3;' $ssel3>
263
                </div>
264
                <div style='float:left;background-color:$scol4;'>
265
                    <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_RIGHT . "' style='background-color:$scol4;' $ssel4>
266
                </div>
267
                <div style='float:left;'>-</div>
268
                <div style='float:left;background-color:$scol1;'>
269
                    <input type='radio' name='side[$i]' value='" . XOOPS_SIDEBLOCK_RIGHT . "' style='background-color:$scol1;' $ssel1>
270
                </div>
271
                <br>
272
                <br>
273
                <div style='float:left;width:40px;'>&nbsp;</div>
274
                <div style='float:left;background-color:$scoln;'>
275
                    <input type='radio' name='side[$i]' value='-1' style='background-color:$scoln;' $sseln>
276
                </div>
277
                <div style='float:left;'>" . _NONE . "</div>
278
            </td>
279
            <td class='$class' align='center'>
280
                <input type='text' name=weight[$i] value='$weight' size='3' maxlength='5' style='text-align:right;'>
281
            </td>
282
            <td class='$class' align='center'>
283
                <select name='bmodule[$i][]' size='5' multiple='multiple'>
284
                    $module_options
285
                </select>
286
            </td>
287
            <td class='$class' align='center'>
288
                <select name='bcachetime[$i]' size='1'>
289
                    $cachetime_options
290
                </select>
291
            </td>
292
            <td class='$class' align='right'>
293
                <a href='" . XOOPS_URL . "/modules/system/admin.php?fct=blocksadmin&amp;op=edit&amp;id=$i'>" . _EDIT . "</a>{$delete_link}
294
                <input type='hidden' name='id[$i]' value='$i'>
295
            </td>
296
        </tr>\n";
297
298
        $class = ('even' === $class) ? 'odd' : 'even';
299
    }
300
301
    // list block classes for add (not instances)
302
    foreach ($block_arr as $bid => $block) {
303
        $description4show = '';
304
        foreach ($block_configs as $bconf) {
305
            if ($block['show_func'] == $bconf['show_func'] && $block['func_file'] == $bconf['file']
306
                && (empty($bconf['template']) || $block['template'] == $bconf['template'])) {
307
                if (!empty($bconf['description'])) {
308
                    $description4show = htmlspecialchars($bconf['description'], ENT_QUOTES | ENT_HTML5);
309
                }
310
            }
311
        }
312
313
        echo "
314
        <tr>
315
            <td class='$class' align='left'>
316
                " . htmlspecialchars($block['name'], ENT_QUOTES | ENT_HTML5) . "
317
            </td>
318
            <td class='$class' align='left' colspan='4'>
319
                $description4show
320
            </td>
321
            <td class='$class' align='center'>
322
                <input type='submit' name='addblock[$bid]' value='" . _ADD . "'>
323
            </td>
324
        </tr>
325
        \n";
326
        $class = ('even' === $class) ? 'odd' : 'even';
327
    }
328
329
    echo "
330
        <tr>
331
            <td class='foot' align='center' colspan='6'>
332
                <input type='hidden' name='query4redirect' value='$query4redirect'>
333
                <input type='hidden' name='fct' value='blocksadmin'>
334
                <input type='hidden' name='op' value='order2'>
335
                " . $GLOBALS['xoopsSecurity']->getTokenHTML('myblocksadmin') . "
336
                <input type='submit' name='submit' value='" . _SUBMIT . "'>
337
            </td>
338
        </tr>
339
        </table>
340
    </form>\n";
341
}
342
343
// for 2.2
344
/**
345
 *
346
 */
347
function list_groups2()
348
{
349
    global $target_mid, $target_mname, $xoopsDB;
350
351
    $result = $xoopsDB->query('SELECT i.instanceid,i.title FROM ' . $xoopsDB->prefix('block_instance') . ' i LEFT JOIN ' . $xoopsDB->prefix('newblocks') . " b ON i.bid=b.bid WHERE b.mid='$target_mid'");
352
353
    $item_list = [];
354
    while (list($iid, $title) = $xoopsDB->fetchRow($result)) {
355
        $item_list[$iid] = $title;
356
    }
357
358
    $form = new GroupPermForm(_MD_AM_ADGS, 1, 'block_read', '');
359
    if ($target_mid > 1) {
360
        $form->addAppendix('module_admin', $target_mid, $target_mname . ' ' . _AM_ACTIVERIGHTS);
361
        $form->addAppendix('module_read', $target_mid, $target_mname . ' ' . _AM_ACCESSRIGHTS);
362
    }
363
    foreach ($item_list as $item_id => $item_name) {
364
        $form->addItem($item_id, $item_name);
365
    }
366
    echo $form->render();
367
}
368
369
if (!empty($_POST['submit'])) {
370
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
371
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
372
    }
373
374
    require __DIR__ . '/mygroupperm.php';
375
    redirect_header(XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/admin/myblocksadmin.php$query4redirect", 1, _MD_AM_DBUPDATED);
376
}
377
378
xoops_cp_header();
379
//if( file_exists( './mymenu.php' ) ) require('./mymenu.php' ) ;
380
require_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/admin/functions.php';
381
//lx_adminMenu(3, _AM_LEXIKON_BLOCKS);
382
383
//echo "<h3 style='text-align:left;'>$target_mname</h3>\n" ;
384
385
if (!empty($block_arr)) {
386
    echo "<h4 style='text-align:left;'>" . _AM_BADMIN . "</h4>\n";
387
    list_blockinstances();
388
}
389
390
list_groups2();
391
xoops_cp_footer();
392