2016-03-28 14:47:43 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Holds the news category 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\DB;
|
2016-12-18 22:00:24 +00:00
|
|
|
use stdClass;
|
2016-03-28 14:47:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* News category object.
|
|
|
|
* @package Sakura
|
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
|
|
*/
|
|
|
|
class Category
|
|
|
|
{
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
2016-12-12 22:22:09 +00:00
|
|
|
* Id of this category.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $id = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of this category.
|
2016-08-05 02:35:37 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-28 14:47:43 +00:00
|
|
|
public $name = "";
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
2016-12-12 22:22:09 +00:00
|
|
|
* Description of this news category.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $description = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this category should be hidden.
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $hidden = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds instances of Post.
|
|
|
|
* @var array
|
2016-08-05 02:35:37 +00:00
|
|
|
*/
|
2016-12-12 22:22:09 +00:00
|
|
|
private $postsCache = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
*/
|
|
|
|
public function __construct(int $id = 0)
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
2016-12-12 22:22:09 +00:00
|
|
|
if ($id !== 0) {
|
|
|
|
$data = DB::table('news_categories')
|
|
|
|
->where('category_id', $id)
|
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($data) {
|
|
|
|
$this->id = intval($data->category_id);
|
|
|
|
$this->name = $data->category_name;
|
2016-12-21 19:55:17 +00:00
|
|
|
$this->description = $data->category_description;
|
2016-12-12 22:22:09 +00:00
|
|
|
$this->hidden = boolval($data->category_hidden);
|
|
|
|
}
|
|
|
|
}
|
2016-03-28 14:47:43 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:35:37 +00:00
|
|
|
/**
|
|
|
|
* Gets the news posts in this category.
|
|
|
|
* @param int $limit
|
2016-12-12 22:22:09 +00:00
|
|
|
* @param bool $excludeFuture
|
|
|
|
* @param bool $excludeDeleted
|
2016-12-04 16:33:52 +00:00
|
|
|
* @return array
|
2016-08-05 02:35:37 +00:00
|
|
|
*/
|
2016-12-12 22:22:09 +00:00
|
|
|
public function posts(int $limit = 0, bool $excludeDeleted = true): array
|
2016-03-28 14:47:43 +00:00
|
|
|
{
|
2016-12-12 22:22:09 +00:00
|
|
|
if (!$this->postsCache) {
|
|
|
|
$posts = DB::table('news_posts')
|
2017-03-22 19:51:50 +00:00
|
|
|
->orderBy('created_at', 'desc');
|
2016-12-12 22:22:09 +00:00
|
|
|
|
|
|
|
if ($this->id !== 0) {
|
|
|
|
$posts->where('category_id', $this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($excludeDeleted) {
|
|
|
|
$posts->whereNull('deleted_at');
|
|
|
|
}
|
2016-03-28 14:47:43 +00:00
|
|
|
|
2016-12-12 22:22:09 +00:00
|
|
|
if ($limit) {
|
|
|
|
$posts->limit($limit);
|
|
|
|
}
|
2016-03-28 14:47:43 +00:00
|
|
|
|
2016-12-18 22:00:24 +00:00
|
|
|
$this->postsCache = array_map(function (stdClass $post) {
|
2016-12-12 22:22:09 +00:00
|
|
|
return new Post($post->post_id);
|
|
|
|
}, $posts->get(['post_id']));
|
2016-03-28 14:47:43 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 22:22:09 +00:00
|
|
|
return $this->postsCache;
|
2016-03-28 14:47:43 +00:00
|
|
|
}
|
|
|
|
}
|