2016-03-28 14:47:43 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Holds the news post object.
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura\News;
|
|
|
|
|
2016-12-12 22:22:09 +00:00
|
|
|
use Carbon\Carbon;
|
2016-03-28 14:47:43 +00:00
|
|
|
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-12-04 16:33:52 +00:00
|
|
|
private const COMMENT_CATEGORY_FORMAT = "news-%s-%u";
|
2016-03-28 14:47:43 +00:00
|
|
|
|
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.
|
2016-12-12 22:22:09 +00:00
|
|
|
* @var int
|
2016-08-05 02:35:37 +00:00
|
|
|
*/
|
2016-12-12 22:22:09 +00:00
|
|
|
public $category = 0;
|
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 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
|
|
|
|
2016-12-12 22:22:09 +00:00
|
|
|
/**
|
|
|
|
* When the post was created.
|
|
|
|
* @var Carbon
|
|
|
|
*/
|
|
|
|
public $created = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the post was last updated.
|
|
|
|
* @var Carbon
|
|
|
|
*/
|
|
|
|
public $updated = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the post was deleted.
|
|
|
|
* @var Carbon
|
|
|
|
*/
|
|
|
|
public $deleted = null;
|
|
|
|
|
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-12-04 16:33:52 +00:00
|
|
|
public function __construct(int $id = 0)
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
2016-12-12 22:22:09 +00:00
|
|
|
$data = DB::table('news_posts')
|
|
|
|
->where('post_id', $id)
|
2016-08-06 14:09:01 +00:00
|
|
|
->first();
|
2016-03-28 14:47:43 +00:00
|
|
|
|
|
|
|
if ($data) {
|
2016-12-12 22:22:09 +00:00
|
|
|
$this->id = intval($data->post_id);
|
|
|
|
$this->category = intval($data->category_id);
|
|
|
|
$this->user = intval($data->user_id);
|
|
|
|
$this->title = $data->post_title;
|
|
|
|
$this->text = $data->post_text;
|
|
|
|
$this->created = new Carbon($data->created_at);
|
|
|
|
$this->updated = new Carbon($data->updated_at);
|
|
|
|
$this->deleted = new Carbon($data->deleted_at);
|
2016-03-28 14:47:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Saving changes to this news post.
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function save(): void
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
2016-12-12 22:22:09 +00:00
|
|
|
// // 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-03-28 14:47:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Deleting this news post.
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function delete(): void
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
2016-12-12 22:22:09 +00:00
|
|
|
// DB::table('news')
|
|
|
|
// ->where('news_id', $this->id)
|
|
|
|
// ->delete();
|
2016-03-28 14:47:43 +00:00
|
|
|
|
2016-12-12 22:22:09 +00:00
|
|
|
// $this->id = 0;
|
2016-03-28 14:47:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Get the user object of the poster.
|
|
|
|
* @return User
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function userData(): User
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
|
|
|
return User::construct($this->user);
|
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Count the amount of comments this post has.
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-12-04 16:33:52 +00:00
|
|
|
public function commentCount(): int
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
|
|
|
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-12-04 16:33:52 +00:00
|
|
|
public function comments(): array
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|