XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
member.php
Go to the documentation of this file.
1 <?php
19 defined('XOOPS_ROOT_PATH') or die('Restricted access');
20 
21 require_once $GLOBALS['xoops']->path('kernel/user.php');
22 require_once $GLOBALS['xoops']->path('kernel/group.php');
23 
36 {
37 
43 
48 
53 
57  var $_members = array();
64  function XoopsMemberHandler(&$db)
65  {
66  $this->_gHandler = new XoopsGroupHandler($db);
67  $this->_uHandler = new XoopsUserHandler($db);
68  $this->_mHandler = new XoopsMembershipHandler($db);
69  }
70 
76  function &createGroup()
77  {
78  $inst =& $this->_gHandler->create();
79  return $inst;
80  }
81 
87  function &createUser()
88  {
89  $inst =& $this->_uHandler->create();
90  return $inst;
91  }
92 
99  function getGroup($id)
100  {
101  return $this->_gHandler->get($id);
102  }
103 
110  function &getUser($id)
111  {
112  if (!isset($this->_members[$id])) {
113  $this->_members[$id] = & $this->_uHandler->get($id);
114  }
115  return $this->_members[$id];
116  }
117 
124  function deleteGroup(&$group)
125  {
126  $this->_gHandler->delete($group);
127  $this->_mHandler->deleteAll(new Criteria('groupid', $group->getVar('groupid')));
128  return true;
129  }
130 
137  function deleteUser(&$user)
138  {
139  $this->_uHandler->delete($user);
140  $this->_mHandler->deleteAll(new Criteria('uid', $user->getVar('uid')));
141  return true;
142  }
143 
151  function insertGroup(&$group)
152  {
153  return $this->_gHandler->insert($group);
154  }
155 
163  function insertUser(&$user, $force = false)
164  {
165  return $this->_uHandler->insert($user, $force);
166  }
167 
175  function getGroups($criteria = null, $id_as_key = false)
176  {
177  return $this->_gHandler->getObjects($criteria, $id_as_key);
178  }
179 
187  function getUsers($criteria = null, $id_as_key = false)
188  {
189  return $this->_uHandler->getObjects($criteria, $id_as_key);
190  }
191 
198  function getGroupList($criteria = null)
199  {
200  $groups = $this->_gHandler->getObjects($criteria, true);
201  $ret = array();
202  foreach (array_keys($groups) as $i) {
203  $ret[$i] = $groups[$i]->getVar('name');
204  }
205  return $ret;
206  }
207 
214  function getUserList($criteria = null)
215  {
216  $users = $this->_uHandler->getObjects($criteria, true);
217  $ret = array();
218  foreach (array_keys($users) as $i) {
219  $ret[$i] = $users[$i]->getVar('uname');
220  }
221  return $ret;
222  }
223 
231  function addUserToGroup($group_id, $user_id)
232  {
233  $mship =& $this->_mHandler->create();
234  $mship->setVar('groupid', $group_id);
235  $mship->setVar('uid', $user_id);
236  return $this->_mHandler->insert($mship);
237  }
238 
246  function removeUsersFromGroup($group_id, $user_ids = array())
247  {
248  $criteria = new CriteriaCompo();
249  $criteria->add(new Criteria('groupid', $group_id));
250  $criteria2 = new CriteriaCompo();
251  foreach ($user_ids as $uid) {
252  $criteria2->add(new Criteria('uid', $uid), 'OR');
253  }
254  $criteria->add($criteria2);
255  return $this->_mHandler->deleteAll($criteria);
256  }
257 
268  function getUsersByGroup($group_id, $asobject = false, $limit = 0, $start = 0)
269  {
270  $user_ids = $this->_mHandler->getUsersByGroup($group_id, $limit, $start);
271  if (!$asobject) {
272  return $user_ids;
273  } else {
274  $ret = array();
275  foreach ($user_ids as $u_id) {
276  $user =& $this->getUser($u_id);
277  if (is_object($user)) {
278  $ret[] = & $user;
279  }
280  unset($user);
281  }
282  return $ret;
283  }
284  }
285 
293  function getGroupsByUser($user_id, $asobject = false)
294  {
295  $group_ids = $this->_mHandler->getGroupsByUser($user_id);
296  if (!$asobject) {
297  return $group_ids;
298  } else {
299  foreach ($group_ids as $g_id) {
300  $ret[] = $this->getGroup($g_id);
301  }
302  return $ret;
303  }
304  }
305 
313  function &loginUser($uname, $pwd)
314  {
315  $criteria = new CriteriaCompo(new Criteria('uname', $uname));
316  $criteria->add(new Criteria('pass', md5($pwd)));
317  $user = $this->_uHandler->getObjects($criteria, false);
318  if (!$user || count($user) != 1) {
319  $user = false;
320  return $user;
321  }
322  return $user[0];
323  }
324 
332  function &loginUserMd5($uname, $md5pwd)
333  {
334  $criteria = new CriteriaCompo(new Criteria('uname', $uname));
335  $criteria->add(new Criteria('pass', $md5pwd));
336  $user = $this->_uHandler->getObjects($criteria, false);
337  if (!$user || count($user) != 1) {
338  $user = false;
339  return $user;
340  }
341  return $user[0];
342  }
343 
350  function getUserCount($criteria = null)
351  {
352  return $this->_uHandler->getCount($criteria);
353  }
354 
361  function getUserCountByGroup($group_id)
362  {
363  return $this->_mHandler->getCount(new Criteria('groupid', $group_id));
364  }
365 
374  function updateUserByField(&$user, $fieldName, $fieldValue)
375  {
376  $user->setVar($fieldName, $fieldValue);
377  return $this->insertUser($user);
378  }
379 
388  function updateUsersByField($fieldName, $fieldValue, $criteria = null)
389  {
390  return $this->_uHandler->updateAll($fieldName, $fieldValue, $criteria);
391  }
392 
399  function activateUser(&$user)
400  {
401  if ($user->getVar('level') != 0) {
402  return true;
403  }
404  $user->setVar('level', 1);
405  return $this->_uHandler->insert($user, true);
406  }
407 
419  function getUsersByGroupLink($groups, $criteria = null, $asobject = false, $id_as_key = false)
420  {
421  $ret = array();
422  $criteriaCompo = new CriteriaCompo();
423  $select = $asobject ? "u.*" : "u.uid";
424  $sql = "SELECT DISTINCT {$select} " . " FROM " . $this->_uHandler->db->prefix("users")
425  . " AS u" . " LEFT JOIN " . $this->_mHandler->db->prefix("groups_users_link")
426  . " AS m ON m.uid = u.uid WHERE ";
427  if (!empty($groups)) {
428  $criteriaCompo->add(new Criteria('m.groupid', "(" . implode(", ", $groups) . ")", 'IN'));
429  }
430 
431  $limit = $start = 0;
432  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
433  $criteriaCompo->add($criteria);
434  $sql_criteria = $criteriaCompo->render();
435  if ($criteria->getSort() != '') {
436  $sql_criteria .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
437  }
438  $limit = $criteria->getLimit();
439  $start = $criteria->getStart();
440  } else {
441  $sql_criteria = $criteriaCompo->render();
442  }
443 
444  if ($sql_criteria) {
445  $sql .= $sql_criteria;
446  } else {
447  $sql .= "1 = 1";
448  }
449 
450  if (!$result = $this->_uHandler->db->query($sql, $limit, $start)) {
451  return $ret;
452  }
453  while ($myrow = $this->_uHandler->db->fetchArray($result)) {
454  if ($asobject) {
455  $user = new XoopsUser();
456  $user->assignVars($myrow);
457  if (!$id_as_key) {
458  $ret[] =& $user;
459  } else {
460  $ret[$myrow['uid']] =& $user;
461  }
462  unset($user);
463  } else {
464  $ret[] = $myrow['uid'];
465  }
466  }
467  return $ret;
468  }
469 
477  function getUserCountByGroupLink($groups, $criteria = null)
478  {
479  $ret = 0;
480  $criteriaCompo = new CriteriaCompo();
481  $sql = "SELECT DISTINCT COUNT(u.uid) "
482  . " FROM " . $this->_uHandler->db->prefix("users") . " AS u"
483  . " LEFT JOIN " . $this->_mHandler->db->prefix("groups_users_link")
484  . " AS m ON m.uid = u.uid" . " WHERE ";
485  if (!empty($groups)) {
486  $criteriaCompo->add(new Criteria('m.groupid', "(" . implode(", ", $groups) . ")", 'IN'));
487  }
488  if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
489  $criteriaCompo->add($criteria);
490  }
491  $sql_criteria = $criteriaCompo->render();
492 
493  if ($sql_criteria) {
494  $sql .= $sql_criteria;
495  } else {
496  $sql .= "1 = 1";
497  }
498 
499  if (!$result = $this->_uHandler->db->query($sql)) {
500  return $ret;
501  }
502  list ($ret) = $this->_uHandler->db->fetchRow($result);
503  return $ret;
504  }
505 
506 }
507 ?>