Completed
Push — master ( 3a6ebc...3a5a05 )
by Michael
04:13
created

block_functions.php ➔ b_altsys_admin_menu_show()   F

Complexity

Conditions 30
Paths 4364

Size

Total Lines 131
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 30
eloc 91
c 5
b 0
f 0
nc 4364
nop 1
dl 0
loc 131
rs 2

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
require_once dirname(__DIR__).'/include/altsys_functions.php' ;
4
5
function b_altsys_admin_menu_show($options)
6
{
7
    global $xoopsUser ;
8
9
    $mydirname = empty($options[0]) ? 'altsys' : $options[0] ;
10
    $this_template = empty($options[1]) ? 'db:'.$mydirname.'_block_admin_menu.html' : trim($options[1]) ;
11
12
    if (preg_match('/[^0-9a-zA-Z_-]/', $mydirname)) {
13
        die('Invalid mydirname') ;
14
    }
15
    if (! is_object(@$xoopsUser)) {
16
        return array() ;
17
    }
18
19
    // coretype
20
    $coretype = altsys_get_core_type() ;
21
22
    // mid_selected
23
    if (is_object(@$GLOBALS['xoopsModule'])) {
24
        $mid_selected = $GLOBALS['xoopsModule']->getVar('mid') ;
25
        // for system->preferences
26
        if ($mid_selected == 1 && @$_GET['fct'] == 'preferences' && @$_GET['op'] == 'showmod' && ! empty($_GET['mod'])) {
27
            $mid_selected = (int)$_GET['mod'];
28
        }
29
    } else {
30
        $mid_selected = 0 ;
31
    }
32
33
    $db = XoopsDatabaseFactory::getDatabaseConnection();
34
    (method_exists('MyTextSanitizer', 'sGetInstance') and $myts =& MyTextSanitizer::sGetInstance()) || $myts = MyTextSanitizer::getInstance();
35
36
    $module_handler = xoops_gethandler('module');
37
    $current_module =& $module_handler->getByDirname($mydirname);
38
    $config_handler = xoops_gethandler('config');
39
    $current_configs = $config_handler->getConfigList($current_module->mid()) ;
40
    $moduleperm_handler = xoops_gethandler('groupperm');
41
    $admin_mids = $moduleperm_handler->getItemIds('module_admin', $xoopsUser->getGroups());
42
    $modules = $module_handler->getObjects(new Criteria('mid', '('.implode(',', $admin_mids) . ')', 'IN'), true) ;
43
44
    $block = array(
45
        'mydirname' => $mydirname ,
46
        'mod_url' => XOOPS_URL.'/modules/'.$mydirname ,
47
        'mod_imageurl' => XOOPS_URL.'/modules/'.$mydirname.'/'.$current_configs['images_dir'] ,
48
        'mod_config' => $current_configs
49
    ) ;
50
51
    foreach ($modules as $mod) {
52
        $mid = (int)$mod->getVar('mid');
53
        $dirname = $mod->getVar('dirname') ;
54
        $modinfo = $mod->getInfo() ;
55
        $submenus4assign = array() ;
56
        $adminmenu = array() ;
57
        $adminmenu4altsys = array() ;
58
        unset($adminmenu_use_altsys) ;
59
        @include XOOPS_ROOT_PATH.'/modules/'.$dirname.'/'.@$modinfo['adminmenu'] ;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
60
        // from admin_menu.php etc.
61
        $adminmenu = array_merge($adminmenu, $adminmenu4altsys) ;
62
        foreach ($adminmenu as $sub) {
63
            $link = empty($sub['altsys_link']) ? $sub['link'] : $sub['altsys_link'] ;
64
            if (isset($sub['show']) && $sub['show'] === false) {
65
                continue ;
66
            }
67
            $submenus4assign[] = array(
68
                'title' => $myts->makeTboxData4Show($sub['title']) ,
69
                'url' => XOOPS_URL.'/modules/'.$dirname.'/'.htmlspecialchars($link, ENT_QUOTES)
70
            ) ;
71
        }
72
73
        // for modules overriding Module.class.php (eg. Analyzer for XC)
74
        if (empty($submenus4assign) && defined('XOOPS_CUBE_LEGACY') && ! empty($modinfo['cube_style'])) {
75
            $module_handler = xoops_gethandler('module');
76
            $module = $module_handler->get($mid);
77
            $moduleObj =& Legacy_Utils::createModule($module);
78
            $modinfo['adminindex'] = $moduleObj->getAdminIndex() ;
79
            $modinfo['adminindex_absolute'] = true ;
80
            foreach ($moduleObj->getAdminMenu() as $sub) {
81
                if (@$sub['show'] === false) {
82
                    continue ;
83
                }
84
                $submenus4assign[] = array(
85
                    'title' => $myts->makeTboxData4Show($sub['title']) ,
86
                    'url' => strncmp($sub['link'], 'http', 4) === 0 ? htmlspecialchars($sub['link'], ENT_QUOTES) : XOOPS_URL.'/modules/'.$dirname.'/'.htmlspecialchars($sub['link'], ENT_QUOTES)
87
                ) ;
88
            }
89
        } elseif (empty($adminmenu4altsys)) {
90
91
            // add preferences
92
            if ($mod->getVar('hasconfig') && ! in_array($mod->getVar('dirname'), array( 'system', 'legacy' ))) {
93
                $submenus4assign[] = array(
94
                    'title' => _PREFERENCES ,
95
                    'url' => htmlspecialchars(altsys_get_link2modpreferences($mid, $coretype), ENT_QUOTES)
96
                ) ;
97
            }
98
99
            // add help
100
            if (defined('XOOPS_CUBE_LEGACY') && ! empty($modinfo['help'])) {
101
                $submenus4assign[] = array(
102
                    'title' => _HELP ,
103
                    'url' => XOOPS_URL.'/modules/legacy/admin/index.php?action=Help&amp;dirname='.$dirname
104
                ) ;
105
            }
106
        }
107
108
        $module4assign = array(
109
            'mid' => $mid ,
110
            'dirname' => $dirname ,
111
            'name' => $mod->getVar('name') ,
112
            'version_in_db' => sprintf('%.2f', $mod->getVar('version') / 100.0) ,
113
            'version_in_file' => sprintf('%.2f', $modinfo['version']) ,
114
            'description' => htmlspecialchars(@$modinfo['description'], ENT_QUOTES) ,
115
            'image' => htmlspecialchars($modinfo['image'], ENT_QUOTES) ,
116
            'isactive' => $mod->getVar('isactive') ,
117
            'hasmain' => $mod->getVar('hasmain') ,
118
            'hasadmin' => $mod->getVar('hasadmin') ,
119
            'hasconfig' => $mod->getVar('hasconfig') ,
120
            'weight' => $mod->getVar('weight') ,
121
            'adminindex' => htmlspecialchars(@$modinfo['adminindex'], ENT_QUOTES) ,
122
            'adminindex_absolute' => @$modinfo['adminindex_absolute'] ,
123
            'submenu' => $submenus4assign ,
124
            'selected' => $mid == $mid_selected ? true : false ,
125
            'dot_suffix' => $mid == $mid_selected ? 'selected_opened' : 'closed'
126
        ) ;
127
        $block['modules'][] = $module4assign ;
128
    }
129
130
    require_once XOOPS_TRUST_PATH.'/libs/altsys/class/D3Tpl.class.php' ;
131
    $tpl = new D3Tpl() ;
132
    $tpl->assign('block', $block) ;
133
    $ret['content'] = $tpl->fetch($this_template) ;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ret was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ret = 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...
134
    return $ret ;
135
}
136
137
138
function b_altsys_admin_menu_edit($options)
139
{
140
    $mydirname = empty($options[0]) ? 'd3forum' : $options[0] ;
141
    $this_template = empty($options[1]) ? 'db:'.$mydirname.'_block_admin_menu.html' : trim($options[1]) ;
142
143
    if (preg_match('/[^0-9a-zA-Z_-]/', $mydirname)) {
144
        die('Invalid mydirname') ;
145
    }
146
147
    $form = "
148
		<input type='hidden' name='options[0]' value='$mydirname' />
149
		<label for='this_template'>"._MB_ALTSYS_THISTEMPLATE."</label>&nbsp;:
150
		<input type='text' size='60' name='options[1]' id='this_template' value='".htmlspecialchars($this_template, ENT_QUOTES)."' />
151
		<br />
152
	\n" ;
153
154
    return $form;
155
}
156