summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcflip <36554078+cflip@users.noreply.github.com>2021-06-23 15:19:21 -0600
committercflip <36554078+cflip@users.noreply.github.com>2021-06-23 15:19:21 -0600
commitfd0c3a283153d6f2d759e5e14888e40e65dc61b7 (patch)
tree431fb27b6a87efedbc33f728d08a99c6c4c4adb4
parent4b01ac4021c26c05141f0fbeae20a1c50adbaabf (diff)
Add latest threads to index page
-rwxr-xr-ximg/favicon.pngbin0 -> 612 bytes
-rwxr-xr-x[-rw-r--r--]includes/model/Thread.php272
-rwxr-xr-xincludes/templates/head.php4
-rwxr-xr-x[-rw-r--r--]index.php115
4 files changed, 208 insertions, 183 deletions
diff --git a/img/favicon.png b/img/favicon.png
new file mode 100755
index 0000000..db45531
--- /dev/null
+++ b/img/favicon.png
Binary files differ
diff --git a/includes/model/Thread.php b/includes/model/Thread.php
index 95bd3d8..e02b4b2 100644..100755
--- a/includes/model/Thread.php
+++ b/includes/model/Thread.php
@@ -1,128 +1,144 @@
-<?php
-include_once './includes/Database.php';
-include_once './includes/Session.php';
-include_once 'Category.php';
-include_once 'User.php';
-include_once 'Post.php';
-
-class Thread
-{
- public $id;
- public $subject;
- public $date_created = 0;
- public $date_lastpost = 0;
- public $category;
- public $author;
-
- private $has_value = false;
-
- function __construct($id)
- {
- $sql = "SELECT thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author FROM threads WHERE thread_id = ?;";
- $result = Database::get()->query($sql, "i", $id);
-
- if (empty($result)) {
- return;
- }
-
- $this->id = $id;
- $this->subject = $result[0]['thread_subject'];
- $this->date_created = $result[0]['thread_date_created'];
- $this->date_lastpost = $result[0]['thread_date_lastpost'];
- $this->category = new Category($result[0]['thread_category']);
-
- $this->author = new User();
- $this->author->get_by_id($result[0]['thread_author']);
-
- $this->has_value = true;
- }
-
- public static function create($subject, $category): int
- {
- if (!Session::get()->is_signed_in()) {
- trigger_error('You must be signed in to create a thread');
- return 0;
- }
-
- $user = Session::get()->get_current_user();
-
- // Insert the new thread into the database
- $sql = "INSERT INTO threads(thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
- Database::get()->query($sql, "sii", $subject, $category, $user->id);
-
- // Get the ID of the thread we just created
- $thread_id = Database::get()->get_last_id();
-
- // Increment the category's thread count
- $sql = "UPDATE categories SET `cat_thread_count` = `cat_thread_count` + '1' WHERE cat_id = ?;";
- Database::get()->query($sql, "i", $category);
-
- return $thread_id;
- }
-
- public static function delete($thread)
- {
- // User must be signed in
- if (!Session::get()->is_signed_in()) {
- trigger_error('You must be signed in to delete a thread.');
- return;
- }
-
- // User must be a moderator to delete a thread
- $current_user = Session::get()->get_current_user();
- if ($current_user->level != USER_LEVEL_MODERATOR) {
- trigger_error("You must be a moderator to delete this post.");
- return;
- }
-
- // Delete the thread from the database
- Database::get()->query("DELETE FROM threads WHERE thread_id = ?", "i", $thread->id);
-
- // Decrement the thread count of the category
- Database::get()->query("UPDATE categories SET `cat_thread_count` = `cat_thread_count` - '1' WHERE cat_id = ?", "i", $thread->category->id);
- }
-
- public function has_value()
- {
- return $this->has_value;
- }
-
- public static function get_all(): array
- {
- $sql = "SELECT thread_id FROM threads";
- $result = Database::get()->query($sql);
-
- $threads = array();
-
- foreach ($result as $row) {
- $thread = new Thread($row['thread_id']);
- if ($thread->has_value())
- array_push($threads, $thread);
- }
-
- return $threads;
- }
-
- public function get_posts(): array
- {
- $sql = "SELECT post_id FROM posts WHERE post_thread = ?";
- $result = Database::get()->query($sql, "i", $this->id);
-
- $posts = array();
-
- foreach ($result as $row) {
- $post = new Post($row['post_id']);
- if ($post->has_value())
- array_push($posts, $post);
- }
-
- return $posts;
- }
-
- public function get_latest_post(): Post
- {
- $sql = "SELECT post_id FROM posts WHERE post_thread = ? ORDER BY post_date_created DESC LIMIT 1";
- $result = Database::get()->query($sql, "i", $this->id);
- return new Post($result[0]['post_id']);
- }
-}
+<?php
+include_once './includes/Database.php';
+include_once './includes/Session.php';
+include_once 'Category.php';
+include_once 'User.php';
+include_once 'Post.php';
+
+class Thread
+{
+ public $id;
+ public $subject;
+ public $date_created = 0;
+ public $date_lastpost = 0;
+ public $category;
+ public $author;
+
+ private $has_value = false;
+
+ function __construct($id)
+ {
+ $sql = "SELECT thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author FROM threads WHERE thread_id = ?;";
+ $result = Database::get()->query($sql, "i", $id);
+
+ if (empty($result)) {
+ return;
+ }
+
+ $this->id = $id;
+ $this->subject = $result[0]['thread_subject'];
+ $this->date_created = $result[0]['thread_date_created'];
+ $this->date_lastpost = $result[0]['thread_date_lastpost'];
+ $this->category = new Category($result[0]['thread_category']);
+
+ $this->author = new User();
+ $this->author->get_by_id($result[0]['thread_author']);
+
+ $this->has_value = true;
+ }
+
+ public static function create($subject, $category): int
+ {
+ if (!Session::get()->is_signed_in()) {
+ trigger_error('You must be signed in to create a thread');
+ return 0;
+ }
+
+ $user = Session::get()->get_current_user();
+
+ // Insert the new thread into the database
+ $sql = "INSERT INTO threads(thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author) VALUES (?, CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), CONVERT_TZ(NOW(), 'SYSTEM', '+00:00'), ?, ?);";
+ Database::get()->query($sql, "sii", $subject, $category, $user->id);
+
+ // Get the ID of the thread we just created
+ $thread_id = Database::get()->get_last_id();
+
+ // Increment the category's thread count
+ $sql = "UPDATE categories SET `cat_thread_count` = `cat_thread_count` + '1' WHERE cat_id = ?;";
+ Database::get()->query($sql, "i", $category);
+
+ return $thread_id;
+ }
+
+ public static function delete($thread)
+ {
+ // User must be signed in
+ if (!Session::get()->is_signed_in()) {
+ trigger_error('You must be signed in to delete a thread.');
+ return;
+ }
+
+ // User must be a moderator to delete a thread
+ $current_user = Session::get()->get_current_user();
+ if ($current_user->level != USER_LEVEL_MODERATOR) {
+ trigger_error("You must be a moderator to delete this post.");
+ return;
+ }
+
+ // Delete the thread from the database
+ Database::get()->query("DELETE FROM threads WHERE thread_id = ?", "i", $thread->id);
+
+ // Decrement the thread count of the category
+ Database::get()->query("UPDATE categories SET `cat_thread_count` = `cat_thread_count` - '1' WHERE cat_id = ?", "i", $thread->category->id);
+ }
+
+ public function has_value()
+ {
+ return $this->has_value;
+ }
+
+ public static function get_all(): array
+ {
+ $sql = "SELECT thread_id FROM threads";
+ $result = Database::get()->query($sql);
+
+ $threads = array();
+
+ foreach ($result as $row) {
+ $thread = new Thread($row['thread_id']);
+ if ($thread->has_value())
+ array_push($threads, $thread);
+ }
+
+ return $threads;
+ }
+
+ public static function get_latest(): array
+ {
+ $sql = "SELECT thread_id FROM threads ORDER BY thread_date_lastpost DESC LIMIT 10";
+ $result = Database::get()->query($sql);
+
+ $threads = array();
+
+ foreach ($result as $row) {
+ $thread = new Thread($row['thread_id']);
+ if ($thread->has_value())
+ array_push($threads, $thread);
+ }
+
+ return $threads;
+ }
+
+ public function get_posts(): array
+ {
+ $sql = "SELECT post_id FROM posts WHERE post_thread = ?";
+ $result = Database::get()->query($sql, "i", $this->id);
+
+ $posts = array();
+
+ foreach ($result as $row) {
+ $post = new Post($row['post_id']);
+ if ($post->has_value())
+ array_push($posts, $post);
+ }
+
+ return $posts;
+ }
+
+ public function get_latest_post(): Post
+ {
+ $sql = "SELECT post_id FROM posts WHERE post_thread = ? ORDER BY post_date_created DESC LIMIT 1";
+ $result = Database::get()->query($sql, "i", $this->id);
+ return new Post($result[0]['post_id']);
+ }
+}
diff --git a/includes/templates/head.php b/includes/templates/head.php
new file mode 100755
index 0000000..d7c5758
--- /dev/null
+++ b/includes/templates/head.php
@@ -0,0 +1,4 @@
+<link rel="stylesheet" href="styles/style.css">
+<link rel="icon" href="img/favicon.png">
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1.0"> \ No newline at end of file
diff --git a/index.php b/index.php
index ac2dd3e..3b3f21b 100644..100755
--- a/index.php
+++ b/index.php
@@ -1,55 +1,60 @@
-<?php
-include_once './includes/model/Category.php';
-session_start();
-?>
-<!DOCTYPE html>
-<html lang="en">
- <head>
- <title>cflip.net forum</title>
- <link rel="stylesheet" href="styles/style.css">
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
-<?php include_once 'includes/templates/header.php'; ?>
- <h2>Welcome to the cflip.net forum!</h2>
- <p>
- This is the beta test of the forum website, so there are lots of features missing. Since there are no moderation
- features built into the website,
- <i>for the most part</i> I don't care that much about what is posted here. Some links and buttons may not have any
- functionality either!
- </p>
- <p>
- If you notice a problem or have an idea for a feature that is missing, <a
- href="http://51.195.90.7/forum/thread.php?id=40">reply to this thread!</a>
- </p>
- <h2>Categories</h2>
- <table>
- <tr>
- <th>Category</th>
- <th>Threads</th>
- <th>Posts</th>
- <th>Latest Thread</th>
- </tr>
-<?php foreach (Category::get_all_categories() as $category): ?>
- <tr>
- <td>
- <b><a href="viewcategory.php?id=<?= $category->id ?>"><?= $category->name ?></a></b>
- <br><small><?= $category->description ?></small>
- </td>
- <td><?= $category->thread_count ?></td>
- <td><?= $category->post_count ?></td>
-<?php $latest_thread = $category->get_latest_thread(); if ($latest_thread->has_value()): ?>
- <td>
- <b><a href="viewthread.php?id=<?= $latest_thread->id ?>"><?= $latest_thread->subject ?></a></b>
- <br>
- <small>by <b><a href="viewuser.php?id=<?= $latest_thread->author->id ?>"><?= $latest_thread->author->name ?></a></b>, <?= $latest_thread->date_created ?></small>
- </td>
-<?php else: ?>
- <td>No threads yet!</td>
-<?php endif ?>
- </tr>
-<?php endforeach ?>
- </table>
- </body>
-</html>
+<?php include_once './includes/model/Category.php'; ?>
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <title>cflip.net forum</title>
+<?php include_once 'includes/templates/head.php'; ?>
+ </head>
+ <body>
+<?php include_once 'includes/templates/header.php'; ?>
+ <h2>Categories</h2>
+ <table>
+ <tr>
+ <th>Category</th>
+ <th>Latest Thread</th>
+ </tr>
+<?php foreach (Category::get_all_categories() as $category): ?>
+ <tr>
+ <td>
+ <a style="font-size: larger;" href="viewcategory.php?id=<?= $category->id ?>"><?= $category->name ?></a></h4>
+ <br><?= $category->description ?>
+ </td>
+<?php $latest_thread = $category->get_latest_thread(); if ($latest_thread->has_value()): ?>
+ <td>
+ <b><a href="viewthread.php?id=<?= $latest_thread->id ?>"><?= $latest_thread->subject ?></a></b>
+ <br>
+ <small>by <b><a href="viewuser.php?id=<?= $latest_thread->author->id ?>"><?= $latest_thread->author->name ?></a></b>, <?= $latest_thread->date_created ?></small>
+ </td>
+<?php else: ?>
+ <td>No threads yet!</td>
+<?php endif ?>
+ </tr>
+<?php endforeach ?>
+ </table>
+ <h2>Latest Threads</h2>
+ <table>
+ <tr>
+ <th>Thread</th>
+ <th>Category</th>
+ <th>Latest Post</th>
+ </tr>
+<?php foreach (Thread::get_latest() as $thread): ?>
+ <tr>
+ <td>
+ <b><a href="viewthread.php?id=<?= $thread->id ?>"><?= $thread->subject ?></a></b>
+ <small>on <?= date('M d, Y', strtotime($thread->date_created)); ?></small>
+ </td>
+ <td><a href="viewcategory.php?id=<?= $thread->category->id ?>"><?= $thread->category->name ?></a></td>
+<?php $latest_post = $thread->get_latest_post(); if ($latest_post->has_value()): ?>
+ <td>
+ <small>by <b><a href="viewuser.php?id=<?= $latest_post->author->id ?>"><?= $latest_post->author->name ?></a></b>
+ on <?= $latest_post->date_created ?></small>
+ </td>
+<?php else: ?>
+ <td>No posts yet!</td>
+<?php endif ?>
+ </tr>
+<?php endforeach ?>
+ </table>
+ </body>
+</html>