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

IshotHandler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 2
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Yogurt;
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
use CriteriaElement;
16
use XoopsDatabase;
17
use XoopsObject;
18
use XoopsPersistableObjectHandler;
19
20
/**
21
 * @copyright    XOOPS Project https://xoops.org/
22
 * @license      GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23
 * @author       Marcello Brandão aka  Suico
24
 * @author       XOOPS Development Team
25
 * @since
26
 */
27
if (!\defined(
28
    'XOOPS_ROOT_PATH'
29
)) {
30
    exit();
31
}
32
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
33
34
// -------------------------------------------------------------------------
35
// ------------------Ishot user handler class -------------------
36
// -------------------------------------------------------------------------
37
38
/**
39
 * yogurt_ishothandler class.
40
 * This class provides simple mecanisme for Ishot object
41
 */
42
class IshotHandler extends XoopsPersistableObjectHandler
43
{
44
    public $helper;
45
46
    public $isAdmin;
47
48
    /**
49
     * Constructor
50
     * @param \XoopsDatabase|null              $xoopsDatabase
51
     * @param \XoopsModules\Yogurt\Helper|null $helper
52
     */
53
    public function __construct(
54
        ?XoopsDatabase $xoopsDatabase = null,
55
        $helper = null
56
    ) {
57
        /** @var \XoopsModules\Yogurt\Helper $this ->helper */
58
        if (null === $helper) {
59
            $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Yogurt\Helper.
Loading history...
60
        } else {
61
            $this->helper = $helper;
62
        }
63
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
64
        //        parent::__construct($db, 'yogurt_groups', Image::class, 'group_id', 'group_title');
65
    }
66
67
    /**
68
     * create a new Groups
69
     *
70
     * @param bool $isNew flag the new objects as "new"?
71
     * @return \XoopsObject Groups
72
     */
73
    public function create(
74
        $isNew = true
75
    ) {
76
        $obj = parent::create($isNew);
77
        if ($isNew) {
78
            $obj->setNew();
79
        } else {
80
            $obj->unsetNew();
81
        }
82
        $obj->helper = $this->helper;
83
84
        return $obj;
85
    }
86
87
    /**
88
     * retrieve a Ishot
89
     *
90
     * @param int  $id of the Ishot
91
     * @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...
92
     * @return mixed reference to the {@link Ishot} object, FALSE if failed
93
     */
94
    public function get2(
95
        $id = null,
96
        $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

96
        /** @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...
97
    ) {
98
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_ishot') . ' WHERE cod_ishot=' . $id;
99
        if (!$result = $this->db->query($sql)) {
100
            return false;
101
        }
102
        $numrows = $this->db->getRowsNum($result);
103
        if (1 === $numrows) {
104
            $yogurt_ishot = new Ishot();
105
            $yogurt_ishot->assignVars($this->db->fetchArray($result));
106
107
            return $yogurt_ishot;
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * insert a new Ishot in the database
115
     *
116
     * @param \XoopsObject $xoopsObject  reference to the {@link Ishot}
117
     *                                   object
118
     * @param bool         $force
119
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
120
     */
121
    public function insert2(
122
        XoopsObject $xoopsObject,
123
        $force = false
124
    ) {
125
        global $xoopsConfig;
126
        if (!$xoopsObject instanceof Ishot) {
127
            return false;
128
        }
129
        if (!$xoopsObject->isDirty()) {
130
            return true;
131
        }
132
        if (!$xoopsObject->cleanVars()) {
133
            return false;
134
        }
135
136
        $cod_ishot = $uid_voter = $uid_voted = $ishot = '';
137
138
        foreach ($xoopsObject->cleanVars as $k => $v) {
139
            ${$k} = $v;
140
        }
141
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
142
143
        if ($xoopsObject->isNew()) {
144
            // ajout/modification d'un Ishot
145
            $xoopsObject = new Ishot();
146
            $format      = 'INSERT INTO %s (cod_ishot, uid_voter, uid_voted, ishot, DATE)';
147
            $format      .= 'VALUES (%u, %u, %u, %u, %s)';
148
            $sql         = \sprintf(
149
                $format,
150
                $this->db->prefix('yogurt_ishot'),
151
                $cod_ishot,
152
                $uid_voter,
153
                $uid_voted,
154
                $ishot,
155
                $this->db->quoteString($date)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
156
            );
157
            $force       = true;
158
        } else {
159
            $format = 'UPDATE %s SET ';
160
            $format .= 'cod_ishot=%u, uid_voter=%u, uid_voted=%u, ishot=%u, date_created=%s';
161
            $format .= ' WHERE cod_ishot = %u';
162
            $sql    = \sprintf(
163
                $format,
164
                $this->db->prefix('yogurt_ishot'),
165
                $cod_ishot,
166
                $uid_voter,
167
                $uid_voted,
168
                $ishot,
169
                $this->db->quoteString($date),
170
                $cod_ishot
171
            );
172
        }
173
        if ($force) {
174
            $result = $this->db->queryF($sql);
175
        } else {
176
            $result = $this->db->query($sql);
177
        }
178
        if (!$result) {
179
            return false;
180
        }
181
        if (empty($cod_ishot)) {
0 ignored issues
show
introduced by
The condition empty($cod_ishot) is always true.
Loading history...
182
            $cod_ishot = $this->db->getInsertId();
183
        }
184
        $xoopsObject->assignVar('cod_ishot', $cod_ishot);
185
186
        return true;
187
    }
188
189
    /**
190
     * delete a Ishot from the database
191
     *
192
     * @param \XoopsObject $xoopsObject reference to the Ishot to delete
193
     * @param bool         $force
194
     * @return bool FALSE if failed.
195
     */
196
    public function delete(
197
        XoopsObject $xoopsObject,
198
        $force = false
199
    ) {
200
        if (!$xoopsObject instanceof Ishot) {
201
            return false;
202
        }
203
        $sql = \sprintf(
204
            'DELETE FROM %s WHERE cod_ishot = %u',
205
            $this->db->prefix('yogurt_ishot'),
206
            $xoopsObject->getVar('cod_ishot')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('cod_ishot') can also be of type array and array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

206
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('cod_ishot')
Loading history...
207
        );
208
        if ($force) {
209
            $result = $this->db->queryF($sql);
210
        } else {
211
            $result = $this->db->query($sql);
212
        }
213
        if (!$result) {
214
            return false;
215
        }
216
217
        return true;
218
    }
219
220
    /**
221
     * retrieve yogurt_ishots from the database
222
     *
223
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
224
     * @param bool                                 $id_as_key       use the UID as key for the array?
225
     * @param bool                                 $as_object
226
     * @return array array of {@link Ishot} objects
227
     */
228
    public function &getObjects(
229
        ?CriteriaElement $criteriaElement = null,
230
        $id_as_key = false,
231
        $as_object = true
232
    ) {
233
        $ret   = [];
234
        $limit = $start = 0;
235
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_ishot');
236
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
237
            $sql .= ' ' . $criteriaElement->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

237
            $sql .= ' ' . $criteriaElement->/** @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...
238
            if ('' !== $criteriaElement->getSort()) {
239
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
240
            }
241
            $limit = $criteriaElement->getLimit();
242
            $start = $criteriaElement->getStart();
243
        }
244
        $result = $this->db->query($sql, $limit, $start);
245
        if (!$result) {
246
            return $ret;
247
        }
248
        while (false !== ($myrow = $this->db->fetchArray($result))) {
249
            $yogurt_ishot = new Ishot();
250
            $yogurt_ishot->assignVars($myrow);
251
            if (!$id_as_key) {
252
                $ret[] = &$yogurt_ishot;
253
            } else {
254
                $ret[$myrow['cod_ishot']] = &$yogurt_ishot;
255
            }
256
            unset($yogurt_ishot);
257
        }
258
259
        return $ret;
260
    }
261
262
    /**
263
     * count yogurt_ishots matching a condition
264
     *
265
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement} to match
266
     * @return int count of yogurt_ishots
267
     */
268
    public function getCount(
269
        ?CriteriaElement $criteriaElement = null
270
    ) {
271
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_ishot');
272
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
273
            $sql .= ' ' . $criteriaElement->renderWhere();
274
        }
275
        $result = $this->db->query($sql);
276
        if (!$result) {
277
            return 0;
278
        }
279
        [$count] = $this->db->fetchRow($result);
280
281
        return $count;
282
    }
283
284
    /**
285
     * delete yogurt_ishots matching a set of conditions
286
     *
287
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link CriteriaElement}
288
     * @param bool                                 $force
289
     * @param bool                                 $asObject
290
     * @return bool FALSE if deletion failed
291
     */
292
    public function deleteAll(
293
        ?CriteriaElement $criteriaElement = null,
294
        $force = true,
295
        $asObject = false
296
    ) {
297
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_ishot');
298
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
299
            $sql .= ' ' . $criteriaElement->renderWhere();
300
        }
301
        if (!$result = $this->db->query($sql)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
302
            return false;
303
        }
304
305
        return true;
306
    }
307
308
    /**
309
     * @param null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
310
     * @return array
311
     */
312
    public function getHottest($criteria = null)
313
    {
314
        $sql = 'SELECT DISTINCTROW uname, user_avatar, uid_voted, COUNT(cod_ishot) AS qtd FROM ' . $this->db->prefix(
315
                'yogurt_ishot'
316
            ) . ', ' . $this->db->prefix(
317
                'users'
318
            );
319
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
320
            $sql .= ' ' . $criteria->renderWhere();
321
        }
322
        //attention here this is kind of a hack
323
        $sql .= ' AND uid = uid_voted';
324
        if ('' !== $criteria->getGroupby()) {
325
            $sql .= $criteria->getGroupby();
326
        }
327
        if ('' !== $criteria->getSort()) {
328
            $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
329
        }
330
        $limit = $criteria->getLimit();
331
        $start = $criteria->getStart();
332
333
        $result = $this->db->query($sql, $limit, $start);
334
        $vetor  = [];
335
        $i      = 0;
336
        while (false !== ($myrow = $this->db->fetchArray($result))) {
337
            $vetor[$i]['qtd']         = $myrow['qtd'];
338
            $vetor[$i]['uid_voted']   = $myrow['uid_voted'];
339
            $vetor[$i]['uname']       = $myrow['uname'];
340
            $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
341
            $i++;
342
        }
343
344
        return $vetor;
345
    }
