101 lines
2.1 KiB
PHP
101 lines
2.1 KiB
PHP
<?php
|
|
/**
|
|
* Holds the news category object.
|
|
* @package Sakura
|
|
*/
|
|
|
|
namespace Sakura\News;
|
|
|
|
use Carbon\Carbon;
|
|
use Sakura\DB;
|
|
use stdClass;
|
|
|
|
/**
|
|
* News category object.
|
|
* @package Sakura
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
*/
|
|
class Category
|
|
{
|
|
/**
|
|
* Id of this category.
|
|
* @var int
|
|
*/
|
|
public $id = 0;
|
|
|
|
/**
|
|
* Name of this category.
|
|
* @var string
|
|
*/
|
|
public $name = "";
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
private $postsCache = [];
|
|
|
|
/**
|
|
* @param int $id
|
|
*/
|
|
public function __construct(int $id = 0)
|
|
{
|
|
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;
|
|
$this->description = $data->category_description;
|
|
$this->hidden = boolval($data->category_hidden);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets the news posts in this category.
|
|
* @param int $limit
|
|
* @param bool $excludeFuture
|
|
* @param bool $excludeDeleted
|
|
* @return array
|
|
*/
|
|
public function posts(int $limit = 0, bool $excludeDeleted = true): array
|
|
{
|
|
if (!$this->postsCache) {
|
|
$posts = DB::table('news_posts')
|
|
->orderBy('created_at', 'desc');
|
|
|
|
if ($this->id !== 0) {
|
|
$posts->where('category_id', $this->id);
|
|
}
|
|
|
|
if ($excludeDeleted) {
|
|
$posts->whereNull('deleted_at');
|
|
}
|
|
|
|
if ($limit) {
|
|
$posts->limit($limit);
|
|
}
|
|
|
|
$this->postsCache = array_map(function (stdClass $post) {
|
|
return new Post($post->post_id);
|
|
}, $posts->get(['post_id']));
|
|
}
|
|
|
|
return $this->postsCache;
|
|
}
|
|
}
|