Passed
Push — master ( 08b589...961b46 )
by Michael
08:46
created

list_blocks()   F

Complexity

Conditions 28
Paths 2881

Size

Total Lines 217
Code Lines 149

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 149
nc 2881
nop 0
dl 0
loc 217
rs 0
c 0
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.php                              //
4
// - XOOPS block admin for each modules -                     //
5
// GIJOE <http://www.peak.ne.jp>                   //
6
// ------------------------------------------------------------------------- //
7
use Xmf\Request;
8
9
require_once dirname(__DIR__, 3) . '/include/cp_header.php';
10
require_once __DIR__ . '/mygrouppermform.php';
11
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
12
//require_once  dirname(__DIR__) . '/include/gtickets.php'; // GIJ
13
14
$xoops_system_path = XOOPS_ROOT_PATH . '/modules/system';
15
// language files
16
$language = $xoopsConfig['language'];
17
if (!file_exists("$xoops_system_path/language/$language/admin/blocksadmin.php")) {
18
    $language = 'english';
19
}
20
// to prevent from notice that constants already defined
21
$error_reporting_level = error_reporting(0);
22
require_once "$xoops_system_path/constants.php";
23
require_once "$xoops_system_path/language/$language/admin.php";
24
require_once "$xoops_system_path/language/$language/admin/blocksadmin.php";
25
error_reporting($error_reporting_level);
26
27
$group_defs = file("$xoops_system_path/language/$language/admin/groups.php");
28
foreach ($group_defs as $def) {
29
    if (false !== mb_strpos($def, '_AM_ACCESSRIGHTS') || false !== mb_strpos($def, '_AM_ACTIVERIGHTS')) {
30
        eval($def);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
31
    }
32
}
33
// check $xoopsModule
34
if (!is_object($xoopsModule)) {
35
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
36
}
37
// set target_module if specified by $_GET['dirname']
38
/** @var \XoopsModuleHandler $moduleHandler */
39
$moduleHandler = xoops_getHandler('module');
40
if (!empty($_GET['dirname'])) {
41
    $target_module = $moduleHandler->getByDirname($_GET['dirname']);
42
}
43
/* else if ( ! empty( $_GET['mid'] ) ) {
44
    $target_module = $moduleHandler->get( (int)( $_GET['mid'] ) );
45
}*/
46
47
if (!empty($target_module) && is_object($target_module)) {
48
    // specified by dirname
49
    $target_mid     = $target_module->getVar('mid');
50
    $target_mname   = $target_module->getVar('name');
51
    $query4redirect = '?dirname=' . urlencode(strip_tags($_GET['dirname']));
52
} elseif (Request::hasVar('mid', 'GET') && 0 == $_GET['mid'] || 'blocksadmin' === $xoopsModule->getVar('dirname')) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (Xmf\Request::hasVar('mi...dule->getVar('dirname'), Probably Intended Meaning: Xmf\Request::hasVar('mid...ule->getVar('dirname'))
Loading history...
53
    $target_mid     = 0;
54
    $target_mname   = '';
55
    $query4redirect = '?mid=0';
56
} else {
57
    $target_mid     = $xoopsModule->getVar('mid');
58
    $target_mname   = $xoopsModule->getVar('name');
59
    $query4redirect = '';
60
}
61
// check access right (needs system_admin of BLOCK)
62
$grouppermHandler = xoops_getHandler('groupperm');
63
if (!$grouppermHandler->checkRight('system_admin', XOOPS_SYSTEM_BLOCK, $xoopsUser->getGroups())) {
0 ignored issues
show
Bug introduced by
The method checkRight() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

63
if (!$grouppermHandler->/** @scrutinizer ignore-call */ checkRight('system_admin', XOOPS_SYSTEM_BLOCK, $xoopsUser->getGroups())) {
Loading history...
64
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
65
}
66
// get blocks owned by the module (Imported from xoopsblock.php then modified)
67
// $block_arr = \XoopsBlock::getByModule( $target_mid ) ;
68
$db        = \XoopsDatabaseFactory:: getDatabaseConnection();
69
$sql       = 'SELECT * FROM ' . $db->prefix('newblocks') . " WHERE mid='$target_mid' ORDER BY visible DESC,side,weight";
70
$result    = $db->query($sql);
71
$block_arr = [];
72
while (false !== ($myrow = $db->fetchArray($result))) {
73
    $block_arr[] = new \XoopsBlock($myrow);
74
}
75
76
function list_blocks()
77
{
78
    global $query4redirect, $block_arr;
79
    // cachetime options
80
    $cachetimes = [
81
        '0'       => _NOCACHE,
82
        '30'      => sprintf(_SECONDS, 30),
83
        '60'      => _MINUTE,
84
        '300'     => sprintf(_MINUTES, 5),
85
        '1800'    => sprintf(_MINUTES, 30),
86
        '3600'    => _HOUR,
87
        '18000'   => sprintf(_HOURS, 5),
88
        '86400'   => _DAY,
89
        '259200'  => sprintf(_DAYS, 3),
90
        '604800'  => _WEEK,
91
        '2592000' => _MONTH,
92
    ];
93
    // displaying TH
94
    echo "
95
    <form action='admin.php' name='blockadmin' method='post'>
96
        <table width='95%' class='outer' cellpadding='4' cellspacing='1'>
97
        <tr valign='middle'>
98
            <th style='text-align: left;'>" . _AM_TITLE . "</th>
0 ignored issues
show
Bug introduced by
The constant _AM_TITLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99
            <th class='center;' nowrap='nowrap'>" . _AM_SIDE . "</th>
0 ignored issues
show
Bug introduced by
The constant _AM_SIDE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
100
            <th class='center;'>" . _AM_WEIGHT . "</th>
0 ignored issues
show
Bug introduced by
The constant _AM_WEIGHT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
101
            <th class='center;'>" . _AM_VISIBLEIN . "</th>
0 ignored issues
show
Bug introduced by
The constant _AM_VISIBLEIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
102
            <th class='center;'>" . _AM_BCACHETIME . "</th>
0 ignored issues
show
Bug introduced by
The constant _AM_BCACHETIME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
103
            <th class='center;'>" . _AM_ACTION . "</th>
0 ignored issues
show
Bug introduced by
The constant _AM_ACTION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
104
        </tr>\n";
105
    // blocks displaying loop
106
    $class         = 'even';
107
    $block_configs = get_block_configs();
108
    foreach (array_keys($block_arr) as $i) {
109
        $sseln = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = $ssel5 = $ssel6 = $ssel7 = '';
110
        $scoln = $scol0 = $scol1 = $scol2 = $scol3 = $scol4 = $scol5 = $scol6 = $scol7 = '#FFFFFF';
111
112
        $weight     = $block_arr[$i]->getVar('weight');
113
        $title      = $block_arr[$i]->getVar('title');
114
        $name       = $block_arr[$i]->getVar('name');
115
        $bcachetime = $block_arr[$i]->getVar('bcachetime');
116
117
        $bid = $block_arr[$i]->getVar('bid');
118
        // visible and side
119
        if (1 != $block_arr[$i]->getVar('visible')) {
120
            $sseln = ' checked';
121
            $scoln = '#FF0000';
122
        } else {
123
            switch ($block_arr[$i]->getVar('side')) {
124
                default:
125
                case XOOPS_SIDEBLOCK_LEFT:
126
                    $ssel0 = ' checked';
127
                    $scol0 = '#00FF00';
128
                    break;
129
                case XOOPS_SIDEBLOCK_RIGHT:
130
                    $ssel1 = ' checked';
131
                    $scol1 = '#00FF00';
132
                    break;
133
                case XOOPS_CENTERBLOCK_LEFT:
134
                    $ssel2 = ' checked';
135
                    $scol2 = '#00FF00';
136
                    break;
137
                case XOOPS_CENTERBLOCK_RIGHT:
138
                    $ssel4 = ' checked';
139
                    $scol4 = '#00FF00';
140
                    break;
141
                case XOOPS_CENTERBLOCK_CENTER:
142
                    $ssel3 = ' checked';
143
                    $scol3 = '#00FF00';
144
                    break;
145
                case XOOPS_CENTERBLOCK_BOTTOMLEFT:
146
                    $ssel5 = ' checked';
147
                    $scol5 = '#00FF00';
148
                    break;
149
                case XOOPS_CENTERBLOCK_BOTTOMRIGHT:
150
                    $ssel7 = ' checked';
151
                    $scol7 = '#00FF00';
152
                    break;
153
                case XOOPS_CENTERBLOCK_BOTTOM:
154
                    $ssel6 = ' checked';
155
                    $scol6 = '#00FF00';
156
                    break;
157
            }
158
        }
159
        // bcachetime
160
        $cachetime_options = '';
161
        foreach ($cachetimes as $cachetime => $cachetime_name) {
162
            if ($bcachetime == $cachetime) {
163
                $cachetime_options .= "<option value='$cachetime' selected>$cachetime_name</option>\n";
164
            } else {
165
                $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
166
            }
167
        }
168
        // target modules
169
        $db            = \XoopsDatabaseFactory:: getDatabaseConnection();
170
        $result        = $db->query('SELECT module_id FROM ' . $db->prefix('block_module_link') . " WHERE block_id='$bid'");
171
        $selected_mids = [];
172
        while (list($selected_mid) = $db->fetchRow($result)) {
173
            $selected_mids[] = (int)$selected_mid;
174
        }
175
        /** @var \XoopsModuleHandler $moduleHandler */
176
        $moduleHandler = xoops_getHandler('module');
177
        $criteria      = new \CriteriaCompo(new \Criteria('hasmain', 1));
178
        $criteria->add(new \Criteria('isactive', 1));
179
        $module_list     = $moduleHandler->getList($criteria);
180
        $module_list[-1] = _AM_TOPPAGE;
0 ignored issues
show
Bug introduced by
The constant _AM_TOPPAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
181
        $module_list[0]  = _AM_ALLPAGES;
0 ignored issues
show
Bug introduced by
The constant _AM_ALLPAGES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
182
        ksort($module_list);
183
        $module_options = '';
184
        foreach ($module_list as $mid => $mname) {
185
            if (in_array($mid, $selected_mids)) {
186
                $module_options .= "<option value='$mid' selected>$mname</option>\n";
187
            } else {
188
                $module_options .= "<option value='$mid'>$mname</option>\n";
189
            }
190
        }
191
        // delete link if it is cloned block
192
        $delete_link = '';
193
        if ('D' === $block_arr[$i]->getVar('block_type') || 'C' === $block_arr[$i]->getVar('block_type')) {
194
            $delete_link = "<br><a href='admin.php?fct=blocksadmin&amp;op=delete&amp;bid=$bid'>" . _DELETE . '</a>';
195
        }
196
        // clone link if it is marked as cloneable block
197
        // $modversion['blocks'][n]['can_clone']
198
        if ('D' === $block_arr[$i]->getVar('block_type') || 'C' === $block_arr[$i]->getVar('block_type')) {
199
            $can_clone = true;
200
        } else {
201
            $can_clone = false;
202
            foreach ($block_configs as $bconf) {
203
                if ($block_arr[$i]->getVar('show_func') == $bconf['show_func']
204
                    && $block_arr[$i]->getVar('func_file') == $bconf['file']
205
                    && (empty($bconf['template'])
206
                        || $block_arr[$i]->getVar('template') == $bconf['template'])) {
207
                    if (!empty($bconf['can_clone'])) {
208
                        $can_clone = true;
209
                    }
210
                }
211
            }
212
        }
213
        $clone_link = '';
214
        if ($can_clone) {
215
            $clone_link = "<br><a href='admin.php?fct=blocksadmin&amp;op=clone&amp;bid=$bid'>" . _CLONE . '</a>';
216
        }
217
        // displaying part
218
        echo "
219
        <tr valign='middle'>
220
            <td class='$class'>
221
                $name
222
                <br>
223
                <input type='text' name='title[$bid]' value='$title' size='20'>
224
            </td>
225
            <td class='$class' class='center;' nowrap='nowrap' width='110px'>
226
                <div style='float:left;background-color:$scol0;'>
227
                    <input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_LEFT . "' style='background-color:$scol0;' $ssel0>
228
                </div>
229
                <div style='float:left;'>-</div>
230
                <div style='float:left;background-color:$scol2;'>
231
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_LEFT . "' style='background-color:$scol2;' $ssel2>
232
                </div>
233
                <div style='float:left;background-color:$scol3;'>
234
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_CENTER . "' style='background-color:$scol3;' $ssel3>
235
                </div>
236
                <div style='float:left;background-color:$scol4;'>
237
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_RIGHT . "' style='background-color:$scol4;' $ssel4>
238
                </div>
239
                <div style='float:left;'>-</div>
240
                <div style='float:left;background-color:$scol1;'>
241
                    <input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_RIGHT . "' style='background-color:$scol1;' $ssel1>
242
                </div>
243
                <br>
244
                                <div style='float:left;width:25px;'>&nbsp;</div>
245
                <div style='float:left;background-color:$scol5;'>
246
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_BOTTOMLEFT . "' style='background-color:$scol5;' $ssel5>
247
    </div>
248
                <div style='float:left;background-color:$scol6;'>
249
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_BOTTOM . "' style='background-color:$scol6;' $ssel6>
250
                </div>
251
                <div style='float:left;background-color:$scol7;'>
252
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_BOTTOMRIGHT . "' style='background-color:$scol7;' $ssel7>
253
                </div>
254
                <div style='float:left;width:25px;'>&nbsp;</div>
255
                <br>
256
                <br>
257
                <div style='float:left;width:40px;'>&nbsp;</div>
258
                <div style='float:left;background-color:$scoln;'>
259
                    <input type='radio' name='side[$bid]' value='-1' style='background-color:$scoln;' $sseln>
260
                </div>
261
                <div style='float:left;'>" . _NONE . "</div>
262
            </td>
263
            <td class='$class' class='center;'>
264
                <input type='text' name=weight[$bid] value='$weight' size='3' maxlength='5' style='text-align:right;'>
265
            </td>
266
            <td class='$class' class='center;'>
267
                <select name='bmodule[$bid][]' size='5' multiple='multiple'>
268
                    $module_options
269
                </select>
270
            </td>
271
            <td class='$class' class='center;'>
272
                <select name='bcachetime[$bid]' size='1'>
273
                    $cachetime_options
274
                </select>
275
            </td>
276
            <td class='$class' class='center;'>
277
                <a href='admin.php?fct=blocksadmin&amp;op=edit&amp;bid=$bid'>" . _EDIT . "</a>{$delete_link}{$clone_link}
278
                <input type='hidden' name='bid[$bid]' value='$bid'>
279
            </td>
280
        </tr>\n";
281
282
        $class = ('even' === $class) ? 'odd' : 'even';
283
    }
284
285
    echo "
286
        <tr>
287
            <td class='foot' class='center;' colspan='6'>
288
                <input type='hidden' name='query4redirect' value='$query4redirect'>
289
                <input type='hidden' name='fct' value='blocksadmin'>
290
                <input type='hidden' name='op' value='order'>
291
                " . $GLOBALS['xoopsSecurity']->getTokenHTML() . "
292
                <input type='submit' name='submit' value='" . _SUBMIT . "'>
293
            </td>
294
        </tr>
295
        </table>
296
    </form>\n";
297
}
298
299
/**
300
 * @return array
301
 */
302
function get_block_configs()
303
{
304
    $error_reporting_level = error_reporting(0);
305
    require_once dirname(__DIR__) . '/xoops_version.php';
306
    error_reporting($error_reporting_level);
307
    if (empty($modversion['blocks'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $modversion seems to never exist and therefore empty should always be true.
Loading history...
308
        return [];
309
    }
310
311
    return $modversion['blocks'];
312
}
313
314
function list_groups()
315
{
316
    global $target_mid, $target_mname, $block_arr;
317
318
    $item_list = [];
319
    foreach (array_keys($block_arr) as $i) {
320
        $item_list[$block_arr[$i]->getVar('bid')] = $block_arr[$i]->getVar('title');
321
    }
322
323
    $form = new MyXoopsGroupPermForm(_MD_AM_ADGS, 1, 'block_read', '');
0 ignored issues
show
Bug introduced by
The constant _MD_AM_ADGS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
324
    if ($target_mid > 1) {
325
        $form->addAppendix('module_admin', $target_mid, $target_mname . ' ' . _AM_ACTIVERIGHTS);
0 ignored issues
show
Bug introduced by
The constant _AM_ACTIVERIGHTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
326
        $form->addAppendix('module_read', $target_mid, $target_mname . ' ' . _AM_ACCESSRIGHTS);
0 ignored issues
show
Bug introduced by
The constant _AM_ACCESSRIGHTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
327
    }
328
    foreach ($item_list as $item_id => $item_name) {
329
        $form->addItem($item_id, $item_name);
330
    }
331
    echo $form->render();
332
}
333
334
if (!empty($_POST['submit'])) {
335
    if (!$GLOBALS['xoopsSecurity']->check()) {
336
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
337
    }
338
339
    require_once __DIR__ . '/mygroupperm.php';
340
    redirect_header(XOOPS_URL . '/modules/' . $xoopsModule->getVar('dirname') . "/admin/myblocksadmin.php$query4redirect", 1, _AM_SYSTEM_DBUPDATED);
341
}
342
343
xoops_cp_header();
344
345
if (file_exists('./mymenu.php')) {
346
    require_once __DIR__ . '/mymenu.php';
347
}
348
349
echo "<h3 style='text-align:left;'>$target_mname</h3>\n";
350
351
if (!empty($block_arr)) {
352
    echo "<h4 style='text-align:left;'>" . _AM_BADMIN . "</h4>\n";
0 ignored issues
show
Bug introduced by
The constant _AM_BADMIN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
353
    list_blocks();
354
}
355
356
list_groups();
357
xoops_cp_footer();
358