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 XOOPS Project (http://xoops.org)
13: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.6.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: * @version $Id$
18: */
19:
20: namespace Xoops\Core\Kernel\Handlers;
21:
22: use Xoops\Core\Database\Connection;
23: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
24:
25: /**
26: * XOOPS membership handler class. (Singleton)
27: *
28: * This class is responsible for providing data access mechanisms to the data source
29: * of XOOPS group membership class objects.
30: *
31: * @category Xoops\Core\Kernel\Handlers\XoopsMembershipHandler
32: * @package Xoops\Core\Kernel
33: * @author Kazumi Ono <onokazu@xoops.org>
34: * @copyright 2000-2015 XOOPS Project (http://xoops.org)
35: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
36: * @link http://xoops.org
37: */
38: class XoopsMembershipHandler extends XoopsPersistableObjectHandler
39: {
40: /**
41: * Constructor
42: *
43: * @param Connection|null $db database
44: */
45: public function __construct(Connection $db = null)
46: {
47: parent::__construct(
48: $db,
49: 'system_usergroup',
50: '\Xoops\Core\Kernel\Handlers\XoopsMembership',
51: 'linkid',
52: 'groupid'
53: );
54: }
55:
56: /**
57: * retrieve groups for a user
58: *
59: * @param int $uid ID of the user objects
60: *
61: * @return array array of groups the user belongs to
62: */
63: public function getGroupsByUser($uid)
64: {
65: $ret = array();
66: $qb = $this->db2->createXoopsQueryBuilder();
67: $eb = $qb->expr();
68: $qb ->select('groupid')
69: ->fromPrefix('system_usergroup', 'g')
70: ->where($eb->eq('g.uid', ':uid'))
71: ->setParameter(':uid', $uid, \PDO::PARAM_INT);
72: $result = $qb->execute();
73: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
74: $ret[] = $myrow['groupid'];
75: }
76:
77: return $ret;
78: }
79:
80: /**
81: * retrieve users belonging to a group
82: *
83: * @param int $groupid ID of the group
84: * @param int $limit number of entries to return
85: * @param int $start offset of first entry to return
86: *
87: * @return array array of users belonging to the group
88: */
89: public function getUsersByGroup($groupid, $limit = 0, $start = 0)
90: {
91: $ret = array();
92: $qb = $this->db2->createXoopsQueryBuilder();
93: $eb = $qb->expr();
94: $qb ->select('uid')
95: ->fromPrefix('system_usergroup', 'g')
96: ->where($eb->eq('g.groupid', ':gid'))
97: ->setParameter(':gid', $groupid, \PDO::PARAM_INT);
98: if ($limit!=0 || $start!=0) {
99: $qb->setFirstResult($start)
100: ->setMaxResults($limit);
101: }
102: $result = $qb->execute();
103: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
104: $ret[] = $myrow['uid'];
105: }
106:
107: return $ret;
108: }
109: }
110: