This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/News.php

77 lines
1.6 KiB
PHP
Raw Normal View History

<?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
*/
namespace Sakura;
/**
2016-02-02 21:04:15 +00:00
* Used to serve news posts.
2016-03-08 23:07:58 +00:00
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
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 = [];
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.
*/
public function __construct($category)
{
// Get the news posts and assign them to $posts
$posts = DB::table('news')
->where('news_category', $category)
->orderBy('news_id', 'desc')
->get();
// Attach poster data
foreach ($posts as $post) {
// See Comments.php
$post = get_object_vars($post);
// Attach the poster
2016-01-22 20:07:44 +00:00
$post['news_poster'] = User::construct($post['user_id']);
// Load comments
$post['news_comments'] = $this->comments = new Comments('news-' . $category . '-' . $post['news_id']);
// Add post to posts array
$this->posts[$post['news_id']] = $post;
}
}
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.
*/
public function getCount()
{
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.
*/
public function postExists($pid)
{
return array_key_exists($pid, $this->posts) ? $pid : 0;
}
}