Фиксили телеграф, немного дописали чат...

This commit is contained in:
lopar 2021-02-01 03:39:50 +02:00
parent da410d57c8
commit 7fa1720c40
2 changed files with 36 additions and 32 deletions

View File

@ -5,48 +5,52 @@
* Project name: Battles-Game * Project name: Battles-Game
*/ */
use Battles\Database\DBPDO;
use Battles\Template;
session_start(); session_start();
require_once "config.php"; require_once "config.php";
$msg = $_POST['msg'] ?? null; $msg = $_POST['msg'] ?? null;
$uid = $_SESSION['uid'] ?? null; $uid = $_SESSION['uid'] ?? null;
if ($msg) { if ($msg) {
try { $db = new DBPDO();
db::c()->query('INSERT INTO `chat` (`user_id`, `msg`) VALUES (?i, "?s")', $uid, $msg); $db->execute('INSERT INTO chat (user_id,msg) VALUES (?,?)', [$uid, $msg]);
} catch (\Krugozor\Database\Mysql\Exception $e) {
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
}
} }
function show_messages() function show_messages()
{ {
try { $db = new DBPDO();
$chat = db::c()->query('
SELECT $chat = $db->ofetchALL('SELECT msg,msgdate,type,s.login AS sender, r.login AS receiver, s.id AS sid, r.id AS rid FROM chat
`msg`, LEFT JOIN users s on s.id = chat.user_id
`msgdate`, LEFT JOIN users r on r.id = chat.receiver_id
(SELECT `login` FROM `users` WHERE `users`.`id` = `user_id`) AS `from`, WHERE r.id = ? OR r.id IS NULL OR s.id = ? ORDER BY chat.id', [$_SESSION['uid'], $_SESSION['uid']]);
`type` $i = 0;
FROM `chat` ORDER BY `id` LIMIT 50'); while ($i < count($chat)) {
while ($message = $chat->fetch_assoc()) { $d = new DateTime($chat[$i]->msgdate);
$d = new DateTime($message['msgdate']); $m = htmlspecialchars($chat[$i]->msg);
$m = htmlspecialchars($message['msg']); if ($chat[$i]->type == 'sys') { /* Системка */
if ($message['type'] == 'sys') { /* Системка */ echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m);
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m); } elseif ($chat[$i]->rid == $_SESSION['uid']) { /* С указанным получателем */
} elseif ($message['type'] == 'sms') { /* Телеграмма */ if ($chat[$i]->type == 'sms') { /* Телеграмма */
echo sprintf('<span style="color:darkgreen;background:#afa;">[Телеграмма]: %s %s</span><br>', $d->format('H:i'), $m); echo sprintf('<span style="color:darkgreen;background:#afa;">%s Телеграмма от [%s]: %s</span><br>', $d->format('d.m.Y H:i'), $chat[$i]->sender, $m);
} else { } elseif ($chat[$i]->type == 'private') { /* Приват */
echo sprintf('%s [%s]: %s<br>', $d->format('H:i'), $message['from'], $m); echo sprintf('<span style="background:#efe;">%s [%s] → [%s]: %s</span><br>', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
} else { /* Общак */
echo sprintf('%s [%s] → [%s]: %s<br>', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
} }
} else { /* Без указанного получателя */
echo sprintf('%s [%s]: %s<br>', $d->format('H:i'), $chat[$i]->sender, $m);
} }
} catch (\Krugozor\Database\Mysql\Exception $e) { $i++;
echo "<div style='background-color: #ffaaaa;'>Ошибка: " . $e->getMessage() . "<br> В файле: " . $e->getFile() . " (" . $e->getLine() . ")</div>";
} }
unset($i, $chat, $db);
} }
Template::header('chat');
show_messages(); show_messages();
\Battles\Template::header('chat');
?> ?>
<style> <style>
form { form {

View File

@ -925,17 +925,17 @@ function err($t)
/** /**
* @param $name * @param int $userId
* @param $text * @param string $text
* *
* @throws \Krugozor\Database\Mysql\Exception
*/ */
function telegraph($userId, $text) function telegraph(int $userId, string $text)
{ {
db::c()->query('SELECT 1 FROM `users` WHERE `id` = ?i', $userId)->fetch_assoc(); $db = DBPDO::INIT();
if (db::c()->getAffectedRows()) { if ($db->ofetch('SELECT 1 FROM users WHERE id = ?', $userId)) {
db::c()->query('INSERT INTO `telegraph` (receiver, text) VALUES (?i,"?s")', $userId, $text); $db->execute('INSERT INTO chat (user_id,receiver_id,msg,type) VALUES (-1,?,?,?)', [$userId, $text, 'sms']);
} }
unset($db);
} }
function get_meshok() function get_meshok()