views.php ➔ userlog_views_edit()   C
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 93
Code Lines 68

Duplication

Lines 6
Ratio 6.45 %

Importance

Changes 0
Metric Value
cc 9
eloc 68
nc 10
nop 1
dl 6
loc 93
rs 5.0669
c 0
b 0
f 0

How to fix   Long Method   

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 28 and the first side effect is on line 22.

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
 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
 *  userlog module
13
 *
14
 * @copyright       XOOPS Project (https://xoops.org)
15
 * @license         GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
16
 * @package         userlog blocks
17
 * @since           1
18
 * @author          irmtfan ([email protected])
19
 * @author          XOOPS Project <www.xoops.org> <www.xoops.ir>
20
 */
21
22
defined('XOOPS_ROOT_PATH') || exit('Restricted access.');
23
require_once __DIR__ . '/../include/common.php';
24
25
if (defined('USERLOG_BLOCK_VIEWS_DEFINED')) {
26
    return;
27
}
28
define('USERLOG_BLOCK_VIEWS_DEFINED', true);
29
xoops_loadLanguage('admin', USERLOG_DIRNAME);
30
31
// options[0] - number of items to show in block. the default is 10
32
// options[1] - items to select in Where claus
33
// options[2] - Time period - default: 1 day
34
// options[3] - Uid in WHERE claus: select some users to only count views by them -1 -> all (by default)
35
// options[4] - Gid in WHERE claus: select some groups to only count views by them 0 -> all (by default)
36
// options[5] - Sort - views, module dirname, module name, module views default: views
37
// options[6] - Order - DESC, ASC default: DESC
38
39
/**
40
 * @param $options
41
 *
42
 * @return array
43
 */
44
function userlog_views_show($options)
45
{
46
    $loglogObj = UserlogLog::getInstance();
47
    $module    = [];
48
    if (!empty($options[1])) {
49
        $options_views = explode(',', $options[1]); // item views in where claus eg: news-storyid, newbb-topic_id, news-storytopic
50 View Code Duplication
        foreach ($options_views as $key => $item) {
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...
51
            $module_script_item = explode('-', $item); // news:article.php-storyid news:index.php-storytopic => $module["news"]=array("storyid","storytopic");
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
            $module_script      = explode(':', $module_script_item[0]); //  news:article.php => $module_script = array(news,article.php);
53
            if (!isset($module[$module_script[0]])) {
54
                $module[$module_script[0]]['item_name'] = [];
55
                $module[$module_script[0]]['script']    = array_slice($module_script, 1);
56
            }
57
            $module[$module_script[0]]['script']      = array_unique(array_merge($module[$module_script[0]]['script'], array_slice($module_script, 1)));
58
            $module[$module_script[0]]['item_name'][] = $module_script_item[1];
59
        }
60
    }
61
    $users  = ($options[3] != -1) ? explode(',', $options[3]) : [];
62
    $groups = !empty($options[4]) ? explode(',', $options[4]) : [];
63
64
    $items          = $loglogObj->getViews($options[0], 0, $options[5], $options[6], $module, $options[2], $users, $groups);
65
    $block          = [];
66
    $block['items'] = $items;
67
    $block['sort']  = $options[5];
68
69
    return $block;
70
}
71
72
/**
73
 * @param $options
74
 *
75
 * @return string
76
 */
77
function userlog_views_edit($options)
78
{
79
    // require_once XOOPS_ROOT_PATH . "/class/blockform.php"; //reserve for 2.6
80
    xoops_load('XoopsFormLoader');
81
    // $form = new XoopsBlockForm(); //reserve for 2.6
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
    $form = new XoopsThemeForm(_AM_USERLOG_VIEW, 'views', '');
83
84
    /** @var XoopsModuleHandler $moduleHandler */
85
    $moduleHandler = xoops_getHandler('module');
86
    $criteria      = new CriteriaCompo();
87
    $criteria->add(new Criteria('hasnotification', 1));
88
    $criteria->add(new Criteria('isactive', 1));
89
    $modules  = $moduleHandler->getObjects($criteria, true);
90
    $hasviews = [];
91
    foreach ($modules as $module) {
92
        $not_config = $module->getInfo('notification');
93 View Code Duplication
        foreach ($not_config['category'] as $category) {
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...
94
            if (!empty($category['item_name'])) {
95
                $script                                                                      = is_array($category['subscribe_from']) ? implode(':', $category['subscribe_from']) : $category['subscribe_from'];
96
                $hasviews[$module->dirname() . ':' . $script . '-' . $category['item_name']] = $module->dirname() . '/' . $script . '?' . $category['item_name'] . '=ITEM_ID';
97
            }
98
        }
99
    }
100
101
    $i = 0;
102
    // number of items to display element
103
    $numitemsEle = new XoopsFormText(_AM_USERLOG_ITEMS_NUM, "options[{$i}]", 10, 255, (int)$options[$i]);
104
105
    ++$i;
106
    // views element
107
    $options_views     = explode(',', $options[$i]);
108
    $viewsEle          = new XoopsFormCheckBox(_AM_USERLOG_ITEMS, "options[{$i}][]", !empty($options_views) ? $options_views : 0);
109
    $viewsEle->columns = 3;
110
    if (!empty($hasviews)) {
111
        $viewsEle->addOptionArray($hasviews);
112
        $viewsEle->setExtra("onchange = \"validate('options[{$i}][]','checkbox', true)\""); // prevent user select no option
113
        $check_all = _ALL . ": <input type=\"checkbox\" name=\"item_check\" id=\"item_check\" value=\"1\" onclick=\"xoopsCheckGroup('blockform', 'item_check','options[{$i}][]');\">"; // blockform is the main form
114
        $viewsEle  = new XoopsFormLabel(_AM_USERLOG_ITEMS, $check_all . "<br\>" . $viewsEle->render());
115
    } else {
116
        // prevent to select
117
        $viewsEle->addOption(0, _NONE);
118
        $viewsEle->setExtra('class="hidden"');
119
    }
120
121
    $viewsEle->setDescription(_AM_USERLOG_ITEMS_DSC);
122
123
    ++$i;
124
    $timeEle = new XoopsFormText(_AM_USERLOG_LOG_TIMEGT, "options[{$i}]", 10, 255, $options[$i]);
125
    $timeEle->setDescription(_AM_USERLOG_LOG_TIMEGT_FORM);
126
127
    ++$i;
128
    $userRadioEle = new XoopsFormRadio(_AM_USERLOG_UID, "options[{$i}]", $options[$i]);
129
    $userRadioEle->addOption(-1, _ALL);
130
    $userRadioEle->addOption(($options[$i] != -1) ? $options[$i] : 0, _SELECT); // if no user in selection box it select uid=0 anon users
131
    $userRadioEle->setExtra("onchange=\"var el=document.getElementById('options[{$i}]'); el.disabled=(this.id == 'options[{$i}]1'); if (!el.value) {el.value= this.value}\""); // if user dont select any option it select "all"
132
    $userSelectEle = new XoopsFormSelectUser(_AM_USERLOG_UID, "options[{$i}]", true, explode(',', $options[$i]), 3, true);
133
    $userEle       = new XoopsFormLabel(_AM_USERLOG_UID, $userRadioEle->render() . $userSelectEle->render());
134
135
    ++$i;
136
    $groupRadioEle = new XoopsFormRadio(_AM_USERLOG_GROUPS, "options[{$i}]", !empty($options[$i]));
137
    $groupRadioEle->addOption(0, _ALL);
138
    $groupRadioEle->addOption(!empty($options[$i]) ? $options[$i] : 2, _SELECT); // if no group in selection box it select gid=2 registered users
139
    $groupRadioEle->setExtra("onchange=\"var el=document.getElementById('options[{$i}]'); el.disabled=(this.id == 'options[{$i}]1'); if (!el.value) {el.value= this.value}\""); // if group dont select any option it select "all"
140
    $groupSelectEle = new XoopsFormSelectGroup(_AM_USERLOG_GROUPS, "options[{$i}]", true, explode(',', $options[$i]), 3, true);
141
    $groupEle       = new XoopsFormLabel(_AM_USERLOG_GROUPS, $groupRadioEle->render() . $groupSelectEle->render());
142
143
    ++$i;
144
    $sortEle = new XoopsFormSelect(_AM_USERLOG_SORT, "options[{$i}]", $options[$i]);
145
    $sortEle->addOptionArray([
146
                                 'count'        => _AM_USERLOG_VIEW,
147
                                 'module'       => _AM_USERLOG_MODULE,
148
                                 'module_name'  => _AM_USERLOG_MODULE_NAME,
149
                                 'module_count' => _AM_USERLOG_VIEW_MODULE
150
                             ]);
151
    $sortEle->setDescription(_AM_USERLOG_SORT_DSC);
152
153
    ++$i;
154
    $orderEle = new XoopsFormSelect(_AM_USERLOG_ORDER, "options[{$i}]", $options[$i]);
155
    $orderEle->addOption('DESC', _DESCENDING);
156
    $orderEle->addOption('ASC', _ASCENDING);
157
    $orderEle->setDescription(_AM_USERLOG_ORDER_DSC);
158
159
    // add all elements to form
160
    $form->addElement($numitemsEle);
161
    $form->addElement($viewsEle);
162
    $form->addElement($timeEle);
163
    $form->addElement($userEle);
164
    $form->addElement($groupEle);
165
    $form->addElement($sortEle);
166
    $form->addElement($orderEle);
167
168
    return $form->render();
169
}
170