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

402 lines
9.9 KiB
PHP
Raw Permalink Normal View History

<?php
2016-02-03 22:22:56 +00:00
/**
2016-07-30 13:48:09 +00:00
* Holds the topic object class.
2016-02-03 22:22:56 +00:00
* @package Sakura
*/
namespace Sakura\Forum;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
/**
2016-07-30 13:48:09 +00:00
* Used to serve, create and update topics.
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
2016-07-30 13:48:09 +00:00
class Topic
{
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The ID of this topic.
2016-02-02 21:04:15 +00:00
* @var int
*/
public $id = 0;
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The ID of the forum this topic is a part of.
2016-02-02 21:04:15 +00:00
* @var int
*/
public $forum = 0;
2016-02-02 21:04:15 +00:00
2016-12-22 18:10:09 +00:00
/**
* 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;
2016-02-02 21:04:15 +00:00
/**
* Is this forum hidden from the listing?
* @var bool
*/
public $hidden = false;
/**
2016-07-30 13:48:09 +00:00
* The title of the topic.
2016-02-02 21:04:15 +00:00
* @var string
*/
public $title = "";
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The UNIX timestamp of when this topic was created.
2016-02-02 21:04:15 +00:00
* @var int
*/
public $time = 0;
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The UNIX timestamp of when this topic should be autolocked (currently unused).
2016-02-02 21:04:15 +00:00
* @var int
*/
public $timeLimit = 0;
2016-02-02 21:04:15 +00:00
2016-12-22 18:10:09 +00:00
/**
* Replies to this topic.
* @var int
*/
public $replies = 0;
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The amount of times this topic has been viewed.
2016-02-02 21:04:15 +00:00
* @var int
*/
public $views = 0;
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The status of this topic.
2016-02-02 21:04:15 +00:00
* 0 - Unlocked
* 1 - Locked
* @var int
*/
public $status = 0;
2016-02-02 21:04:15 +00:00
/**
* The UNIX timestamp of when the status was last changed.
* @var int
*/
public $statusChange = 0;
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The topic type
* 0 - Normal topic
* 1 - Sticky topic
2016-02-02 21:04:15 +00:00
* 2 - Announcement
* @var int
*/
public $type = 0;
2016-02-02 21:04:15 +00:00
2016-12-22 18:10:09 +00:00
/**
* Time when this topic was last replied to.
* @var int
*/
public $lastReply = 0;
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* The ID of the forum this topic was a part of before the last move.
2016-02-02 21:04:15 +00:00
* @var int
*/
2016-01-10 18:24:47 +00:00
public $oldForum = 0;
2016-02-02 21:04:15 +00:00
/**
2016-12-22 18:10:09 +00:00
* Username of the person that created this topic.
* @var string
2016-02-02 21:04:15 +00:00
*/
2016-12-22 18:10:09 +00:00
public $firstPostUsername = "";
2016-02-02 21:04:15 +00:00
/**
2016-12-22 18:10:09 +00:00
* User colour of the person that created this topic.
* @var string
2016-02-02 21:04:15 +00:00
*/
2016-12-22 18:10:09 +00:00
public $firstPostUserColour = "";
2016-02-02 21:04:15 +00:00
/**
2016-12-22 18:10:09 +00:00
* 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
2016-02-02 21:04:15 +00:00
*/
2016-12-22 18:10:09 +00:00
private $postsCache = [];
2016-02-02 21:04:15 +00:00
/**
* Constructor.
2016-08-05 02:35:37 +00:00
* @param int $topicId
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function __construct(int $topicId)
{
// Attempt to get the database row
2016-07-30 13:48:09 +00:00
$topicRow = DB::table('topics')
->where('topic_id', $topicId)
2016-08-06 14:09:01 +00:00
->first();
// Assign data if a row was returned
2016-07-30 13:48:09 +00:00
if ($topicRow) {
2016-07-31 01:32:37 +00:00
$this->id = intval($topicRow->topic_id);
$this->forum = intval($topicRow->forum_id);
2016-12-22 18:10:09 +00:00
$this->post = intval($topicRow->post_id);
$this->user = intval($topicRow->user_id);
2016-07-31 01:32:37 +00:00
$this->hidden = boolval($topicRow->topic_hidden);
2016-07-30 13:48:09 +00:00
$this->title = $topicRow->topic_title;
2016-07-31 01:32:37 +00:00
$this->time = intval($topicRow->topic_time);
$this->timeLimit = intval($topicRow->topic_time_limit);
2016-12-22 18:10:09 +00:00
$this->replies = intval($topicRow->topic_replies);
2016-07-31 01:32:37 +00:00
$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);
2016-12-22 18:10:09 +00:00
$this->lastReply = intval($topicRow->topic_last_reply);
2016-07-31 01:32:37 +00:00
$this->oldForum = intval($topicRow->topic_old_forum);
2016-12-22 18:10:09 +00:00
$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;
}
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Create a new topic.
2016-08-05 02:35:37 +00:00
* @param int $forum
* @param string $title
* @param int $status
* @param int $type
* @return Topic
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public static function create(int $forum, string $title, int $status = 0, int $type = 0)
{
// Create the database entry
2016-02-25 16:06:29 +00:00
$id = DB::table('topics')
->insertGetId([
'forum_id' => $forum,
'topic_title' => $title,
'topic_time' => time(),
'topic_status' => $status,
'topic_type' => $type,
]);
2016-07-30 13:48:09 +00:00
// Return the topic object
return new Topic($id);
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Delete the current topic.
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function delete(): void
2016-01-10 18:24:47 +00:00
{
// Delete all posts
2016-02-25 16:06:29 +00:00
DB::table('posts')
->where('topic_id', $this->id)
->delete();
2016-01-10 18:24:47 +00:00
2016-07-30 13:48:09 +00:00
// Delete topic meta
2016-02-25 16:06:29 +00:00
DB::table('topics')
->where('topic_id', $this->id)
->delete();
2016-01-10 18:24:47 +00:00
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Move the topic.
2016-08-05 02:35:37 +00:00
* @param int $forum
* @param bool $setOld
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function move(int $forum, bool $setOld = true): void
2016-01-10 18:24:47 +00:00
{
// Update all posts
2016-02-25 16:06:29 +00:00
DB::table('posts')
->where('topic_id', $this->id)
->update(['forum_id' => $forum]);
2016-01-10 18:24:47 +00:00
2016-07-30 13:48:09 +00:00
// Update topic meta
2016-02-25 16:06:29 +00:00
DB::table('topics')
->where('topic_id', $this->id)
->update([
'forum_id' => $forum,
'topic_old_forum' => ($setOld ? $this->forum : 0),
]);
2016-01-10 18:24:47 +00:00
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Update the topic data.
2016-08-05 02:35:37 +00:00
* @return Topic
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function update(): Topic
2016-01-10 18:24:47 +00:00
{
// Update row
2016-02-25 16:06:29 +00:00
DB::table('topics')
->where('topic_id', $this->id)
->update([
'topic_hidden' => $this->hidden,
'topic_title' => $this->title,
2016-03-10 18:54:36 +00:00
'topic_time_limit' => $this->timeLimit,
2016-02-25 16:06:29 +00:00
'topic_status' => $this->status,
'topic_status_change' => $this->statusChange,
'topic_type' => $this->type,
'topic_old_forum' => $this->oldForum,
]);
2016-01-10 18:24:47 +00:00
// Return new object
2016-07-30 13:48:09 +00:00
return new Topic($this->id);
2016-01-10 18:24:47 +00:00
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Get the replies to this topic.
2016-08-05 02:35:37 +00:00
* @return array
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function posts(): array
{
// Check if postsCache is something
if (!count($this->postsCache)) {
2016-07-30 13:48:09 +00:00
// Get all rows with the topic id
2016-02-25 16:06:29 +00:00
$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) {
2016-02-18 23:28:44 +00:00
$posts[$post->post_id] = new Post($post->post_id);
}
$this->postsCache = $posts;
}
2016-12-04 16:33:52 +00:00
return $this->postsCache;
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Check if a user has read this topic before.
2016-08-05 02:35:37 +00:00
* @param int $user
* @return bool
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function unread(int $user): bool
{
2016-12-04 16:33:52 +00:00
// 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)
2016-12-22 18:10:09 +00:00
->where('mark_time', '>', $this->lastReply)
2016-12-04 16:33:52 +00:00
->count();
2016-12-04 16:33:52 +00:00
return !$track;
}
// Else just return false meaning everything is read
return false;
}
2016-02-02 21:04:15 +00:00
/**
* Update the read status.
2016-08-05 02:35:37 +00:00
* @param int $user
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function trackUpdate(int $user): void
{
// Check if we already have a track record
2016-02-25 16:06:29 +00:00
$track = DB::table('topics_track')
->where('user_id', $user)
->where('topic_id', $this->id)
->where('forum_id', $this->forum)
->count();
2016-04-02 13:14:07 +00:00
// Adding a second to this to avoid own posts getting marked unread
$time = time() + 1;
// If so update it
2016-02-25 16:06:29 +00:00
if ($track) {
DB::table('topics_track')
->where('user_id', $user)
->where('topic_id', $this->id)
2016-04-02 13:14:07 +00:00
->update(['mark_time' => $time]);
} else {
// If not create a new record
2016-02-25 16:06:29 +00:00
DB::table('topics_track')
->insert([
'user_id' => $user,
'topic_id' => $this->id,
'forum_id' => $this->forum,
2016-04-02 13:14:07 +00:00
'mark_time' => $time,
2016-02-25 16:06:29 +00:00
]);
}
}
2016-02-02 21:04:15 +00:00
/**
* Update the view count.
*/
2016-12-04 16:33:52 +00:00
public function viewsUpdate(): void
{
2016-02-25 16:06:29 +00:00
DB::table('topics')
->where('topic_id', $this->id)
->increment('topic_views');
}
2016-02-02 21:04:15 +00:00
/**
2016-07-30 13:48:09 +00:00
* Update the timestamp of when this topic was last replied to.
2016-02-02 21:04:15 +00:00
*/
2016-12-04 16:33:52 +00:00
public function lastUpdate(): void
{
2016-02-25 16:06:29 +00:00
DB::table('topics')
->where('topic_id', $this->id)
->update(['topic_last_reply' => time()]);
}
2016-12-22 18:10:09 +00:00
/**
* 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);
}
}