myblocksadmin.php ➔ list_blocks()   F
last analyzed

Complexity

Conditions 25
Paths 2017

Size

Total Lines 208

Duplication

Lines 45
Ratio 21.63 %

Importance

Changes 0
Metric Value
cc 25
nc 2017
nop 0
dl 45
loc 208
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
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright   {@link http://xoops.org/ XOOPS Project}
14
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
15
 * @package
16
 * @since
17
 * @author       XOOPS Development Team,
18
 * @author       GIJ=CHECKMATE (PEAK Corp. http://www.peak.ne.jp/)
19
 */
20
21
require_once __DIR__ . '/../../../include/cp_header.php';
22
23
if (substr(XOOPS_VERSION, 6, 3) > 2.0) {
24
    include __DIR__ . '/myblocksadmin2.php';
25
    exit;
26
}
27
28
require_once __DIR__ . '/mygrouppermform.php';
29
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';
30
require_once __DIR__ . '/../include/gtickets.php';// GIJ
31
32
$xoops_system_path = XOOPS_ROOT_PATH . '/modules/system';
33
34
// language files
35
$language = $xoopsConfig['language'];
36
if (!file_exists("$xoops_system_path/language/$language/admin/blocksadmin.php")) {
37
    $language = 'english';
38
}
39
40
// to prevent from notice that constants already defined
41
$error_reporting_level = error_reporting(0);
42
require_once "$xoops_system_path/constants.php";
43
require_once "$xoops_system_path/language/$language/admin.php";
44
require_once "$xoops_system_path/language/$language/admin/blocksadmin.php";
45
error_reporting($error_reporting_level);
46
47
$group_defs = file("$xoops_system_path/language/$language/admin/groups.php");
48 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...
49
    if (true === strpos($def, '_AM_APCAL_ACCESSRIGHTS') || true === strpos($def, '_AM_APCAL_ACTIVERIGHTS')) {
50
        eval($def);
51
    }
52
}
53
54
// check $xoopsModule
55
if (!is_object($xoopsModule)) {
56
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
57
}
58
59
// set target_module if specified by $_GET['dirname']
60
/** @var XoopsModuleHandler $moduleHandler */
61
$moduleHandler = xoops_getHandler('module');
62
if (!empty($_GET['dirname'])) {
63
    $target_module = $moduleHandler->getByDirname($_GET['dirname']);
64
}/* else if ( ! empty( $_GET['mid'] ) ) {
65
    $target_module = $moduleHandler->get( (int)( $_GET['mid'] ) );
66
}*/
67
68 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...
69
    // specified by dirname
70
    $target_mid     = $target_module->getVar('mid');
71
    $target_mname   = $target_module->getVar('name') . '&nbsp;' . sprintf('(%2.2f)', $target_module->getVar('version') / 100.0);
72
    $query4redirect = '?dirname=' . urlencode(strip_tags($_GET['dirname']));
73
} elseif (isset($_GET['mid']) && $_GET['mid'] == 0 || $xoopsModule->getVar('dirname') === 'blocksadmin') {
74
    $target_mid     = 0;
75
    $target_mname   = '';
76
    $query4redirect = '?mid=0';
77
} else {
78
    $target_mid     = $xoopsModule->getVar('mid');
79
    $target_mname   = $xoopsModule->getVar('name');
80
    $query4redirect = '';
81
}
82
83
// check access right (needs system_admin of BLOCK)
84
$syspermHandler = xoops_getHandler('groupperm');
85 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...
86
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
87
}
88
89
// get blocks owned by the module (Imported from xoopsblock.php then modified)
90
//$block_arr = XoopsBlock::getByModule( $target_mid ) ;
91
$db        = XoopsDatabaseFactory::getDatabaseConnection();
92
$sql       = 'SELECT * FROM ' . $db->prefix('newblocks') . " WHERE mid='$target_mid' ORDER BY visible DESC,side,weight";
93
$result    = $db->query($sql);
94
$block_arr = array();
95
while ($myrow = $db->fetchArray($result)) {
96
    $block_arr[] = new XoopsBlock($myrow);
97
}
98
99
function list_blocks()
100
{
101
    global $query4redirect, $block_arr, $xoopsGTicket;
102
103
    // cachetime options
104
    $cachetimes = array(
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 class='apcalForm' action='admin.php' name='blockadmin' method='post'>
121
        <table width='95%' class='outer' cellpadding='4' cellspacing='1'>
122
        <tr valign='middle'>
123
            <th>" . _AM_APCAL_TITLE . "</th>
124
            <th align='center' nowrap='nowrap'>" . _AM_APCAL_SIDE . "</th>
125
            <th align='center'>" . _AM_APCAL_WEIGHT . "</th>
126
            <th align='center'>" . _AM_APCAL_VISIBLEIN . "</th>
127
            <th align='center'>" . _AM_APCAL_BCACHETIME . "</th>
128
            <th align='right'>" . _AM_APCAL_ACTION . "</th>
129
        </tr>\n";
130
131
    // blocks displaying loop
132
    $class         = 'even';
133
    $block_configs = get_block_configs();
134
    foreach (array_keys($block_arr) as $i) {
135
        $sseln = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = '';
136
        $scoln = $scol0 = $scol1 = $scol2 = $scol3 = $scol4 = '#FFFFFF';
137
138
        $weight     = $block_arr[$i]->getVar('weight');
139
        $title      = $block_arr[$i]->getVar('title');
140
        $name       = $block_arr[$i]->getVar('name');
141
        $bcachetime = $block_arr[$i]->getVar('bcachetime');
142
        $bid        = $block_arr[$i]->getVar('bid');
143
144
        // visible and side
145 View Code Duplication
        if ($block_arr[$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...
146
            $sseln = ' checked';
147
            $scoln = '#FF0000';
148
        } else {
149
            switch ($block_arr[$i]->getVar('side')) {
150
                default:
151
                case XOOPS_SIDEBLOCK_LEFT:
152
                    $ssel0 = ' checked';
153
                    $scol0 = '#00FF00';
154
                    break;
155
                case XOOPS_SIDEBLOCK_RIGHT:
156
                    $ssel1 = ' checked';
157
                    $scol1 = '#00FF00';
158
                    break;
159
                case XOOPS_CENTERBLOCK_LEFT:
160
                    $ssel2 = ' checked';
161
                    $scol2 = '#00FF00';
162
                    break;
163
                case XOOPS_CENTERBLOCK_RIGHT:
164
                    $ssel4 = ' checked';
165
                    $scol4 = '#00FF00';
166
                    break;
167
                case XOOPS_CENTERBLOCK_CENTER:
168
                    $ssel3 = ' checked';
169
                    $scol3 = '#00FF00';
170
                    break;
171
            }
172
        }
173
174
        // bcachetime
175
        $cachetime_options = '';
176 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...
177
            if ($bcachetime == $cachetime) {
178
                $cachetime_options .= "<option value='$cachetime' selected>$cachetime_name</option>\n";
179
            } else {
180
                $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
181
            }
182
        }
183
184
        // target modules
185
        $db            = XoopsDatabaseFactory::getDatabaseConnection();
186
        $result        = $db->query('SELECT module_id FROM ' . $db->prefix('block_module_link') . " WHERE block_id='$bid'");
187
        $selected_mids = array();
188
        while (list($selected_mid) = $db->fetchRow($result)) {
189
            $selected_mids[] = (int)$selected_mid;
190
        }
191
        /** @var XoopsModuleHandler $moduleHandler */
192
        $moduleHandler = xoops_getHandler('module');
193
        $criteria      = new CriteriaCompo(new Criteria('hasmain', 1));
194
        $criteria->add(new Criteria('isactive', 1));
195
        $module_list     = $moduleHandler->getList($criteria);
196
        $module_list[-1] = _AM_APCAL_TOPPAGE;
197
        $module_list[0]  = _AM_APCAL_ALLPAGES;
198
        ksort($module_list);
199
        $module_options = '';
200
        foreach ($module_list as $mid => $mname) {
201
            if (in_array($mid, $selected_mids)) {
202
                $module_options .= "<option value='$mid' selected>$mname</option>\n";
203
            } else {
204
                $module_options .= "<option value='$mid'>$mname</option>\n";
205
            }
206
        }
207
208
        // delete link if it is cloned block
209
        if ($block_arr[$i]->getVar('block_type') === 'D' || $block_arr[$i]->getVar('block_type') === 'C') {
210
            $delete_link = "<br><a href='admin.php?fct=blocksadmin&amp;op=delete&amp;bid=$bid'>" . _DELETE . '</a>';
211
        } else {
212
            $delete_link = '';
213
        }
214
215
        // clone link if it is marked as cloneable block
216
        // $modversion['blocks'][n]['can_clone']
217
        if ($block_arr[$i]->getVar('block_type') === 'D' || $block_arr[$i]->getVar('block_type') === 'C') {
218
            $can_clone = true;
219
        } else {
220
            $can_clone = false;
221 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...
222
                if ($block_arr[$i]->getVar('show_func') == $bconf['show_func']
223
                    && $block_arr[$i]->getVar('func_file') == $bconf['file']
224
                    && (empty($bconf['template']) || $block_arr[$i]->getVar('template') == $bconf['template'])
225
                ) {
226
                    if (!empty($bconf['can_clone'])) {
227
                        $can_clone = true;
228
                    }
229
                }
230
            }
231
        }
232
        if ($can_clone) {
233
            $clone_link = "<br><a href='admin.php?fct=blocksadmin&amp;op=clone&amp;bid=$bid'>" . _CLONE . '</a>';
234
        } else {
235
            $clone_link = '';
236
        }
237
238
        // displaying part
239
        echo "
240
        <tr valign='middle'>
241
            <td class='$class'>
242
                $name
243
                <br>
244
                <input type='text' name='title[$bid]' value='$title' size='20' />
245
            </td>
246
            <td class='$class' align='center' nowrap='nowrap' width='125px'>
247
                <div style='float:left;background-color:$scol0;'>
248
                    <input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_LEFT . "' style='background-color:$scol0;' $ssel0 />
249
                </div>
250
                <div style='float:left;'>-</div>
251
                <div style='float:left;background-color:$scol2;'>
252
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_LEFT . "' style='background-color:$scol2;' $ssel2 />
253
                </div>
254
                <div style='float:left;background-color:$scol3;'>
255
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_CENTER . "' style='background-color:$scol3;' $ssel3 />
256
                </div>
257
                <div style='float:left;background-color:$scol4;'>
258
                    <input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_RIGHT . "' style='background-color:$scol4;' $ssel4 />
259
                </div>
260
                <div style='float:left;'>-</div>
261
                <div style='float:left;background-color:$scol1;'>
262
                    <input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_RIGHT . "' style='background-color:$scol1;' $ssel1 />
263
                </div>
264
                <br>
265
                <br>
266
                <div style='float:left;width:40px;'>&nbsp;</div>
267
                <div style='float:left;background-color:$scoln;'>
268
                    <input type='radio' name='side[$bid]' value='-1' style='background-color:$scoln;' $sseln />
269
                </div>
270
                <div style='float:left;'>" . _NONE . "</div>
271
            </td>
272
            <td class='$class' align='center'>
273
                <input type='text' name=weight[$bid] value='$weight' size='3' maxlength='5' style='text-align:right;' />
274
            </td>
275
            <td class='$class' align='center'>
276
                <select name='bmodule[$bid][]' size='5' multiple='multiple'>
277
                    $module_options
278
                </select>
279
            </td>
280
            <td class='$class' align='center'>
281
                <select name='bcachetime[$bid]' size='1'>
282
                    $cachetime_options
283
                </select>
284
            </td>
285
            <td class='$class' align='right'>
286
                <a href='admin.php?fct=blocksadmin&amp;op=edit&amp;bid=$bid'>" . _EDIT . "</a>{$delete_link}{$clone_link}
287
                <input type='hidden' name='bid[$bid]' value='$bid' />
288
            </td>
289
        </tr>\n";
290
291
        $class = ($class === 'even') ? 'odd' : 'even';
292
    }
293
294
    echo "
295
        <tr>
296
            <td class='foot' align='center' colspan='6'>
297
                <input type='hidden' name='query4redirect' value='$query4redirect' />
298
                <input type='hidden' name='fct' value='blocksadmin' />
299
                <input type='hidden' name='op' value='order' />
300
                " . $xoopsGTicket->getTicketHtml(__LINE__, 1800, 'myblocksadmin') . "
301
                <input type='submit' name='submit' value='" . _SUBMIT . "' />
302
            </td>
303
        </tr>
304
        </table>
305
    </form>\n";
306
}
307
308
/**
309
 * @return array
310
 */
