Passed
Push — master ( 06e1aa...b7b18e )
by
unknown
03:40 queued 51s
created

RelgroupuserHandler::getGroups()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 39
rs 8.8337
c 0
b 0
f 0
cc 6
nc 9
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
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         suico
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
 * suico_relgroupuserhandler class.
34
 * This class provides simple mechanism for Relgroupuser object
35
 */
36
class RelgroupuserHandler extends XoopsPersistableObjectHandler
37
{
38
    public $helper;
39
    public $isAdmin;
40
41
    /**
42
     * Constructor
43
     * @param \XoopsDatabase|null             $xoopsDatabase
44
     * @param \XoopsModules\Suico\Helper|null $helper
45
     */
46
    public function __construct(
47
        ?XoopsDatabase $xoopsDatabase = null,
48
        $helper = null
49
    ) {
50
        /** @var \XoopsModules\Suico\Helper $this ->helper */
51
        if (null === $helper) {
52
            $this->helper = Helper::getInstance();
0 ignored issues
show
Bug introduced by
The property helper does not seem to exist on XoopsModules\Suico\Helper.
Loading history...
53
        } else {
54
            $this->helper = $helper;
55
        }
56
        $isAdmin = $this->helper->isUserAdmin();
0 ignored issues
show
Unused Code introduced by
The assignment to $isAdmin is dead and can be removed.
Loading history...
57
        parent::__construct($xoopsDatabase, 'suico_relgroupuser', Relgroupuser::class, 'rel_id', 'rel_id');
58
    }
59
60
    /**
61
     * create a new Groups
62
     *
63
     * @param bool $isNew flag the new objects as "new"?
64
     * @return \XoopsObject Groups
65
     */
66
    public function create(
67
        $isNew = true
68
    ) {
69
        $obj = parent::create($isNew);
70
        if ($isNew) {
71
            $obj->setNew();
72
        } else {
73
            $obj->unsetNew();
74
        }
75
        $obj->helper = $this->helper;
76
        return $obj;
77
    }
78
79
    /**
80
     * retrieve a Relgroupuser
81
     *
82
     * @param int  $id of the Relgroupuser
83
     * @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...
84
     * @return mixed reference to the {@link Relgroupuser} object, FALSE if failed
85
     */
86
    public function get2(
87
        $id = null,
88
        $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

88
        /** @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...
89
    ) {
90
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_relgroupuser') . ' WHERE rel_id=' . $id;
91
        if (!$result = $this->db->query($sql)) {
92
            return false;
93
        }
94
        $numrows = $this->db->getRowsNum($result);
95
        if (1 === $numrows) {
96
            $suico_relgroupuser = new Relgroupuser();
97
            $suico_relgroupuser->assignVars($this->db->fetchArray($result));
98
            return $suico_relgroupuser;
99
        }
100
        return false;
101
    }
102
103
    /**
104
     * insert a new Relgroupuser in the database
105
     *
106
     * @param \XoopsObject $xoopsObject         reference to the {@link Relgroupuser}
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 Relgroupuser) {
117
            return false;
118
        }
119
        if (!$xoopsObject->isDirty()) {
120
            return true;
121
        }
122
        if (!$xoopsObject->cleanVars()) {
123
            return false;
124
        }
125
        foreach ($xoopsObject->cleanVars as $k => $v) {
126
            ${$k} = $v;
127
        }
128
        $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...
129
        if ($xoopsObject->isNew()) {
130
            // ajout/modification d'un Relgroupuser
131
            $xoopsObject = new Relgroupuser();
132
            $format      = 'INSERT INTO %s (rel_id, rel_group_id, rel_user_uid)';
133
            $format      .= 'VALUES (%u, %u, %u)';
134
            $sql         = \sprintf($format, $this->db->prefix('suico_relgroupuser'), $rel_id, $rel_group_id, $rel_user_uid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rel_user_uid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $rel_group_id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $rel_id seems to be never defined.
Loading history...
135
            $force       = true;
136
        } else {
137
            $format = 'UPDATE %s SET ';
138
            $format .= 'rel_id=%u, rel_group_id=%u, rel_user_uid=%u';
139
            $format .= ' WHERE rel_id = %u';
140
            $sql    = \sprintf(
141
                $format,
142
                $this->db->prefix('suico_relgroupuser'),
143
                $rel_id,
144
                $rel_group_id,
145
                $rel_user_uid,
146
                $rel_id
147
            );
148
        }
149
        if ($force) {
150
            $result = $this->db->queryF($sql);
151
        } else {
152
            $result = $this->db->query($sql);
153
        }
154
        if (!$result) {
155
            return false;
156
        }
157
        if (empty($rel_id)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rel_id seems to never exist and therefore empty should always be true.
Loading history...
158
            $rel_id = $this->db->getInsertId();
159
        }
160
        $xoopsObject->assignVar('rel_id', $rel_id);
161
        return true;
162
    }
163
164
    /**
165
     * delete a Relgroupuser from the database
166
     *
167
     * @param \XoopsObject $xoopsObject reference to the Relgroupuser to delete
168
     * @param bool         $force
169
     * @return bool FALSE if failed.
170
     */
171
    public function delete(
172
        XoopsObject $xoopsObject,
173
        $force = false
174
    ) {
175
        if (!$xoopsObject instanceof Relgroupuser) {
176
            return false;
177
        }
178
        $sql = \sprintf(
179
            'DELETE FROM %s WHERE rel_id = %u',
180
            $this->db->prefix('suico_relgroupuser'),
181
            $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

181
            /** @scrutinizer ignore-type */ $xoopsObject->getVar('rel_id')
Loading history...
182
        );
183
        if ($force) {
184
            $result = $this->db->queryF($sql);
185
        } else {
186
            $result = $this->db->query($sql);
187
        }
188
        if (!$result) {
189
            return false;
190
        }
191
        return true;
192
    }
193
194
    /**
195
     * retrieve suico_relgroupusers from the database
196
     *
197
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
198
     * @param bool                                 $id_as_key       use the UID as key for the array?
199
     * @param bool                                 $as_object
200
     * @return array array of {@link Relgroupuser} objects
201
     */
202
    public function &getObjects(
203
        ?CriteriaElement $criteriaElement = null,
204
        $id_as_key = false,
205
        $as_object = true
206
    ) {
207
        $ret   = [];
208
        $limit = $start = 0;
209
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_relgroupuser');
210
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
211
            $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

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