myblocksadmin2.php ➔ list_blockinstances()   F
last analyzed

Complexity

Conditions 28
Paths 3042

Size

Total Lines 234

Duplication

Lines 44
Ratio 18.8 %

Importance

Changes 0
Metric Value
cc 28
nc 3042
nop 0
dl 44
loc 234
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_for_2.2.php                             //
4
//                - XOOPS block admin for each modules -                     //
5
//                          GIJOE <http://www.peak.ne.jp/>                   //
6
// ------------------------------------------------------------------------- //
7
8
defined('XOOPS_ROOT_PATH') || exit('XOOPS Root Path not defined');
9
10
require_once __DIR__ . '/../../../include/cp_header.php';
11
12
require_once __DIR__ . '/mygrouppermform.php';
13
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
14
require_once __DIR__ . '/../include/gtickets.php';
15
16
$xoops_system_path = XOOPS_ROOT_PATH . '/modules/system';
17
18
// language files
19
$language = $xoopsConfig['language'];
20
if (!file_exists("$xoops_system_path/language/$language/admin/blocksadmin.php")) {
21
    $language = 'english';
22
}
23
24
// to prevent from notice that constants already defined
25
$error_reporting_level = error_reporting(0);
26
require_once "$xoops_system_path/constants.php";
27
require_once "$xoops_system_path/language/$language/admin.php";
28
require_once "$xoops_system_path/language/$language/admin/blocksadmin.php";
29
error_reporting($error_reporting_level);
30
31
$group_defs = file("$xoops_system_path/language/$language/admin/groups.php");
32 View Code Duplication
foreach ($group_defs as $def) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    if (true === strpos($def, '_AM_APCAL_ACCESSRIGHTS') || true === strpos($def, '_AM_APCAL_ACTIVERIGHTS')) {
34
        eval($def);
35
    }
