wfdownloads_top_edit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
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
/**
13
 * Wfdownloads module
14
 *
15
 * @copyright       XOOPS Project (https://xoops.org)
16
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package         wfdownload
18
 * @since           3.23
19
 * @author          Xoops Development Team
20
 */
21
22
/**
23
 * Function: b_mydownloads_top_show
24
 * Input   : $options[0] = date for the most recent downloads
25
 *                     hits for the most popular downloads
26
 *            $block['content'] = The optional above content
27
 *            $options[1]   = How many downloads are displayes
28
 * Output  : Returns the most recent or most popular downloads
29
 */
30
31
use XoopsModules\Wfdownloads;
32
use XoopsModules\Wfdownloads\Helper;
33
34
defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
35
36
require_once dirname(__DIR__) . '/include/common.php';
37
/**
38
 * @param $options
39
 *
40
 * @return array
41
 */
42
function wfdownloads_top_show($options)
43
{
44
    if (!class_exists(Helper::class)) {
45
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type array.
Loading history...
46
    }
47
    $helper = Helper::getInstance();
48
49
    $groups = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS];
50
    /** @var \XoopsGroupPermHandler $grouppermHandler */
51
    $grouppermHandler         = xoops_getHandler('groupperm');
52
    $allowedDownCategoriesIds = $grouppermHandler->getItemIds('WFDownCatPerm', $groups, $helper->getModule()->mid());
53
54
    $block = [];
55
56
    // get downloads
57
    $criteria = new CriteriaCompo();
58
    $criteria->add(new Criteria('cid', '(' . implode(',', $allowedDownCategoriesIds) . ')', 'IN'));
59
    $criteria->add(new Criteria('offline', 'false'));
60
    $criteria->setSort($options[0]);
61
    $criteria->setOrder('DESC');
62
    $criteria->setLimit($options[1]);
63
    $downloadObjs = $helper->getHandler('Download')->getObjects($criteria);
64
65
    foreach ($downloadObjs as $downloadObj) {
66
        $download = $downloadObj->toArray();
67
        if (!in_array((int)$download['cid'], $allowedDownCategoriesIds)) {
68
            continue;
69
        }
70
        $download['title'] = xoops_substr($download['title'], 0, $options[2] - 1);
71
        $download['id']    = (int)$download['lid'];
72
        if ('published' === $options[0]) {
73
            $download['date'] = formatTimestamp($download['published'], $helper->getConfig('dateformat'));
74
        } else {
75
            $download['date'] = formatTimestamp($download['date'], $helper->getConfig('dateformat'));
76
        }
77
        $download['dirname']  = $helper->getModule()->dirname();
78
        $block['downloads'][] = $download;
79
    }
80
81
    return $block;
82
}
83
84
/**
85
 * @param $options
86
 *
87
 * @return string
88
 */
89
function wfdownloads_top_edit($options)
90
{
91
    $form = '' . _MB_WFDOWNLOADS_DISP . '&nbsp;';
92
    $form .= "<input type='hidden' name='options[]' value='" . (('published' === $options[0]) ? 'published' : 'hits') . "'>";
93
    $form .= "<input type='text' name='options[]' value='" . $options[1] . "'>&nbsp;" . _MB_WFDOWNLOADS_FILES . '';
94
    $form .= '<br>';
95
    $form .= '' . _MB_WFDOWNLOADS_CHARS . "&nbsp;<input type='text' name='options[]' value='" . $options[2] . "'>&nbsp;" . _MB_WFDOWNLOADS_LENGTH . '';
96
97
    return $form;
98
}
99