XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
user.php
Go to the documentation of this file.
1 <?php
2 // $Id: image.php 825 2011-12-09 00:06:11Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 class RMUser extends RMObject
12 {
13  private $groups = array();
14 
15  public function __construct($id=''){
16 
17  $this->db = XoopsDatabaseFactory::getDatabaseConnection();
18  $this->_dbtable = $this->db->prefix("users");
19  $this->setNew();
20  $this->initVarsFromTable();
21 
22  if ($this->loadValues($id)){
23  $this->unsetNew();
24  return;
25  }
26 
27  $this->primary = 'uname';
28  if($this->loadValues($id))
29  $this->unsetNew ();
30 
31  $this->primary = 'uid';
32 
33  }
34 
35  function setGroups($groupsArr){
36  $this->groups = array();
37  if (is_array($groupsArr))
38  $this->groups =& $groupsArr;
39  }
40 
41  public function &getGroups(){
42 
43  if (!empty($this->groups)) return $this->groups;
44 
45  $sql = 'SELECT groupid FROM '.$this->db->prefix('groups_users_link').' WHERE uid='.intval($this->getVar('uid'));
46  $result = $this->db->query($sql);
47 
48  if (!$result) {
49  return false;
50  }
51  while ($myrow = $this->db->fetchArray($result)) {
52  $this->groups[] = $myrow['groupid'];
53  }
54 
55  return $this->groups;
56  }
57 
58  function groups($data=false, $fields='groupid'){
59  $groups =& $this->getGroups();
60 
61  if (!$data || $fields=='') return $groups;
62 
63  // Gets all groups based in their id
64  $sql = "SELECT ".($fields!='' ? "$fields" : '')." FROM ".$this->db->prefix("groups")." WHERE groupid IN(".implode(',',$groups).")";
65  $result = $this->db->query($sql);
66  $groups = array();
67  while ($row = $this->db->fetchArray($result)){
68  $groups[] = $row;
69  }
70  return $groups;
71  }
72 
73  function save(){
74  $ret = true;
78  if ($this->isNew()){
79  $ret = $this->saveToTable();
80  } else {
81  $ret = $this->updateTable();
82  }
88  if (!$ret) return $ret;
92  if (!empty($this->groups)){
93  if (!$this->isNew())
94  $this->db->queryF("DELETE FROM ".$this->db->prefix("groups_users_link")." WHERE uid='".$this->getVar('uid')."'");
95 
96  $sql = "INSERT INTO ".$this->db->prefix("groups_users_link")." (`groupid`,`uid`) VALUES ";
97  foreach ($this->groups as $k){
98  $sql .= "('$k','".$this->getVar('uid')."'),";
99  }
100 
101  $sql = substr($sql, 0, strlen($sql) - 1);
102 
103  $this->db->queryF($sql);
104  }
105 
106  return $ret;
107 
108  }
109 
110 }