36
}
37
38
// check $xoopsModule
39
if (!is_object($xoopsModule)) {
40
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
41
}
42
43
// set target_module if specified by $_GET['dirname']
44
/** @var XoopsModuleHandler $moduleHandler */
45
$moduleHandler = xoops_getHandler('module');
46
if (!empty($_GET['dirname'])) {
47
    $target_module = $moduleHandler->getByDirname($_GET['dirname']);
48
}/* else if ( ! empty( $_GET['mid'] ) ) {
49
    $target_module = $moduleHandler->get( (int)( $_GET['mid'] ) );
50
}*/
51
52 View Code Duplication
if (!empty($target_module) && is_object($target_module)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    // specified by dirname
54
    $target_mid     = $target_module->getVar('mid');
55
    $target_mname   = $target_module->getVar('name') . '&nbsp;' . sprintf('(%2.2f)', $target_module->getVar('version') / 100.0);
56
    $query4redirect = '?dirname=' . urlencode(strip_tags($_GET['dirname']));
57
} elseif (isset($_GET['mid']) && $_GET['mid'] == 0 || $xoopsModule->getVar('dirname') === 'blocksadmin') {
58
    $target_mid     = 0;
59
    $target_mname   = '';
60
    $query4redirect = '?mid=0';
61
} else {
62
    $target_mid     = $xoopsModule->getVar('mid');
63
    $target_mname   = $xoopsModule->getVar('name');
64
    $query4redirect = '';
65
}
66
67
// check access right (needs system_admin of BLOCK)
68
$syspermHandler = xoops_getHandler('groupperm');
69 View Code Duplication
if (!$syspermHandler->checkRight('system_admin', XOOPS_SYSTEM_BLOCK, $xoopsUser->getGroups())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
71
}
72
73
// get blocks owned by the module (Imported from xoopsblock.php then modified)
74
$db        = XoopsDatabaseFactory::getDatabaseConnection();
75
$sql       = 'SELECT bid,name,show_func,func_file,template FROM ' . $db->prefix('newblocks') . " WHERE mid='$target_mid'";
76
$result    = $db->query($sql);
77
$block_arr = array();
78
while (list($bid, $bname, $show_func, $func_file, $template) = $db->fetchRow($result)) {
79
    $block_arr[$bid] = array(
80
        'name'      => $bname,
81
        'show_func' => $show_func,
82
        'func_file' => $func_file,
83
        'template'  => $template
84
    );
85
}
86
87
// for 2.2
88
function list_blockinstances()
89
{
90
    global $query4redirect, $block_arr, $xoopsGTicket;
91
92
    $myts = MyTextSanitizer::getInstance();
93
94
    // cachetime options
95
    $cachetimes = array(
96
        '0'       => _NOCACHE,
97
        '30'      => sprintf(_SECONDS, 30),
98
        '60'      => _MINUTE,
99
        '300'     => sprintf(_MINUTES, 5),
100
        '1800'    => sprintf(_MINUTES, 30),
101
        '3600'    => _HOUR,
102
        '18000'   => sprintf(_HOURS, 5),
103
        '86400'   => _DAY,
104
        '259200'  => sprintf(_DAYS, 3),
105
        '604800'  => _WEEK,
106
        '2592000' => _MONTH
107
    );
108
109
    // displaying TH
110
    echo "
111
    <form class='apcalForm' action='admin.php' name='blockadmin' method='post'>
112
        <table width='95%' class='outer' cellpadding='4' cellspacing='1'>
113
        <tr valign='middle'>
114
            <th>" . _AM_APCAL_TITLE . "</th>
115
            <th align='center' nowrap='nowrap'>" . _AM_APCAL_SIDE . "</th>
116
            <th align='center'>" . _AM_APCAL_WEIGHT . "</th>
117
            <th align='center'>" . _AM_APCAL_VISIBLEIN . "</th>
118
            <th align='center'>" . _AM_APCAL_BCACHETIME . "</th>
119
            <th align='right'>" . _AM_APCAL_ACTION . "</th>
120
        </tr>\n";
121
122
    // get block instances
123
    $crit     = new Criteria('bid', '(' . implode(',', array_keys($block_arr)) . ')', 'IN');
124
    $criteria = new CriteriaCompo($crit);
125
    $criteria->setSort('visible DESC, side ASC, weight');
126
    $instanceHandler = xoops_getHandler('blockinstance');
127
    $instances        = $instanceHandler->getObjects($criteria, true, true);
128
129
    //Get modules and pages for visible in
130
    $module_list[_AM_APCAL_SYSTEMLEVEL]['0-2'] = _AM_APCAL_ADMINBLOCK;
0 ignored issues
show
Coding Style Comprehensibility 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.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
131
    $module_list[_AM_APCAL_SYSTEMLEVEL]['0-1'] = _AM_APCAL_TOPPAGE;
132
    $module_list[_AM_APCAL_SYSTEMLEVEL]['0-0'] = _AM_APCAL_ALLPAGES;
133
    $criteria                                  = new CriteriaCompo(new Criteria('hasmain', 1));
134
    $criteria->add(new Criteria('isactive', 1));
135
    /** @var XoopsModuleHandler $moduleHandler */
136
    $moduleHandler = xoops_getHandler('module');
137
    $module_main   = $moduleHandler->getObjects($criteria, true);
138
    if (count($module_main) > 0) {
139
        foreach (array_keys($module_main) as $mid) {
140
            $module_list[$module_main[$mid]->getVar('name')][$mid . '-0'] = _AM_APCAL_ALLMODULEPAGES;
141
            $pages                                                        = $module_main[$mid]->getInfo('pages');
142
            if ($pages === false) {
143
                $pages = $module_main[$mid]->getInfo('sub');
144
            }
145
            if (is_array($pages) && $pages != array()) {
146
                foreach ($pages as $id => $pageinfo) {
147
                    $module_list[$module_main[$mid]->getVar('name')][$mid . '-' . $id] = $pageinfo['name'];
148
                }
149
            }
150
        }
151
    }
152
153
    // blocks displaying loop
154
    $class         = 'even';
155
    $block_configs = get_block_configs();
156
    foreach (array_keys($instances) as $i) {
157
        $sseln = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = '';
158
        $scoln = $scol0 = $scol1 = $scol2 = $scol3 = $scol4 = '#FFFFFF';
159
160
        $weight     = $instances[$i]->getVar('weight');
161
        $title      = $instances[$i]->getVar('title');
162
        $bcachetime = $instances[$i]->getVar('bcachetime');
163
        $bid        = $instances[$i]->getVar('bid');
164
        $name       = $myts->htmlSpecialChars($block_arr[$bid]['name']);
165
166
        $visiblein = $instances[$i]->getVisibleIn();
167
168
        // visible and side
169 View Code Duplication
        if ($instances[$i]->getVar('visible') != 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
            $sseln = ' checked';
171
            $scoln = '#FF0000';
172
        } else {
173
            switch ($instances[$i]->getVar('side')) {
174
                default:
175
                case XOOPS_SIDEBLOCK_LEFT:
176
                    $ssel0 = ' checked';
177
                    $scol0 = '#00FF00';
178
                    break;
179
                case XOOPS_SIDEBLOCK_RIGHT:
180
                    $ssel1 = ' checked';
181
                    $scol1 = '#00FF00';
182
                    break;
183
                case XOOPS_CENTERBLOCK_LEFT:
184
                    $ssel2 = ' checked';
185
                    $scol2 = '#00FF00';
186
                    break;
187
                case XOOPS_CENTERBLOCK_RIGHT:
188
                    $ssel4 = ' checked';
189
                    $scol4 = '#00FF00';
190
                    break;
191
                case XOOPS_CENTERBLOCK_CENTER:
192
                    $ssel3 = ' checked';
193
                    $scol3 = '#00FF00';
194
                    break;
195
            }
196
        }
197
198
        // bcachetime
199
        $cachetime_options = '';
200 View Code Duplication
        foreach ($cachetimes as $cachetime => $cachetime_name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
201
            if ($bcachetime == $cachetime) {
202
                $cachetime_options .= "<option value='$cachetime' selected>$cachetime_name</option>\n";
203
            } else {
204
                $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
205
            }
206
        }
207
208
        $module_options = '';
209
        foreach ($module_list as $mname => $module) {
210
            $module_options .= "<optgroup label='$mname'>\n";
211
            foreach ($module as $mkey => $mval) {
212
                if (in_array($mkey, $visiblein)) {
213
                    $module_options .= "<option value='$mkey' selected>$mval</option>\n";
214
                } else {
215
                    $module_options .= "<option label='$mval' value='$mkey'>$mval</option>\n";
216
                }
217
            }
218
            $module_options .= "</optgroup>\n";
219
        }
220
221
        // delete link if it is cloned block
222
        $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
Bug introduced by
The variable $mid seems to be defined by a foreach iteration on line 139. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
223
224
        // displaying part
225
        echo "
226
        <tr valign='middle'>
227
            <td class='$class'>
228
                $name
229
                <br>
230
                <input type='text' name='title[$i]' value='$title' size='20' />
231
            </td>
232
            <td class='$class' align='center' nowrap='nowrap' width='125px'>
233
                <div style='float:left;background-color:$scol0;'>
234
                    <input type='radio' name='side[$i]' value='" . XOOPS_SIDEBLOCK_LEFT . "' style='background-color:$scol0;' $ssel0 />
235
                </div>
236
                <div style='float:left;'>-</div>
237
                <div style='float:left;background-color:$scol2;'>
238
                    <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_LEFT . "' style='background-color:$scol2;' $ssel2 />
239
                </div>
240
                <div style='float:left;background-color:$scol3;'>
241
                    <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_CENTER . "' style='background-color:$scol3;' $ssel3 />
242
                </div>
243
                <div style='float:left;background-color:$scol4;'>
244
                    <input type='radio' name='side[$i]' value='" . XOOPS_CENTERBLOCK_RIGHT . "' style='background-color:$scol4;' $ssel4 />
245
                </div>
246
                <div style='float:left;'>-</div>
247
                <div style='float:left;background-color:$scol1;'>
248
                    <input type='radio' name='side[$i]' value='" . XOOPS_SIDEBLOCK_RIGHT . "' style='background-color:$scol1;' $ssel1 />
249
                </div>
250
                <br>
251
                <br>
252
                <div style='float:left;width:40px;'>&nbsp;</div>
253
                <div style='float:left;background-color:$scoln;'>
254
                    <input type='radio' name='side[$i]' value='-1' style='background-color:$scoln;' $sseln />
255
                </div>
256
                <div style='float:left;'>" . _NONE . "</div>
257
            </td>
258
            <td class='$class' align='center'>
259
                <input type='text' name=weight[$i] value='$weight' size='3' maxlength='5' style='text-align:right;' />
260
            </td>
261
            <td class='$class' align='center'>
262
                <select name='bmodule[$i][]' size='5' multiple='multiple'>
263
                    $module_options
264
                </select>
265
            </td>
266
            <td class='$class' align='center'>
267
                <select name='bcachetime[$i]' size='1'>
268
                    $cachetime_options
269
                </select>
270
            </td>
271
            <td class='$class' align='right'>
272
                <a href='" . XOOPS_URL . "/modules/system/admin.php?fct=blocksadmin&amp;op=edit&amp;id=$i'>" . _EDIT . "</a>{$delete_link}
273
                <input type='hidden' name='id[$i]' value='$i' />
274
            </td>
275
        </tr>\n";
276
277
        $class = ($class === 'even') ? 'odd' : 'even';
278
    }
279
280
    // list block classes for add (not instances)
281
    foreach ($block_arr as $bid => $block) {
282
        $description4show = '';
283 View Code Duplication
        foreach ($block_configs as $bconf) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
284
            if ($block['show_func'] == $bconf['show_func'] && $block['func_file'] == $bconf['file']
285
                && (empty($bconf['template']) || $block['template'] == $bconf['template'])
286
            ) {
287
                if (!empty($bconf['description'])) {
288
                    $description4show = $myts->htmlSpecialChars($bconf['description']);
289
                }
290
            }
291
        }
292
293
        echo "
294
        <tr>
295
            <td class='$class' align='left'>
296
                " . $myts->htmlSpecialChars($block['name']) . "
297
            </td>
298
            <td class='$class' align='left' colspan='4'>
299
                $description4show
300
            </td>
301
            <td class='$class' align='center'>
302
                <input type='submit' name='addblock[$bid]' value='" . _ADD . "' />
303
            </td>
304
        </tr>
305
        \n";
306
        $class = ($class === 'even') ? 'odd' : 'even';
307
    }
