chiro-canto/public/forum/topics/topics.php

160 lines
5.5 KiB
PHP
Executable File

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
include("$root/vendor/erusev/parsedown/Parsedown.php");
$Parsedown = new Parsedown();
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());
}
if (isset($_GET['topic'])){
$topic_id = $_GET['topic'];
$req = $db->prepare('SELECT topic_subject FROM topics WHERE id=:id');
$req->execute(array(
"id"=>$topic_id
));
if ($data = $req->fetch())
{
$subject = $data['topic_subject'];
}
$req = $db->prepare('SELECT * FROM replies WHERE reply_topic=:id_topic');
$req->execute(array(
"id_topic"=>$topic_id
));
try {
$rows = $req->fetchAll(PDO::FETCH_ASSOC);
?>
<link rel="stylesheet" href="https://unpkg.com/easymde/dist/easymde.min.css">
<script src="https://unpkg.com/easymde/dist/easymde.min.js"></script>
<h3><?=$subject?></h3>
<table id="replies">
<thead>
<tr>
<th scope="col"><?=_('Author')?></th>
<th scope="col"><?=_('Reply')?></th>
<th scope="col"><?=_('Date')?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
$reply_by = $row['reply_by'];
$req = $db->prepare('SELECT firstname, lastname, username FROM `authors` WHERE id=:user_id');
$req->execute(array(
"user_id"=>$reply_by
));
if ($data = $req->fetch())
{
$username = $data['username'];
$firstname = $data['firstname'];
$lastname = $data['lastname'];
}
?>
<tr>
<td class="author"><?="<span class='name'>$firstname $lastname</span><br> <span class='username'>($username)</span>"?></td>
<td class="reply"><?=$Parsedown->text($row['reply_content'])?></td>
<td class="date"><?=$row['reply_date']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<br>
<h2><?=_('Your Reply')?></h2>
<!--
<div class="container column">
<div class="container items row">
<button id="bold" title="bold" onclick="bold()"><b>B</b></button>
<button id="italic" title="italic"><i>I</i></button>
<button id="underline" title="underline"><u>U</u></button>
<button id="crossed" title="crossed"><s>s</s></button>
</div>
<br>
<div class="container items row">
<button id="url" title="url">🔗</button>
<button id="code" title="code"><img src="/media/icons/code.svg" alt="code"></button>
<button id="cite" title="cite"><img src="/media/icons/cite.svg" alt="cite"></button>
<button id="image" title="image"><img src="/media/icons/dslr.png" alt="image"></button>
</div>
</div> -->
<form action="sendreply.php" method="post">
<input type="hidden" name="topic_id" value="<?=$topic_id?>">
<textarea name="reply" id="reply" cols="30" rows="10" placeholder="<?=_('Enter your reply (support Markdown)..')?>"></textarea>
<script>
var easyMDE = new EasyMDE({showIcons: ['strikethrough', 'code', 'table', 'redo', 'heading', 'undo', 'heading-bigger', 'heading-smaller', 'heading-1', 'heading-2', 'heading-3', 'clean-block', 'horizontal-rule']});
</script>
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" value="reset">
</form>
<script src="scripts/editor.js"></script>
<?php
} catch (Exception $e)
{
die("Error : ".$e->getMessage());
echo _("Can't fetch topic data.") .'\n';
}
}
else {
$req = $db->prepare('SELECT * FROM `topics`');
$req->execute();
$rows = $req->fetchAll(PDO::FETCH_ASSOC);
?>
<h3>Topics</h3>
<table id="categories">
<thead>
<tr>
<th scope="col"><?=_('Subjects')?></th>
<th scope="col"><?=_('Answers')?></th>
<th scope="col"><?=_('Author')?></th>
<th scope="col"><?=_('Creation date')?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
$topic_by = $row['topic_by'];
$req = $db->prepare('SELECT firstname, lastname, username FROM `authors` WHERE id=:user_id');
$req->execute(array(
"user_id"=>$topic_by
));
if ($data = $req->fetch())
{
$username = $data['username'];
$firstname = $data['firstname'];
$lastname = $data['lastname'];
}
$req = $db->prepare('SELECT COUNT(*) AS counter FROM replies WHERE reply_topic=:reply_topic');
$req->execute(array(
"reply_topic"=>$row['id']
));
if ($data = $req->fetch()) {
$n_messages = $data['counter'];
}
?>
<tr>
<td><a href="<?="?topic=".$row['id']?>"><?=$row['topic_subject']?></a></td>
<td><?=isset($n_messages) ? $n_messages : _("No reply")?></td>
<td><?="$firstname $lastname ($username)"?></td>
<td><?=$row['topic_date']?></td>
</tr>
<?php
}
?> </tbody>
</table>
<?php
}