346
347
    /**
348
     * @param null $criteria
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $criteria is correct as it would always require null to be passed?
Loading history...
349
     * @param bool $id_as_key
350
     * @return array
351
     */
352
    public function getHotFriends(
353
        $criteria = null,
354
        $id_as_key = false
0 ignored issues
show
Unused Code introduced by
The parameter $id_as_key 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

354
        /** @scrutinizer ignore-unused */ $id_as_key = false

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...
355
    ) {
356
        $ret   = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
357
        $limit = $start = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $start is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $limit is dead and can be removed.
Loading history...
358
        $sql   = 'SELECT uname, user_avatar, uid_voted FROM ' . $this->db->prefix(
359
                'yogurt_ishot'
360
            ) . ', ' . $this->db->prefix(
361
                'users'
362
            );
363
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
364
            $sql .= ' ' . $criteria->renderWhere();
365
            //attention here this is kind of a hack
366
            $sql .= ' AND uid = uid_voted AND ishot=1';
367
            if ('' !== $criteria->getSort()) {
368
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
369
            }
370
            $limit = $criteria->getLimit();
371
            $start = $criteria->getStart();
372
373
            $result = $this->db->query($sql, $limit, $start);
374
            $vetor  = [];
375
            $i      = 0;
376
            while (false !== ($myrow = $this->db->fetchArray($result))) {
377
                $vetor[$i]['uid_voted']   = $myrow['uid_voted'];
378
                $vetor[$i]['uname']       = $myrow['uname'];
379
                $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
380
                $i++;
381
            }
382
383
            return $vetor;
384
        }
385
    }
386
}
387