2015-09-03 19:44:14 +00:00
|
|
|
<?php
|
2016-02-03 22:22:56 +00:00
|
|
|
/**
|
|
|
|
* Holds the news handler.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-03 22:22:56 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $posts = [];
|
2015-09-03 19:44:14 +00:00
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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-03-13 20:35:51 +00:00
|
|
|
$posts = DB::table('news')
|
|
|
|
->where('news_category', $category)
|
|
|
|
->orderBy('news_id', 'desc')
|
|
|
|
->get();
|
2015-09-11 23:31:54 +00:00
|
|
|
|
|
|
|
// Attach poster data
|
2015-09-14 20:51:23 +00:00
|
|
|
foreach ($posts as $post) {
|
2016-03-13 20:35:51 +00:00
|
|
|
// See Comments.php
|
|
|
|
$post = get_object_vars($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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @param int $pid The ID of the post.
|
2016-03-08 23:07:58 +00:00
|
|
|
*
|
2016-02-02 21:04:15 +00:00
|
|
|
* @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
|
|
|
}
|