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/app/Forum/Forum.php

327 lines
7.5 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;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
use Sakura\CurrentSession;
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
/**
* Holds the permission handler.
* @var ForumPerms
*/
public $perms;
2016-02-02 21:04:15 +00:00
/**
* A cached instance of the first post in this forum.
* @var Post
*/
private $firstPostCache = null;
2016-02-02 21:04:15 +00:00
/**
* A cached instance of the last post in this forum.
* @var Post
*/
private $lastPostCache = null;
2016-02-02 21:04:15 +00:00
/**
* Cached instances of the subforums.
* @var array
*/
private $forumsCache = [];
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Cached instances of the topics in this forum.
2016-02-02 21:04:15 +00:00
* @var array
*/
2016-07-30 13:48:09 +00:00
private $topicsCache = [];
2016-02-02 21:04:15 +00:00
/**
* Constructor.
2016-08-05 02:35:37 +00:00
* @param int $forumId
2016-02-02 21:04:15 +00:00
*/
2016-07-31 01:32:37 +00:00
public function __construct(int $forumId = 0)
{
// Get the row from the database
2016-02-25 16:06:29 +00:00
$forumRow = DB::table('forums')
->where('forum_id', $forumId)
2016-08-06 14:09:01 +00:00
->first();
// Populate the variables
if ($forumRow) {
2016-07-31 01:32:37 +00:00
$this->id = intval($forumRow->forum_id);
$this->order = intval($forumRow->forum_order);
2016-02-18 23:28:44 +00:00
$this->name = $forumRow->forum_name;
$this->description = $forumRow->forum_desc;
$this->link = $forumRow->forum_link;
2016-07-31 01:32:37 +00:00
$this->category = intval($forumRow->forum_category);
$this->type = intval($forumRow->forum_type);
2016-02-18 23:28:44 +00:00
$this->icon = $forumRow->forum_icon;
2016-07-31 01:32:37 +00:00
} elseif ($forumId !== 0) {
$this->id = -1;
}
$this->perms = new ForumPerms($this, CurrentSession::$user);
}
2016-02-02 21:04:15 +00:00
/**
* Checking a permission flag.
2016-08-05 02:35:37 +00:00
* @param int $flag
* @param int $user
* @param bool $raw
* @return bool|int
2016-02-02 21:04:15 +00:00
*/
public function permission($flag, $user, $raw = false)
{
return $raw ? 1024 : true;
2015-12-29 21:52:19 +00:00
}
2016-02-03 22:22:56 +00:00
/**
* Gets all subforums of this forum.
2016-08-05 02:35:37 +00:00
* @return array
2016-02-03 22:22:56 +00:00
*/
public function forums()
{
// Check if forumsCache is populated
if (!count($this->forumsCache)) {
// 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']);
// Create a storage array
$forums = [];
// 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);
}
$this->forumsCache = $forums;
} else {
$forums = $this->forumsCache;
}
// Return the forum objects
return $forums;
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Gets the topics in this forum.
2016-08-05 02:35:37 +00:00
* @return array
2016-02-02 21:04:15 +00:00
*/
2016-07-30 13:48:09 +00:00
public function topics()
{
2016-07-30 13:48:09 +00:00
// Check if topicsCache is populated
if (!count($this->topicsCache)) {
// Get all rows with the forum id for this forum
2016-07-30 13:48:09 +00:00
$topicRows = DB::table('topics')
2016-02-25 16:06:29 +00:00
->where('forum_id', $this->id)
->orderBy('topic_type', 'desc')
->orderBy('topic_last_reply', 'desc')
->get(['topic_id']);
// Create a storage array
2016-07-30 13:48:09 +00:00
$topics = [];
2016-07-30 13:48:09 +00:00
// Create new objects for each topic
foreach ($topicRows as $topic) {
$topics[$topic->topic_id] = new Topic($topic->topic_id);
}
2016-07-30 13:48:09 +00:00
$this->topicsCache = $topics;
} else {
2016-07-30 13:48:09 +00:00
$topics = $this->topicsCache;
}
2016-07-30 13:48:09 +00:00
// Return the topic objects
return $topics;
}
2016-02-02 21:04:15 +00:00
/**
* Gets the first post in this forum.
2016-08-05 02:35:37 +00:00
* @return Post
2016-02-02 21:04:15 +00:00
*/
public function firstPost()
{
// Check if firstPostCache is set
if ($this->firstPostCache === 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)
2016-08-06 14:09:01 +00:00
->first(['post_id']);
// Create the post object
2016-08-06 14:09:01 +00:00
$post = new Post($firstPost->post_id ?? 0);
// Assign it to a "cache" variable
$this->firstPostCache = $post;
// Return the post object
return $post;
} else {
return $this->firstPostCache;
}
}
2016-02-02 21:04:15 +00:00
/**
* Gets the last post in this forum.
2016-08-05 02:35:37 +00:00
* @return Post
2016-02-02 21:04:15 +00:00
*/
public function lastPost()
{
// Check if lastPostCache is set
if ($this->lastPostCache === 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)
2016-08-06 14:09:01 +00:00
->first(['post_id']);
// Create the post object
2016-08-06 14:09:01 +00:00
$post = new Post($lastPost->post_id ?? 0);
2016-01-01 04:30:27 +00:00
// Assign it to a "cache" variable
$this->lastPostCache = $post;
// Return the post object
return $post;
} else {
return $this->lastPostCache;
}
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Counts the amount of topics in this forum.
2016-08-05 02:35:37 +00:00
* @return int
2016-02-02 21:04:15 +00:00
*/
2016-07-30 13:48:09 +00:00
public function topicCount()
{
2016-02-25 16:06:29 +00:00
return DB::table('topics')
->where('forum_id', $this->id)
->count();
}
2016-02-02 21:04:15 +00:00
/**
* Counts the amount of posts in this forum.
2016-08-05 02:35:37 +00:00
* @return int
2016-02-02 21:04:15 +00:00
*/
public function postCount()
{
2016-02-25 16:06:29 +00:00
return DB::table('posts')
->where('forum_id', $this->id)
->count();
}
2016-02-02 21:04:15 +00:00
/**
* Checks if a user has read every post in the specified forum.
2016-08-05 02:35:37 +00:00
* @param int $user
* @return bool
2016-02-02 21:04:15 +00:00
*/
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;
}
}
2016-07-30 13:48:09 +00:00
// Check each topic
foreach ($this->topics() as $topic) {
if ($topic->unread($user)) {
return true;
}
}
// Return false if negative
return false;
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Update the read status of all topics in this forum at once.
2016-08-05 02:35:37 +00:00
* @param int $user
2016-02-02 21:04:15 +00:00
*/
public function trackUpdateAll($user)
{
// Iterate over every forum
foreach ($this->forums() as $forum) {
// Update every forum
$forum->trackUpdateAll($user);
}
2016-07-30 13:48:09 +00:00
// Iterate over every topic
foreach ($this->topics() as $topic) {
// Update every topic
$topic->trackUpdate($user);
}
}
2015-11-07 20:20:11 +00:00
}