XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
users_search.php
1<?php
2
3if (!defined('XOOPS_ROOT_PATH')) {
4 exit();
5}
6
7require_once XOOPS_ROOT_PATH . '/modules/user/class/users.php';
8
10{
11 public function &getObjects($criteria = null, $limit = null, $start = null, $id_as_key = false)
12 {
13 $ret = [];
14
15 $uTable = $this->db->prefix('users') . ' as u';
16 $gTable = $this->db->prefix('groups_users_link') . ' as g';
17
18 $sql = "SELECT DISTINCT u.* FROM {$uTable} LEFT JOIN {$gTable} ON u.uid=g.uid";
19
20 if (null !== $criteria && $criteria instanceof \CriteriaElement) {
21 $where = $this->_makeCriteria4sql($criteria);
22
23 // Fix trim() null deprecation
24 if ($where && '' !== trim((string)$where)) {
25 $sql .= ' WHERE ' . $where;
26 }
27
28 $sorts = [];
29 foreach ($criteria->getSorts() as $sort) {
30 $sorts[] = $sort['sort'] . ' ' . $sort['order'];
31 }
32 if ('' != $criteria->getSort()) {
33 $sql .= ' ORDER BY ' . implode(',', $sorts);
34 }
35
36 if (null === $limit) {
37 $limit = $criteria->getLimit();
38 }
39
40 if (null === $start) {
41 $start = $criteria->getStart();
42 }
43 } else {
44 if (null === $limit) {
45 $limit = 0;
46 }
47
48 if (null === $start) {
49 $start = 0;
50 }
51 }
52
53 $result = $this->db->query($sql, $limit, $start);
54
55 if (!$result) {
56 return $ret;
57 }
58
59 while ($row = $this->db->fetchArray($result)) {
60 $obj = new $this->mClass();
61 $obj->assignVars($row);
62 $obj->unsetNew();
63
64 if ($id_as_key) {
65 $ret[$obj->get($this->mPrimary)] = &$obj;
66 } else {
67 $ret[] = &$obj;
68 }
69
70 unset($obj);
71 }
72
73 if (count($ret)) {
74 foreach (array_keys($ret) as $key) {
75 $ret[$key]->_loadGroups();
76 }
77 }
78
79 return $ret;
80 }
81
92 public function &getUids($criteria = null, $limit = null, $start = null, $id_as_key = false)
93 {
94 $ret = [];
95
96 $uTable = $this->db->prefix('users') . ' as u';
97 $gTable = $this->db->prefix('groups_users_link') . ' as g';
98
99 $sql = "SELECT DISTINCT u.uid FROM {$uTable} LEFT JOIN {$gTable} ON u.uid=g.uid";
100
101 if (null !== $criteria && $criteria instanceof \CriteriaElement) {
102 $where = $this->_makeCriteria4sql($criteria);
103
104 if (trim($where)) {
105 $sql .= ' WHERE ' . $where;
106 }
107
108 $sorts = [];
109 foreach ($criteria->getSorts() as $sort) {
110 $sorts[] = $sort['sort'] . ' ' . $sort['order'];
111 }
112 if ('' != $criteria->getSort()) {
113 $sql .= ' ORDER BY ' . implode(',', $sorts);
114 }
115
116 if (null === $limit) {
117 $limit = $criteria->getLimit();
118 }
119
120 if (null === $start) {
121 $start = $criteria->getStart();
122 }
123 } else {
124 if (null === $limit) {
125 $limit = 0;
126 }
127
128 if (null === $start) {
129 $start = 0;
130 }
131 }
132
133 $result = $this->db->query($sql, $limit, $start);
134
135 if (!$result) {
136 return $ret;
137 }
138
139 while ($row = $this->db->fetchArray($result)) {
140 $ret[] = $row['uid'];
141 }
142
143 return $ret;
144 }
145
146 public function getCount($criteria = null)
147 {
148 $ret = [];
149
150 $uTable = $this->db->prefix('users') . ' as u';
151 $gTable = $this->db->prefix('groups_users_link') . ' as g';
152
153 $sql = "SELECT COUNT(DISTINCT u.uid) c FROM {$uTable} LEFT JOIN {$gTable} ON u.uid=g.uid";
154 if (null !== $criteria && $criteria instanceof \CriteriaElement) {
155 $where = $this->_makeCriteria4sql($criteria);
156
157 if ($where) {
158 $sql .= ' WHERE ' . $where;
159 }
160 }
161
162 return $this->_getCount($sql);
163 }
164
165 public function insert(&$user, $force = false)
166 {
167 if (parent::insert($user, $force)) {
168 $flag = true;
169
170 $user->_loadGroups();
171
172 $handler = &xoops_getmodulehandler('groups_users_link', 'user');
173 $oldLinkArr = &$handler->getObjects(new Criteria('uid', $user->get('uid')), $force);
174
175 //
176 // Delete
177 //
178 $oldGroupidArr = [];
179 foreach (array_keys($oldLinkArr) as $key) {
180 $oldGroupidArr[] = $oldLinkArr[$key]->get('groupid');
181 if (!in_array($oldLinkArr[$key]->get('groupid'), $user->Groups)) {
182 $handler->delete($oldLinkArr[$key], $force);
183 }
184 }
185
186 foreach ($user->Groups as $gid) {
187 if (!in_array($gid, $oldGroupidArr)) {
188 $link = &$handler->create();
189
190 $link->set('groupid', $gid);
191 $link->set('uid', $user->get('uid'));
192
193 $flag &= $handler->insert($link, $force);
194
195 unset($link);
196 }
197 }
198
199 return $flag;
200 }
201
202 return false;
203 }
204
205 public function deleteAll($criteria, $force = false)
206 { }
207}
& getObjects($criteria=null, $limit=null, $start=null, $id_as_key=false)
& getUids($criteria=null, $limit=null, $start=null, $id_as_key=false)
deleteAll($criteria, $force=false)