2016-03-28 14:47:43 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Holds the news post object.
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura\News;
|
|
|
|
|
|
|
|
use Sakura\Comment;
|
|
|
|
use Sakura\DB;
|
|
|
|
use Sakura\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* News post object.
|
|
|
|
* @package Sakura
|
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
|
|
*/
|
|
|
|
class Post
|
|
|
|
{
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* The format the comment categories should follow.
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
const COMMENT_CATEGORY_FORMAT = "news-%s-%u";
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* The id of this news post.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public $id = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The category this post is part of.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public $category = "";
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The user who made this post.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public $user = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The timestamp when this post was made.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public $time = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The title of this news post.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public $title = "";
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The content of this news post.
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public $text = "";
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache of the amount of comments this post has.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
private $commentCountCache = 0;
|
2016-08-05 02:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cache of comments.
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
private $commentsCache = [];
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param int $id
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function __construct($id = 0)
|
|
|
|
{
|
|
|
|
// Get comment data from the database
|
|
|
|
$data = DB::table('news')
|
|
|
|
->where('news_id', $id)
|
2016-08-06 14:09:01 +00:00
|
|
|
->first();
|
2016-03-28 14:47:43 +00:00
|
|
|
|
|
|
|
// Check if anything was returned and assign data
|
|
|
|
if ($data) {
|
|
|
|
$this->id = $data->news_id;
|
|
|
|
$this->category = $data->news_category;
|
|
|
|
$this->user = $data->user_id;
|
|
|
|
$this->time = $data->news_timestamp;
|
|
|
|
$this->title = $data->news_title;
|
|
|
|
$this->text = $data->news_content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Saving changes to this news post.
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
// Create submission data, insert and update take the same format
|
|
|
|
$data = [
|
|
|
|
'news_category' => $this->category,
|
|
|
|
'user_id' => $this->user,
|
|
|
|
'news_timestamp' => $this->time,
|
|
|
|
'news_title' => $this->title,
|
|
|
|
'news_content' => $this->text,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Update if id isn't 0
|
|
|
|
if ($this->id) {
|
|
|
|
DB::table('news')
|
|
|
|
->where('news_id', $this->id)
|
|
|
|
->update($data);
|
|
|
|
} else {
|
|
|
|
$this->id = DB::table('news')
|
|
|
|
->insertGetId($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Deleting this news post.
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
DB::table('news')
|
|
|
|
->where('news_id', $this->id)
|
|
|
|
->delete();
|
|
|
|
|
|
|
|
$this->id = 0;
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Get the user object of the poster.
|
|
|
|
* @return User
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function userData()
|
|
|
|
{
|
|
|
|
return User::construct($this->user);
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Count the amount of comments this post has.
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function commentCount()
|
|
|
|
{
|
|
|
|
if (!$this->commentCountCache) {
|
|
|
|
$this->commentCountCache = DB::table('comments')
|
|
|
|
->where('comment_category', sprintf(self::COMMENT_CATEGORY_FORMAT, $this->category, $this->id))
|
|
|
|
->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->commentCountCache;
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Get the comments on this post.
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public function comments()
|
|
|
|
{
|
|
|
|
if (!$this->commentsCache) {
|
|
|
|
$commentIds = DB::table('comments')
|
|
|
|
->where('comment_category', sprintf(self::COMMENT_CATEGORY_FORMAT, $this->category, $this->id))
|
|
|
|
->orderBy('comment_id', 'desc')
|
|
|
|
->where('comment_reply_to', 0)
|
|
|
|
->get(['comment_id']);
|
|
|
|
$commentIds = array_column($commentIds, 'comment_id');
|
|
|
|
|
|
|
|
foreach ($commentIds as $comment) {
|
|
|
|
$this->commentsCache[$comment] = new Comment($comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->commentsCache;
|
|
|
|
}
|
|
|
|
}
|