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

class/IshotHandler.php (1 issue)

Severity
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
use CriteriaElement;
18
use XoopsDatabase;
19
use XoopsObject;
20
use XoopsPersistableObjectHandler;
21
22
/**
23
 * @category        Module
24
 * @package         yogurt
25
 * @copyright       {@link https://xoops.org/ XOOPS Project}
26
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
27
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
28
 */
29
if (!\defined(
30
    'XOOPS_ROOT_PATH'
31
)) {
32
    exit();
33
}
34
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
35
36
// -------------------------------------------------------------------------
37
// ------------------Ishot user handler class -------------------
38
// -------------------------------------------------------------------------
39
40
/**
41
 * yogurt_ishothandler class.
42
 * This class provides simple mecanisme for Ishot object
43
 */
44
class IshotHandler extends XoopsPersistableObjectHandler
45
{
46
    public $helper;
47
48
    public $isAdmin;
49
50
    /**
51
     * Constructor
52
     * @param \XoopsDatabase|null              $xoopsDatabase
53
     * @param \XoopsModules\Yogurt\Helper|null $helper
54
     */
55
56
    public function __construct(
57
        ?XoopsDatabase $xoopsDatabase = null,
58
        $helper = null
59
    ) {
60
        /** @var \XoopsModules\Yogurt\Helper $this ->helper */
61
62
        if (null === $helper) {
63
            $this->helper = Helper::getInstance();
64
        } else {
65
            $this->helper = $helper;
66
        }
67
68
        $isAdmin = $this->helper->isUserAdmin();
69
        //        parent::__construct($db, 'yogurt_groups', Image::class, 'group_id', 'group_title');
70
    }
71
72
    /**
73
     * create a new Groups
74
     *
75
     * @param bool $isNew flag the new objects as "new"?
76
     * @return \XoopsObject Groups
77
     */
78
79
    public function create(
80
        $isNew = true
81
    ) {
82
        $obj = parent::create($isNew);
83
84
        if ($isNew) {
85
            $obj->setNew();
86
        } else {
87
            $obj->unsetNew();
88
        }
89
90
        $obj->helper = $this->helper;
91
92
        return $obj;
93
    }
94
95
    /**
96
     * retrieve a Ishot
97
     *
98
     * @param int  $id of the Ishot
99
     * @param null $fields
100
     * @return mixed reference to the {@link Ishot} object, FALSE if failed
101
     */
102
103
    public function get2(
104
        $id = null,
105
        $fields = null
106
    ) {
107
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_ishot') . ' WHERE cod_ishot=' . $id;
108
109
        if (!$result = $this->db->query($sql)) {
110
            return false;
111
        }
112
113
        $numrows = $this->db->getRowsNum($result);
114
115
        if (1 === $numrows) {
116
            $yogurt_ishot = new Ishot();
117
118
            $yogurt_ishot->assignVars($this->db->fetchArray($result));
119
120
            return $yogurt_ishot;
121
        }
122
123
        return false;
124
    }
125
126
    /**
127
     * insert a new Ishot in the database
128
     *
129
     * @param \XoopsObject $xoopsObject  reference to the {@link Ishot}
130
     *                                   object
131
     * @param bool         $force
132
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
133
     */
134
135
    public function insert2(
136
        XoopsObject $xoopsObject,
137
        $force = false
138
    ) {
139
        global $xoopsConfig;
140
141
        if (!$xoopsObject instanceof Ishot) {
142
            return false;
143
        }
144
145
        if (!$xoopsObject->isDirty()) {
146
            return true;
147
        }
148
149
        if (!$xoopsObject->cleanVars()) {
150
            return false;
151
        }
152
153
        $cod_ishot = $uid_voter = $uid_voted = $ishot = '';
154
155
        foreach ($xoopsObject->cleanVars as $k => $v) {
156
            ${$k} = $v;
157
        }
158
159
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
160
161
        if ($xoopsObject->isNew()) {
162
            // ajout/modification d'un Ishot
163
164
            $xoopsObject = new Ishot();
165
166
            $format = 'INSERT INTO %s (cod_ishot, uid_voter, uid_voted, ishot, DATE)';
167
168
            $format .= 'VALUES (%u, %u, %u, %u, %s)';
169
170
            $sql = \sprintf(
171
                $format,
172
                $this->db->prefix('yogurt_ishot'),
173
                $cod_ishot,
174
                $uid_voter,
175
                $uid_voted,
176
                $ishot,
177
                $this->db->quoteString($date)
178
            );
179
180
            $force = true;
181
        } else {
182
            $format = 'UPDATE %s SET ';
183
184
            $format .= 'cod_ishot=%u, uid_voter=%u, uid_voted=%u, ishot=%u, date_created=%s';
185
186
            $format .= ' WHERE cod_ishot = %u';
187
188
            $sql = \sprintf(
189
                $format,
190
                $this->db->prefix('yogurt_ishot'),
191
                $cod_ishot,
192
                $uid_voter,
193
                $uid_voted,
194
                $ishot,
195
                $this->db->quoteString($date),
196
                $cod_ishot
197
            );
198
        }
199
200
        if ($force) {
201
            $result = $this->db->queryF($sql);
202
        } else {
203
            $result = $this->db->query($sql);
204
        }
205
206
        if (!$result) {
207
            return false;
208
        }
209
210
        if (empty($cod_ishot)) {
0 ignored issues
show
The condition empty($cod_ishot) is always true.
Loading history...
211
            $cod_ishot = $this->db->getInsertId();
212
        }
213
214
        $xoopsObject->assignVar('cod_ishot', $cod_ishot);
215
216
        return true;
217
    }
218
219
    /**
220
     * delete a Ishot from the database
221
     *
222
     * @param \XoopsObject $xoopsObject reference to the Ishot to delete
223
     * @param bool         $force
224
     * @return bool FALSE if failed.
225
     */
226
227
    public function delete(
228
        XoopsObject $xoopsObject,
229
        $force = false
230
    ) {
231
        if (!$xoopsObject instanceof Ishot) {
232
            return false;
233
        }
234
235
        $sql = \sprintf(
236
            'DELETE FROM %s WHERE cod_ishot = %u',
237
            $this->db->prefix('yogurt_ishot'),
238
            $xoopsObject->getVar('cod_ishot')
239
        );
240
241
        if ($force) {
242
            $result = $this->db->queryF($sql);
243
        } else {
244
            $result = $this->db->query($sql);
245
        }
246
247
        if (!$result) {
248
            return false;
249
        }
250
251
        return true;
252
    }
253
254
    /**
255
     * retrieve yogurt_ishots from the database
256
     *
257
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
258
     * @param bool                                 $id_as_key       use the UID as key for the array?
259
     * @param bool                                 $as_object
260
     * @return array array of {@link Ishot} objects
261
     */
262
263
    public function &getObjects(
264
        ?CriteriaElement $criteriaElement = null,
265
        $id_as_key = false,
266
        $as_object = true
267
    ) {
268
        $ret = [];
269
270
        $limit = $start = 0;
271
272
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_ishot');
273
274
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
275
            $sql .= ' ' . $criteriaElement->renderWhere();
276
277
            if ('' !== $criteriaElement->getSort()) {
278
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
279
            }
280
281
            $limit = $criteriaElement->getLimit();
282
283
            $start = $criteriaElement->getStart();
284
        }
285
286
        $result = $this->db->query($sql, $limit, $start);
287
288
        if (!$result) {
289
            return $ret;
290
        }
291
292
        while (false !== ($myrow = $this->db->fetchArray($result))) {
293
            $yogurt_ishot = new Ishot();
294
295
            $yogurt_ishot->assignVars($myrow);
296
297
            if (!$id_as_key) {
298
                $ret[] = &$yogurt_ishot;
299
            } else {
300
                $ret[$myrow['cod_ishot']] = &$yogurt_ishot;
301
            }
302
303
            unset($yogurt_ishot);
304
        }
305
306
        return $ret;
307
    }
