DownloadHandler::incrementHits()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Wfdownloads;
4
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
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * Wfdownloads module
17
 *
18
 * @copyright       XOOPS Project (https://xoops.org)
19
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
20
 * @package         wfdownload
21
 * @since           3.23
22
 * @author          Xoops Development Team
23
 */
24
25
require_once \dirname(__DIR__) . '/include/common.php';
26
27
/**
28
 * Class DownloadHandler
29
 */
30
class DownloadHandler extends \XoopsPersistableObjectHandler
31
{
32
    /**
33
     * @access public
34
     */
35
    public $helper;
36
37
    /**
38
     * @param null|\XoopsDatabase $db
39
     */
40
    public function __construct(\XoopsDatabase $db = null)
41
    {
42
        parent::__construct($db, 'wfdownloads_downloads', Download::class, 'lid', 'title');
43
        /** @var \XoopsModules\Wfdownloads\Helper $this ->helper */
44
        $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Wfdownloads\Helper.
Loading history...
45
    }
46
47
    /**
48
     * Get maximum published date from a criteria
49
     *
50
     * @param \CriteriaElement|null $criteria
51
     *
52
     * @return mixed
53
     */
54
    public function getMaxPublishdate(\CriteriaElement $criteria = null)
55
    {
56
        $field = '';
57
        $groupby = false;
58
        if (null !== $criteria && $criteria instanceof \CriteriaElement) {
59
            if ('' != $criteria->groupby) {
60
                $groupby = true;
61
                $field   = $criteria->groupby . ', '; //Not entirely secure unless you KNOW that no criteria's groupby clause is going to be mis-used
62
            }
63
        }
64
        $sql = 'SELECT ' . $field . 'MAX(published) FROM ' . $this->table;
65
        if (\is_object($criteria)) {
66
            $sql .= ' ' . $criteria->renderWhere();
67
            if ('' != $criteria->groupby) {
68
                $sql .= $criteria->getGroupby();
69
            }
70
        }
71
        $result = $this->db->query($sql);
72
        if (!$result) {
73
            return 0;
74
        }
75
        if (!$groupby) {
76
            [$count] = $this->db->fetchRow($result);
77
78
            return $count;
79
        }
80
        $ret = [];
81
        while (list($id, $count) = $this->db->fetchRow($result)) {
82
            $ret[$id] = $count;
83
        }
84
85
        return $ret;
86
    }
87
88
    /**
89
     * Get criteria for active downloads
90
     *
91
     * @return \CriteriaCompo
92
     */
93
    public function getActiveCriteria()
94
    {
95
        /** @var \XoopsGroupPermHandler $grouppermHandler */
96
        $grouppermHandler = \xoops_getHandler('groupperm');
97
98
        $criteria = new \CriteriaCompo(new \Criteria('offline', false));
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $value of Criteria::__construct(). ( Ignorable by Annotation )

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

98
        $criteria = new \CriteriaCompo(new \Criteria('offline', /** @scrutinizer ignore-type */ false));
Loading history...
99
        $criteria->add(new \Criteria('published', 0, '>'));
100
        $criteria->add(new \Criteria('published', \time(), '<='));
101
        $expiredCriteria = new \CriteriaCompo(new \Criteria('expired', 0));
102
        $expiredCriteria->add(new \Criteria('expired', \time(), '>='), 'OR');
103
        $criteria->add($expiredCriteria);
104
        // add criteria for categories that the user has permissions for
105
        $groups                   = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS];
106
        $allowedDownCategoriesIds = $grouppermHandler->getItemIds('WFDownCatPerm', $groups, $this->helper->getModule()->mid());
107
        $criteria->add(new \Criteria('cid', '(' . \implode(',', $allowedDownCategoriesIds) . ')', 'IN'));
108
109
        return $criteria;
110
    }
111
112
    /**
113
     * Get array of active downloads with optional additional criteria
114
     *
115
     * @param \CriteriaCompo|null $crit Additional criteria
116
     *
117
     * @return array
118
     */
119
    public function getActiveDownloads(\CriteriaCompo $crit = null)
120
    {
121
        if (\is_object($crit)) {
122
            $criteria = $crit;
123
        } else {
124
            $criteria = new \CriteriaCompo();
125
        }
126
        $active_crit = $this->getActiveCriteria();
127
        $criteria->add($active_crit);
128
129
        return $this->getObjects($criteria);
130
    }
131
132
    /**
133
     * Get count of active downloads
134
     *
135
     * @param \CriteriaElement|null $crit Additional criteria
136
     *
137
     * @return int /int
138
     */
139
    public function getActiveCount(\CriteriaElement $crit = null)
140
    {
141
        $criteria = $this->getActiveCriteria();
142
        if (\is_object($crit)) {
143
            $criteria->add($crit);
144
        }
145
146
        return $this->getCount($criteria);
147
    }
148
149
    /**
150
     * Increment hit counter for a download
151
     *
152
     * @param int $lid
153
     *
154
     * @return bool
155
     */
156
    public function incrementHits($lid)
157
    {
158
        $sql = 'UPDATE ' . $this->table . " SET hits=hits+1 WHERE lid='" . (int)$lid . "'";
159
160
        return $this->db->queryF($sql);
161
    }
162
163
    /**
164
     * @param \XoopsObject $download
165
     * @param bool         $force
166
     *
167
     * @return bool
168
     */
169
    public function delete(\XoopsObject $download, $force = false)
170
    {
171
        if (parent::delete($download, $force)) {
172
            $criteria = new \Criteria('lid', (int)$download->getVar('lid'));
173
            $this->helper->getHandler('Rating')->deleteAll($criteria);
174
            $this->helper->getHandler('Mirror')->deleteAll($criteria);
175
            $this->helper->getHandler('Review')->deleteAll($criteria);
176
            $this->helper->getHandler('Report')->deleteAll($criteria);
177
            // delete comments
178
            \xoops_comment_delete((int)$this->helper->getModule()->mid(), (int)$download->getVar('lid'));
179
180
            // Formulize module support (2006/05/04) jpc - start
181
            if (Utility::checkModule('formulize')) {
182
                if (\is_file(XOOPS_ROOT_PATH . '/modules/formulize/include/functions.php') && $download->getVar('formulize_idreq') > 0) {
183
                    require_once XOOPS_ROOT_PATH . '/modules/formulize/include/functions.php';
184
                    //deleteFormEntries(array($download->getVar('formulize_idreq')));
185
                    $category = $this->helper->getHandler('Category')->get($download->getVar('cid'));
186
                    deleteFormEntries([$download->getVar('formulize_idreq')], $category->getVar('formulize_fid'));
0 ignored issues
show
Bug introduced by
The function deleteFormEntries was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

186
                    /** @scrutinizer ignore-call */ 
187
                    deleteFormEntries([$download->getVar('formulize_idreq')], $category->getVar('formulize_fid'));
Loading history...
187
                }
188
            }
189
190
            // Formulize module support (2006/05/04) jpc - end
191
            return true;
192
        }
193
194
        return false;
195
    }
196
}
197