Passed
Pull Request — master (#81)
by Michael
02:58
created

class/VisitorsHandler.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         yogurt
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use CriteriaElement;
26
use XoopsDatabase;
27
use XoopsObject;
28
use XoopsPersistableObjectHandler;
29
30
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
31
32
/**
33
 * yogurt_visitorshandler class.
34
 * This class provides simple mecanisme for Yogurt\Visitors object
35
 */
36
class VisitorsHandler extends XoopsPersistableObjectHandler
37
{
38
    public $helper;
39
40
    public $isAdmin;
41
42
    /**
43
     * Constructor
44
     * @param \XoopsDatabase|null              $xoopsDatabase
45
     * @param \XoopsModules\Yogurt\Helper|null $helper
46
     */
47
48
    public function __construct(
49
        ?XoopsDatabase $xoopsDatabase = null,
50
        $helper = null
51
    ) {
52
        /** @var \XoopsModules\Yogurt\Helper $this ->helper */
53
54
        if (null === $helper) {
55
            $this->helper = Helper::getInstance();
56
        } else {
57
            $this->helper = $helper;
58
        }
59
60
        $isAdmin = $this->helper->isUserAdmin();
61
62
        parent::__construct($xoopsDatabase, 'yogurt_visitors', Visitors::class, 'cod_visit', 'uname_visitor');
63
    }
64
65
    /**
66
     * create a new Groups
67
     *
68
     * @param bool $isNew flag the new objects as "new"?
69
     * @return \XoopsObject Groups
70
     */
71
72
    public function create(
73
        $isNew = true
74
    ) {
75
        $obj = parent::create($isNew);
76
77
        if ($isNew) {
78
            $obj->setNew();
79
        } else {
80
            $obj->unsetNew();
81
        }
82
83
        $obj->helper = $this->helper;
84
85
        return $obj;
86
    }
87
88
    /**
89
     * retrieve a Yogurt\Visitors
90
     *
91
     * @param int  $id of the Yogurt\Visitors
92
     * @param null $fields
93
     * @return mixed reference to the {@link yogurt_visitors} object, FALSE if failed
94
     */
95
96
    public function get2(
97
        $id = null,
98
        $fields = null
99
    ) {
100
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_visitors') . ' WHERE cod_visit=' . $id;
101
102
        if (!$result = $this->db->query($sql)) {
103
            return false;
104
        }
105
106
        $numrows = $this->db->getRowsNum($result);
107
108
        if (1 === $numrows) {
109
            $visitors = new Visitors();
110
111
            $visitors->assignVars($this->db->fetchArray($result));
112
113
            return $visitors;
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * insert a new Visitors in the database
121
     *
122
     * @param \XoopsObject $xoopsObject     reference to the {@link Visitors}
123
     *                                      object
124
     * @param bool         $force
125
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
126
     */
127
128
    public function insert2(
129
        XoopsObject $xoopsObject,
130
        $force = false
131
    ) {
132
        global $xoopsConfig;
133
134
        if (!$xoopsObject instanceof Visitors) {
135
            return false;
136
        }
137
138
        if (!$xoopsObject->isDirty()) {
139
            return true;
140
        }
141
142
        if (!$xoopsObject->cleanVars()) {
143
            return false;
144
        }
145
146
        $cod_visit = $uid_owner = $uid_visitor = '';
147
148
        foreach ($xoopsObject->cleanVars as $k => $v) {
149
            ${$k} = $v;
150
        }
151
152
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
153
154
        if ($xoopsObject->isNew()) {
155
            // ajout/modification d'un Yogurt\Visitors
156
157
            $xoopsObject = new Visitors();
158
159
            $format = 'INSERT INTO %s (cod_visit, uid_owner, uid_visitor,uname_visitor)';
160
161
            $format .= 'VALUES (%u, %u, %u, %s)';
162
163
            $sql = \sprintf(
164
                $format,
165
                $this->db->prefix('yogurt_visitors'),
166
                $cod_visit,
167
                $uid_owner,
168
                $uid_visitor,
169
                $this->db->quoteString($uname_visitor)
170
            );
171
172
            $force = true;
173
        } else {
174
            $format = 'UPDATE %s SET ';
175
176
            $format .= 'cod_visit=%u, uid_owner=%u, uid_visitor=%u, uname_visitor=%s ';
177
178
            $format .= ' WHERE cod_visit = %u';
179
180
            $sql = \sprintf(
181
                $format,
182
                $this->db->prefix('yogurt_visitors'),
183
                $cod_visit,
184
                $uid_owner,
185
                $uid_visitor,
186
                $this->db->quoteString($uname_visitor),
187
                $cod_visit
188
            );
189
        }
190
191
        if ($force) {
192
            $result = $this->db->queryF($sql);
193
        } else {
194
            $result = $this->db->query($sql);
195
        }
196
197
        if (!$result) {
198
            return false;
199
        }
200
201
        if (empty($cod_visit)) {
0 ignored issues
show
The condition empty($cod_visit) is always true.
Loading history...
202
            $cod_visit = $this->db->getInsertId();
203
        }
204
205
        $xoopsObject->assignVar('cod_visit', $cod_visit);
206
207
        return true;
208
    }
209
210
    /**
211
     * delete a yogurt_visitors from the database
212
     *
213
     * @param \XoopsObject $xoopsObject reference to the Yogurt\Visitors to delete
214
     * @param bool         $force
215
     * @return bool FALSE if failed.
216
     */
217
218
    public function delete2(
219
        XoopsObject $xoopsObject,
220
        $force = false
221
    ) {
222
        if (!$xoopsObject instanceof Visitors) {
223
            return false;
224
        }
225
226
        $sql = \sprintf(
227
            'DELETE FROM %s WHERE cod_visit = %u',
228
            $this->db->prefix('yogurt_visitors'),
229
            $xoopsObject->getVar('cod_visit')
230
        );
231
232
        if ($force) {
233
            $result = $this->db->queryF($sql);
234
        } else {
235
            $result = $this->db->query($sql);
236
        }
237
238
        if (!$result) {
239
            return false;
240
        }
241
242
        return true;
243
    }
244
245
    /**
246
     * retrieve yogurt_visitorss from the database
247
     *
248
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
249
     * @param bool                                 $id_as_key       use the UID as key for the array?
250
     * @param bool                                 $as_object
251
     * @return array array of {@link Visitors} objects
252
     */
253
254
    public function &getObjects(
255
        ?CriteriaElement $criteriaElement = null,
256
        $id_as_key = false,
257
        $as_object = true
258
    ) {
259
        $ret = [];
260
261
        $limit = $start = 0;
262
263
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_visitors');
264
265
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
266
            $sql .= ' ' . $criteriaElement->renderWhere();
267
268
            if ('' !== $criteriaElement->getSort()) {
269
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
270
            }
271
272
            $limit = $criteriaElement->getLimit();
273
274
            $start = $criteriaElement->getStart();
275
        }
276
277
        $result = $this->db->query($sql, $limit, $start);
278
279
        if (!$result) {
280
            return $ret;
281
        }
282
283
        while (false !== ($myrow = $this->db->fetchArray($result))) {
284
            $visitors = new Visitors();
285
286
            $visitors->assignVars($myrow);
287
288
            if (!$id_as_key) {
289
                $ret[] = &$yogurt_visitors;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $yogurt_visitors does not exist. Did you maybe mean $visitors?
Loading history...
290
            } else {
291
                $ret[$myrow['cod_visit']] = &$yogurt_visitors;
292
            }
293
294
            unset($yogurt_visitors);
295
        }
296
297
        return $ret;
298
    }
299
300
    /**
301
     * count yogurt_visitorss matching a condition
302
     *
303
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
304
     * @return int count of yogurt_visitorss
305
     */
306
307
    public function getCount(
308
        ?CriteriaElement $criteriaElement = null
309
    ) {
310
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_visitors');
311
312
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
313
            $sql .= ' ' . $criteriaElement->renderWhere();
314
        }
315
316
        $result = $this->db->query($sql);
317
318
        if (!$result) {
319
            return 0;
320
        }
321
322
        [$count] = $this->db->fetchRow($result);
323
324
        return $count;
325
    }
326
327
    /**
328
     * delete yogurt_visitorss matching a set of conditions
329
     *
330
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
331
     * @param bool                                 $force
332
     * @param bool                                 $asObject
333
     * @return bool FALSE if deletion failed
334
     */
335
336
    public function deleteAll(
337
        ?CriteriaElement $criteriaElement = null,
338
        $force = true,
339
        $asObject = false
340
    ) {
341
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_visitors');
342
343
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
344
            $sql .= ' ' . $criteriaElement->renderWhere();
345
        }
346
347
        if ($force) {
348
            if (!$result = $this->db->queryF($sql)) {
349
                return false;
350
            }
351
        } elseif (!$result = $this->db->query($sql)) {
352
            return false;
353
        }
354
355
        return true;
356
    }
357
358
    /**
359
     * @return bool
360
     */
361
362
    public function purgeVisits()
363
    {
364
        $sql = 'DELETE FROM ' . $this->db->prefix(
365
                'yogurt_visitors'
366
            ) . ' WHERE (date_visited<(DATE_SUB(NOW(), INTERVAL 7 DAY))) ';
367
368
        if (!$result = $this->db->queryF($sql)) {
369
            return false;
370
        }
371
372
        return true;
373
    }
374
}
375