308
309
    echo "
310
        <tr>
311
            <td class='foot' align='center' colspan='6'>
312
                <input type='hidden' name='query4redirect' value='$query4redirect' />
313
                <input type='hidden' name='fct' value='blocksadmin' />
314
                <input type='hidden' name='op' value='order2' />
315
                " . $xoopsGTicket->getTicketHtml(__LINE__, 1800, 'myblocksadmin') . "
316
                <input type='submit' name='submit' value='" . _SUBMIT . "' />
317
            </td>
318
        </tr>
319
        </table>
320
    </form>\n";
321
}
322
323
// for 2.2
324
function list_groups2()
325
{
326
    global $target_mid, $target_mname, $xoopsDB;
327
328
    $result = $GLOBALS['xoopsDB']->query('SELECT i.instanceid,i.title FROM '
329
                                         . $GLOBALS['xoopsDB']->prefix('block_instance')
330
                                         . ' i LEFT JOIN '
331
                                         . $GLOBALS['xoopsDB']->prefix('newblocks')
332
                                         . " b ON i.bid=b.bid WHERE b.mid='$target_mid'");
333
334
    $item_list = array();
335
    while (list($iid, $title) = $GLOBALS['xoopsDB']->fetchRow($result)) {
336
        $item_list[$iid] = $title;
337
    }
338
339
    $form = new MyXoopsGroupPermForm(_AM_APCAL_ADGS, 1, 'block_read', '');
340 View Code Duplication
    if ($target_mid > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
        $form->addAppendix('module_admin', $target_mid, $target_mname . ' ' . _AM_APCAL_ACTIVERIGHTS);
342
        $form->addAppendix('module_read', $target_mid, $target_mname . ' ' . _AM_APCAL_ACCESSRIGHTS);
343
    }
344
    foreach ($item_list as $item_id => $item_name) {
345
        $form->addItem($item_id, $item_name);
346
    }
347
    echo $form->render();
348
}
349
350 View Code Duplication
if (!empty($_POST['submit'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
351
    if (!$xoopsGTicket->check(true, 'myblocksadmin')) {
352
        redirect_header(XOOPS_URL . '/', 3, $xoopsGTicket->getErrors());
353
    }
354
355
    include __DIR__ . '/mygroupperm.php';
356
    redirect_header(XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/admin/myblocksadmin.php$query4redirect", 1, _AM_APCALAM_APCALDBUPDATED);
357
}
358
359
xoops_cp_header();
360
if (file_exists('./mymenu.php')) {
361
    include __DIR__ . '/mymenu.php';
362
}
363
364
echo "<h3 style='text-align:left;'>$target_mname</h3>\n";
365
366
if (!empty($block_arr)) {
367
    echo "<h4 style='text-align:left;'>" . _AM_APCAL_BADMIN . "</h4>\n";
368
    list_blockinstances();
369
}
370
371
list_groups2();
372
xoops_cp_footer();
373