Passed
Pull Request — master (#88)
by Michael
02:56
created

blocks/tdmdownloads_top.php (1 issue)

Severity
1
<?php declare(strict_types=1);
2
3
/**
4
 * TDMDownload
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 *
13
 * @param $options
14
 * @return array
15
 * @author      Gregory Mage (Aka Mage)
16
 * @copyright   Gregory Mage (Aka Mage)
17
 * @license     GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
18
 */
19
20
use XoopsModules\Tdmdownloads\Helper;
21
22
/**
23
 * @param $options
24
 * @return array
25
 */
26
function b_tdmdownloads_top_show($options)
27
{
28
    require dirname(__DIR__) . '/include/common.php';
29
30
    /** @var \XoopsModuleHandler $moduleHandler */
31
32
    $moduleHandler = xoops_getHandler('module');
33
34
    // 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
35
36
    //$mydir          = basename(dirname(__DIR__));
37
38
    $moduleDirName = basename(dirname(__DIR__));
39
40
    $mymodule = $moduleHandler->getByDirname($moduleDirName);
41
42
    //appel de la class
43
44
    /** @var \XoopsModules\Tdmdownloads\DownloadsHandler $downloadsHandler */
45
46
    $downloadsHandler = Helper::getInstance()->getHandler('Downloads');
47
48
    $block = [];
49
50
    $type_block = $options[0];
51
52
    $nb_entree = $options[1];
53
54
    $lenght_title = (int)$options[2];
55
56
    $use_logo = $options[3];
57
58
    $use_description = $options[4];
59
60
    $show_information = $options[5];
61
62
    $logo_float = $options[6];
63
64
    $logo_width = $options[7];
65
66
    $length_description = (int)$options[8];
67
68
    $blockstyle = $options[9];
69
70
    array_shift($options);
71
72
    array_shift($options);
73
74
    array_shift($options);
75
76
    array_shift($options);
77
78
    array_shift($options);
79
80
    array_shift($options);
81
82
    array_shift($options);
83
84
    array_shift($options);
85
86
    array_shift($options);
87
88
    array_shift($options);
89
90
    // Add styles
91
92
    global $xoTheme;
93
94
    $db = null;
0 ignored issues
show
The assignment to $db is dead and can be removed.
Loading history...
95
96
    /** @var \xos_opal_Theme $xoTheme */
97
98
    $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $moduleDirName . '/assets/css/blocks.css', null);
99
100
    /** @var \XoopsModules\Tdmdownloads\Utility $utility */
101
102
    $utility = new \XoopsModules\Tdmdownloads\Utility();
103
104
    /** @var \XoopsModules\Tdmdownloads\Helper $helper */
105
106
    $helper->loadLanguage('main');
107
108
    $categories = $utility->getItemIds('tdmdownloads_view', $moduleDirName);
109
110
    $criteria = new \CriteriaCompo();
111
112
    $criteria->add(new \Criteria('cid', '(' . implode(',', $categories) . ')', 'IN'));
113
114
    if (is_array($options) && !empty($options) && !0 == $options[0] && 1 === count($options)) {
115
        $criteria->add(new \Criteria('cid', '(' . implode(',', $options) . ')', 'IN'));
116
    }
117
118
    $criteria->add(new \Criteria('status', 0, '!='));
119
120
    switch ($type_block) {    // pour le bloc: dernier fichier
121
        case 'date':
122
            $criteria->setSort('date');
123
            $criteria->setOrder('DESC');
124
            break;
125
        // pour le bloc: plus téléchargé
126
        case 'hits':
127
            $criteria->setSort('hits');
128
            $criteria->setOrder('DESC');
129
            break;
130
        // pour le bloc: mieux noté
131
        case 'rating':
132
            $criteria->setSort('rating');
133
            $criteria->setOrder('DESC');
134
            break;
135
        // pour le bloc: aléatoire
136
        case 'random':
137
            $criteria->setSort('RAND()');
138
            break;
139
    }
140
141
    $criteria->setLimit($nb_entree);
142
143
    $downloadsArray = $downloadsHandler->getAll($criteria);
144
145
    foreach (array_keys($downloadsArray) as $i) {
146
        /** @var \XoopsModules\Tdmdownloads\Downloads[] $downloadsArray */
147
148
        $block[$i]['lid'] = $downloadsArray[$i]->getVar('lid');
149
150
        $titleFinal = $downloadsArray[$i]->getVar('title');
151
152
        if ($lenght_title > 0) {
153
            $titleFinal = mb_strlen($titleFinal) > $lenght_title ? mb_substr($titleFinal, 0, $lenght_title) . '...' : $titleFinal;
154
        }
155
156
        $block[$i]['title'] = $titleFinal;
157
158
        $descriptionFinal = '';
159
160
        if (true == $use_description) {
161
            $description = $downloadsArray[$i]->getVar('description');
162
163
            //permet d'afficher uniquement la description courte
164
165
            if ($length_description > 0) {
166
                if (false === mb_strpos($description, '[pagebreak]')) {
167
                    $descriptionFinal = mb_substr($description, 0, $length_description);
168
169
                    if (mb_strlen($description) > mb_strlen($descriptionFinal)) {
170
                        $descriptionFinal .= ' ...';
171
                    }
172
                } else {
173
                    $descriptionFinal = mb_substr($description, 0, mb_strpos($description, '[pagebreak]')) . ' ...';
174
                }
175
            } else {
176
                $descriptionFinal = $description;
177
            }
178
        }
179
180
        $block[$i]['description'] = $descriptionFinal;
181
182
        $logourl = '';
183
184
        if (true == $use_logo) {
185
            if ('blank.gif' === $downloadsArray[$i]->getVar('logourl') || '' === $downloadsArray[$i]->getVar('logourl')) {
186
                $logourl = '';
187
            } else {
188
                $logourl = XOOPS_URL . '/uploads/' . $moduleDirName . '/images/shots/' . $downloadsArray[$i]->getVar('logourl');
189
            }
190
        }
191
192
        $block[$i]['logourl'] = $logourl;
193
194
        $block[$i]['logourl_class'] = $logo_float;
195
196
        $block[$i]['logourl_width'] = $logo_width;
197
198
        $block[$i]['hits'] = $downloadsArray[$i]->getVar('hits');
199
200
        $block[$i]['rating'] = number_format((float)$downloadsArray[$i]->getVar('rating'), 1);
201
202
        $block[$i]['date'] = formatTimestamp($downloadsArray[$i]->getVar('date'), 's');
203
204
        $block[$i]['submitter'] = \XoopsUser::getUnameFromId($downloadsArray[$i]->getVar('submitter'));
205
206
        $block[$i]['inforation'] = $show_information;
207
208
        $block[$i]['blockstyle'] = $blockstyle;
209
    }
210
211
    $GLOBALS['xoopsTpl']->assign('tdmblockstyle', $blockstyle);
212
213
    /** @var \XoopsGroupPermHandler $grouppermHandler */
214
    $grouppermHandler = xoops_getHandler('groupperm');
215
216
    $groups = XOOPS_GROUP_ANONYMOUS;
217
218
    if (is_object($GLOBALS['xoopsUser'])) {
219
        $groups = $GLOBALS['xoopsUser']->getGroups();
220
    }
221
222
    $perm_submit = $grouppermHandler->checkRight('tdmdownloads_ac', 4, $groups, $mymodule->getVar('mid')) ? true : false;
223
224
    $perm_modif = $grouppermHandler->checkRight('tdmdownloads_ac', 8, $groups, $mymodule->getVar('mid')) ? true : false;
225
226
    $GLOBALS['xoopsTpl']->assign('perm_submit', $perm_submit);
227
228
    $GLOBALS['xoopsTpl']->assign('perm_modif', $perm_modif);
229
230
    return $block;
231
}
232
233
/**
234
 * @param $options
235
 *
236
 * @return string
237
 */
238
function b_tdmdownloads_top_edit($options)
239
{
240
    //appel de la class
241
242
    $moduleDirName = basename(dirname(__DIR__));
243
244
    $categoryHandler = Helper::getInstance()->getHandler('Category');
245
246
    $criteria = new \CriteriaCompo();
247
248
    $criteria->setSort('cat_weight ASC, cat_title');
249
250
    $criteria->setOrder('ASC');
251
252
    $downloadscatArray = $categoryHandler->getAll($criteria);
253
254
    $form = _MB_TDMDOWNLOADS_DISP . "&nbsp;\n";
255
256
    $form .= '<input type="hidden" name="options[0]" value="' . $options[0] . "\">\n";
257
258
    $form .= '<input name="options[1]" size="5" maxlength="255" value="' . $options[1] . '" type="text">&nbsp;' . _MB_TDMDOWNLOADS_FILES . "<br>\n";
259
260
    $form .= _MB_TDMDOWNLOADS_CHARS . ' (<small>' . _MB_TDMDOWNLOADS_CHARSDSC . '</small>): <input name="options[2]" size="5" maxlength="255" value="' . $options[2] . "\" type=\"text\"><br>\n";
261
262
    if (false == $options[3]) {
263
        $checked_yes = '';
264
265
        $checked_no = 'checked';
266
    } else {
267
        $checked_yes = 'checked';
268
269
        $checked_no = '';
270
    }
271
272
    $form .= _MB_TDMDOWNLOADS_LOGO . ' : <input name="options[3]" value="1" type="radio" ' . $checked_yes . '>' . _YES . "&nbsp;\n";
273
274
    $form .= '<input name="options[3]" value="0" type="radio" ' . $checked_no . '>' . _NO . "<br>\n";
275
276
    if (false == $options[4]) {
277
        $checked_yes = '';
278
279
        $checked_no = 'checked';
280
    } else {
281
        $checked_yes = 'checked';
282
283
        $checked_no = '';
284
    }
285
286
    $form .= _MB_TDMDOWNLOADS_DESCRIPTION . ' : <input name="options[4]" value="1" type="radio" ' . $checked_yes . '>' . _YES . "&nbsp;\n";
287
288
    $form .= '<input name="options[4]" value="0" type="radio" ' . $checked_no . '>' . _NO . "<br>\n";
289
290
    if (false == $options[5]) {
291
        $checked_yes = '';
292
293
        $checked_no = 'checked';
294
    } else {
295
        $checked_yes = 'checked';
296
297
        $checked_no = '';
298
    }
299
300
    $form .= _MB_TDMDOWNLOADS_INFORMATIONS . ' : <input name="options[5]" value="1" type="radio" ' . $checked_yes . '>' . _YES . "&nbsp;\n";
301
302
    $form .= '<input name="options[5]" value="0" type="radio" ' . $checked_no . '>' . _NO . "<br><br>\n";
303
304
    $floatSelect = new \XoopsFormSelect('', 'options[6]', $options[6]);
305
306
    $floatSelect->addOption('left', _MB_TDMDOWNLOADS_FLOAT_LEFT);
307
308
    $floatSelect->addOption('right', _MB_TDMDOWNLOADS_FLOAT_RIGHT);
309
310
    $form .= _MB_TDMDOWNLOADS_FLOAT . $floatSelect->render() . '<br>';
311
312
    $form .= _MB_TDMDOWNLOADS_WIDTH . ' (<small>' . _MB_TDMDOWNLOADS_WIDTHDSC . '</small>): <input name="options[7]" size="5" maxlength="255" value="' . $options[7] . "\" type=\"text\"><br>\n";
313
314
    $form .= _MB_TDMDOWNLOADS_DESCRIPTIONDSC . ': <input name="options[8]" size="5" maxlength="255" value="' . $options[8] . "\" type=\"text\"><br>\n";
315
316
    $styleSelect = new \XoopsFormSelect('', 'options[9]', $options[9]);
317
318
    $styleSelect->addOption('default', 'default');
319
320
    $styleSelect->addOption('simple1', 'simple1');
321
322
    $styleSelect->addOption('simple2', 'simple2');
323
324
    $styleSelect->addOption('simple3', 'simple3');
325
326
    $styleSelect->addOption('simple4', 'simple4');
327
328
    $form .= _MB_TDMDOWNLOADS_BLOCKSTYLE . ': ' . $styleSelect->render() . '<br>';
329
330
    array_shift($options);
331
332
    array_shift($options);
333
334
    array_shift($options);
335
336
    array_shift($options);
337
338
    array_shift($options);
339
340
    array_shift($options);
341
342
    array_shift($options);
343
344
    array_shift($options);
345
346
    array_shift($options);
347
348
    $form .= _MB_TDMDOWNLOADS_CATTODISPLAY . "<br><select name=\"options[]\" multiple=\"multiple\" size=\"5\">\n";
349
350
    $form .= '<option value="0" ' . (!in_array(0, $options, false) ? '' : 'selected="selected"') . '>' . _MB_TDMDOWNLOADS_ALLCAT . "</option>\n";
351
352
    foreach (array_keys($downloadscatArray) as $i) {
353
        /** @var \XoopsModules\Tdmdownloads\Category[] $downloadscatArray */
354
355
        $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";
356
    }
357
358
    $form .= "</select>\n";
359
360
    return $form;
361
}
362