2021-08-25 15:24:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Battles;
|
|
|
|
|
2022-01-26 23:15:33 +00:00
|
|
|
use Battles\Database\Db;
|
2021-08-25 15:24:12 +00:00
|
|
|
use DateTime;
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
class Chat
|
|
|
|
{
|
|
|
|
private DateTime $d;
|
2022-01-26 23:15:33 +00:00
|
|
|
private Db $db;
|
2021-08-25 15:24:12 +00:00
|
|
|
|
2022-01-26 23:15:33 +00:00
|
|
|
public function __construct(Db $db)
|
2021-08-25 15:24:12 +00:00
|
|
|
{
|
|
|
|
$this->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';
|
2022-01-26 23:15:33 +00:00
|
|
|
$chatrows = $this->db->ofetchALL($query, [User::getInstance()->getId(), User::getInstance()->getId()]);
|
2021-08-25 15:24:12 +00:00
|
|
|
$wrappedMessage = null;
|
|
|
|
foreach ($chatrows as $row) {
|
|
|
|
try {
|
|
|
|
$this->d = new DateTime($row->msgdate);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo 'Chat Datetime Whoops!';
|
|
|
|
}
|
|
|
|
$m = htmlspecialchars($row->msg);
|
2021-08-26 14:44:14 +00:00
|
|
|
if ($row->type === 'sys' && empty($row->rid)) {
|
2021-08-25 15:24:12 +00:00
|
|
|
$wrappedMessage .= sprintf('<span class="chatsys">%s %s</span><br>', $this->d->format('H:i'), $m);
|
2022-01-26 23:15:33 +00:00
|
|
|
} elseif ($row->rid === User::getInstance()->getId()) {
|
2021-08-26 14:44:14 +00:00
|
|
|
if ($row->type === 'sys') {
|
|
|
|
$wrappedMessage .= sprintf('<span class="chatsys">%s [Система] → %s</span><br>', $this->d->format('H:i'), $m);
|
|
|
|
} elseif ($row->type == 'sms') {
|
2021-08-25 15:24:12 +00:00
|
|
|
$wrappedMessage .= sprintf('<span class="chatsms">%s Телеграмма от [%s]: %s</span><br>', $this->d->format('d.m.Y H:i'), $row->sender, $m);
|
|
|
|
} elseif ($row->type == 'private') {
|
|
|
|
$wrappedMessage .= sprintf('<span class="chatprivate">%s [%s] → [%s]: %s</span><br>', $this->d->format('H:i'), $row->sender, $row->receiver, $m);
|
|
|
|
} else {
|
|
|
|
$wrappedMessage .= sprintf('%s [%s] → [%s]: %s<br>', $this->d->format('H:i'), $row->sender, $row->receiver, $m);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$wrappedMessage .= sprintf('%s [%s]: %s<br>', $this->d->format('H:i'), $row->sender, $m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $wrappedMessage;
|
|
|
|
}
|
|
|
|
|
2021-08-26 14:44:14 +00:00
|
|
|
public function addMessage(string $msg)
|
2021-08-25 15:24:12 +00:00
|
|
|
{
|
2022-01-26 23:15:33 +00:00
|
|
|
$this->db->execute('insert into chat (user_id, msg) values (?,?)', [User::getInstance()->getId(), $msg]);
|
2021-08-25 15:24:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 14:44:14 +00:00
|
|
|
public static function addSYSMessage( string $msg, ?int $receiver_id = null)
|
|
|
|
{
|
2022-01-26 23:15:33 +00:00
|
|
|
Db::getInstance()->execute('insert into chat (user_id, msg, receiver_id, type) values (?,?,?,?)', [User::getInstance()->getId(), $msg, $receiver_id, 'sys']);
|
2021-08-26 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 15:24:12 +00:00
|
|
|
}
|