* Project name: Battles-Game
*/
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]);
}
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('%s %s
', $d->format('H:i'), $m);
} elseif ($chat[$i]->rid == $_SESSION['uid']) { /* С указанным получателем */
if ($chat[$i]->type == 'sms') { /* Телеграмма */
echo sprintf('%s Телеграмма от [%s]: %s
', $d->format('d.m.Y H:i'), $chat[$i]->sender, $m);
} elseif ($chat[$i]->type == 'private') { /* Приват */
echo sprintf('%s [%s] → [%s]: %s
', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
} else { /* Общак */
echo sprintf('%s [%s] → [%s]: %s
', $d->format('H:i'), $chat[$i]->sender, $chat[$i]->receiver, $m);
}
} else { /* Без указанного получателя */
echo sprintf('%s [%s]: %s
', $d->format('H:i'), $chat[$i]->sender, $m);
}
$i++;
}
unset($i, $chat, $db);
}
Template::header('chat');
show_messages();
?>