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/Topic.php
2016-12-22 19:10:09 +01:00

401 lines
9.9 KiB
PHP

<?php
/**
* Holds the topic object class.
* @package Sakura
*/
namespace Sakura\Forum;
use Sakura\DB;
/**
* Used to serve, create and update topics.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Topic
{
/**
* The ID of this topic.
* @var int
*/
public $id = 0;
/**
* The ID of the forum this topic is a part of.
* @var int
*/
public $forum = 0;
/**
* Id of the opening post this topic is associated with.
* @var int
*/
public $post = 0;
/**
* Id of the user that created this topic.
* @var int
*/
public $user = 0;
/**
* Is this forum hidden from the listing?
* @var bool
*/
public $hidden = false;
/**
* The title of the topic.
* @var string
*/
public $title = "";
/**
* The UNIX timestamp of when this topic was created.
* @var int
*/
public $time = 0;
/**
* The UNIX timestamp of when this topic should be autolocked (currently unused).
* @var int
*/
public $timeLimit = 0;
/**
* Replies to this topic.
* @var int
*/
public $replies = 0;
/**
* The amount of times this topic has been viewed.
* @var int
*/
public $views = 0;
/**
* The status of this topic.
* 0 - Unlocked
* 1 - Locked
* @var int
*/
public $status = 0;
/**
* The UNIX timestamp of when the status was last changed.
* @var int
*/
public $statusChange = 0;
/**
* The topic type
* 0 - Normal topic
* 1 - Sticky topic
* 2 - Announcement
* @var int
*/
public $type = 0;
/**
* Time when this topic was last replied to.
* @var int
*/
public $lastReply = 0;
/**
* The ID of the forum this topic was a part of before the last move.
* @var int
*/
public $oldForum = 0;
/**
* Username of the person that created this topic.
* @var string
*/
public $firstPostUsername = "";
/**
* User colour of the person that created this topic.
* @var string
*/
public $firstPostUserColour = "";
/**
* Id of the last reply to this topic.
* @var int
*/
public $lastPostId = 0;
/**
* User id of the person that last replied to this topic.
* @var int
*/
public $lastPostUserId = 0;
/**
* Username of the person that last replied to this topic.
* @var string
*/
public $lastPostUsername = "";
/**
* User colour of the person that last replied to this topic.
* @var string
*/
public $lastPostUserColour = "";
/**
* The post object cache.
* @var array
*/
private $postsCache = [];
/**
* Constructor.
* @param int $topicId
*/
public function __construct(int $topicId)
{
// Attempt to get the database row
$topicRow = DB::table('topics')
->where('topic_id', $topicId)
->first();
// Assign data if a row was returned
if ($topicRow) {
$this->id = intval($topicRow->topic_id);
$this->forum = intval($topicRow->forum_id);
$this->post = intval($topicRow->post_id);
$this->user = intval($topicRow->user_id);
$this->hidden = boolval($topicRow->topic_hidden);
$this->title = $topicRow->topic_title;
$this->time = intval($topicRow->topic_time);
$this->timeLimit = intval($topicRow->topic_time_limit);
$this->replies = intval($topicRow->topic_replies);
$this->views = intval($topicRow->topic_views);
$this->status = intval($topicRow->topic_status);
$this->statusChange = intval($topicRow->topic_status_change);
$this->type = intval($topicRow->topic_type);
$this->lastReply = intval($topicRow->topic_last_reply);
$this->oldForum = intval($topicRow->topic_old_forum);
$this->firstPostUsername = $topicRow->first_post_username;
$this->firstPostUserColour = $topicRow->first_post_user_colour;
$this->lastPostId = intval($topicRow->last_post_id);
$this->lastPostUserId = intval($topicRow->last_post_user_id);
$this->lastPostUsername = $topicRow->last_post_username;
$this->lastPostUserColour = $topicRow->last_post_user_colour;
}
}
/**
* Create a new topic.
* @param int $forum
* @param string $title
* @param int $status
* @param int $type
* @return Topic
*/
public static function create(int $forum, string $title, int $status = 0, int $type = 0)
{
// Create the database entry
$id = DB::table('topics')
->insertGetId([
'forum_id' => $forum,
'topic_title' => $title,
'topic_time' => time(),
'topic_status' => $status,
'topic_type' => $type,
]);
// Return the topic object
return new Topic($id);
}
/**
* Delete the current topic.
*/
public function delete(): void
{
// Delete all posts
DB::table('posts')
->where('topic_id', $this->id)
->delete();
// Delete topic meta
DB::table('topics')
->where('topic_id', $this->id)
->delete();
}
/**
* Move the topic.
* @param int $forum
* @param bool $setOld
*/
public function move(int $forum, bool $setOld = true): void
{
// Update all posts
DB::table('posts')
->where('topic_id', $this->id)
->update(['forum_id' => $forum]);
// Update topic meta
DB::table('topics')
->where('topic_id', $this->id)
->update([
'forum_id' => $forum,
'topic_old_forum' => ($setOld ? $this->forum : 0),
]);
}
/**
* Update the topic data.
* @return Topic
*/
public function update(): Topic
{
// Update row
DB::table('topics')
->where('topic_id', $this->id)
->update([
'topic_hidden' => $this->hidden,
'topic_title' => $this->title,
'topic_time_limit' => $this->timeLimit,
'topic_status' => $this->status,
'topic_status_change' => $this->statusChange,
'topic_type' => $this->type,
'topic_old_forum' => $this->oldForum,
]);
// Return new object
return new Topic($this->id);
}
/**
* Get the replies to this topic.
* @return array
*/
public function posts(): array
{
// Check if postsCache is something
if (!count($this->postsCache)) {
// Get all rows with the topic id
$postRows = DB::table('posts')
->where('topic_id', $this->id)
->get(['post_id']);
// Create a storage array
$posts = [];
// Create new post objects for each post
foreach ($postRows as $post) {
$posts[$post->post_id] = new Post($post->post_id);
}
$this->postsCache = $posts;
}
return $this->postsCache;
}
/**
* Check if a user has read this topic before.
* @param int $user
* @return bool
*/
public function unread(int $user): bool
{
// Only check if user id is positive
if ($user >= 1) {
// Get track row from the database
$track = DB::table('topics_track')
->where('user_id', $user)
->where('topic_id', $this->id)
->where('mark_time', '>', $this->lastReply)
->count();
return !$track;
}
// Else just return false meaning everything is read
return false;
}
/**
* Update the read status.
* @param int $user
*/
public function trackUpdate(int $user): void
{
// Check if we already have a track record
$track = DB::table('topics_track')
->where('user_id', $user)
->where('topic_id', $this->id)
->where('forum_id', $this->forum)
->count();
// Adding a second to this to avoid own posts getting marked unread
$time = time() + 1;
// If so update it
if ($track) {
DB::table('topics_track')
->where('user_id', $user)
->where('topic_id', $this->id)
->update(['mark_time' => $time]);
} else {
// If not create a new record
DB::table('topics_track')
->insert([
'user_id' => $user,
'topic_id' => $this->id,
'forum_id' => $this->forum,
'mark_time' => $time,
]);
}
}
/**
* Update the view count.
*/
public function viewsUpdate(): void
{
DB::table('topics')
->where('topic_id', $this->id)
->increment('topic_views');
}
/**
* Update the timestamp of when this topic was last replied to.
*/
public function lastUpdate(): void
{
DB::table('topics')
->where('topic_id', $this->id)
->update(['topic_last_reply' => time()]);
}
/**
* Increment reply count.
* @param int $amount
*/
public function incrementReplyCount(int $amount = 1): void
{
$this->replies = (int) DB::table('topics')
->where('topic_id', $this->id)
->increment('topic_replies', $amount);
}
/**
* Decrement reply count.
* @param int $amount
*/
public function decrementPostsCount(int $amount = 1): void
{
$this->replies = (int) DB::table('topics')
->where('topic_id', $this->id)
->decrement('topic_replies', $amount);
}
}