XoopsMembershipHandler::getObjects()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 8
nop 2
dl 0
loc 27
rs 9.0111
c 0
b 0
f 0
1
<?php
2
/**
3
 * XOOPS Kernel Class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @since               2.0.0
16
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17
 */
18
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19
20
/**
21
 * a group of users
22
 *
23
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
24
 * @author              Kazumi Ono <[email protected]>
25
 * @package             kernel
26
 */
27
class XoopsGroup extends XoopsObject
28
{
29
    //PHP 8.2 Dynamic properties deprecated
30
    public $groupid;
31
    public $name;
32
    public $description;
33
    public $group_type;
34
35
    /**
36
     * constructor
37
     */
38
    public function __construct()
39
    {
40
        parent::__construct();
41
        $this->initVar('groupid', XOBJ_DTYPE_INT, null, false);
42
        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
43
        $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false);
44
        $this->initVar('group_type', XOBJ_DTYPE_OTHER, null, false);
45
    }
46
47
    /**
48
     * Returns Class Base Variable groupid
49
     * @param  string $format
50
     * @return mixed
51
     */
52
    public function id($format = 'N')
53
    {
54
        return $this->getVar('groupid', $format);
55
    }
56
57
    /**
58
     * Returns Class Base Variable groupid
59
     * @param  string $format
60
     * @return mixed
61
     */
62
    public function groupid($format = '')
63
    {
64
        return $this->getVar('groupid', $format);
65
    }
66
67
    /**
68
     * Returns Class Base Variable name
69
     * @param  string $format
70
     * @return mixed
71
     */
72
    public function name($format = '')
73
    {
74
        return $this->getVar('name', $format);
75
    }
76
77
    /**
78
     * Returns Class Base Variable description
79
     * @param  string $format
80
     * @return mixed
81
     */
82
    public function description($format = '')
83
    {
84
        return $this->getVar('description', $format);
85
    }
86
87
    /**
88
     * Returns Class Base Variable group_type
89
     * @param  string $format
90
     * @return mixed
91
     */
92
    public function group_type($format = '')
93
    {
94
        return $this->getVar('group_type', $format);
95
    }
96
}
97
98
/**
99
 * XOOPS group handler class.
100
 * This class is responsible for providing data access mechanisms to the data source
101
 * of XOOPS group class objects.
102
 *
103
 * @author              Kazumi Ono <[email protected]>
104
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
105
 * @package             kernel
106
 * @subpackage          member
107
 */
108
class XoopsGroupHandler extends XoopsObjectHandler
109
{
110
    /**
111
     * This should be here, since this really should be a XoopsPersistableObjectHandler
112
     * Here, we fake it for future compatibility
113
     *
114
     * @var string table name
115
     */
116
    public $table;
117
118
    public function __construct(XoopsDatabase $db)
119
    {
120
        parent::__construct($db);
121
        $this->table = $this->db->prefix('groups');
122
    }
123
124
    /**
125
     * create a new {@link XoopsGroup} object
126
     *
127
     * @param  bool $isNew mark the new object as "new"?
128
     * @return XoopsGroup XoopsGroup reference to the new object
129
     *
130
     */
131
    public function create($isNew = true)
132
    {
133
        $group = new XoopsGroup();
134
        if ($isNew) {
135
            $group->setNew();
136
        }
137
138
        return $group;
139
    }
140
141
    /**
142
     * retrieve a specific group
143
     *
144
     * @param  int $id ID of the group to get
145
     * @return XoopsGroup|false XoopsGroup reference to the group object, false if failed
146
     */
147
    public function get($id)
148
    {
149
        $id    = (int)$id;
150
        $group = false;
151
        if ($id > 0) {
152
            $sql = 'SELECT * FROM ' . $this->db->prefix('groups') . ' WHERE groupid=' . $id;
153
            $result = $this->db->query($sql);
0 ignored issues
show
Bug introduced by
The method query() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

153
            /** @scrutinizer ignore-call */ 
154
            $result = $this->db->query($sql);
Loading history...
154
            if (!$this->db->isResultSet($result)) {
155
                return $group;
156
            }
157
            $numrows = $this->db->getRowsNum($result);
0 ignored issues
show
Bug introduced by
The method getRowsNum() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

157
            /** @scrutinizer ignore-call */ 
158
            $numrows = $this->db->getRowsNum($result);
Loading history...
158
            if ($numrows == 1) {
159
                $group = new XoopsGroup();
160
                $group->assignVars($this->db->fetchArray($result));
0 ignored issues
show
Bug introduced by
The method fetchArray() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

160
                $group->assignVars($this->db->/** @scrutinizer ignore-call */ fetchArray($result));
Loading history...
161
            }
162
        }
163
164
        return $group;
165
    }
166
167
    /**
168
     * insert a group into the database
169
     *
170
     * @param XoopsObject|XoopsGroup $group a group object
171
     *
172
     * @return bool true on success, otherwise false
173
     */
174
    public function insert(XoopsObject $group)
175
    {
176
        $className = 'XoopsGroup';
177
        if (!($group instanceof $className)) {
178
            return false;
179
        }
180
        if (!$group->isDirty()) {
181
            return true;
182
        }
183
        if (!$group->cleanVars()) {
184
            return false;
185
        }
186
        foreach ($group->cleanVars as $k => $v) {
187
            ${$k} = $v;
188
        }
189
        if ($group->isNew()) {
190
            $groupid = $this->db->genId('group_groupid_seq');
0 ignored issues
show
Bug introduced by
The method genId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

190
            /** @scrutinizer ignore-call */ 
191
            $groupid = $this->db->genId('group_groupid_seq');
Loading history...
191
            $sql     = sprintf('INSERT INTO %s (groupid, name, description, group_type) VALUES (%u, %s, %s, %s)', $this->db->prefix('groups'), $groupid, $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $name seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $description seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $group_type does not exist. Did you maybe mean $group?
Loading history...
Bug introduced by
The method quoteString() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

191
            $sql     = sprintf('INSERT INTO %s (groupid, name, description, group_type) VALUES (%u, %s, %s, %s)', $this->db->prefix('groups'), $groupid, $this->db->/** @scrutinizer ignore-call */ quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type));
Loading history...
192
        } else {
193
            $sql = sprintf('UPDATE %s SET name = %s, description = %s, group_type = %s WHERE groupid = %u', $this->db->prefix('groups'), $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type), $groupid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $groupid seems to be never defined.
Loading history...
194
        }
195
        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...
196
            return false;
197
        }
198
        if (empty($groupid)) {
199
            $groupid = $this->db->getInsertId();
0 ignored issues
show
Bug introduced by
The method getInsertId() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

199
            /** @scrutinizer ignore-call */ 
200
            $groupid = $this->db->getInsertId();
Loading history...
200
        }
201
        $group->assignVar('groupid', $groupid);
202
203
        return true;
204
    }
