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

list_blocks()   F

Complexity

Conditions 28
Paths 2881

Size

Total Lines 217
Code Lines 147

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 28
eloc 147
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
8
use Xmf\Request;
9
use XoopsModules\Lexikon\{
10
    GroupPermForm,
11
    Helper,
12
    Utility
13
};
14
/** @var Helper $helper */
15
16
require_once __DIR__ . '/admin_header.php';
17
18
//if (false !== strpos(XOOPS_VERSION, 'XOOPS 2.2')) {
19
//    require __DIR__   . '/myblocksadmin2.php';
20
//    exit;
21
//}
22
23
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
24
//require_once  dirname(__DIR__) . '/include/gtickets.php';// GIJ
25
26
$xoops_system_path = XOOPS_ROOT_PATH . '/modules/system';
27
28
// language files
29
$language = $xoopsConfig['language'];
30
$helper->loadLanguage('admin');
31
$helper->loadLanguage('blocksadmin');
32
33
// to prevent from notice that constants already defined
34
$error_reporting_level = error_reporting(0);
35
require_once "$xoops_system_path/constants.php";
36
//require_once "$xoops_system_path/language/$language/admin.php";
37
//require_once "$xoops_system_path/language/$language/admin/blocksadmin.php";
38
39
error_reporting($error_reporting_level);
40
41
$group_defs = file("$xoops_system_path/language/$language/admin/groups.php");
42
foreach ($group_defs as $def) {
43
    if (true === mb_strpos($def, '_AM_ACCESSRIGHTS') || true === mb_strpos($def, '_AM_ACTIVERIGHTS')) {
44
        eval($def);
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
45
    }
46
}
47
// check $xoopsModule
48
if (!is_object($xoopsModule)) {
49
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
50
}
51
52
// set target_module if specified by $_GET['dirname']
53
/** @var \XoopsModuleHandler $moduleHandler */
54
$moduleHandler = xoops_getHandler('module');
55
if (!empty($_GET['dirname'])) {
56
    $target_module = $moduleHandler->getByDirname($_GET['dirname']);
57
}
58
59
if (!empty($target_module) && is_object($target_module)) {
60
    // specified by dirname
61
    $target_mid     = $target_module->getVar('mid');
62
    $target_mname   = $target_module->getVar('name') . '&nbsp;' . sprintf('(%2.2f)', $target_module->getVar('version') / 100.0);
63
    $query4redirect = '?dirname=' . urlencode(strip_tags($_GET['dirname']));
64
} elseif ((Request::hasVar('mid', 'GET') && 0 == $_GET['mid']) || 'blocksadmin' === $xoopsModule->getVar('dirname')) {
65
    $target_mid     = 0;
66
    $target_mname   = '';
67
    $query4redirect = '?mid=0';
68
} else {
69
    $target_mid     = $xoopsModule->getVar('mid');
70
    $target_mname   = $xoopsModule->getVar('name');
71
    $query4redirect = '';
72
}
73
74
// check access right (needs system_admin of BLOCK)
75
/** @var \XoopsGroupPermHandler $grouppermHandler */
76
$grouppermHandler = xoops_getHandler('groupperm');
77
if (!$grouppermHandler->checkRight('system_admin', XOOPS_SYSTEM_BLOCK, $xoopsUser->getGroups())) {
78
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
79
}
80
81
// get blocks owned by the module (Imported from xoopsblock.php then modified)
82
$db        = \XoopsDatabaseFactory::getDatabaseConnection();
83
$sql       = 'SELECT * FROM ' . $db->prefix('newblocks') . " WHERE mid='$target_mid' ORDER BY visible DESC,side,weight";
84
$result    = $db->query($sql);
85
$block_arr = [];
86
while (false !== ($myrow = $db->fetchArray($result))) {
87
    $block_arr[] = new \XoopsBlock($myrow);
88
}
89
90
/**
91
 *
92
 */
93
function list_blocks()
94
{
95
    global $query4redirect, $block_arr;
96
97
    // cachetime options
98
    $cachetimes = [
99
        '0'       => _NOCACHE,
100
        '30'      => sprintf(_SECONDS, 30),
101
        '60'      => _MINUTE,
102
        '300'     => sprintf(_MINUTES, 5),
103
        '1800'    => sprintf(_MINUTES, 30),
104
        '3600'    => _HOUR,
105
        '18000'   => sprintf(_HOURS, 5),
106
        '86400'   => _DAY,
107
        '259200'  => sprintf(_DAYS, 3),
108
        '604800'  => _WEEK,
109
        '2592000' => _MONTH,
110
    ];
111
112
    // displaying TH
113
    lx_collapsableBar('default', 'defaultIcon');
114
    echo "<img  onclick=\"toggle('default'); toggleIcon('defaultIcon');\" id='defaultIcon' src='" . XOOPS_URL . "/modules/lexikon/assets/images/close12.gif' alt=''></a>&nbsp; " . _AM_BADMIN . '<br><br>';
115
    echo "<div id='default' style='float:left; width:100%;'>";
116
    echo "<form action='admin.php' name='blockadmin' method='post'>
117
        <table width='95%' class='outer' cellpadding='4' cellspacing='1'>
118
        <tr valign='middle'>
119
            <th>" . _AM_TITLE . "</th>
120
            <th align='center' nowrap='nowrap'>" . _AM_SIDE . "<div style='font-size:smaller;'>" . _LEFT . '-' . _CENTER . '-' . _RIGHT . "</div></th>
121
            <th align='center'>" . _AM_WEIGHT . "</th>
122
            <th align='center'>" . _AM_VISIBLEIN . "</th>
123
            <th align='center'>" . _AM_BCACHETIME . "</th>
124
            <th align='right'>" . _AM_ACTION . "</th>
125
        </tr>\n";
126
127
    // blocks displaying loop
128
    $class         = 'even';
129
    $block_configs = get_block_configs();
130
    foreach (array_keys($block_arr) as $i) {
131
        $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...
132
        $scoln = $scol0 = $scol1 = $scol2 = $scol3 = $scol4 = $ssel5 = $ssel6 = $ssel7 = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $scol4 is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $scol2 is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $scol0 is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $scol3 is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $scol1 is dead and can be removed.
Loading history...
133
134
        $weight     = $block_arr[$i]->getVar('weight');
135
        $title      = $block_arr[$i]->getVar('title');
136
        $name       = $block_arr[$i]->getVar('name');
137
        $bcachetime = $block_arr[$i]->getVar('bcachetime');
138
        $bid        = $block_arr[$i]->getVar('bid');
139
140
        // visible and side
141
        if (1 != $block_arr[$i]->getVar('visible')) {
142
            $sseln = ' checked';
143
            $scoln = '#FF9966';
144
        } else {
145
            switch ($block_arr[$i]->getVar('side')) {
146
                default:
147
                case XOOPS_SIDEBLOCK_LEFT:
148
                    $ssel0 = ' checked';
149
                    $scol0 = '#00FF00';
150
                    break;
151
                case XOOPS_SIDEBLOCK_RIGHT:
152
                    $ssel1 = ' checked';
153
                    $scol1 = '#00FF00';
154
                    break;
155
                case XOOPS_CENTERBLOCK_LEFT:
156
                    $ssel2 = ' checked';
157
                    $scol2 = '#00FF00';
158
                    break;
159
                case XOOPS_CENTERBLOCK_RIGHT:
160
                    $ssel4 = ' checked';
161
                    $scol4 = '#00FF00';
162
                    break;
163
                case XOOPS_CENTERBLOCK_CENTER:
164
                    $ssel3 = ' checked';
165
                    $scol3 = '#00FF00';
166
                    break;
167
                case XOOPS_CENTERBLOCK_BOTTOMLEFT:
168
                    $ssel5 = ' checked';
169
                    $scol5 = '#00FF00';
0 ignored issues
show
Unused Code introduced by
The assignment to $scol5 is dead and can be removed.
Loading history...
170
                    break;
171
                case XOOPS_CENTERBLOCK_BOTTOMRIGHT:
172
                    $ssel6 = ' checked';
173
                    $scol6 = '#00FF00';
0 ignored issues
show
Unused Code introduced by
The assignment to $scol6 is dead and can be removed.
Loading history...
174
                    break;
175
                case XOOPS_CENTERBLOCK_BOTTOM:
176
                    $ssel7 = ' checked';
177
                    $scol7 = '#00FF00';
0 ignored issues
show
Unused Code introduced by
The assignment to $scol7 is dead and can be removed.
Loading history...
178
                    break;
179
            }
180
        }
181
182
        // bcachetime
183
        $cachetime_options = '';
184
        foreach ($cachetimes as $cachetime => $cachetime_name) {
185
            if ($bcachetime == $cachetime) {
186
                $cachetime_options .= "<option value='$cachetime' selected>$cachetime_name</option>\n";
187
            } else {
188
                $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
189
            }
190
        }
191
192
        // target modules
193
        $db            = \XoopsDatabaseFactory::getDatabaseConnection();
194
        $result        = $db->query('SELECT module_id FROM ' . $db->prefix('block_module_link') . " WHERE block_id='$bid'");
195
        $selected_mids = [];
196
        while (list($selected_mid) = $db->fetchRow($result)) {
197
            $selected_mids[] = (int)$selected_mid;
198
        }
199
        /** @var \XoopsModuleHandler $moduleHandler */
200
        $moduleHandler = xoops_getHandler('module');
201
        $criteria      = new \CriteriaCompo(new \Criteria('hasmain', 1));
202
        $criteria->add(new \Criteria('isactive', 1));
203
        $module_list     = $moduleHandler->getList($criteria);
204
        $module_list[-1] = _AM_TOPPAGE;
205
        $module_list[0]  = _AM_ALLPAGES;
206
        ksort($module_list);
207
        $module_options = '';
208
        foreach ($module_list as $mid => $mname) {
209
            if (in_array($mid, $selected_mids)) {
210
                $module_options .= "<option value='$mid' selected>$mname</option>\n";
211
            } else {
212
                $module_options .= "<option value='$mid'>$mname</option>\n";
213
            }
214
        }
215
216
        // delete link if it is cloned block
217
        if ('D' === $block_arr[$i]->getVar('block_type') || 'C' === $block_arr[$i]->getVar('block_type')) {
218
            $delete_link = "<br><a href='admin.php?fct=blocksadmin&amp;op=delete&amp;bid=$bid'>" . _DELETE . '</a>';
219
        } else {
220
            $delete_link = '';
221
        }
222
223
        // clone link if it is marked as cloneable block
224
        // $modversion['blocks'][n]['can_clone']
225
        if ('D' === $block_arr[$i]->getVar('block_type') || 'C' === $block_arr[$i]->getVar('block_type')) {
226
            $can_clone = true;
227
        } else {
228
            $can_clone = false;
229
            foreach ($block_configs as $bconf) {
230
                if ($block_arr[$i]->getVar('show_func') == $bconf['show_func']
231
                    && $block_arr[$i]->getVar('func_file') == $bconf['file']
232
                    && (empty($bconf['template'])
233
                        || $block_arr[$i]->getVar('template') == $bconf['template'])) {
234
                    if (!empty($bconf['can_clone'])) {
235
                        $can_clone = true;
236
                    }
237
                }
238
            }
239
        }
240
        if ($can_clone) {
241
            $clone_link = "<br><a href='admin.php?fct=blocksadmin&amp;op=clone&amp;bid=$bid'>" . _CLONE . '</a>';
242
        } else {
243
            $clone_link = '';
244
        }
245
246
        // displaying part
247
        echo "
248
        <tr valign='middle'>
249
            <td class='$class'>
250
                $name
251
                <br>
252
                <input type='text' name='title[$bid]' value='$title' size='20' >
253
            </td>
254
            <td class='$class' align='center' nowrap='nowrap' width='125px'>
255
      <div align='center' >
256
          <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_LEFT . "'$ssel2 >
257
          <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_CENTER . "'$ssel3 >
258
          <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_RIGHT . "'$ssel4 >
259
      </div>
260
      <div>
261
          <span style='float:right;'><input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_RIGHT . "'$ssel1 ></span>
262
          <div align='left'><input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_LEFT . "'$ssel0 ></div>
263
      </div>
264
      <div align='center'>
265
          <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_BOTTOMLEFT . "'$ssel5 >
266
          <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_BOTTOM . "'$ssel7 >
267
          <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_BOTTOMRIGHT . "'$ssel6 >
268
      </div>
269
      <br>
270
        <div style='float:left;width:30%;'>&nbsp;</div>
271
                <div style='float:left;background-color:$scoln;'>
272
                    <input type='radio' name='side[$bid]' value='-1'  $sseln >
273
                </div>
274
                <div style='float:left;'>" . _NONE . "</div>
275
            </td>
276
            <td class='$class' align='center'>
277
                <input type='text' name=weight[$bid] value='$weight' size='3' maxlength='5' style='text-align:right;' >
278
            </td>
279
            <td class='$class' align='center'>
280
                <select name='bmodule[$bid][]' size='5' multiple='multiple'>
281
                    $module_options
282
                </select>
283
            </td>
284
            <td class='$class' align='center'>
285
                <select name='bcachetime[$bid]' size='1'>
286
                    $cachetime_options
287
                </select>
288
            </td>
289
            <td class='$class' align='right'>
290
                <a href='admin.php?fct=blocksadmin&amp;op=edit&amp;bid=$bid'>" . _EDIT . "</a>{$delete_link}{$clone_link}
291
                <input type='hidden' name='bid[$bid]' value='$bid' >
292
            </td>
293
        </tr>\n";
294
295
        $class = ('even' === $class) ? 'odd' : 'even';
296
    }
297
298
    echo "<tr>
299
            <td class='foot' align='center' colspan='6'>
300
                <input type='hidden' name='query4redirect' value='$query4redirect' >
301
                <input type='hidden' name='fct' value='blocksadmin' >
302
                <input type='hidden' name='op' value='order' >
303
                " . $GLOBALS['xoopsSecurity']->getTokenHTML('myblocksadmin') . "
304
                <input type='submit' name='submit' value='" . _SUBMIT . "' >
305
            </td>
306
        </tr>
307
        </table>
308
    </form>\n";
309
    echo '</div>';
310
}
311
312
/**
313
 * @return array
314
 */
