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

RelgroupuserHandler::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

100
        /** @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...
101
    ) {
102
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_relgroupuser') . ' WHERE rel_id=' . $id;
103
        if (!$result = $this->db->query($sql)) {
104
            return false;
105
        }
106
        $numrows = $this->db->getRowsNum($result);
107
        if (1 === $numrows) {
108
            $yogurt_relgroupuser = new Relgroupuser();
109
            $yogurt_relgroupuser->assignVars($this->db->fetchArray($result));
110
111
            return $yogurt_relgroupuser;
112
        }
113
114
        return false;
115
    }
116
117
    /**
118
     * insert a new Relgroupuser in the database
119
     *
120
     * @param \XoopsObject $xoopsObject         reference to the {@link Relgroupuser}
121
     *                                          object
122
     * @param bool         $force
123
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
124
     */
125
    public function insert2(
126
        XoopsObject $xoopsObject,
127
        $force = false
128
    ) {
129
        global $xoopsConfig;
130
        if (!$xoopsObject instanceof Relgroupuser) {
131
            return false;
132
        }
133
        if (!$xoopsObject->isDirty()) {
134
            return true;
135
        }
136
        if (!$xoopsObject->cleanVars()) {
137
            return false;
138
        }
139
        foreach ($xoopsObject->cleanVars as $k => $v) {
140
            ${$k} = $v;
141
        }
142
        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
0 ignored issues
show
Unused Code introduced by
The assignment to $now is dead and can be removed.
Loading history...
143
        if ($xoopsObject->isNew()) {
144
            // ajout/modification d'un Relgroupuser
145
            $xoopsObject = new Relgroupuser();
146
            $format      = 'INSERT INTO %s (rel_id, rel_group_id, rel_user_uid)';
147
            $format      .= 'VALUES (%u, %u, %u)';
148
            $sql         = \sprintf($format, $this->db->prefix('yogurt_relgroupuser'), $relId, $rel_group_id, $rel_user_uid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rel_group_id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $relId seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $rel_user_uid seems to be never defined.
Loading history...
149
            $force       = true;
150
        } else {
151
            $format = 'UPDATE %s SET ';
152
            $format .= 'rel_id=%u, rel_group_id=%u, rel_user_uid=%u';
153
            $format .= ' WHERE rel_id = %u';
154
            $sql    = \sprintf(
155
                $format,
156
                $this->db->prefix('yogurt_relgroupuser'),
157
                $relId,
158
                $rel_group_id,
159
                $rel_user_uid,
160
                $relId
161
            );
162
        }
163
        if ($force) {
164
            $result = $this->db->queryF($sql);
165
        } else {
166
            $result = $this->db->query($sql);
167
        }
168
        if (!$result) {
169
            return false;
170
        }
171
        if (empty($relId)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $relId seems to never exist and therefore empty should always be true.
Loading history...
172
            $relId = $this->db->getInsertId();
173
        }
174
        $xoopsObject->assignVar('rel_id', $relId);
175
176
        return true;
177
    }
178
179
    /**
180
     * delete a Relgroupuser from the database
181
     *
182
     * @param \XoopsObject $xoopsObject reference to the Relgroupuser to delete
183
     * @param bool         $force
184
     * @return bool FALSE if failed.
185
     */
186
    public function delete(
187
        XoopsObject $xoopsObject,
188
        $force = false
189
    ) {
190
        if (!$xoopsObject instanceof Relgroupuser) {
191
            return false;
192
        }
193
        $sql = \sprintf(
194
            'DELETE FROM %s WHERE rel_id = %u',
195
            $this->db->prefix('yogurt_relgroupuser'),
196
            $xoopsObject->getVar('rel_id')
0 ignored issues
show
Bug introduced by
It seems like $xoopsObject->getVar('rel_id') 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

196
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('rel_id')
Loading history...
197
        );
198
        if ($force) {
199
            $result = $this->db->queryF($sql);
200
        } else {
201
            $result = $this->db->query($sql);
202
        }
203
        if (!$result) {
204
            return false;
205
        }
206
207
        return true;
208
    }
209
210
    /**
211
     * retrieve yogurt_relgroupusers from the database
212
     *
213
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
214
     * @param bool                                 $id_as_key       use the UID as key for the array?
215
     * @param bool                                 $as_object
216
     * @return array array of {@link Relgroupuser} objects
217
     */
218
    public function &getObjects(
219
        ?CriteriaElement $criteriaElement = null,
220
        $id_as_key = false,
221
        $as_object = true
222
    ) {
223
        $ret   = [];
224
        $limit = $start = 0;
225
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_relgroupuser');
226
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
227
            $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

227
            $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...
228
            if ('' !== $criteriaElement->getSort()) {
229
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
230
            }
231
            $limit = $criteriaElement->getLimit();
232
            $start = $criteriaElement->getStart();
233
        }
234
        $result = $this->db->query($sql, $limit, $start);
235
        if (!$result) {
236
            return $ret;
237
        }
238
        while (false !== ($myrow = $this->db->fetchArray($result))) {
239
            $yogurt_relgroupuser = new Relgroupuser();
240
            $yogurt_relgroupuser->assignVars($myrow);
241
            if (!$id_as_key) {
242
                $ret[] = &$yogurt_relgroupuser;
243
            } else {
244
                $ret[$myrow['rel_id']] = &$yogurt_relgroupuser;
245
            }
246
            unset($yogurt_relgroupuser);
247
        }
248
249
        return $ret;
250
    }
