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/Thread.php

418 lines
9.9 KiB
PHP
Raw Normal View History

<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the thread object class.
*
* @package Sakura
*/
namespace Sakura\Forum;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
/**
2016-02-02 21:04:15 +00:00
* Used to serve, create and update threads.
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Thread
{
2016-02-02 21:04:15 +00:00
/**
* The ID of this thread.
*
* @var int
*/
public $id = 0;
2016-02-02 21:04:15 +00:00
/**
* The ID of the forum this thread is a part of.
*
* @var int
*/
public $forum = 0;
2016-02-02 21:04:15 +00:00
/**
* Is this forum hidden from the listing?
*
* @var bool
*/
public $hidden = false;
/**
* The title of the thread.
*
* @var string
*/
public $title = "";
2016-02-02 21:04:15 +00:00
/**
* The UNIX timestamp of when this thread was created.
*
* @var int
*/
public $time = 0;
2016-02-02 21:04:15 +00:00
/**
* The UNIX timestamp of when this thread should be autolocked (currently unused).
*
* @var int
*/
public $timeLimit = 0;
2016-02-02 21:04:15 +00:00
/**
* The amount of times this thread has been viewed.
*
* @var int
*/
public $views = 0;
2016-02-02 21:04:15 +00:00
/**
* The status of this thread.
* 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
/**
* The thread type
* 0 - Normal thread
* 1 - Sticky thread
* 2 - Announcement
*
* @var int
*/
public $type = 0;
2016-02-02 21:04:15 +00:00
/**
* The ID of the forum this thread was a part of before the last move.
*
* @var int
*/
2016-01-10 18:24:47 +00:00
public $oldForum = 0;
2016-02-02 21:04:15 +00:00
/**
* The post object cache.
*
* @var array
*/
private $_posts = [];
2016-02-02 21:04:15 +00:00
/**
* A cached instance of opening post.
*
* @var Post
*/
private $_firstPost = null;
2016-02-02 21:04:15 +00:00
/**
* A cached instance of the last reply.
*
* @var Post
*/
private $_lastPost = null;
2016-02-02 21:04:15 +00:00
/**
* Constructor.
*
* @param mixed $threadId ID of the thread that should be constructed.
*/
public function __construct($threadId)
{
// Attempt to get the database row
2016-02-25 16:06:29 +00:00
$threadRow = DB::table('topics')
->where('topic_id', $threadId)
->get();
// Assign data if a row was returned
if ($threadRow) {
2016-02-25 16:06:29 +00:00
$threadRow = $threadRow[0];
2016-02-18 23:28:44 +00:00
$this->id = $threadRow->topic_id;
$this->forum = $threadRow->forum_id;
$this->hidden = (bool) $threadRow->topic_hidden;
$this->title = $threadRow->topic_title;
$this->time = $threadRow->topic_time;
$this->timeLimit = $threadRow->topic_time_limit;
$this->views = $threadRow->topic_views;
$this->status = $threadRow->topic_status;
$this->statusChange = $threadRow->topic_status_change;
$this->type = $threadRow->topic_type;
$this->oldForum = $threadRow->topic_old_forum;
}
}
2016-02-02 21:04:15 +00:00
/**
* Create a new thread.
*
* @param mixed $forum ID of the forum this thread is part of.
* @param mixed $title Title of the thread.
* @param mixed $status Status of the thread.
* @param mixed $type Type of thread.
*
2016-02-18 23:28:44 +00:00
* @return self The new thread instance.
2016-02-02 21:04:15 +00:00
*/
public static function create($forum, $title, $status = 0, $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,
]);
// Return the thread object
2016-02-25 16:06:29 +00:00
return new Thread($id);
}
2016-02-02 21:04:15 +00:00
/**
* Delete the current thread.
*/
2016-01-10 18:24:47 +00:00
public function delete()
{
// 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
// Delete thread 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
/**
* Move the thread.
*
* @param mixed $forum The new forum ID.
* @param mixed $setOld Remember the forum ID prior to the move for restoration.
*/
2016-01-10 18:24:47 +00:00
public function move($forum, $setOld = true)
{
// 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
// Update thread 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
/**
* Update the thread data.
*
2016-02-18 23:28:44 +00:00
* @return self The updated thread.
2016-02-02 21:04:15 +00:00
*/
2016-01-10 18:24:47 +00:00
public function update()
{
// 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,
'topic_limit' => $this->timeLimit,
'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
return new Thread($this->id);
}
2016-02-02 21:04:15 +00:00
/**
* Get the replies to this thread.
*
* @return array Array containing Post instances.
*/
public function posts()
{
// Check if _posts is something
if (!count($this->_posts)) {
// Get all rows with the thread 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->_posts = $posts;
} else {
$posts = $this->_posts;
}
// Return the post objects
return $posts;
}
2016-02-02 21:04:15 +00:00
/**
* Get the opening post.
*
* @return Post A Post instance of the opening post.
*/
2015-12-04 13:52:57 +00:00
public function firstPost()
{
// Check if the cache var is set
if ($this->_firstPost !== null) {
return $this->_firstPost;
}
// Get the row from the database
2016-02-25 16:06:29 +00:00
$post = DB::table('posts')
->where('topic_id', $this->id)
->orderBy('post_id')
->limit(1)
->get(['post_id']);
// Create the post class
2016-02-25 16:06:29 +00:00
$post = new Post($post ? $post[0]->post_id : 0);
// Assign it to the cache var
$this->_firstPost = $post;
// Return
return $post;
}
2016-02-02 21:04:15 +00:00
/**
* Get the latest reply.
*
* @return Post A Post instance of the latest reply.
*/
2015-12-04 13:52:57 +00:00
public function lastPost()
{
// Check if the cache var is set
if ($this->_lastPost !== null) {
return $this->_lastPost;
}
// Get the row from the database
2016-02-25 16:06:29 +00:00
$post = DB::table('posts')
->where('topic_id', $this->id)
->orderBy('post_id', 'desc')
->limit(1)
->get(['post_id']);
// Create the post class
2016-02-25 16:06:29 +00:00
$post = new Post($post ? $post[0]->post_id : 0);
// Assign it to the cache var
$this->_lastPost = $post;
// Return
return $post;
}
2016-02-02 21:04:15 +00:00
/**
* Get the amount of replies.
*
* @return int The number of replies to this thread.
*/
public function replyCount()
{
2016-02-25 16:06:29 +00:00
return DB::table('posts')
->where('topic_id', $this->id)
->count();
}
2016-02-02 21:04:15 +00:00
/**
* Check if a user has read this thread before.
*
* @param mixed $user The id of the user in question.
*
* @return bool A boolean indicating the read status.
*/
public function unread($user)
{
// Attempt to get track row from the database
2016-02-25 16:06:29 +00:00
$track = DB::table('topics_track')
->where('user_id', $user)
->where('topic_id', $this->id)
->where('mark_time', '>', $this->lastPost()->time)
->count();
// If nothing was returned it's obvious that the status is unread
2016-02-25 16:06:29 +00:00
if (!$track) {
return true;
}
// Else just return false meaning everything is read
return false;
}
2016-02-02 21:04:15 +00:00
/**
* Update the read status.
*
* @param mixed $user The id of the user in question.
*/
public function trackUpdate($user)
{
// 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();
// 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)
->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,
'mark_time' => time(),
]);
}
}
2016-02-02 21:04:15 +00:00
/**
* Update the view count.
*/
public function viewsUpdate()
{
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
/**
* Update the timestamp of when this thread was last replied to.
*/
public function lastUpdate()
{
2016-02-25 16:06:29 +00:00
DB::table('topics')
->where('topic_id', $this->id)
->update(['topic_last_reply' => time()]);
}
}