2015-09-03 19:44:14 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* The news page backend
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
|
|
|
* Class News
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
2015-09-14 20:51:23 +00:00
|
|
|
class News
|
|
|
|
{
|
2015-11-15 21:43:39 +00:00
|
|
|
public $posts = []; // Posts array
|
2015-09-14 20:51:23 +00:00
|
|
|
private $posters = []; // Posters array (so we don't create a new user object every time)
|
2015-09-03 19:44:14 +00:00
|
|
|
|
|
|
|
// Initialise the news object
|
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
|
2015-10-10 21:17:50 +00:00
|
|
|
$posts = Database::fetch('news', true, ['news_category' => [$category, '=']], ['news_id', true]);
|
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
|
|
|
// Check if we already have an object for this user
|
2015-10-10 21:17:50 +00:00
|
|
|
if (!array_key_exists($post['user_id'], $this->posters)) {
|
2015-09-14 20:51:23 +00:00
|
|
|
// Create new object
|
2015-10-10 21:17:50 +00:00
|
|
|
$this->posters[$post['user_id']] = new User($post['user_id']);
|
2015-09-11 23:31:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the news post
|
2015-10-10 21:17:50 +00:00
|
|
|
$post['news_content_parsed'] = Main::mdParse($post['news_content']);
|
2015-09-11 23:31:54 +00:00
|
|
|
|
|
|
|
// Attach the poster
|
2015-10-10 21:17:50 +00:00
|
|
|
$post['news_poster'] = $this->posters[$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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the amount 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the amount of posts
|
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
|
|
|
}
|