summaryrefslogtreecommitdiff
path: root/includes/model/Category.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/model/Category.php')
-rw-r--r--includes/model/Category.php54
1 files changed, 38 insertions, 16 deletions
diff --git a/includes/model/Category.php b/includes/model/Category.php
index ed53bdc..e8cbe60 100644
--- a/includes/model/Category.php
+++ b/includes/model/Category.php
@@ -4,19 +4,24 @@ include_once 'Thread.php';
class Category
{
- public $id = 0;
- public $name = 'Unknown';
- public $description = 'This category does not exist';
+ public $id;
+ public $name;
+ public $description;
public $thread_count = 0;
public $post_count = 0;
- function get_from_database($id): bool
+ // If an invalid id was passed into the constructor, the database will not have
+ // returned a result, but the object will not be null.
+ // We need to keep track of whether or not this object has a value.
+ private $has_value = false;
+
+ public function __construct($id)
{
$sql = "SELECT cat_name, cat_description, cat_thread_count, cat_post_count FROM categories WHERE cat_id = ?;";
$result = Database::get()->query($sql, "i", $id);
if (empty($result)) {
- return false;
+ return;
}
$this->id = $id;
@@ -25,32 +30,49 @@ class Category
$this->thread_count = $result[0]['cat_thread_count'];
$this->post_count = $result[0]['cat_post_count'];
- return true;
+ $this->has_value = true;
+ }
+
+ // Returns true if this object was successfully fetched from the database
+ public function has_value()
+ {
+ return $this->has_value;
+ }
+
+ public static function get_all_categories(): array
+ {
+ $sql = "SELECT cat_id FROM categories ORDER BY cat_id;";
+ $result = Database::get()->query($sql);
+
+ $categories = array();
+
+ foreach ($result as $row) {
+ $category = new Category($row['cat_id']);
+ array_push($categories, $category);
+ }
+
+ return $categories;
}
- function get_threads(): array
+ public function get_threads(): array
{
$sql = "SELECT thread_id FROM threads WHERE thread_category = ? ORDER BY thread_date_lastpost DESC";
$result = Database::get()->query($sql, "i", $this->id);
$threads = array();
foreach ($result as $row) {
- $thread = new Thread();
- $thread->get_from_database($row['thread_id']);
- array_push($threads, $thread);
+ $thread = new Thread($row['thread_id']);
+ if ($thread->has_value())
+ array_push($threads, $thread);
}
return $threads;
}
- function get_latest_thread(): Thread
+ public function get_latest_thread(): Thread
{
$sql = "SELECT thread_id FROM threads WHERE thread_category = ? ORDER BY thread_date_lastpost DESC LIMIT 1";
$result = Database::get()->query($sql, "i", $this->id);
-
- $thread = new Thread();
- $thread->get_from_database($result[0]['thread_id']);
-
- return $thread;
+ return new Thread($result[0]['thread_id']);
}
}