Passed
Pull Request — master (#47)
by Goffy
02:02
created

b_tdmdownloads_top_show()   F

Complexity

Conditions 16
Paths 1520

Size

Total Lines 120
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 16
eloc 91
c 3
b 0
f 1
nc 1520
nop 1
dl 0
loc 120
rs 1.3163

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
    /** @var \XoopsModules\Tdmdownloads\Helper $helper */
66
    $helper = \XoopsModules\Tdmdownloads\Helper::getInstance();
67
	$helper->loadLanguage('main');
68
69
    $categories = $utility->getItemIds('tdmdownloads_view', $moduleDirName);
70
    $criteria   = new \CriteriaCompo();
71
    $criteria->add(new \Criteria('cid', '(' . implode(',', $categories) . ')', 'IN'));
72
    if (!(0 == $options[0] && 1 === count($options))) {
73
        $criteria->add(new \Criteria('cid', '(' . implode(',', $options) . ')', 'IN'));
74
    }
75
    $criteria->add(new \Criteria('status', 0, '!='));
76
    switch ($type_block) {    // pour le bloc: dernier fichier
77
        case 'date':
78
            $criteria->setSort('date');
79
            $criteria->setOrder('DESC');
80
            break;
81
        // pour le bloc: plus téléchargé
82
        case 'hits':
83
            $criteria->setSort('hits');
84
            $criteria->setOrder('DESC');
85
            break;
86
        // pour le bloc: mieux noté
87
        case 'rating':
88
            $criteria->setSort('rating');
89
            $criteria->setOrder('DESC');
90
            break;
91
        // pour le bloc: aléatoire
92
        case 'random':
93
            $criteria->setSort('RAND()');
94
            break;
95
    }
96
    $criteria->setLimit($nb_entree);
97
    $downloadsArray = $downloadsHandler->getAll($criteria);
98
    foreach (array_keys($downloadsArray) as $i) {
99
        $block[$i]['lid']   = $downloadsArray[$i]->getVar('lid');
100
        $block[$i]['title'] = mb_strlen($downloadsArray[$i]->getVar('title')) > $lenght_title ? mb_substr($downloadsArray[$i]->getVar('title'), 0, $lenght_title) . '...' : $downloadsArray[$i]->getVar('title');
101
        $descriptionShort  = '';
102
        if (true == $use_description) {
103
            $description = $downloadsArray[$i]->getVar('description');
104
            //permet d'afficher uniquement la description courte
105
            if (false === mb_strpos($description, '[pagebreak]')) {
106
                $descriptionShort = mb_substr($description, 0, $lenght_description) . ' ...';
107
            } else {
108
                $descriptionShort = mb_substr($description, 0, mb_strpos($description, '[pagebreak]')) . ' ...';
109
            }
110
        }
111
        $block[$i]['description'] = $descriptionShort;
112
        $logourl                  = '';
113
        if (true == $use_logo) {
114
            if ('blank.gif' === $downloadsArray[$i]->getVar('logourl')) {
115
                $logourl = '';
116
            } else {
117
                $logourl = XOOPS_URL . '/uploads/' . $moduleDirName . '/images/shots/' . $downloadsArray[$i]->getVar('logourl');
118
            }
119
        }
120
        $block[$i]['logourl']       = $logourl;
121
        $block[$i]['logourl_class'] = $logo_float;
122
        $block[$i]['logourl_width'] = $logo_width;
123
        $block[$i]['hits']          = $downloadsArray[$i]->getVar('hits');
124
        $block[$i]['rating']        = number_format($downloadsArray[$i]->getVar('rating'), 1);
125
        $block[$i]['date']          = formatTimestamp($downloadsArray[$i]->getVar('date'), 's');
126
        $block[$i]['submitter']     = \XoopsUser::getUnameFromId($downloadsArray[$i]->getVar('submitter'));
127
        $block[$i]['inforation']    = $show_inforation;
128
        $block[$i]['blockstyle']    = $blockstyle;
129
    }
130
    $GLOBALS['xoopsTpl']->assign('tdmblockstyle', $blockstyle);
131
    
132
    $grouppermHandler = xoops_getHandler('groupperm');
133
    $groups           = XOOPS_GROUP_ANONYMOUS;
134
    if (is_object($GLOBALS['xoopsUser'])) {
135
        $groups = $GLOBALS['xoopsUser']->getGroups();
136
    }
137
    $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

137
    $perm_submit = $grouppermHandler->/** @scrutinizer ignore-call */ checkRight('tdmdownloads_ac', 4, $groups, $mymodule->getVar('mid')) ? true : false;
Loading history...
138
    $perm_modif  = $grouppermHandler->checkRight('tdmdownloads_ac', 8, $groups, $mymodule->getVar('mid')) ? true : false;
139
    $GLOBALS['xoopsTpl']->assign('perm_submit', $perm_submit);
140
    $GLOBALS['xoopsTpl']->assign('perm_modif', $perm_modif);
141
   
142
    return $block;
143
}
144
145
/**
146
 * @param $options
147
 *
148
 * @return string
149
 */
150
function b_tdmdownloads_top_edit($options)
151
{
152
    //appel de la class
153
    $moduleDirName   = basename(dirname(__DIR__));
0 ignored issues
show
Unused Code introduced by
The assignment to $moduleDirName is dead and can be removed.
Loading history...
154
    $categoryHandler = \XoopsModules\Tdmdownloads\Helper::getInstance()->getHandler('Category');
155
    $criteria        = new \CriteriaCompo();
156
    $criteria->setSort('cat_weight ASC, cat_title');
157
    $criteria->setOrder('ASC');
158
    $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

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