2015-11-07 20:20:11 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Forum class
|
|
|
|
*/
|
|
|
|
|
2015-11-22 22:10:23 +00:00
|
|
|
namespace Sakura\Forum;
|
2015-11-17 19:30:34 +00:00
|
|
|
|
|
|
|
use Sakura\Database;
|
2015-12-29 01:27:49 +00:00
|
|
|
use Sakura\Users;
|
|
|
|
use Sakura\User;
|
2015-11-07 20:20:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Forum
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
class Forum
|
|
|
|
{
|
2015-11-13 23:11:55 +00:00
|
|
|
// Variables
|
|
|
|
public $id = 0;
|
|
|
|
public $name = "Forum";
|
|
|
|
public $description = "";
|
|
|
|
public $link = "";
|
|
|
|
public $category = 0;
|
|
|
|
public $type = 0;
|
|
|
|
public $icon = "";
|
2015-12-10 20:55:51 +00:00
|
|
|
private $_firstPost = null;
|
|
|
|
private $_lastPost = null;
|
|
|
|
private $_forums = [];
|
|
|
|
private $_threads = [];
|
2015-12-11 20:49:40 +00:00
|
|
|
private $_permissions;
|
2015-11-13 23:11:55 +00:00
|
|
|
|
|
|
|
// Constructor
|
2015-11-15 14:29:26 +00:00
|
|
|
public function __construct($forumId = 0)
|
2015-11-13 23:11:55 +00:00
|
|
|
{
|
|
|
|
// Get the row from the database
|
|
|
|
$forumRow = Database::fetch('forums', false, ['forum_id' => [$forumId, '=']]);
|
|
|
|
|
|
|
|
// Populate the variables
|
2015-11-15 14:29:26 +00:00
|
|
|
if ($forumRow) {
|
2015-11-13 23:11:55 +00:00
|
|
|
$this->id = $forumRow['forum_id'];
|
|
|
|
$this->name = $forumRow['forum_name'];
|
|
|
|
$this->description = $forumRow['forum_desc'];
|
|
|
|
$this->link = $forumRow['forum_link'];
|
|
|
|
$this->category = $forumRow['forum_category'];
|
|
|
|
$this->type = $forumRow['forum_type'];
|
|
|
|
$this->icon = $forumRow['forum_icon'];
|
2015-11-15 14:29:26 +00:00
|
|
|
} elseif ($forumId != 0) {
|
|
|
|
$this->id = -1;
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subforums
|
2015-12-10 20:55:51 +00:00
|
|
|
public function forums()
|
2015-11-13 23:11:55 +00:00
|
|
|
{
|
2015-12-10 20:55:51 +00:00
|
|
|
// Check if _forums is populated
|
|
|
|
if (!count($this->_forums)) {
|
|
|
|
// Get all rows with the category id set to the forum id
|
|
|
|
$forumRows = Database::fetch('forums', true, ['forum_category' => [$this->id, '=']]);
|
2015-11-13 23:11:55 +00:00
|
|
|
|
2015-12-10 20:55:51 +00:00
|
|
|
// Create a storage array
|
|
|
|
$forums = [];
|
2015-11-13 23:11:55 +00:00
|
|
|
|
2015-12-10 20:55:51 +00:00
|
|
|
// Create new objects for each forum
|
|
|
|
foreach ($forumRows as $forum) {
|
|
|
|
$forums[$forum['forum_id']] = new Forum($forum['forum_id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_forums = $forums;
|
|
|
|
} else {
|
|
|
|
$forums = $this->_forums;
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the forum objects
|
|
|
|
return $forums;
|
|
|
|
}
|
|
|
|
|
2015-11-15 14:29:26 +00:00
|
|
|
// Threads
|
2015-12-10 20:55:51 +00:00
|
|
|
public function threads()
|
2015-11-15 14:29:26 +00:00
|
|
|
{
|
2015-12-10 20:55:51 +00:00
|
|
|
// Check if _threads is populated
|
|
|
|
if (!count($this->_threads)) {
|
|
|
|
// Get all rows with the forum id for this forum
|
|
|
|
$announcements = Database::fetch('topics', true, ['forum_id' => [$this->id, '='], 'topic_type' => ['2', '=']], ['topic_last_reply', true]);
|
|
|
|
$sticky = Database::fetch('topics', true, ['forum_id' => [$this->id, '='], 'topic_type' => ['1', '=']], ['topic_last_reply', true]);
|
|
|
|
$regular = Database::fetch('topics', true, ['forum_id' => [$this->id, '='], 'topic_type' => ['0', '=']], ['topic_last_reply', true]);
|
|
|
|
|
|
|
|
// Combine them into one array
|
|
|
|
$threadRows = array_merge($announcements, $sticky, $regular);
|
|
|
|
|
|
|
|
// Create a storage array
|
|
|
|
$threads = [];
|
|
|
|
|
|
|
|
// Create new objects for each thread
|
|
|
|
foreach ($threadRows as $thread) {
|
|
|
|
$threads[$thread['topic_id']] = new Thread($thread['topic_id']);
|
|
|
|
}
|
2015-11-15 14:29:26 +00:00
|
|
|
|
2015-12-10 20:55:51 +00:00
|
|
|
$this->_threads = $threads;
|
|
|
|
} else {
|
|
|
|
$threads = $this->_threads;
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the thread objects
|
|
|
|
return $threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First post
|
2015-12-10 20:55:51 +00:00
|
|
|
public function firstPost()
|
2015-11-15 14:29:26 +00:00
|
|
|
{
|
2015-12-10 20:55:51 +00:00
|
|
|
// Check if _firstPost is set
|
|
|
|
if ($this->_firstPost === null) {
|
|
|
|
// Get the row
|
|
|
|
$firstPost = Database::fetch('posts', false, ['forum_id' => [$this->id, '=']], ['post_id'], [1]);
|
2015-11-15 14:29:26 +00:00
|
|
|
|
2015-12-10 20:55:51 +00:00
|
|
|
// Create the post object
|
|
|
|
$post = new Post(empty($firstPost) ? 0 : $firstPost['post_id']);
|
2015-11-15 14:29:26 +00:00
|
|
|
|
2015-12-10 20:55:51 +00:00
|
|
|
// Assign it to a "cache" variable
|
|
|
|
$this->_firstPost = $post;
|
|
|
|
|
|
|
|
// Return the post object
|
|
|
|
return $post;
|
|
|
|
} else {
|
|
|
|
return $this->_firstPost;
|
|
|
|
}
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 23:11:55 +00:00
|
|
|
// Last post
|
2015-12-10 20:55:51 +00:00
|
|
|
public function lastPost()
|
2015-11-13 23:11:55 +00:00
|
|
|
{
|
2015-12-10 20:55:51 +00:00
|
|
|
// Check if _lastPost is set
|
|
|
|
if ($this->_lastPost === null) {
|
|
|
|
// Get the row
|
|
|
|
$lastPost = Database::fetch('posts', false, ['forum_id' => [$this->id, '=']], ['post_id', true], [1]);
|
|
|
|
|
|
|
|
// Create the post object
|
|
|
|
$post = new Post(empty($lastPost) ? 0 : $lastPost['post_id']);
|
|
|
|
|
|
|
|
// Assign it to a "cache" variable
|
|
|
|
$this->_lastPost = $post;
|
|
|
|
|
|
|
|
// Return the post object
|
|
|
|
return $post;
|
|
|
|
} else {
|
|
|
|
return $this->_lastPost;
|
|
|
|
}
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Thread count
|
|
|
|
public function threadCount()
|
|
|
|
{
|
2015-11-15 14:29:26 +00:00
|
|
|
return Database::count('topics', ['forum_id' => [$this->id, '=']])[0];
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post count
|
|
|
|
public function postCount()
|
|
|
|
{
|
2015-11-15 14:29:26 +00:00
|
|
|
return Database::count('posts', ['forum_id' => [$this->id, '=']])[0];
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
2015-11-16 22:05:45 +00:00
|
|
|
|
|
|
|
// Read status
|
|
|
|
public function unread($user)
|
|
|
|
{
|
2015-11-21 15:27:37 +00:00
|
|
|
// Return false if the user id is less than 1
|
|
|
|
if ($user < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check forums
|
2015-12-10 20:55:51 +00:00
|
|
|
foreach ($this->forums() as $forum) {
|
2015-11-21 15:27:37 +00:00
|
|
|
if ($forum->unread($user)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 22:05:45 +00:00
|
|
|
// Check each thread
|
2015-12-10 20:55:51 +00:00
|
|
|
foreach ($this->threads() as $thread) {
|
2015-11-16 22:05:45 +00:00
|
|
|
if ($thread->unread($user)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return false if negative
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-21 15:27:37 +00:00
|
|
|
|
|
|
|
// Mark all threads as read
|
|
|
|
public function trackUpdateAll($user)
|
|
|
|
{
|
|
|
|
// Iterate over every forum
|
2015-12-10 20:55:51 +00:00
|
|
|
foreach ($this->forums() as $forum) {
|
2015-11-21 15:27:37 +00:00
|
|
|
// Update every forum
|
|
|
|
$forum->trackUpdateAll($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over every thread
|
2015-12-10 20:55:51 +00:00
|
|
|
foreach ($this->threads() as $thread) {
|
2015-11-21 15:27:37 +00:00
|
|
|
// Update every thread
|
|
|
|
$thread->trackUpdate($user);
|
|
|
|
}
|
|
|
|
}
|
2015-11-07 20:20:11 +00:00
|
|
|
}
|