This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/Forum/Forum.php

351 lines
8.7 KiB
PHP
Raw Normal View History

2015-11-07 20:20:11 +00:00
<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the forum object class.
*
* @package Sakura
*/
namespace Sakura\Forum;
use Sakura\Database;
2015-12-29 01:27:49 +00:00
use Sakura\Users;
use Sakura\User;
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.
*
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.
*
* @var int
*/
public $id = 0;
2016-02-02 21:04:15 +00:00
/**
* The order of the forum.
*
* @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.
*
* @var string
*/
public $name = "Forum";
2016-02-02 21:04:15 +00:00
/**
* The description of the forum.
*
* @var string
*/
public $description = "";
2016-02-02 21:04:15 +00:00
/**
* The link of the forum (if the type is 2).
* @var string
*/
public $link = "";
2016-02-02 21:04:15 +00:00
/**
* The ID of the parent forum.
*
* @var int
*/
public $category = 0;
2016-02-02 21:04:15 +00:00
/**
* The type of forum.
*
* @var int
*/
public $type = 0;
2016-02-02 21:04:15 +00:00
/**
* The icon of this forum.
*
* @var string
*/
public $icon = "";
2016-02-02 21:04:15 +00:00
/**
* A cached instance of the first post in this forum.
*
* @var Post
*/
private $_firstPost = null;
2016-02-02 21:04:15 +00:00
/**
* A cached instance of the last post in this forum.
*
* @var Post
*/
private $_lastPost = null;
2016-02-02 21:04:15 +00:00
/**
* Cached instances of the subforums.
*
* @var array
*/
private $_forums = [];
2016-02-02 21:04:15 +00:00
/**
* Cached instances of the threads in this forum.
*
* @var array
*/
private $_threads = [];
2016-02-02 21:04:15 +00:00
/**
* The permission container.
*
* @var Perms
*/
private $_permissions;
2016-02-02 21:04:15 +00:00
/**
* Constructor.
*
* @param int $forumId The ID of the forum that should be constructed.
*/
public function __construct($forumId = 0)
{
// Get the row from the database
$forumRow = Database::fetch('forums', false, ['forum_id' => [$forumId, '=']]);
2015-12-29 21:52:19 +00:00
// Create permissions object
$this->_permissions = new Perms(Perms::FORUM);
// Populate the variables
if ($forumRow) {
$this->id = $forumRow['forum_id'];
2016-01-01 04:30:27 +00:00
$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'];
} elseif ($forumId != 0) {
$this->id = -1;
}
}
2016-02-02 21:04:15 +00:00
/**
* Checking a permission flag.
*
* @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.
*
* @return bool|int Either a bool indicating the permission or the full flag.
*/
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) {
$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, '=']]);
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.
*
* @return array Array containing forum objects.
*/
public function forums()
{
// Check if _forums is populated
if (!count($this->_forums)) {
// Get all rows with the category id set to the forum id
2016-01-01 04:30:27 +00:00
$forumRows = Database::fetch('forums', true, ['forum_category' => [$this->id, '=']], ['forum_order']);
// Create a storage array
$forums = [];
// 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;
}
// Return the forum objects
return $forums;
}
2016-02-02 21:04:15 +00:00
/**
* Gets the threads in this forum.
*
* @return array Array containing all threads.
*/
public function threads()
{
// 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']);
}
$this->_threads = $threads;
} else {
$threads = $this->_threads;
}
// Return the thread objects
return $threads;
}
2016-02-02 21:04:15 +00:00
/**
* Gets the first post in this forum.
*
* @return Post The object of the first post.
*/
public function firstPost()
{
// Check if _firstPost is set
if ($this->_firstPost === null) {
// Get the row
$firstPost = Database::fetch('posts', false, ['forum_id' => [$this->id, '=']], ['post_id'], [1]);
// Create the post object
$post = new Post(empty($firstPost) ? 0 : $firstPost['post_id']);
// Assign it to a "cache" variable
$this->_firstPost = $post;
// Return the post object
return $post;
} else {
return $this->_firstPost;
}
}
2016-02-02 21:04:15 +00:00
/**
* Gets the last post in this forum.
*
* @return Post The object of the last post.
*/
public function lastPost()
{
// 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']);
2016-01-01 04:30:27 +00:00
// Assign it to a "cache" variable
$this->_lastPost = $post;
// Return the post object
return $post;
} else {
return $this->_lastPost;
}
}
2016-02-02 21:04:15 +00:00
/**
* Counts the amount of threads in this forum.
*
* @return int Number of threads in this forum.
*/
public function threadCount()
{
return Database::count('topics', ['forum_id' => [$this->id, '=']])[0];
}
2016-02-02 21:04:15 +00:00
/**
* Counts the amount of posts in this forum.
*
* @return int Number of posts in this forum.
*/
public function postCount()
{
return Database::count('posts', ['forum_id' => [$this->id, '=']])[0];
}
2016-02-02 21:04:15 +00:00
/**
* Checks if a user has read every post in the specified forum.
*
* @param int $user Id of the user in question.
*
* @return bool Indicator if read or not.
*/
public function unread($user)
{
// Return false if the user id is less than 1
if ($user < 1) {
return false;
}
// Check forums
foreach ($this->forums() as $forum) {
if ($forum->unread($user)) {
return true;
}
}
// Check each thread
foreach ($this->threads() as $thread) {
if ($thread->unread($user)) {
return true;
}
}
// Return false if negative
return false;
}
2016-02-02 21:04:15 +00:00
/**
* Update the read status of all threads in this forum at once.
*
* @param int $user The id of the user in question.
*/
public function trackUpdateAll($user)
{
// Iterate over every forum
foreach ($this->forums() as $forum) {
// Update every forum
$forum->trackUpdateAll($user);
}
// Iterate over every thread
foreach ($this->threads() as $thread) {
// Update every thread
$thread->trackUpdate($user);
}
}
2015-11-07 20:20:11 +00:00
}