311
function get_block_configs()
312
{
313
    $error_reporting_level = error_reporting(0);
314
    if (preg_match('/^[.0-9a-zA-Z_-]+$/', @$_GET['dirname'])) {
315
        include __DIR__ . '/../../' . $_GET['dirname'] . '/xoops_version.php';
316
    } else {
317
        include __DIR__ . '/../xoops_version.php';
318
    }
319
    error_reporting($error_reporting_level);
320
    if (empty($modversion['blocks'])) {
0 ignored issues
show
Bug introduced by
The variable $modversion seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
321
        return array();
322
    } else {
323
        return $modversion['blocks'];
324
    }
325
}
326
327
function list_groups()
328
{
329
    global $target_mid, $target_mname, $block_arr;
330
331
    $item_list = array();
332
    foreach (array_keys($block_arr) as $i) {
333
        $item_list[$block_arr[$i]->getVar('bid')] = $block_arr[$i]->getVar('title');
334
    }
335
336
    $form = new MyXoopsGroupPermForm(_AM_APCAL_ADGS, 1, 'block_read', '');
337 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...
338
        $form->addAppendix('module_admin', $target_mid, $target_mname . ' ' . _AM_APCAL_ACTIVERIGHTS);
339
        $form->addAppendix('module_read', $target_mid, $target_mname . ' ' . _AM_APCAL_ACCESSRIGHTS);
340
    }
341
    foreach ($item_list as $item_id => $item_name) {
342
        $form->addItem($item_id, $item_name);
343
    }
344
    echo $form->render();
345
}
346
347 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...
348
    if (!$xoopsGTicket->check(true, 'myblocksadmin')) {
349
        redirect_header(XOOPS_URL . '/', 3, $xoopsGTicket->getErrors());
350
    }
351
352
    include __DIR__ . '/mygroupperm.php';
353
    redirect_header(XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/admin/myblocksadmin.php$query4redirect", 1, _AM_APCALAM_APCALDBUPDATED);
354
}
355
356
xoops_cp_header();
357
358
echo "<h3 style='text-align:left;'>$target_mname</h3>\n";
359
360
if (!empty($block_arr)) {
361
    echo "<h4 style='text-align:left;'>" . _AM_APCAL_BADMIN . "</h4>\n";
362
    list_blocks();
363
}
364
365
list_groups();
366
xoops_cp_footer();
367