diff options
Diffstat (limited to 'web/index.php')
| -rw-r--r-- | web/index.php | 109 |
1 files changed, 103 insertions, 6 deletions
diff --git a/web/index.php b/web/index.php index fd5246a..b71e61b 100644 --- a/web/index.php +++ b/web/index.php @@ -3,21 +3,118 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/config.php'; $db = new PDO(DB_URL, DB_USER, DB_PASS); -$stmt = $db->query('SELECT name FROM rooms ORDER BY joined_at, departed_at DESC'); -$stmt->execute(); -$rooms = $stmt->fetchAll(PDO::FETCH_ASSOC); +$room = $_GET['r'] ?: null; + +if (isset($room)) { + $room = urldecode($room); + $stmt = $db->prepare('SELECT id, `name`, joined_at, departed_at FROM rooms WHERE `name` = ?'); + $stmt->execute([$room]); + + $room = $stmt->fetch(PDO::FETCH_ASSOC) ?: null; + if (!$room) { + http_response_code(404); + exit("No room found."); + } + $room['encoded'] = urlencode($room['name']); + + $stmt = $db->prepare('SELECT YEAR(sent_at) AS msg_year, + MONTH(sent_at) AS msg_month, + DAY(sent_at) AS msg_day, + COUNT(*) AS msg_count + FROM messages + WHERE room_id = ? + GROUP BY YEAR(sent_at), MONTH(sent_at), DAY(sent_at) + ORDER BY YEAR(sent_at) DESC, MONTH(sent_at) DESC, DAY(sent_at) DESC + '); + $stmt->execute([$room['id']]); + $raw_dates = $stmt->fetchAll(PDO::FETCH_ASSOC); + $dates = []; + + foreach ($raw_dates as $rm) { + $y = $rm['msg_year']; + $m = $rm['msg_month']; + $d = $rm['msg_day']; + $c = $rm['msg_count']; + if (!array_key_exists($y, $dates)) { + $dates[$y] = []; + } + if (!array_key_exists($m, $dates[$y])) { + $dates[$y][$m] = []; + } + if (!array_key_exists($d, $dates[$y][$m])) { + $dates[$y][$m] = []; + } + $dates[$y][$m][$d] = $c; + } +} else { + $stmt = $db->query('SELECT name FROM rooms ORDER BY joined_at, departed_at DESC'); + $stmt->execute(); + $rooms = $stmt->fetchAll(PDO::FETCH_ASSOC); +} ?> <!DOCTYPE html> <html> <head> - <title>Index - <?= INSTANCE_NAME ?></title> + <?php if (isset($room)): ?> + <title><?= $room['name'] ?> - <?= INSTANCE_NAME ?></title> + <?php else: ?> + <title>Index - <?= INSTANCE_NAME ?></title> + <?php endif; ?> <link rel="stylesheet" href="/static/style.css"> <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"> </head> <body> - <?php if (isset($rooms)): ?> + <?php if (isset($room)): ?> + <h1><a href="/"><?= INSTANCE_NAME ?></a> - Index of <?= $room['name'] ?></h1> + <div class="calendar-wrapper"> + <?php foreach ($dates as $year => $months): ?> + <?php foreach ($months as $month => $days): ?> + <?php + $fd = new DateTime("$year-$month-01"); + $dm = (int) $fd->format('t'); + $sw = (int) $fd->format('N'); + $ms = $fd->format('M'); + ?> + <table class="calendar"> + <caption><?= "{$ms} {$year}" ?></caption> + <tr> + <th>Mo</th> + <th>Tu</th> + <th>We</th> + <th>Th</th> + <th>Fr</th> + <th>Sa</th> + <th>Su</th> + </tr> + <tr> + <?php + for ($i = 1; $i < $sw; $i++) + echo '<td></td>'; + + for ($day = 1; $day <= $dm; $day++) { + echo '<td>'; + if (array_key_exists($day, $days) && $days[$day] > 0) { + echo "<a href='/?r={$room['encoded']}&d=$year-$month-$day'>$day</a>"; + } else { + echo $day; + } + echo '</td>'; + if ((($day + $sw - 1) % 7) == 0 && $day != $dm) + echo '</tr><tr>'; + } + + $remainingdays = (7 - (($dm + $sw - 1) % 7)) % 7; + for ($i = 0; $i < $remainingdays; $i++) + echo '<td></td>'; + ?> + </tr> + </table> + <?php endforeach; ?> + <?php endforeach; ?> + </div> + <?php elseif (isset($rooms)): ?> <h1>Index of <?= INSTANCE_NAME ?></h1> <?php if (empty($rooms)): ?> <p>There are no rooms. <a href="/mod/">Add one!</a></p> @@ -29,7 +126,7 @@ $rooms = $stmt->fetchAll(PDO::FETCH_ASSOC); </tr> <?php foreach ($rooms as $r): ?> <tr> - <td><a href="/?c=<?= $r['name'] ?>"><?= $r['name'] ?></a></td> + <td><a href="/?r=<?= urlencode($r['name']) ?>"><?= $r['name'] ?></a></td> <td><?= '1 minute ago' ?></td> </tr> <?php endforeach; ?> |
