VisitorsHandler::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
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
 * @category        Module
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
22
use CriteriaElement;
23
use XoopsDatabase;
24
use XoopsObject;
25
use XoopsPersistableObjectHandler;
26
27
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
28
29
/**
30
 * suico_visitorshandler class.
31
 * This class provides simple mechanism for Suico\Visitors object
32
 */
33
class VisitorsHandler extends XoopsPersistableObjectHandler
34
{
35
    public Helper $helper;
36
    public        $isAdmin;
37
38
    /**
39
     * Constructor
40
     * @param \XoopsDatabase|null             $xoopsDatabase
41
     * @param \XoopsModules\Suico\Helper|null $helper
42
     */
43
    public function __construct(
44
        ?XoopsDatabase $xoopsDatabase = null,
45
        $helper = null
46
    ) {
47
        /** @var \XoopsModules\Suico\Helper $this- >helper */
48
        if (null === $helper) {
49
            $this->helper = Helper::getInstance();
50
        } else {
51
            $this->helper = $helper;
52
        }
53
        $this->isAdmin = $this->helper->isUserAdmin();
54
        parent::__construct($xoopsDatabase, 'suico_visitors', Visitors::class, 'visit_id', 'uname_visitor');
55
    }
56
57
    /**
58
     * create a new Groups
59
     *
60
     * @param bool $isNew flag the new objects as "new"?
61
     * @return \XoopsObject Groups
62
     */
63
    public function create(
64
        $isNew = true
65
    ) {
66
        $obj = parent::create($isNew);
67
        if ($isNew) {
68
            $obj->setNew();
69
        } else {
70
            $obj->unsetNew();
71
        }
72
        $obj->helper = $this->helper;
73
74
        return $obj;
75
    }
76
77
    /**
78
     * retrieve a Suico\Visitors
79
     *
80
     * @param int|null $id of the Suico\Visitors
81
     * @param null     $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
82
     * @return false|\XoopsModules\Suico\Visitors reference to the {@link suico_visitors} object, FALSE if failed
83
     */
84
    public function get2(
85
        $id = null,
86
        $fields = null
0 ignored issues
show
Unused Code introduced by
The parameter $fields is not used and could be removed. ( Ignorable by Annotation )

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

86
        /** @scrutinizer ignore-unused */ $fields = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    ) {
88
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_visitors') . ' WHERE visit_id=' . $id;
89
        if (!$result = $this->db->query($sql)) {
90
            return false;
91
        }
92
        $numrows = $this->db->getRowsNum($result);
93
        if (1 === $numrows) {
94
            $visitors = new Visitors();
95
            $visitors->assignVars($this->db->fetchArray($result));
96
97
            return $visitors;
98
        }
99
100
        return false;
101
    }
102
103
    /**
104
     * insert a new Visitors in the database
105
     *
106
     * @param \XoopsObject $xoopsObject     reference to the {@link Visitors}
107
     *                                      object
108
     * @param bool         $force
109
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
110
     */
111
    public function insert2(
112
        XoopsObject $xoopsObject,
113
        $force = false
114
    ) {
115
        global $xoopsConfig;
116
        if (!$xoopsObject instanceof Visitors) {
117
            return false;
118
        }
119
        if (!$xoopsObject->isDirty()) {
120
            return true;
121
        }
122
        if (!$xoopsObject->cleanVars()) {
123
            return false;
124
        }
125
        $uid_visitor = '';
126
        $uid_owner   = '';
127
        $visit_id    = '';
128
        foreach ($xoopsObject->cleanVars as $k => $v) {
129
            ${$k} = $v;
130
        }
131
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
132
        if ($xoopsObject->isNew()) {
133
            // ajout/modification d'un Suico\Visitors
134
            $xoopsObject = new Visitors();
135
            $format      = 'INSERT INTO %s (visit_id, uid_owner, uid_visitor, uname_visitor, date_visited)';
136
            $format      .= 'VALUES (%u, %u, %u, %s, %u)';
137
            $sql         = \sprintf(
138
                $format,
139
                $this->db->prefix('suico_visitors'),
140
                $visit_id,
141
                $uid_owner,
142
                $uid_visitor,
143
                $this->db->quoteString($uname_visitor),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $uname_visitor seems to be never defined.
Loading history...
144
                \time()
145
            );
146
            $force       = true;
147
        } else {
148
            $format = 'UPDATE %s SET ';
149
            $format .= 'visit_id=%u, uid_owner=%u, uid_visitor=%u, uname_visitor=%s, date_visited=%u ';
150
            $format .= ' WHERE visit_id = %u';
151
            $sql    = \sprintf(
152
                $format,
153
                $this->db->prefix('suico_visitors'),
154
                $visit_id,
155
                $uid_owner,
156
                $uid_visitor,
157
                \time(),
158
                $this->db->quoteString($uname_visitor),
159
                $visit_id
160
            );
161
        }
162
        if ($force) {
163
            $result = $this->db->queryF($sql);
164
        } else {
165
            $result = $this->db->query($sql);
166
        }
167
        if (!$result) {
168
            return false;
169
        }
170
        if (empty($visit_id)) {
171
            $visit_id = $this->db->getInsertId();
172
        }
173
        $xoopsObject->assignVar('visit_id', $visit_id);
174
175
        return true;
176
    }
177
178
    /**
179
     * delete a suico_visitors from the database
180
     *
181
     * @param \XoopsObject $xoopsObject reference to the Suico\Visitors to delete
182
     * @param bool         $force
183
     * @return bool FALSE if failed.
184
     */
185
    public function delete2(
186
        XoopsObject $xoopsObject,
187
        $force = false
188
    ) {
189
        if (!$xoopsObject instanceof Visitors) {
190
            return false;
191
        }
192
        $sql = \sprintf(
193
            'DELETE FROM %s WHERE visit_id = %u',
194
            $this->db->prefix('suico_visitors'),
195
            (int)$xoopsObject->getVar('visit_id')
196
        );
197
        if ($force) {
198
            $result = $this->db->queryF($sql);
199
        } else {
200
            $result = $this->db->query($sql);
201
        }
202
        if (!$result) {
203
            return false;
204
        }
205
206
        return true;
207
    }
208
209
    /**
210
     * count suico_visitorss matching a condition
211
     *
212
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match
213
     * @return int count of suico_visitorss
214
     */
215
    public function getCount(
216
        ?CriteriaElement $criteria = null
217
    ) {
218
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_visitors');
219
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
220
            $sql .= ' ' . $criteria->renderWhere();
0 ignored issues
show
Bug introduced by
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

220
            $sql .= ' ' . $criteria->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
221
        }
222
        $result = $this->db->query($sql);
223
        if (!$result) {
224
            return 0;
225
        }
226
        [$count] = $this->db->fetchRow($result);
227
228
        return $count;
229
    }
230
231
    /**
232
     * delete suico_visitorss matching a set of conditions
233
     *
234
     * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement}
235
     * @param bool                                 $force
236
     * @param bool                                 $asObject
237
     * @return bool FALSE if deletion failed
238
     */
239
    public function deleteAll(
240
        ?CriteriaElement $criteria = null,
241
        $force = true,
242
        $asObject = false
243
    ) {
244
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_visitors');
245
        if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
246
            $sql .= ' ' . $criteria->renderWhere();
247
        }
248
        if ($force) {
249
            if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
250
                return false;
251
            }
252
        } elseif (!$result = $this->db->query($sql)) {
253
            return false;
254
        }
255
256
        return true;
257
    }
258
259
    /**
260
     * @return bool
261
     */
262
    public function purgeVisits()
263
    {
264
        $sql = 'DELETE FROM ' . $this->db->prefix(
265
                'suico_visitors'
266
            ) . ' WHERE (date_visited<UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY))) ';
267
        if (!$result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
268
            return false;
269
        }
270
271
        return true;
272
    }
273
}
274