Combu Server  3.1.1
PHP API Documentation
GameMail.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class GameMail extends DataClass {
11 
12  const TABLE_NAME = "Mail";
13 
14  public $Id = 0;
15  public $IdAccount = 0;
16  public $IdGroup = 0;
17  public $IdSender = 0;
18  public $IsPublic = 0;
19  public $SendDate = "";
20  public $ReadDate = "";
21  public $Subject = "";
22  public $Message = "";
23 
27  public function __construct($src = null, $stripSlashes = false) {
28  global $Database;
29  if ($src == null)
30  return;
31  if (is_array($src)) {
32  // Load by array
33  $this->_loadByRow($src, $stripSlashes);
34  } else if (is_numeric($src) && intval($src) > 0) {
35  // Load by Id
36  $this->_loadFilter(self::GetTableName(__CLASS__), "Id = " . intval($src));
37  }
38  }
39 
53  public static function Load ($idAccount = 0, $idSender = 0, $idGroup = 0, $unread = FALSE, $limit = null, $offset = null, &$count = null, $returnArray = false) {
54  $where = NULL;
55  if ($idAccount > 0) {
56  $where .= ($where ? " AND " : "") . sprintf("(IdAccount = %d)", $idAccount);
57  }
58  if ($idSender > 0) {
59  $where .= ($where ? " AND " : "") . sprintf("(IdSender = %d)", $idSender);
60  }
61  if ($idGroup > 0) {
62  $where .= ($where ? " AND " : "") . sprintf("(IdGroup = %d)", $idGroup);
63  }
64  if ($unread === TRUE) {
65  $where .= ($where ? " AND " : "") . "(ReadDate IS NULL)";
66  }
67  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "SendDate DESC", $limit, $offset, $count);
68  }
69 
75  public static function LoadUnread($idAccount, $idSenders = array(), $idGroups = array(), $returnArray = false) {
76  $where = sprintf("(ReadDate IS NULL AND IdAccount = %d)", $idAccount);
77  if (count($idSenders) > 0)
78  $where .= ($where ? " AND " : "") . sprintf("(IdGroup = 0 AND IdSender IN (%s))", implode(",", $idSenders));
79  if (count($idGroups) > 0)
80  $where .= ($where ? " AND " : "") . sprintf("(IdGroup IN (%s))", implode(",", $idGroups));
81  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "SendDate DESC", $limit, $offset, $count);
82  }
83 
89  public static function LoadConversations ($idAccount) {
90  $where = sprintf("(IdAccount = %d OR IdSender = %d)", $idAccount, $idAccount);
91  $query = "SELECT * FROM (" .
92  "SELECT IF(IdSender = " . $idAccount . ", IdAccount, IdSender) AS IdSender, IdGroup, SendDate FROM " . self::GetTableName(__CLASS__) . " WHERE IdGroup = 0 AND " . $where . " GROUP BY IF(IdSender = " . $idAccount . ", IdAccount, IdSender)" .
93  " UNION SELECT 0 AS IdSender, IdGroup, SendDate FROM " . self::GetTableName(__CLASS__) . " WHERE IdGroup > 0 AND IdAccount = " . $idAccount . " GROUP BY IdGroup ORDER BY SendDate DESC" .
94  ") T ORDER BY SendDate DESC";
95  $recs = self::_loadQuery($query);
96  $result = array();
97  foreach ($recs as $r) {
98  if ($r["IdGroup"] > 0) {
99  $group = new UserGroup($r["IdGroup"]);
100  $result[] = $group->ToArray();
101  } else {
102  $user = new Account($r["IdSender"]);
103  $result[] = $user->ToArray();
104  }
105  }
106  return $result;
107  }
108 
120  public static function LoadConversationMessages ($myAccount, $otherAccount, $idGroup, $limit = null, $offset = null, &$count = null, $returnArray = false) {
121  if ($otherAccount < 1 && $idGroup < 1) {
122  return array();
123  }
124  $where = sprintf("(IdAccount = %d OR IdSender = %d)", $myAccount, $myAccount);
125  if ($otherAccount > 0)
126  $where .= sprintf(" AND (IdAccount = %d OR IdSender = %d)", $otherAccount, $otherAccount);
127  if ($idGroup > 0)
128  $where .= sprintf(" AND (IdGroup = %d)", $idGroup) . " GROUP BY IdSender, SendDate";
129  return self::_load(self::GetTableName(__CLASS__), ($returnArray ? "" : __CLASS__), $where, "SendDate DESC", $limit, $offset, $count);
130  }
131 
137  public function Save() {
138  global $Database;
139  if ($this->Id > 0) {
140  $query = sprintf("UPDATE %s SET IsPublic = %d, Subject = '%s', Message = '%s', ReadDate = %s WHERE Id = %d",
141  self::GetTableName(__CLASS__),
142  $this->IsPublic,
143  $Database->Escape($this->Subject),
144  $Database->Escape($this->Message),
145  $Database->EscapeDate($this->ReadDate),
146  $this->Id);
147  } else {
148  if (!$this->SendDate)
149  $this->SendDate = date("Y-m-d H:i:s");
150  $query = sprintf("INSERT INTO %s (SendDate, ReadDate, IdAccount, IdGroup, IdSender, IsPublic, Subject, Message) VALUES (%s, NULL, %d, %d, %d, %d, '%s', '%s')",
151  self::GetTableName(__CLASS__),
152  $Database->EscapeDate($this->SendDate),
153  $this->IdAccount,
154  $this->IdGroup,
155  $this->IdSender,
156  $this->IsPublic,
157  $Database->Escape($this->Subject),
158  $Database->Escape($this->Message));
159  }
160  if ($Database->Query($query)) {
161  if ($this->Id < 1)
162  $this->Id = $Database->InsertedId();
163  return TRUE;
164  }
165  return FALSE;
166  }
167 
173  public function Delete() {
174  if ($this->Id < 1)
175  return FALSE;
176  return $this->_Delete(self::GetTableName(__CLASS__), "Id = " . $this->Id);
177  }
178 
182  public static function Prune() {
183  self::TruncateClass(__CLASS__);
184  }
185 
186  public function ToArray() {
187  $from = new Account($this->IdSender);
188  $to = new Account($this->IdAccount);
189  $array = Utils::ObjectToArray($this);
190  $array["From"] = $from->ToJson();
191  $array["To"] = $to->ToJson();
192  if ($this->IdGroup > 0) {
193  $group = new UserGroup($this->IdGroup);
194  $array["ToGroup"] = $group->ToArray();
195  }
196  return $array;
197  }
198 
199  public function ToJson() {
200  $array = $this->ToArray();
201  return json_encode($array);
202  }
203 }
__construct($src=null, $stripSlashes=false)
Definition: GameMail.php:27
static LoadConversationMessages($myAccount, $otherAccount, $idGroup, $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: GameMail.php:120
static Prune()
Definition: GameMail.php:182
static LoadConversations($idAccount)
Definition: GameMail.php:89
static LoadUnread($idAccount, $idSenders=array(), $idGroups=array(), $returnArray=false)
Definition: GameMail.php:75
Definition: Account.php:3
static Load($idAccount=0, $idSender=0, $idGroup=0, $unread=FALSE, $limit=null, $offset=null, &$count=null, $returnArray=false)
Definition: GameMail.php:53