chiro-canto/public/discussion/index.php

90 lines
2.8 KiB
PHP

<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
$lang = isset($_COOKIE['lang']) ? $_COOKIE['lang'] : 'en';
require("$root/lang/gettext.php");
require "$root/database/credentials.php";
// Connect the database
try {
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf8",
$user,
$password,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
} catch (Exception $e) {
die("Error : ".$e->getMessage());
}
$req = $db->prepare('SELECT id FROM `authors` WHERE username=:username');
$req->execute(array(
"username"=>$_SESSION['username']
));
$data = $req->fetch();
$user_id = $data['id'];
$req = $db->prepare('SELECT message_by, COUNT(message_by) AS n_msg FROM `messages` WHERE message_to=:user_id AND message_read=0');
$req->execute(array(
"user_id"=>$user_id
));
$result = $req->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?=_('Discussion')?> | Chiro - Canto</title>
<link rel="stylesheet" type="text/css" href="/styles/style.css">
</head>
<?php
include("$root/analytics/owa.php");
include("$root/analytics/matomo.php");
?>
<body>
<?php include("$root/menu.php");?>
<?php include("$root/header.php");?>
<section>
<h2><?=_('Discussion')?></h2>
<h3><?=_('New messages')?></h3>
<ul>
<?php
$no_msg = true;
foreach($result as $sender) {
$req = $db->prepare('SELECT username FROM `authors` WHERE id=:id');
$req->execute(array(
"id"=>$sender['message_by']
));
if($data = $req->fetch()) {
$username = $data['username'];
?>
<li>
<a href="messages/?author=<?=$sender['message_by']?>" class="notification">
<span><?=$username?></span>
<span class="badge"><?=$sender['n_msg']?></span>
</a>
</li>
<?php
$no_msg = false;
}
}
if ($no_msg) {
echo _('No message received.').'\n';
}
?>
</ul>
<h3><?_('Send a message')?></h3>
<form action="messages" method="get">
<label for="author"><?=_('Addressee')?></label>
<input type="text" name="author" id="author">
<input type="submit" value="<?=_('Send')?>">
</form>
</section>
<?php include("$root/footer.php");?>
</body>
<script src="/scripts/script.js"></script>
</html>