Completed
Push — master ( 6c5294...348995 )
by Michael
02:27
created

myblocksadmin.php ➔ list_blocks()   F

Complexity

Conditions 14
Paths 253

Size

Total Lines 143
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 90
nc 253
nop 0
dl 0
loc 143
rs 3.7522
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 57 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
// ------------------------------------------------------------------------- //
10
//                            myblocksadmin.php                              //
11
//                - XOOPS block admin for each modules -                     //
12
//                          GIJOE <http://www.peak.ne.jp/>                   //
13
// ------------------------------------------------------------------------- //
14
15
include_once dirname(dirname(dirname(__DIR__))) . '/include/cp_header.php';
16
include_once 'mygrouppermform.php';
17
include_once(XOOPS_ROOT_PATH . '/class/xoopsblock.php');
18
include_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->dirname() . '/include/functions.php';
19
20
$xoops_system_path = XOOPS_ROOT_PATH . '/modules/system';
21
22
// language files
23
$language = $xoopsConfig['language'];
24
if (!file_exists("$xoops_system_path/language/$language/admin/blocksadmin.php")) {
25
    $language = 'english';
26
}
27
28
// to prevent from notice that constants already defined
29
$error_reporting_level = error_reporting(0);
30
include_once("$xoops_system_path/constants.php");
31
include_once("$xoops_system_path/language/$language/admin.php");
32
include_once("$xoops_system_path/language/$language/admin/blocksadmin.php");
33
include_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/functions.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 !== strpos($def, '_AM_ACCESSRIGHTS') || false !== strpos($def, '_AM_ACTIVERIGHTS')) {
39
        eval($def);
40
    }
