12 const TABLE_NAME =
"Mail";
15 public $IdAccount = 0;
19 public $SendDate =
"";
20 public $ReadDate =
"";
27 public function __construct($src = null, $stripSlashes =
false) {
33 $this->_loadByRow($src, $stripSlashes);
34 }
else if (is_numeric($src) && intval($src) > 0) {
36 $this->_loadFilter(self::GetTableName(__CLASS__),
"Id = " . intval($src));
53 public static function Load ($idAccount = 0, $idSender = 0, $idGroup = 0, $unread = FALSE, $limit = null, $offset = null, &$count = null, $returnArray =
false) {
56 $where .= ($where ?
" AND " :
"") . sprintf(
"(IdAccount = %d)", $idAccount);
59 $where .= ($where ?
" AND " :
"") . sprintf(
"(IdSender = %d)", $idSender);
62 $where .= ($where ?
" AND " :
"") . sprintf(
"(IdGroup = %d)", $idGroup);
64 if ($unread === TRUE) {
65 $where .= ($where ?
" AND " :
"") .
"(ReadDate IS NULL)";
67 return self::_load(self::GetTableName(__CLASS__), ($returnArray ?
"" : __CLASS__), $where,
"SendDate DESC", $limit, $offset, $count);
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);
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);
97 foreach ($recs as $r) {
98 if ($r[
"IdGroup"] > 0) {
100 $result[] = $group->ToArray();
102 $user =
new Account($r[
"IdSender"]);
103 $result[] = $user->ToArray();
120 public static function LoadConversationMessages ($myAccount, $otherAccount, $idGroup, $limit = null, $offset = null, &$count = null, $returnArray =
false) {
121 if ($otherAccount < 1 && $idGroup < 1) {
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);
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);
140 $query = sprintf(
"UPDATE %s SET IsPublic = %d, Subject = '%s', Message = '%s', ReadDate = %s WHERE Id = %d",
141 self::GetTableName(__CLASS__),
143 $Database->Escape($this->Subject),
144 $Database->Escape($this->Message),
145 $Database->EscapeDate($this->ReadDate),
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),
157 $Database->Escape($this->Subject),
158 $Database->Escape($this->Message));
160 if ($Database->Query($query)) {
162 $this->Id = $Database->InsertedId();
176 return $this->_Delete(self::GetTableName(__CLASS__),
"Id = " . $this->Id);
183 self::TruncateClass(__CLASS__);
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) {
194 $array[
"ToGroup"] = $group->ToArray();
200 $array = $this->ToArray();
201 return json_encode($array);
__construct($src=null, $stripSlashes=false)
static LoadConversationMessages($myAccount, $otherAccount, $idGroup, $limit=null, $offset=null, &$count=null, $returnArray=false)
static LoadConversations($idAccount)
static LoadUnread($idAccount, $idSenders=array(), $idGroups=array(), $returnArray=false)
static Load($idAccount=0, $idSender=0, $idGroup=0, $unread=FALSE, $limit=null, $offset=null, &$count=null, $returnArray=false)