db = $db; } public function getMessages(): ?string { $query = 'select msg, msgdate, type, s.login as sender, r.login as receiver, s.id as sid, r.id as rid from chat left join users s on s.id = chat.user_id left join users r on r.id = chat.receiver_id where r.id = ? or r.id is null or s.id = ? order by chat.id'; $chatrows = $this->db->ofetchALL($query, [User::$current->getId(), User::$current->getId()]); $wrappedMessage = null; foreach ($chatrows as $row) { try { $this->d = new DateTime($row->msgdate); } catch (Exception $e) { echo 'Chat Datetime Whoops!'; } $m = htmlspecialchars($row->msg); if ($row->type === 'sys' && empty($row->rid)) { $wrappedMessage .= sprintf('%s %s
', $this->d->format('H:i'), $m); } elseif ($row->rid === User::$current->getId()) { if ($row->type === 'sys') { $wrappedMessage .= sprintf('%s [Система] → %s
', $this->d->format('H:i'), $m); } elseif ($row->type == 'sms') { $wrappedMessage .= sprintf('%s Телеграмма от [%s]: %s
', $this->d->format('d.m.Y H:i'), $row->sender, $m); } elseif ($row->type == 'private') { $wrappedMessage .= sprintf('%s [%s] → [%s]: %s
', $this->d->format('H:i'), $row->sender, $row->receiver, $m); } else { $wrappedMessage .= sprintf('%s [%s] → [%s]: %s
', $this->d->format('H:i'), $row->sender, $row->receiver, $m); } } else { $wrappedMessage .= sprintf('%s [%s]: %s
', $this->d->format('H:i'), $row->sender, $m); } } return $wrappedMessage; } public function addMessage(string $msg) { $this->db->execute('insert into chat (user_id, msg) values (?,?)', [User::$current->getId(), $msg]); } public static function addSYSMessage( string $msg, ?int $receiver_id = null) { DBPDO::$db->execute('insert into chat (user_id, msg, receiver_id, type) values (?,?,?,?)', [User::$current->getId(), $msg, $receiver_id, 'sys']); } }