205
206
    /**
207
     * remove a group from the database
208
     *
209
     * @param XoopsObject|XoopsGroup $group a group object
210
     *
211
     * @return bool true on success, otherwise false
212
     */
213
    public function delete(XoopsObject $group)
214
    {
215
        $className = 'XoopsGroup';
216
        if (!($group instanceof $className)) {
217
            return false;
218
        }
219
        $sql = sprintf('DELETE FROM %s WHERE groupid = %u', $this->db->prefix('groups'), $group->getVar('groupid'));
0 ignored issues
show
Bug introduced by
It seems like $group->getVar('groupid') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

219
        $sql = sprintf('DELETE FROM %s WHERE groupid = %u', $this->db->prefix('groups'), /** @scrutinizer ignore-type */ $group->getVar('groupid'));
Loading history...
220
        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...
221
            return false;
222
        }
223
224
        return true;
225
    }
226
227
    /**
228
     * retrieve groups from the database
229
     *
230
     * @param  CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement} with conditions for the groups
231
     * @param  bool            $id_as_key should the groups' IDs be used as keys for the associative array?
232
     * @return mixed           Array of groups
233
     */
234
    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
235
    {
236
        $ret   = array();
237
        $limit = $start = 0;
238
        $sql   = 'SELECT * FROM ' . $this->db->prefix('groups');
239
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
240
            $sql .= ' ' . $criteria->renderWhere();
241
            $limit = $criteria->getLimit();
242
            $start = $criteria->getStart();
243
        }
244
        $result = $this->db->query($sql, $limit, $start);
245
         if (!$this->db->isResultSet($result)) {
246
            return $ret;
247
        }
248
        /** @var array $myrow */
249
        while (false !== ($myrow = $this->db->fetchArray($result))) {
250
            $group = new XoopsGroup();
251
            $group->assignVars($myrow);
252
            if (!$id_as_key) {
253
                $ret[] =& $group;
254
            } else {
255
                $ret[$myrow['groupid']] = &$group;
256
            }
257
            unset($group);
258
        }
259
260
        return $ret;
261
    }
262
}
263
264
/**
265
 * membership of a user in a group
266
 *
267
 * @author              Kazumi Ono <[email protected]>
268
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
269
 * @package             kernel
270
 */
