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
|
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
$query = 'select
|
2021-08-25 15:24:12 +00:00
|
|
|
msg,
|
|
|
|
msgdate,
|
|
|
|
type,
|
2022-12-16 23:20:43 +00:00
|
|
|
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 = ?
|
2021-08-25 15:24:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-10 21:36:30 +00:00
|
|
|
public function sendMessage(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
|
|
|
}
|
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
public static function sendSys(string $msg, ?int $receiverId = null)
|
2021-08-26 14:44:14 +00:00
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
Db::getInstance()->execute('insert into chat (user_id, msg, receiver_id, type) values (-1,?,?,?)', [$msg, $receiverId, 'sys']);
|
2022-06-10 21:36:30 +00:00
|
|
|
}
|
2022-12-16 23:20:43 +00:00
|
|
|
|
|
|
|
public static function sendTelegraf(string $msg, int $receiverId)
|
2022-06-10 21:36:30 +00:00
|
|
|
{
|
2022-12-16 23:20:43 +00:00
|
|
|
Db::getInstance()->execute('insert into chat (user_id, msg, receiver_id, type) values (-1,?,?,?)', [$msg, $receiverId, 'sms']);
|
2021-08-26 14:44:14 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 23:20:43 +00:00
|
|
|
}
|