Combu Server  3.1.1
PHP API Documentation
UserGroup.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class UserGroup extends DataClass {
11 
12  const TABLE_NAME = "UserGroup";
13 
14  public $Id = 0;
15  public $Name = "";
16  public $IdOwner = 0;
17  public $Public = 0;
18  public $CustomData = "";
19 
23  public function __construct($src = null, $stripSlashes = false) {
24  global $Database;
25  if ($src == null)
26  return;
27  if (is_array($src)) {
28  // Load by array
29  $this->_loadByRow($src, $stripSlashes);
30  } else if (is_numeric($src) && intval($src) > 0) {
31  // Load by Id
32  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
33  }
34  }
35 
41  public function Exists() {
42  global $Database;
43  $sql = "IdOwner = " . $this->IdOwner . " AND Name = '" . $Database->Escape($this->Name) . "'";
44  if ($this->Id > 0)
45  $sql .= " AND Id <> " . $this->Id;
46  $recs = self::_load(self::GetTableName(__CLASS__), "", $sql);
47  if (count($recs) > 0)
48  return TRUE;
49  return FALSE;
50  }
51 
63  public static function Load ($name = NULL, $idOwner = 0, $idAccount = 0, $limit = null, $offset = null, &$count = null, $returnArray = false) {
64  global $Database;
65  $where = "(Public = 1)";
66  if ($name)
67  $where .= ($where ? " AND " : "") . sprintf("(Name REGEXP '%s')", $Database->Escape($name));
68  if ($idOwner > 0 && $idOwner == $idAccount) {
69  $where .= ($where ? " AND " : "") . "(" .
70  sprintf("(IdOwner = %d)", $idOwner) .
71  " OR " . sprintf("(Id IN (SELECT IdGroup FROM %s WHERE IdAccount = %d))", self::GetTableName(UserGroupAccount::class), $idAccount) .
72  ")";
73  } else {
74  if ($idOwner > 0)
75  $where .= ($where ? " AND " : "") . sprintf("(IdOwner = %d)", $idOwner);
76  if ($idAccount > 0)
77  $where .= ($where ? " AND " : "") . sprintf("(Id IN (SELECT IdGroup FROM %s WHERE IdAccount = %d))", self::GetTableName(UserGroupAccount::class), $idAccount);
78  }
79  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "Name", $limit, $offset, $count);
80  }
81 
87  public function Save() {
88  global $Database;
89  if ($this->Id > 0) {
90  $query = sprintf("UPDATE %s SET Name = '%s', IdOwner = %d, CustomData = '%s' WHERE Id = %d",
91  self::GetTableName(__CLASS__),
92  $Database->Escape($this->Name),
93  $this->IdOwner,
94  $Database->Escape($this->CustomData),
95  $this->Id);
96  } else {
97  $query = sprintf("INSERT INTO %s (Name, IdOwner, Public, CustomData) VALUES ('%s', %d, %d, '%s')",
98  self::GetTableName(__CLASS__),
99  $Database->Escape($this->Name),
100  $this->IdOwner,
101  $this->Public,
102  $Database->Escape($this->CustomData));
103  }
104  if ($Database->Query($query)) {
105  if ($this->Id < 1)
106  $this->Id = $Database->InsertedId();
107  return TRUE;
108  }
109  return FALSE;
110  }
111 
117  public function Delete() {
118  if ($this->Id < 1)
119  return FALSE;
120  if ($this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id)) {
121  // Delete all memberships
122  $this->_Delete(self::GetTableName(UserGroupAccount::class), "IdGroup = " . $this->Id);
123  // Delete all messages
124  $this->_Delete(self::GetTableName(GameMail::class), "IdGroup = " . $this->Id);
125  return TRUE;
126  }
127  return FALSE;
128  }
129 
134  public function GetMembers() {
135  $members = array();
136  if ($this->Id > 0) {
137  $members = UserGroupAccount::Load($this->Id);
138  }
139  return $members;
140  }
141 
146  public function GetMembersAccount() {
147  $members = array();
148  $recs = $this->GetMembers();
149  foreach ($recs as $member) {
150  $user = new Account($member->IdAccount);
151  if ($user->Id > 0)
152  $members[] = $user;
153  }
154  return $members;
155  }
156 
161  public function ToArray() {
162  $array = Utils::ObjectToArray($this);
163  $array["Owner"] = new Account($this->IdOwner);
164  $array["Owner"] = $array["Owner"]->ToJson();
165  $array["Users"] = array();
166  $members = $this->GetMembersAccount();
167  foreach ($members as $user) {
168  $array["Users"][] = $user->ToArray();
169  }
170  return $array;
171  }
172 }
__construct($src=null, $stripSlashes=false)
Definition: UserGroup.php:23
static Load($name=NULL, $idOwner=0, $idAccount=0, $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: UserGroup.php:63
Definition: Account.php:3