271
class XoopsMembership extends XoopsObject
272
{
273
    //PHP 8.2 Dynamic properties deprecated
274
    public $linkid;
275
    public $groupid;
276
    public $uid;
277
278
    /**
279
     * constructor
280
     */
281
    public function __construct()
282
    {
283
        parent::__construct();
284
        $this->initVar('linkid', XOBJ_DTYPE_INT, null, false);
285
        $this->initVar('groupid', XOBJ_DTYPE_INT, null, false);
286
        $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
287
    }
288
}
289
290
/**
291
 * XOOPS membership handler class. (Singleton)
292
 *
293
 * This class is responsible for providing data access mechanisms to the data source
294
 * of XOOPS group membership class objects.
295
 *
296
 * @author              Kazumi Ono <[email protected]>
297
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
298
 * @package             kernel
299
 */
300
class XoopsMembershipHandler extends XoopsObjectHandler
301
{
302
    /**
303
     * This should be here, since this really should be a XoopsPersistableObjectHandler
304
     * Here, we fake it for future compatibility
305
     *
306
     * @var string table name
307
     */
308
    public $table;
309
310
    public function __construct(XoopsDatabase $db)
311
    {
312
        parent::__construct($db);
313
        $this->table = $this->db->prefix('groups_users_link');
314
    }
315
316
    /**
317
     * create a new membership
318
     *
319
     * @param  bool $isNew should the new object be set to "new"?
320
     * @return XoopsMembership XoopsMembership
321
     */
322
    public function create($isNew = true)
323
    {
324
        $mship = new XoopsMembership();
325
        if ($isNew) {
326
            $mship->setNew();
327
        }
328
329
        return $mship;
330
    }
331
332
    /**
333
     * retrieve a membership
334
     *
335
     * @param  int $id ID of the membership to get
336
     * @return mixed reference to the object if successful, else FALSE
337
     */
338
    public function get($id)
339
    {
340
        $id    = (int)$id;
341
        $mship = false;
342
        if ($id > 0) {
343
            $sql = 'SELECT * FROM ' . $this->db->prefix('groups_users_link') . ' WHERE linkid=' . $id;
344
            $result = $this->db->query($sql);
345
            if (!$this->db->isResultSet($result)) {
346
                return $mship;
347
            }
348
            $numrows = $this->db->getRowsNum($result);
349
            if ($numrows == 1) {
350
                $mship = new XoopsMembership();
351
                $mship->assignVars($this->db->fetchArray($result));
352
            }
353
        }
354
355
        return $mship;
356
    }
357
358
    /**
359
     * inserts a membership in the database
360
     *
361
     * @param  XoopsObject|XoopsMembership $mship a XoopsMembership object
362
     *
363
     * @return bool true on success, otherwise false
364
     */
365
    public function insert(XoopsObject $mship)
366
    {
367
        $className = 'XoopsMembership';
368
        if (!($mship instanceof $className)) {
369
            return false;
370
        }
371
        if (!$mship->isDirty()) {
372
            return true;
373
        }
374
        if (!$mship->cleanVars()) {
375
            return false;
376
        }
377
        foreach ($mship->cleanVars as $k => $v) {
378
            ${$k} = $v;
379
        }
380
        if ($mship->isNew()) {
381
            $linkid = $this->db->genId('groups_users_link_linkid_seq');
382
            $sql    = sprintf('INSERT INTO %s (linkid, groupid, uid) VALUES (%u, %u, %u)', $this->db->prefix('groups_users_link'), $linkid, $groupid, $uid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $groupid seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $uid seems to be never defined.
Loading history...
383
        } else {
384
            $sql = sprintf('UPDATE %s SET groupid = %u, uid = %u WHERE linkid = %u', $this->db->prefix('groups_users_link'), $groupid, $uid, $linkid);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $linkid seems to be never defined.
Loading history...
385
        }
386
        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...
387
            return false;
388
        }
389
        if (empty($linkid)) {
390
            $linkid = $this->db->getInsertId();
391
        }
392
        $mship->assignVar('linkid', $linkid);
393
394
        return true;
395
    }
396
397
    /**
398
     * delete a membership from the database
399
     *
400
     * @param  XoopsObject|XoopsMembership $mship a XoopsMembership object
401
     *
402
     * @return bool true on success, otherwise false
403
     */
404
    public function delete(XoopsObject $mship)
405
    {
406
        $className = 'XoopsMembership';
407
        if (!($mship instanceof $className)) {
408
            return false;
409
        }
410
411
        $sql = sprintf('DELETE FROM %s WHERE linkid = %u', $this->db->prefix('groups_users_link'), $mship->getVar('linkid'));
0 ignored issues
show
Bug introduced by
It seems like $mship->getVar('linkid') can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|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

411
        $sql = sprintf('DELETE FROM %s WHERE linkid = %u', $this->db->prefix('groups_users_link'), /** @scrutinizer ignore-type */ $mship->getVar('linkid'));
Loading history...
412
        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...
413
            return false;
414
        }
415
416
        return true;
417
    }