315
function get_block_configs()
316
{
317
    $error_reporting_level = error_reporting(0);
318
    if (preg_match('/^[.0-9a-zA-Z_-]+$/', @$_GET['dirname'])) {
319
        require \dirname(__DIR__, 2) . '/' . $_GET['dirname'] . '/xoops_version.php';
320
    } else {
321
        require dirname(__DIR__) . '/xoops_version.php';
322
    }
323
    error_reporting($error_reporting_level);
324
    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...
325
        return [];
326
    }
327
328
    return $modversion['blocks'];
329
}
330
331
/**
332
 *
333
 */
334
function list_groups()
335
{
336
    global $target_mid, $target_mname, $block_arr;
337
    lx_collapsableBar('groups', 'groupIcon');
338
    echo "<img  onclick=\"toggle('groups'); toggleIcon('groupsIcon');\" id='groupsIcon' src='" . XOOPS_URL . "/modules/lexikon/assets/images/close12.gif' alt='' ></a>&nbsp; " . _MD_AM_ADGS . ' <br>';
339
    echo "<div id='groups' style='float:left; width:100%;'>";
340
    $item_list = [];
341
    foreach (array_keys($block_arr) as $i) {
342
        $item_list[$block_arr[$i]->getVar('bid')] = $block_arr[$i]->getVar('title');
343
    }
344
345
    $form = new GroupPermForm(_MD_AM_ADGS, 1, 'block_read', '');
346
    if ($target_mid > 1) {
347
        $form->addAppendix('module_admin', $target_mid, $target_mname . ' ' . _AM_ACTIVERIGHTS);
348
        $form->addAppendix('module_read', $target_mid, $target_mname . ' ' . _AM_ACCESSRIGHTS);
349
    }
350
    foreach ($item_list as $item_id => $item_name) {
351
        $form->addItem($item_id, $item_name);
352
    }
353
    echo $form->render();
354
    echo '</div>';
355
}
356
357
if (!empty($_POST['submit'])) {
358
    if (!$GLOBALS['xoopsSecurity']->check(true, $_REQUEST['myblocksadmin'])) {
359
        redirect_header(XOOPS_URL . '/', 3, $GLOBALS['xoopsSecurity']->getErrors());
360
    }
361
362
    require __DIR__ . '/mygroupperm.php';
363
    redirect_header(XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/admin/myblocksadmin.php$query4redirect", 1, _MD_AM_DBUPDATED);
364
}
365
366
xoops_cp_header();
367
require_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/admin/functions.php';
368
369
if (!empty($block_arr)) {
370
    echo "<h4 style='text-align:left;'>$target_mname : " . _AM_BADMIN . "</h4>\n";
371
    list_blocks();
372
}
373
374
list_groups();
375
xoops_cp_footer();
376