2015-11-13 23:11:55 +00:00
|
|
|
<?php
|
2015-11-22 22:10:23 +00:00
|
|
|
namespace Sakura\Forum;
|
2015-11-17 19:30:34 +00:00
|
|
|
|
|
|
|
use Sakura\Database;
|
2016-01-17 01:58:31 +00:00
|
|
|
use Sakura\Utils;
|
2015-11-13 23:11:55 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Used to serve, create and update threads.
|
|
|
|
*
|
2015-11-13 23:11:55 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-11-13 23:11:55 +00:00
|
|
|
*/
|
|
|
|
class Thread
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The ID of this thread.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $id = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the forum this thread is a part of.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
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
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $title = "";
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of when this thread was created.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
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
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $timeLimit = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount of times this thread has been viewed.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $views = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The status of this thread.
|
|
|
|
* 0 - Unlocked
|
|
|
|
* 1 - Locked
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $status = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of when the status was last changed.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public $statusChange = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The thread type
|
|
|
|
* 0 - Normal thread
|
|
|
|
* 1 - Sticky thread
|
|
|
|
* 2 - Announcement
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
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
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
private $_posts = [];
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cached instance of opening post.
|
|
|
|
*
|
|
|
|
* @var Post
|
|
|
|
*/
|
2015-12-10 20:55:51 +00:00
|
|
|
private $_firstPost = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cached instance of the last reply.
|
|
|
|
*
|
|
|
|
* @var Post
|
|
|
|
*/
|
2015-12-10 20:55:51 +00:00
|
|
|
private $_lastPost = null;
|
2015-11-13 23:11:55 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param mixed $threadId ID of the thread that should be constructed.
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public function __construct($threadId)
|
|
|
|
{
|
|
|
|
// Attempt to get the database row
|
|
|
|
$threadRow = Database::fetch('topics', false, ['topic_id' => [$threadId, '=']]);
|
|
|
|
|
|
|
|
// Assign data if a row was returned
|
|
|
|
if ($threadRow) {
|
|
|
|
$this->id = $threadRow['topic_id'];
|
|
|
|
$this->forum = $threadRow['forum_id'];
|
2016-02-02 21:04:15 +00:00
|
|
|
$this->hidden = (bool) $threadRow['topic_hidden'];
|
2015-11-13 23:11:55 +00:00
|
|
|
$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'];
|
2016-01-10 18:24:47 +00:00
|
|
|
$this->oldForum = $threadRow['topic_old_forum'];
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @return Thread The new thread instance.
|
|
|
|
*/
|
2015-12-11 20:49:40 +00:00
|
|
|
public static function create($forum, $title, $status = 0, $type = 0)
|
|
|
|
{
|
|
|
|
// Create the database entry
|
|
|
|
Database::insert('topics', [
|
|
|
|
'forum_id' => $forum,
|
|
|
|
'topic_title' => $title,
|
|
|
|
'topic_time' => time(),
|
|
|
|
'topic_status' => $status,
|
|
|
|
'topic_type' => $type,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Return the thread object
|
|
|
|
return new Thread(Database::lastInsertID());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
Database::delete('posts', [
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Delete thread meta
|
|
|
|
Database::delete('topics', [
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
Database::update('posts', [
|
|
|
|
[
|
|
|
|
'forum_id' => $forum,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Update thread meta
|
|
|
|
Database::update('topics', [
|
|
|
|
[
|
|
|
|
'forum_id' => $forum,
|
|
|
|
'topic_old_forum' => ($setOld ? $this->forum : 0),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Update the thread data.
|
|
|
|
*
|
|
|
|
* @return Thread The updated thread.
|
|
|
|
*/
|
2016-01-10 18:24:47 +00:00
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
// Update row
|
|
|
|
Database::update('topics', [
|
|
|
|
[
|
|
|
|
'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,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
*/
|
2015-12-03 18:41:14 +00:00
|
|
|
public function posts()
|
2015-11-15 14:29:26 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if _posts is something
|
|
|
|
if (!count($this->_posts)) {
|
|
|
|
// Get all rows with the thread id
|
|
|
|
$postRows = Database::fetch('posts', true, ['topic_id' => [$this->id, '=']]);
|
|
|
|
|
|
|
|
// Create a storage array
|
|
|
|
$posts = [];
|
2015-11-15 14:29:26 +00:00
|
|
|
|
2015-12-03 18:41:14 +00:00
|
|
|
// Create new post objects for each post
|
|
|
|
foreach ($postRows as $post) {
|
|
|
|
$posts[$post['post_id']] = new Post($post['post_id']);
|
|
|
|
}
|
2015-11-15 14:29:26 +00:00
|
|
|
|
2015-12-03 18:41:14 +00:00
|
|
|
$this->_posts = $posts;
|
|
|
|
} else {
|
|
|
|
$posts = $this->_posts;
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the post objects
|
|
|
|
return $posts;
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2015-12-10 20:55:51 +00:00
|
|
|
// Check if the cache var is set
|
|
|
|
if ($this->_firstPost !== null) {
|
|
|
|
return $this->_firstPost;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the row from the database
|
|
|
|
$post = Database::fetch('posts', false, ['topic_id' => [$this->id, '=']], ['post_id'], [1]);
|
|
|
|
|
|
|
|
// Create the post class
|
|
|
|
$post = new Post($post ? $post['post_id'] : 0);
|
|
|
|
|
|
|
|
// Assign it to the cache var
|
|
|
|
$this->_firstPost = $post;
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return $post;
|
2015-12-03 18:41:14 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
2015-12-10 20:55:51 +00:00
|
|
|
// Check if the cache var is set
|
|
|
|
if ($this->_lastPost !== null) {
|
2015-12-16 22:29:09 +00:00
|
|
|
return $this->_lastPost;
|
2015-12-10 20:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the row from the database
|
|
|
|
$post = Database::fetch('posts', false, ['topic_id' => [$this->id, '=']], ['post_id', true], [1]);
|
|
|
|
|
|
|
|
// Create the post class
|
|
|
|
$post = new Post($post ? $post['post_id'] : 0);
|
|
|
|
|
|
|
|
// Assign it to the cache var
|
|
|
|
$this->_lastPost = $post;
|
|
|
|
|
|
|
|
// Return
|
|
|
|
return $post;
|
2015-12-03 18:41:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get the amount of replies.
|
|
|
|
*
|
|
|
|
* @return int The number of replies to this thread.
|
|
|
|
*/
|
2015-11-13 23:11:55 +00:00
|
|
|
public function replyCount()
|
|
|
|
{
|
2015-11-15 14:29:26 +00:00
|
|
|
return Database::count('posts', ['topic_id' => [$this->id, '=']])[0];
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The "elapsed" string for this thread since it was created.
|
|
|
|
*
|
|
|
|
* @return string Readable time elapsed since this thread was created.
|
|
|
|
*/
|
2015-11-15 14:29:26 +00:00
|
|
|
public function timeElapsed()
|
|
|
|
{
|
2016-01-17 01:58:31 +00:00
|
|
|
return Utils::timeElapsed($this->time);
|
2015-11-15 14:29:26 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* The "elapsed" string for this thread since the status was last changed.
|
|
|
|
*
|
|
|
|
* @return string Readble time elapsed since the status was last changed.
|
|
|
|
*/
|
2015-11-15 14:29:26 +00:00
|
|
|
public function statusChangeElapsed()
|
|
|
|
{
|
2016-01-17 01:58:31 +00:00
|
|
|
return Utils::timeElapsed($this->statusChange);
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|
2015-11-16 22:05:45 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2015-11-16 22:05:45 +00:00
|
|
|
public function unread($user)
|
|
|
|
{
|
|
|
|
// Attempt to get track row from the database
|
2015-12-10 20:55:51 +00:00
|
|
|
$track = Database::fetch('topics_track', false, ['user_id' => [$user, '='], 'topic_id' => [$this->id, '='], 'mark_time' => [$this->lastPost()->time, '>']]);
|
2015-11-16 22:05:45 +00:00
|
|
|
|
|
|
|
// If nothing was returned it's obvious that the status is unread
|
|
|
|
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.
|
|
|
|
*/
|
2015-11-16 22:05:45 +00:00
|
|
|
public function trackUpdate($user)
|
|
|
|
{
|
|
|
|
// Check if we already have a track record
|
|
|
|
$track = Database::fetch('topics_track', false, ['user_id' => [$user, '='], 'topic_id' => [$this->id, '='], 'forum_id' => [$this->forum, '=']]);
|
|
|
|
|
|
|
|
// If so update it
|
|
|
|
if ($track) {
|
|
|
|
Database::update('topics_track', [
|
|
|
|
[
|
|
|
|
'mark_time' => time(),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'user_id' => [$user, '='],
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
// If not create a new record
|
|
|
|
Database::insert('topics_track', [
|
|
|
|
'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.
|
|
|
|
*/
|
2015-11-16 22:05:45 +00:00
|
|
|
public function viewsUpdate()
|
|
|
|
{
|
|
|
|
Database::update('topics', [
|
|
|
|
[
|
|
|
|
'topic_views' => $this->views + 1,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
2015-12-11 20:49:40 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Update the timestamp of when this thread was last replied to.
|
|
|
|
*/
|
2015-12-11 20:49:40 +00:00
|
|
|
public function lastUpdate()
|
|
|
|
{
|
|
|
|
Database::update('topics', [
|
|
|
|
[
|
|
|
|
'topic_last_reply' => time(),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'topic_id' => [$this->id, '='],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
2015-11-13 23:11:55 +00:00
|
|
|
}
|