308
309
    /**
310
     * count yogurt_ishots matching a condition
311
     *
312
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} to match
313
     * @return int count of yogurt_ishots
314
     */
315
316
    public function getCount(
317
        ?CriteriaElement $criteriaElement = null
318
    ) {
319
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_ishot');
320
321
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
322
            $sql .= ' ' . $criteriaElement->renderWhere();
323
        }
324
325
        $result = $this->db->query($sql);
326
327
        if (!$result) {
328
            return 0;
329
        }
330
331
        [$count] = $this->db->fetchRow($result);
332
333
        return $count;
334
    }
335
336
    /**
337
     * delete yogurt_ishots matching a set of conditions
338
     *
339
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement}
340
     * @param bool                                 $force
341
     * @param bool                                 $asObject
342
     * @return bool FALSE if deletion failed
343
     */
344
345
    public function deleteAll(
346
        ?CriteriaElement $criteriaElement = null,
347
        $force = true,
348
        $asObject = false
349
    ) {
350
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_ishot');
351
352
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
353
            $sql .= ' ' . $criteriaElement->renderWhere();
354
        }
355
356
        if (!$result = $this->db->query($sql)) {
357
            return false;
358
        }
359
360
        return true;
361
    }
362
363
    /**
364
     * @param null $criteria
365
     * @return array
366
     */