41
}
42
43
// check $xoopsModule
44
if (!is_object($xoopsModule)) {
45
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
46
}
47
48
// check access right (needs system_admin of BLOCK)
49
$syspermHandler = xoops_getHandler('groupperm');
50
if (!$syspermHandler->checkRight('system_admin', XOOPS_SYSTEM_BLOCK, $xoopsUser->getGroups())) {
51
    redirect_header(XOOPS_URL . '/user.php', 1, _NOPERM);
52
}
53
54
// get blocks owned by the module
55
$block_arr = XoopsBlock::getByModule($xoopsModule->mid());
56
57
function list_blocks()
58
{
59
    global $block_arr, $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
60
61
    // cachetime options
62
    $cachetimes = array('0' => _NOCACHE, '30' => sprintf(_SECONDS, 30), '60' => _MINUTE, '300' => sprintf(_MINUTES, 5), '1800' => sprintf(_MINUTES, 30), '3600' => _HOUR, '18000' => sprintf(_HOURS, 5), '86400' => _DAY, '259200' => sprintf(_DAYS, 3), '604800' => _WEEK, '2592000' => _MONTH);
63
64
    // displaying TH
65
    sf_collapsableBar('toptable', 'toptableicon');
66
    echo "<img id='toptableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . _AM_SF_BLOCKS . '</h3>';
67
    echo "<div id='toptable'>";
68
    echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . _AM_SF_BLOCKSTXT . '</span>';
69
70
    echo "
71
    <form action='admin.php' name='blockadmin' method='post'>
72
        <table width='100%' class='outer' cellpadding='4' cellspacing='1'>
73
        <tr valign='middle'>
74
            <th>" . _AM_TITLE . "</th>
75
            <th align='center' nowrap='nowrap'>" . _AM_SF_POSITION . "</th>
76
            <th align='center'>" . _AM_WEIGHT . "</th>
77
            <th align='center'>" . _AM_VISIBLEIN . "</th>
78
            <th align='center'>" . _AM_BCACHETIME . "</th>
79
            <th align='center'>" . _AM_ACTION . "</th>
80
        </tr>\n";
81
82
    // blocks displaying loop
83
    $class = 'even';
84
    foreach (array_keys($block_arr) as $i) {
85
        $sseln = $ssel0 = $ssel1 = $ssel2 = $ssel3 = $ssel4 = '';
86
87
        $weight     = $block_arr[$i]->getVar('weight');
88
        $title      = $block_arr[$i]->getVar('title');
89
        $name       = $block_arr[$i]->getVar('name');
90
        $bcachetime = $block_arr[$i]->getVar('bcachetime');
91
        $bid        = $block_arr[$i]->getVar('bid');
92
93
        // visible and side
94
        if ($block_arr[$i]->getVar('visible') != 1) {
95
            $sseln = " checked='checked' style='background-color:#FF0000;'";
96
        } else {
97
            switch ($block_arr[$i]->getVar('side')) {
98
                default:
99
                case XOOPS_SIDEBLOCK_LEFT:
100
                    $ssel0 = " checked='checked' style='background-color:#00FF00;'";
101
                    break;
102
                case XOOPS_SIDEBLOCK_RIGHT:
103
                    $ssel1 = " checked='checked' style='background-color:#00FF00;'";
104
                    break;
105
                case XOOPS_CENTERBLOCK_LEFT:
106
                    $ssel2 = " checked='checked' style='background-color:#00FF00;'";
107
                    break;
108
                case XOOPS_CENTERBLOCK_RIGHT:
109
                    $ssel4 = " checked='checked' style='background-color:#00FF00;'";
110
                    break;
111
                case XOOPS_CENTERBLOCK_CENTER:
112
                    $ssel3 = " checked='checked' style='background-color:#00FF00;'";
113
                    break;
114
            }
115
        }
116
117
        // bcachetime
118
        $cachetime_options = '';
119
        foreach ($cachetimes as $cachetime => $cachetime_name) {
120
            if ($bcachetime == $cachetime) {
121
                $cachetime_options .= "<option value='$cachetime' selected='selected'>$cachetime_name</option>\n";
122
            } else {
123
                $cachetime_options .= "<option value='$cachetime'>$cachetime_name</option>\n";
124
            }
125
        }
126
127
        // target modules
128
        $db            = XoopsDatabaseFactory::getDatabaseConnection();
129
        $result        = $db->query('SELECT module_id FROM ' . $db->prefix('block_module_link') . " WHERE block_id='$bid'");
130
        $selected_mids = array();
131
        while (list($selected_mid) = $db->fetchRow($result)) {
132
            $selected_mids[] = (int)$selected_mid;
133
        }
134
        $moduleHandler = xoops_getHandler('module');
135
        $criteria      = new CriteriaCompo(new Criteria('hasmain', 1));
136
        $criteria->add(new Criteria('isactive', 1));
137
        $module_list     = $moduleHandler->getList($criteria);
138
        $module_list[-1] = _AM_TOPPAGE;
139
        $module_list[0]  = _AM_ALLPAGES;
140
        ksort($module_list);
141
        $module_options = '';
142
        $myts           = MyTextSanitizer::getInstance();
143
        foreach ($module_list as $mid => $mname) {
144
            if (in_array($mid, $selected_mids)) {
145
                $module_options .= "<option value='$mid' selected='selected'>" . $myts->displayTarea($mname) . "</option>\n";
146
            } else {
147
                $module_options .= "<option value='$mid'>" . $myts->displayTarea($mname) . "</option>\n";
148
            }
149
        }
150
151
        // displaying part
152
        echo "
153
        <tr valign='middle'>
154
            <td class='$class'>
155
                $name
156
                <br />
157
                <input type='text' name='title[$bid]' value='$title' size='20' />
158
            </td>
159
            <td class='$class' align='center' nowrap='nowrap'>
160
                <input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_LEFT . "'$ssel0 />-<input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_LEFT . "'$ssel2 /><input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_CENTER . "'$ssel3 /><input type='radio' name='side[$bid]' value='" . XOOPS_CENTERBLOCK_RIGHT . "'$ssel4 />-<input type='radio' name='side[$bid]' value='" . XOOPS_SIDEBLOCK_RIGHT . "'$ssel1 />
161
                <br />
162
                <br />
163
                <input type='radio' name='side[$bid]' value='-1'$sseln />
164
                " . _NONE . "
165
            </td>
166
            <td class='$class' align='center'>
167
                <input type='text' name=weight[$bid] value='$weight' size='5' maxlength='5' style='text-align:right;' />
168
            </td>
169
            <td class='$class' align='center'>
170
                <select name='bmodule[$bid][]' size='5' multiple='multiple'>
171
                    $module_options
172
                </select>
173
            </td>
174
            <td class='$class' align='center'>
175
                <select name='bcachetime[$bid]' size='1'>
176
                    $cachetime_options
177
                </select>
178
            </td>
179
            <td class='$class' align='center'>
180
                <a href='admin.php?fct=blocksadmin&amp;op=edit&amp;bid=$bid'>" . _EDIT . "</a>
181
                <input type='hidden' name='bid[$bid]' value='$bid' />
182
            </td>
183
        </tr>\n";
184
185
        $class = ($class === 'even') ? 'odd' : 'even';
186
    }
187
188
    echo "
189
        <tr>
190
            <td class='foot' align='center' colspan='6'>
191
                <input type='hidden' name='fct' value='blocksadmin' />
192
                <input type='hidden' name='op' value='order' />
193
                <input type='submit' name='submit' value='" . _SUBMIT . "' />
194
            </td>
195
        </tr>
196
        </table>
197
    </form>\n";
198
    echo '</div>';
199
}
200
201
function list_groups()
202
{
203
    global $xoopsModule, $block_arr;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
204
    $myts = MyTextSanitizer::getInstance();
205
206
    sf_collapsableBar('bottomtable', 'bottomtableicon');
207
208
    foreach (array_keys($block_arr) as $i) {
209
        $item_list[$block_arr[$i]->getVar('bid')] = $block_arr[$i]->getVar('title');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item_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...
210
    }
211
212
    $form = new MyXoopsGroupPermForm('', 1, 'block_read', "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . _AM_SF_GROUPS . "</h3><div id='bottomtable'><span style=\"color: #567; margin: 3px 0 0 0; font-size: small; display: block; \">" . _AM_SF_GROUPSINFO . '</span>');
213
    $form->addAppendix('module_admin', $xoopsModule->mid(), $xoopsModule->name() . ' ' . _AM_ACTIVERIGHTS);
214
    $form->addAppendix('module_read', $xoopsModule->mid(), $xoopsModule->name() . ' ' . _AM_ACCESSRIGHTS);
215
    foreach ($item_list as $item_id => $item_name) {
0 ignored issues
show
Bug introduced by
The variable $item_list does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
216
        $form->addItem($item_id, $myts->displayTarea($item_name));
217
    }
218
    echo $form->render();
219
    echo '</div>';
220
}
221
222
if (!empty($_POST['submit'])) {
223
    include __DIR__ . '/mygroupperm.php';
224
    include_once("$xoops_system_path/language/$language/admin.php");
225
    redirect_header(XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/admin/myblocksadmin.php', 1, _AM_DBUPDATED);
226
}
227
228
xoops_cp_header();
229
if (file_exists('./mymenu.php')) {
230
    include('./mymenu.php');
231
}
232
233
list_blocks();
234
list_groups();
235
xoops_cp_footer();
236