Passed
Push — master ( ddf2c3...147716 )
by
unknown
04:37 queued 02:06
created

b_tdmdownloads_top_show()   F

Complexity

Conditions 16
Paths 1520

Size

Total Lines 119
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 16
eloc 90
c 3
b 0
f 1
nc 1520
nop 1
dl 0
loc 119
rs 1.3381

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
 * TDMDownload
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   Gregory Mage (Aka Mage)
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Gregory Mage (Aka Mage)
15
 * @param $options
16
 * @return array
17
 */
18
19
/**
20
 * @param $options
21
 * @return array
22
 */
23
function b_tdmdownloads_top_show($options)
24
{
25
    require dirname(__DIR__) . '/include/common.php';
26
    /** @var \XoopsModuleHandler $moduleHandler */
27
    $moduleHandler = xoops_getHandler('module');
28
    // get the name of the file's directory to get the "owner" of the block, i.e. its module, and not the "user", where it is currently
29
    //$mydir          = basename(dirname(__DIR__));
30
    $moduleDirName = basename(dirname(__DIR__));
31
    $mymodule      = $moduleHandler->getByDirname($moduleDirName);
32
    //appel de la class
33
    /** @var \XoopsModules\Tdmdownloads\DownloadsHandler $downloadsHandler */
34
    $downloadsHandler   = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Downloads');
35
    $block              = [];
36
    $type_block         = $options[0];
37
    $nb_entree          = $options[1];
38
    $lenght_title       = $options[2];
39
    $use_logo           = $options[3];
40
    $use_description    = $options[4];
41
    $show_inforation    = $options[5];
42
    $logo_float         = $options[6];
43
    $logo_width         = $options[7];
44
    $lenght_description = $options[8];
45
    $blockstyle         = $options[9];
46
47
    array_shift($options);
48
    array_shift($options);
49
    array_shift($options);
50
    array_shift($options);
51
    array_shift($options);
52
    array_shift($options);
53
    array_shift($options);
54
    array_shift($options);
55
    array_shift($options);
56
    array_shift($options);
57
58
    // Add styles
59
    global $xoTheme;
60
61
    /** @var \xos_opal_Theme $xoTheme */
62
    $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $moduleDirName . '/assets/css/blocks.css', null);
63
    /** @var \XoopsModules\Tdmdownloads\Utility $utility */
64
    $utility = new \XoopsModules\Tdmdownloads\Utility();
65
	$helper->loadLanguage('main');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $helper seems to be never defined.
Loading history...
66
67
    $categories = $utility->getItemIds('tdmdownloads_view', $moduleDirName);
68
    $criteria   = new \CriteriaCompo();
69
    $criteria->add(new \Criteria('cid', '(' . implode(',', $categories) . ')', 'IN'));
70
    if (!(0 == $options[0] && 1 === count($options))) {
71
        $criteria->add(new \Criteria('cid', '(' . implode(',', $options) . ')', 'IN'));
72
    }
73
    $criteria->add(new \Criteria('status', 0, '!='));
74
    switch ($type_block) {    // pour le bloc: dernier fichier
75
        case 'date':
76
            $criteria->setSort('date');
77
            $criteria->setOrder('DESC');
78
            break;
79
        // pour le bloc: plus téléchargé
80
        case 'hits':
81
            $criteria->setSort('hits');
82
            $criteria->setOrder('DESC');
83
            break;
84
        // pour le bloc: mieux noté
85
        case 'rating':
86
            $criteria->setSort('rating');
87
            $criteria->setOrder('DESC');
88
            break;
89
        // pour le bloc: aléatoire
90
        case 'random':
91
            $criteria->setSort('RAND()');
92
            break;
93
    }
94
    $criteria->setLimit($nb_entree);
95
    $downloadsArray = $downloadsHandler->getAll($criteria);