418
419
    /**
420
     * retrieve memberships from the database
421
     *
422
     * @param  CriteriaElement|CriteriaCompo $criteria  {@link CriteriaElement} conditions to meet
423
     * @param  bool            $id_as_key should the ID be used as the array's key?
424
     * @return array           array of references
425
     */
426
    public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
427
    {
428
        $ret   = array();
429
        $limit = $start = 0;
430
        $sql   = 'SELECT * FROM ' . $this->db->prefix('groups_users_link');
431
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
432
            $sql .= ' ' . $criteria->renderWhere();
433
            $limit = $criteria->getLimit();
434
            $start = $criteria->getStart();
435
        }
436
        $result = $this->db->query($sql, $limit, $start);
437
        if (!$this->db->isResultSet($result)) {
438
            return $ret;
439
        }
440
        /** @var array $myrow */
441
        while (false !== ($myrow = $this->db->fetchArray($result))) {
442
            $mship = new XoopsMembership();
443
            $mship->assignVars($myrow);
444
            if (!$id_as_key) {
445
                $ret[] =& $mship;
446
            } else {
447
                $ret[$myrow['linkid']] = &$mship;
448
            }
449
            unset($mship);
450
        }
451
452
        return $ret;
453
    }
454
455
    /**
456
     * count how many memberships meet the conditions
457
     *
458
     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} conditions to meet
459
     * @return int
460
     */
461
    public function getCount(CriteriaElement $criteria = null)
462
    {
463
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('groups_users_link');
464
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
465
            $sql .= ' ' . $criteria->renderWhere();
466
        }
467
        $result = $this->db->query($sql);
468
        if (!$result) {
469
            return 0;
470
        }
471
        list($count) = $this->db->fetchRow($result);
0 ignored issues
show
Bug introduced by
The method fetchRow() does not exist on XoopsDatabase. Since it exists in all sub-types, consider adding an abstract or default implementation to XoopsDatabase. ( Ignorable by Annotation )

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

471
        /** @scrutinizer ignore-call */ 
472
        list($count) = $this->db->fetchRow($result);
Loading history...
472
473
        return (int)$count;
474
    }
475
476
    /**
477
     * delete all memberships meeting the conditions
478
     *
479
     * @param  CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement} with conditions to meet
480
     * @return bool
481
     */
482
    public function deleteAll(CriteriaElement $criteria = null)
483
    {
484
        $sql = 'DELETE FROM ' . $this->db->prefix('groups_users_link');
485
        if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
486
            $sql .= ' ' . $criteria->renderWhere();
487
        }
488
        $result = $this->db->query($sql);
489
        if (!$result) {
490
            return false;
491
        }
492
493
        return true;
494
    }
495
496
    /**
497
     * retrieve groups for a user
498
     *
499
     * @param int $uid ID of the user
500
     *
501
     * @internal param bool $asobject should the groups be returned as {@link XoopsGroup}
502
     *           objects? FALSE returns associative array.
503
     * @return array array of groups the user belongs to
504
     */
505
    public function getGroupsByUser($uid)
506
    {
507
        $ret    = array();
508
        $sql    = 'SELECT groupid FROM ' . $this->db->prefix('groups_users_link') . ' WHERE uid=' . (int)$uid;
509
        $result = $this->db->query($sql);
510
        if (!$this->db->isResultSet($result)) {
511
            return $ret;
512
        }
513
        /** @var array $myrow */
514
        while (false !== ($myrow = $this->db->fetchArray($result))) {
515
            $ret[] = $myrow['groupid'];
516
        }
517
518
        return $ret;
519
    }
520
521
    /**
522
     * retrieve users belonging to a group
523
     *
524
     * @param int $groupid    ID of the group
525
     * @param int $limit      number of entries to return
526
     * @param int $start      offset of first entry to return
527
     * @internal param bool $asobject return users as {@link XoopsUser} objects? objects?
528
     *                        FALSE will return arrays
529
     * @return array array of users belonging to the group
530
     */
531
    public function getUsersByGroup($groupid, $limit = 0, $start = 0)
532
    {
533
        $ret    = array();
534
        $sql    = 'SELECT uid FROM ' . $this->db->prefix('groups_users_link') . ' WHERE groupid=' . (int)$groupid;
535
        $result = $this->db->query($sql, $limit, $start);
536
        if (!$this->db->isResultSet($result)) {
537
            return $ret;
538
        }
539
        /** @var array $myrow */
540
        while (false !== ($myrow = $this->db->fetchArray($result))) {
541
            $ret[] = $myrow['uid'];
542
        }
543
544
        return $ret;
545
    }
546
}
547