367
368
    public function getHottest($criteria = null)
369
    {
370
        $sql = 'SELECT DISTINCTROW uname, user_avatar, uid_voted, COUNT(cod_ishot) AS qtd FROM ' . $this->db->prefix(
371
                'yogurt_ishot'
372
            ) . ', ' . $this->db->prefix(
373
                'users'
374
            );
375
376
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
377
            $sql .= ' ' . $criteria->renderWhere();
378
        }
379
380
        //attention here this is kind of a hack
381
382
        $sql .= ' AND uid = uid_voted';
383
384
        if ('' !== $criteria->getGroupby()) {
385
            $sql .= $criteria->getGroupby();
386
        }
387
388
        if ('' !== $criteria->getSort()) {
389
            $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
390
        }
391
392
        $limit = $criteria->getLimit();
393
394
        $start = $criteria->getStart();
395
396
        $result = $this->db->query($sql, $limit, $start);
397
398
        $vetor = [];
399
400
        $i = 0;
401
402
        while (false !== ($myrow = $this->db->fetchArray($result))) {
403
            $vetor[$i]['qtd'] = $myrow['qtd'];
404
405
            $vetor[$i]['uid_voted'] = $myrow['uid_voted'];
406
407
            $vetor[$i]['uname'] = $myrow['uname'];
408
409
            $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
410
411
            $i++;
412
        }
413
414
        return $vetor;
415
    }
416
417
    /**
418
     * @param null $criteria
419
     * @param bool $id_as_key
420
     * @return array
421
     */
422
423
    public function getHotFriends(
424
        $criteria = null,
425
        $id_as_key = false
426
    ) {
427
        $ret = [];
428
429
        $limit = $start = 0;
430
431
        $sql = 'SELECT uname, user_avatar, uid_voted FROM ' . $this->db->prefix(
432
                'yogurt_ishot'
433
            ) . ', ' . $this->db->prefix(
434
                'users'
435
            );
436
437
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
438
            $sql .= ' ' . $criteria->renderWhere();
439
440
            //attention here this is kind of a hack
441
442
            $sql .= ' AND uid = uid_voted AND ishot=1';
443
444
            if ('' !== $criteria->getSort()) {
445
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
446
            }
447
448
            $limit = $criteria->getLimit();
449
450
            $start = $criteria->getStart();
451
452
            $result = $this->db->query($sql, $limit, $start);
453
454
            $vetor = [];
455
456
            $i = 0;
457
458
            while (false !== ($myrow = $this->db->fetchArray($result))) {
459
                $vetor[$i]['uid_voted'] = $myrow['uid_voted'];
460
461
                $vetor[$i]['uname'] = $myrow['uname'];
462
463
                $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
464
465
                $i++;
466
            }
467
468
            return $vetor;
469
        }
470
    }
471
}
472