96
    foreach (array_keys($downloadsArray) as $i) {
97
        /** @var \XoopsModules\Tdmdownloads\Downloads[] $downloadsArray */
98
        $block[$i]['lid']   = $downloadsArray[$i]->getVar('lid');
99
        $block[$i]['title'] = mb_strlen($downloadsArray[$i]->getVar('title')) > $lenght_title ? mb_substr($downloadsArray[$i]->getVar('title'), 0, $lenght_title) . '...' : $downloadsArray[$i]->getVar('title');
0 ignored issues
show
Bug introduced by
It seems like $downloadsArray[$i]->getVar('title') can also be of type array and array; however, parameter $str of mb_substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        $block[$i]['title'] = mb_strlen($downloadsArray[$i]->getVar('title')) > $lenght_title ? mb_substr(/** @scrutinizer ignore-type */ $downloadsArray[$i]->getVar('title'), 0, $lenght_title) . '...' : $downloadsArray[$i]->getVar('title');
Loading history...
Bug introduced by
It seems like $downloadsArray[$i]->getVar('title') can also be of type array and array; however, parameter $str of mb_strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        $block[$i]['title'] = mb_strlen(/** @scrutinizer ignore-type */ $downloadsArray[$i]->getVar('title')) > $lenght_title ? mb_substr($downloadsArray[$i]->getVar('title'), 0, $lenght_title) . '...' : $downloadsArray[$i]->getVar('title');
Loading history...
100
        $descriptionShort  = '';
101
        if (true == $use_description) {
102
            $description = $downloadsArray[$i]->getVar('description');
103
            //permet d'afficher uniquement la description courte
104
            if (false === mb_strpos($description, '[pagebreak]')) {
0 ignored issues
show
Bug introduced by
It seems like $description can also be of type array and array; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
            if (false === mb_strpos(/** @scrutinizer ignore-type */ $description, '[pagebreak]')) {
Loading history...
105
                $descriptionShort = mb_substr($description, 0, $lenght_description) . ' ...';
106
            } else {
107
                $descriptionShort = mb_substr($description, 0, mb_strpos($description, '[pagebreak]')) . ' ...';
108
            }
109
        }
110
        $block[$i]['description'] = $descriptionShort;
111
        $logourl                  = '';
112
        if (true == $use_logo) {
113
            if ('blank.gif' === $downloadsArray[$i]->getVar('logourl')) {
114
                $logourl = '';
115
            } else {
116
                $logourl = XOOPS_URL . '/uploads/' . $moduleDirName . '/images/shots/' . $downloadsArray[$i]->getVar('logourl');
117
            }
118
        }
119
        $block[$i]['logourl']       = $logourl;
120
        $block[$i]['logourl_class'] = $logo_float;
121
        $block[$i]['logourl_width'] = $logo_width;
122
        $block[$i]['hits']          = $downloadsArray[$i]->getVar('hits');
123
        $block[$i]['rating']        = number_format($downloadsArray[$i]->getVar('rating'), 1);
124
        $block[$i]['date']          = formatTimestamp($downloadsArray[$i]->getVar('date'), 's');
125
        $block[$i]['submitter']     = \XoopsUser::getUnameFromId($downloadsArray[$i]->getVar('submitter'));
126
        $block[$i]['inforation']    = $show_inforation;
127
        $block[$i]['blockstyle']    = $blockstyle;
128
    }
129
    $GLOBALS['xoopsTpl']->assign('tdmblockstyle', $blockstyle);
130
    
131
    $grouppermHandler = xoops_getHandler('groupperm');
132
    $groups           = XOOPS_GROUP_ANONYMOUS;
133
    if (is_object($GLOBALS['xoopsUser'])) {
134
        $groups = $GLOBALS['xoopsUser']->getGroups();
135
    }
136
    $perm_submit = $grouppermHandler->checkRight('tdmdownloads_ac', 4, $groups, $mymodule->getVar('mid')) ? true : false;
0 ignored issues
show
Bug introduced by
The method checkRight() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
    $perm_submit = $grouppermHandler->/** @scrutinizer ignore-call */ checkRight('tdmdownloads_ac', 4, $groups, $mymodule->getVar('mid')) ? true : false;
Loading history...
137
    $perm_modif  = $grouppermHandler->checkRight('tdmdownloads_ac', 8, $groups, $mymodule->getVar('mid')) ? true : false;
138
    $GLOBALS['xoopsTpl']->assign('perm_submit', $perm_submit);
139
    $GLOBALS['xoopsTpl']->assign('perm_modif', $perm_modif);
140
   
141
    return $block;
142
}
143
144
/**
145
 * @param $options
146
 *
147
 * @return string
148
 */