251
252
    /**
253
     * count yogurt_relgroupusers matching a condition
254
     *
255
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
256
     * @return int count of yogurt_relgroupusers
257
     */
258
    public function getCount(
259
        ?CriteriaElement $criteriaElement = null
260
    ) {
261
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_relgroupuser');
262
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
263
            $sql .= ' ' . $criteriaElement->renderWhere();
264
        }
265
        $result = $this->db->query($sql);
266
        if (!$result) {
267
            return 0;
268
        }
269
        [$count] = $this->db->fetchRow($result);
270
271
        return (int)$count;
272
    }
273
274
    /**
275
     * delete yogurt_relgroupusers matching a set of conditions
276
     *
277
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
278
     * @param bool                                 $force
279
     * @param bool                                 $asObject
280
     * @return bool FALSE if deletion failed
281
     */
282
    public function deleteAll(
283
        ?CriteriaElement $criteriaElement = null,
284
        $force = true,
285
        $asObject = false
286
    ) {
287
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_relgroupuser');
288
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
289
            $sql .= ' ' . $criteriaElement->renderWhere();
290
        }
291
        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...
292
            return false;
293
        }
294
295
        return true;
296
    }
297
298
    /**
299
     * @param      $nbgroups
300
     * @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...
301
     * @param int  $shuffle
302
     * @return array
303
     */
304
    public function getGroups(
305
        $nbgroups,
306
        $criteria = null,
307
        $shuffle = 1
308
    ) {
309
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
310
311
        $sql = 'SELECT rel_id, rel_group_id, rel_user_uid, group_title, group_desc, group_img, owner_uid FROM ' . $this->db->prefix(
312
                'yogurt_groups'
313
            ) . ', ' . $this->db->prefix(
314
                'yogurt_relgroupuser'
315
            );
316
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
317
            $sql .= ' ' . $criteria->renderWhere();
318
            //attention here this is kind of a hack
319
            $sql .= ' AND group_id = rel_group_id ';
320
            if ('' !== $criteria->getSort()) {
321
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
322
            }
323
            $limit = $criteria->getLimit();
324
            $start = $criteria->getStart();
325
326
            $result = $this->db->query($sql, $limit, $start);
327
            $vetor  = [];
328
            $i      = 0;
329
330
            while (false !== ($myrow = $this->db->fetchArray($result))) {
331
                $vetor[$i]['title']    = $myrow['group_title'];
332
                $vetor[$i]['desc']     = $myrow['group_desc'];
333
                $vetor[$i]['img']      = $myrow['group_img'];
334
                $vetor[$i]['id']       = $myrow['rel_id'];
335
                $vetor[$i]['uid']      = $myrow['owner_uid'];
336
                $vetor[$i]['group_id'] = $myrow['rel_group_id'];
337
338
                $i++;
339
            }
340
341
            if (1 === $shuffle) {
342
                \shuffle($vetor);
343
                $vetor = \array_slice($vetor, 0, $nbgroups);
344
            }
345
346
            return $vetor;
347
        }
348
    }
349
350
    /**
351
     * @param     $groupId
352
     * @param     $start
353
     * @param     $nbUsers
354
     * @param int $isShuffle
355
     * @return array
356
     */
357
    public function getUsersFromGroup(
358
        $groupId,
359
        $start,
360
        $nbUsers,
361
        $isShuffle = 0
362
    ) {
363
        $ret = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
364
365
        $sql = 'SELECT rel_group_id, rel_user_uid, owner_uid, uname, user_avatar, uid FROM ' . $this->db->prefix(
366
                'users'
367
            ) . ', ' . $this->db->prefix(
368
                'yogurt_groups'
369
            ) . ', ' . $this->db->prefix(
370
                'yogurt_relgroupuser'
371
            );
372
        $sql .= ' WHERE rel_user_uid = uid AND rel_group_id = group_id AND group_id =' . $groupId . ' GROUP BY rel_user_uid ';
373
374
        $result = $this->db->query($sql, $nbUsers, $start);
375
        $ret    = [];
376
        $i      = 0;
377
378
        while (false !== ($myrow = $this->db->fetchArray($result))) {
379
            $ret[$i]['uid']     = $myrow['uid'];
380
            $ret[$i]['uname']   = $myrow['uname'];
381
            $ret[$i]['avatar']  = $myrow['user_avatar'];
382
            $isOwner            = $myrow['rel_user_uid'] === $myrow['owner_uid'] ? 1 : 0;
383
            $ret[$i]['isOwner'] = $isOwner;
384
            $i++;
385
        }
386
387
        if (1 === $isShuffle) {
388
            \shuffle($ret);
389
            $ret = \array_slice($ret, 0, $nbUsers);
390
        }
391
392
        return $ret;
393
    }
394
}
395