2015-11-07 20:20:11 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the forum object class.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-11-22 22:10:23 +00:00
|
|
|
namespace Sakura\Forum;
|
2015-11-17 19:30:34 +00:00
|
|
|
|
2016-02-18 23:28:44 +00:00
|
|
|
use Sakura\DB;
|
2015-12-29 21:52:19 +00:00
|
|
|
use Sakura\Perms;
|
2015-11-07 20:20:11 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Used to serve forums.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2015-11-07 20:20:11 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-11-07 20:20:11 +00:00
|
|
|
*/
|
|
|
|
class Forum
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The ID of the forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $id = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The order of the forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-01 04:30:27 +00:00
|
|
|
public $order = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $name = "Forum";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The description of the forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $description = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The link of the forum (if the type is 2).
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $link = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the parent forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $category = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $type = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The icon of this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $icon = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cached instance of the first post in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var Post
|
|
|
|
*/
|
2015-12-10 20:55:51 +00:00
|
|
|
private $_firstPost = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cached instance of the last post in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var Post
|
|
|
|
*/
|
2015-12-10 20:55:51 +00:00
|
|
|
private $_lastPost = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached instances of the subforums.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2015-12-10 20:55:51 +00:00
|
|
|
private $_forums = [];
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cached instances of the threads in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2015-12-10 20:55:51 +00:00
|
|
|
private $_threads = [];
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The permission container.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var Perms
|
|
|
|
*/
|
2015-12-11 20:49:40 +00:00
|
|
|
private $_permissions;
|
2015-11-13 23:11:55 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $forumId The ID of the forum that should be constructed.
|
|
|
|
*/
|
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
|
2016-02-25 16:06:29 +00:00
|
|
|
$forumRow = DB::table('forums')
|
|
|
|
->where('forum_id', $forumId)
|
|
|
|
->get();
|
2015-11-13 23:11:55 +00:00
|
|
|
|
2015-12-29 21:52:19 +00:00
|
|
|
// Create permissions object
|
|
|
|
$this->_permissions = new Perms(Perms::FORUM);
|
|
|
|
|
2015-11-13 23:11:55 +00:00
|
|
|
// Populate the variables
|
2015-11-15 14:29:26 +00:00
|
|
|
if ($forumRow) {
|
2016-02-25 16:06:29 +00:00
|
|
|
$forumRow = $forumRow[0];
|
2016-02-18 23:28:44 +00:00
|
|
|
$this->id = $forumRow->forum_id;
|
|
|
|
$this->order = $forumRow->forum_order;
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Checking a permission flag.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $flag Forum permission flag.
|
|
|
|
* @param int $user The ID of the user that is being checked.
|
|
|
|
* @param bool $raw Whether the raw full permission flag should be returned.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return bool|int Either a bool indicating the permission or the full flag.
|
|
|
|
*/
|
2016-01-02 17:55:31 +00:00
|
|
|
public function permission($flag, $user, $raw = false)
|
|
|
|
{
|
2015-12-29 21:52:19 +00:00
|
|
|
// Set default permission value
|
|
|
|
$perm = 0;
|
|
|
|
|
|
|
|
// Get the permissions of the parent forum if there is one
|
|
|
|
if ($this->category) {
|
2016-01-02 17:55:31 +00:00
|
|
|
$perm = $perm | (new Forum($this->category))->permission($flag, $user, true);
|
2015-12-29 21:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bitwise OR it with the permissions for this forum
|
|
|
|
$perm = $perm | $this->_permissions->user($user, ['forum_id' => [$this->id, '=']]);
|
|
|
|
|
2016-01-02 17:55:31 +00:00
|
|
|
return $raw ? $perm : $this->_permissions->check($flag, $perm);
|
2015-12-29 21:52:19 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Gets all subforums of this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @return array Array containing forum objects.
|
|
|
|
*/
|
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
|
2016-02-25 16:06:29 +00:00
|
|
|
$forumRows = DB::table('forums')
|
|
|
|
->where('forum_category', $this->id)
|
|
|
|
->orderBy('forum_order')
|
|
|
|
->get(['forum_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) {
|
2016-02-18 23:28:44 +00:00
|
|
|
$forums[$forum->forum_id] = new Forum($forum->forum_id);
|
2015-12-10 20:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_forums = $forums;
|
|
|
|
} else {
|
|
|
|
$forums = $this->_forums;
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the forum objects
|
|
|
|
return $forums;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the threads in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return array Array containing all 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
|
2016-02-25 16:06:29 +00:00
|
|
|
$threadRows = DB::table('topics')
|
|
|
|
->where('forum_id', $this->id)
|
|
|
|
->orderBy('topic_type', 'desc')
|
|
|
|
->orderBy('topic_last_reply', 'desc')
|
|
|
|
->get(['topic_id']);
|
2015-12-10 20:55:51 +00:00
|
|
|
|
|
|
|
// Create a storage array
|
|
|
|
$threads = [];
|
|
|
|
|
|
|
|
// Create new objects for each thread
|
|
|
|
foreach ($threadRows as $thread) {
|
2016-02-18 23:28:44 +00:00
|
|
|
$threads[$thread->topic_id] = new Thread($thread->topic_id);
|
2015-12-10 20:55:51 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the first post in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return Post The object of the 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
|
2016-02-25 16:06:29 +00:00
|
|
|
$firstPost = DB::table('posts')
|
|
|
|
->where('forum_id', $this->id)
|
|
|
|
->orderBy('post_id')
|
|
|
|
->limit(1)
|
|
|
|
->get(['post_id']);
|
2015-11-15 14:29:26 +00:00
|
|
|
|
2015-12-10 20:55:51 +00:00
|
|
|
// Create the post object
|
2016-02-25 16:06:29 +00:00
|
|
|
$post = new Post(empty($firstPost) ? 0 : $firstPost[0]->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
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the last post in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return Post The object of the 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
|
2016-02-25 16:06:29 +00:00
|
|
|
$lastPost = DB::table('posts')
|
|
|
|
->where('forum_id', $this->id)
|
|
|
|
->orderBy('post_id', 'desc')
|
|
|
|
->limit(1)
|
|
|
|
->get(['post_id']);
|
2015-12-10 20:55:51 +00:00
|
|
|
|
|
|
|
// Create the post object
|
2016-02-25 16:06:29 +00:00
|
|
|
$post = new Post(empty($lastPost) ? 0 : $lastPost[0]->post_id);
|
2016-01-01 04:30:27 +00:00
|
|
|
|
2015-12-10 20:55:51 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Counts the amount of threads in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return int Number of threads in this forum.
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public function threadCount()
|
|
|
|
{
|
2016-02-25 16:06:29 +00:00
|
|
|
return DB::table('topics')
|
|
|
|
->where('forum_id', $this->id)
|
|
|
|
->count();
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Counts the amount of posts in this forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return int Number of posts in this forum.
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public function postCount()
|
|
|
|
{
|
2016-02-25 16:06:29 +00:00
|
|
|
return DB::table('posts')
|
|
|
|
->where('forum_id', $this->id)
|
|
|
|
->count();
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
2015-11-16 22:05:45 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Checks if a user has read every post in the specified forum.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $user Id of the user in question.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @return bool Indicator if read or not.
|
|
|
|
*/
|
2015-11-16 22:05:45 +00:00
|
|
|
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
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Update the read status of all threads in this forum at once.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $user The id of the user in question.
|
|
|
|
*/
|
2015-11-21 15:27:37 +00:00
|
|
|
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
|
|
|
}
|