149
function b_tdmdownloads_top_edit($options)
150
{
151
    //appel de la class
152
    $moduleDirName   = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
153
    $categoryHandler = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Category');
154
    $criteria        = new \CriteriaCompo();
155
    $criteria->setSort('cat_weight ASC, cat_title');
156
    $criteria->setOrder('ASC');
157
    $downloadscatArray = $categoryHandler->getAll($criteria);
0 ignored issues
show
Bug introduced by
The method getAll() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoUserHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
    /** @scrutinizer ignore-call */ 
158
    $downloadscatArray = $categoryHandler->getAll($criteria);
Loading history...
158
    $form              = _MB_TDMDOWNLOADS_DISP . "&nbsp;\n";
159
    $form              .= '<input type="hidden" name="options[0]" value="' . $options[0] . "\">\n";
160
    $form              .= '<input name="options[1]" size="5" maxlength="255" value="' . $options[1] . '" type="text">&nbsp;' . _MB_TDMDOWNLOADS_FILES . "<br>\n";
161
    $form              .= _MB_TDMDOWNLOADS_CHARS . ' : <input name="options[2]" size="5" maxlength="255" value="' . $options[2] . "\" type=\"text\"><br>\n";
162
    if (false == $options[3]) {
163
        $checked_yes = '';
164
        $checked_no  = 'checked';
165
    } else {
166
        $checked_yes = 'checked';
167
        $checked_no  = '';
168
    }
169
    $form .= _MB_TDMDOWNLOADS_LOGO . ' : <input name="options[3]" value="1" type="radio" ' . $checked_yes . '>' . _YES . "&nbsp;\n";
170
    $form .= '<input name="options[3]" value="0" type="radio" ' . $checked_no . '>' . _NO . "<br>\n";
171
    if (false == $options[4]) {
172
        $checked_yes = '';
173
        $checked_no  = 'checked';
174
    } else {
175
        $checked_yes = 'checked';
176
        $checked_no  = '';
177
    }
178
    $form .= _MB_TDMDOWNLOADS_DESCRIPTION . ' : <input name="options[4]" value="1" type="radio" ' . $checked_yes . '>' . _YES . "&nbsp;\n";
179
    $form .= '<input name="options[4]" value="0" type="radio" ' . $checked_no . '>' . _NO . "<br>\n";
180
    if (false == $options[5]) {
181
        $checked_yes = '';
182
        $checked_no  = 'checked';
183
    } else {
184
        $checked_yes = 'checked';
185
        $checked_no  = '';
186
    }
187
    $form       .= _MB_TDMDOWNLOADS_INFORMATIONS . ' : <input name="options[5]" value="1" type="radio" ' . $checked_yes . '>' . _YES . "&nbsp;\n";
188
    $form       .= '<input name="options[5]" value="0" type="radio" ' . $checked_no . '>' . _NO . "<br><br>\n";
189
    $floatSelect = new \XoopsFormSelect('', 'options[6]', $options[6]);
190
    $floatSelect->addOption('left', _MB_TDMDOWNLOADS_FLOAT_LEFT);
191
    $floatSelect->addOption('right', _MB_TDMDOWNLOADS_FLOAT_RIGHT);
192
    $form .= _MB_TDMDOWNLOADS_FLOAT . $floatSelect->render() . '<br>';
193
    $form .= _MB_TDMDOWNLOADS_WHITE . ': <input name="options[7]" size="5" maxlength="255" value="' . $options[7] . "\" type=\"text\"><br>\n";
194
    $form .= _MB_TDMDOWNLOADS_CHARSDSC . ': <input name="options[8]" size="5" maxlength="255" value="' . $options[8] . "\" type=\"text\"><br>\n";    
195
    $styleSelect = new \XoopsFormSelect('', 'options[9]', $options[9]);
196
    $styleSelect->addOption('default', 'default');
197
    $styleSelect->addOption('simple1', 'simple1');
198
    $styleSelect->addOption('simple4', 'simple4');
199
    $form .= _MB_TDMDOWNLOADS_BLOCKSTYLE . ': ' . $styleSelect->render() . '<br>';
200
    
201
    array_shift($options);
202
    array_shift($options);
203
    array_shift($options);
204
    array_shift($options);
205
    array_shift($options);
206
    array_shift($options);
207
    array_shift($options);
208
    array_shift($options);
209
    array_shift($options);
210
    $form .= _MB_TDMDOWNLOADS_CATTODISPLAY . "<br><select name=\"options[]\" multiple=\"multiple\" size=\"5\">\n";
211
    $form .= '<option value="0" ' . (!in_array(0, $options, false) ? '' : 'selected="selected"') . '>' . _MB_TDMDOWNLOADS_ALLCAT . "</option>\n";
212
    foreach (array_keys($downloadscatArray) as $i) {
213
        /** @var \XoopsModules\Tdmdownloads\Category[] $downloadscatArray */
214
        $form .= '<option value="' . $downloadscatArray[$i]->getVar('cat_cid') . '" ' . (!in_array($downloadscatArray[$i]->getVar('cat_cid'), $options, false) ? '' : 'selected') . '>' . $downloadscatArray[$i]->getVar('cat_title') . "</option>\n";
215
    }
216
    $form .= "</select>\n";
217
218
    return $form;
219
}
220