2015-09-03 19:44:14 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the news handler.
|
|
|
|
*
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
|
2015-09-03 19:44:14 +00:00
|
|
|
namespace Sakura;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Used to serve news posts.
|
|
|
|
*
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-18 19:06:30 +00:00
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class News
|
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Array containing news posts.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $posts = [];
|
2015-09-03 19:44:14 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param mixed $category ID of the category that should be constructed.
|
|
|
|
*/
|
2015-09-18 21:56:54 +00:00
|
|
|
public function __construct($category)
|
2015-09-14 20:51:23 +00:00
|
|
|
{
|
2015-09-03 19:44:14 +00:00
|
|
|
|
|
|
|
// Get the news posts and assign them to $posts
|
2016-02-18 23:28:44 +00:00
|
|
|
$posts = DB::prepare('SELECT * FROM `{prefix}news` WHERE `news_category` = :cat ORDER BY `news_id` DESC');
|
|
|
|
$posts->execute([
|
|
|
|
'cat' => $category,
|
|
|
|
]);
|
|
|
|
$posts = $posts->fetchAll(\PDO::FETCH_ASSOC);
|
2015-09-11 23:31:54 +00:00
|
|
|
|
|
|
|
// Attach poster data
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($posts as $post) {
|
2015-09-11 23:31:54 +00:00
|
|
|
// Attach the poster
|
2016-01-22 20:07:44 +00:00
|
|
|
$post['news_poster'] = User::construct($post['user_id']);
|
2015-09-11 23:31:54 +00:00
|
|
|
|
2015-09-18 21:56:54 +00:00
|
|
|
// Load comments
|
2015-10-10 21:17:50 +00:00
|
|
|
$post['news_comments'] = $this->comments = new Comments('news-' . $category . '-' . $post['news_id']);
|
2015-09-18 21:56:54 +00:00
|
|
|
|
2015-09-11 23:31:54 +00:00
|
|
|
// Add post to posts array
|
2015-10-10 21:17:50 +00:00
|
|
|
$this->posts[$post['news_id']] = $post;
|
2015-09-11 23:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Get the amount of news posts.
|
|
|
|
*
|
|
|
|
* @return int Number of posts.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
public function getCount()
|
|
|
|
{
|
2015-09-11 23:31:54 +00:00
|
|
|
return count($this->posts);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Check if a post exists in this category.
|
|
|
|
*
|
|
|
|
* @param int $pid The ID of the post.
|
|
|
|
*
|
|
|
|
* @return int If true the post it gets returns, else 0.
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
public function postExists($pid)
|
|
|
|
{
|
|
|
|
return array_key_exists($pid, $this->posts) ? $pid : 0;
|
2015-09-11 23:31:54 +00:00
|
|
|
}
|
2015-09-03 19:44:14 +00:00
|
|
|
}
|