Новый класс Chat; session_start() уехал централизованно в конфиг с проверкой на существование ессии перед открытием. Уборка deprecated-артефактов. Работа по внедрению #42.
This commit is contained in:
56
chat.php
56
chat.php
@@ -5,52 +5,18 @@
|
||||
* Project name: Battles-Game
|
||||
*/
|
||||
|
||||
use Battles\Chat;
|
||||
use Battles\Database\DBPDO;
|
||||
use Battles\Template;
|
||||
|
||||
session_start();
|
||||
require_once "config.php";
|
||||
|
||||
$msg = $_POST['msg'] ?? null;
|
||||
$uid = $_SESSION['uid'] ?? null;
|
||||
if ($msg) {
|
||||
$db = new DBPDO();
|
||||
$db->execute('INSERT INTO chat (user_id,msg) VALUES (?,?)', [$uid, $msg]);
|
||||
$chat = new Chat(new DBPDO());
|
||||
if (!empty($_POST['msg'])) {
|
||||
$chat->addMessage($_POST['msg']);
|
||||
}
|
||||
|
||||
function show_messages()
|
||||
{
|
||||
$db = new DBPDO();
|
||||
|
||||
$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
|
||||
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', [$_SESSION['uid'], $_SESSION['uid']]);
|
||||
$i = 0;
|
||||
while ($i < count($chat)) {
|
||||
$d = new DateTime($chat[$i]->msgdate);
|
||||
$m = htmlspecialchars($chat[$i]->msg);
|
||||
if ($chat[$i]->type == 'sys') { /* Системка */
|
||||
echo sprintf('<span style="color:maroon;background:#faa;">%s %s</span><br>', $d->format('H:i'), $m);
|
||||
} elseif ($chat[$i]->rid == $_SESSION['uid']) { /* С указанным получателем */
|
||||
if ($chat[$i]->type == 'sms') { /* Телеграмма */
|
||||
echo sprintf('<span style="color:darkgreen;background:#afa;">%s Телеграмма от [%s]: %s</span><br>', $d->format('d.m.Y H:i'), $chat[$i]->sender, $m);
|
||||
} elseif ($chat[$i]->type == 'private') { /* Приват */
|
||||
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);
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
unset($i, $chat, $db);
|
||||
}
|
||||
|
||||
|
||||
Template::header('chat');
|
||||
show_messages();
|
||||
echo $chat->getMessages();
|
||||
?>
|
||||
<style>
|
||||
form {
|
||||
@@ -72,6 +38,18 @@ show_messages();
|
||||
border: none;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
span.chatsys {
|
||||
color:maroon;
|
||||
background:#faa;
|
||||
}
|
||||
span.chatsms {
|
||||
color:darkgreen;
|
||||
background:#afa;
|
||||
}
|
||||
span.chatprivate {
|
||||
background:#efe;
|
||||
}
|
||||
</style>
|
||||
<div>
|
||||
<?php ?>
|
||||
|
||||
Reference in New Issue
Block a user