84 lines
2 KiB
PHP
84 lines
2 KiB
PHP
<?php
|
|
/**
|
|
* Holds the news controller.
|
|
* @package Sakura
|
|
*/
|
|
|
|
namespace Sakura\Controllers;
|
|
|
|
use Illuminate\Database\Query\JoinClause;
|
|
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
|
|
use Sakura\DB;
|
|
use Sakura\Config;
|
|
use Sakura\News\Category;
|
|
use Sakura\News\Post;
|
|
use stdClass;
|
|
|
|
/**
|
|
* News controller.
|
|
* @package Sakura
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
*/
|
|
class NewsController extends Controller
|
|
{
|
|
/**
|
|
* Shows all news posts in any category.
|
|
* @return string
|
|
*/
|
|
public function index(): string
|
|
{
|
|
$categories = DB::table('news_categories')
|
|
->where('category_hidden', 0)
|
|
->get();
|
|
|
|
$posts = DB::table('news_posts')
|
|
->whereNull('news_posts.deleted_at')
|
|
->join('news_categories', function (JoinClause $join) {
|
|
$join->on('news_posts.category_id', '=', 'news_categories.category_id')
|
|
->where('news_categories.category_hidden', '=', 0);
|
|
})
|
|
->limit(5)
|
|
->get();
|
|
|
|
$posts = array_map(function (stdClass $row) {
|
|
return Post::fromRow($row);
|
|
}, $posts);
|
|
|
|
return view('news/index', compact('categories', 'posts'));
|
|
}
|
|
|
|
/**
|
|
* Shows all posts in a specific category.
|
|
* @param int $catId
|
|
* @throws HttpRouteNotFoundException
|
|
* @return string
|
|
*/
|
|
public function category(int $catId): string
|
|
{
|
|
$category = new Category($catId);
|
|
|
|
if ($category->id === 0) {
|
|
throw new HttpRouteNotFoundException;
|
|
}
|
|
|
|
return view('news/category', compact('category'));
|
|
}
|
|
|
|
/**
|
|
* Returns a news post.
|
|
* @param int $id
|
|
* @throws HttpRouteNotFoundException
|
|
* @return string
|
|
*/
|
|
public function post(int $id = 0): string
|
|
{
|
|
// Create the post object
|
|
$post = new Post($id);
|
|
|
|
if (!$post->id) {
|
|
throw new HttpRouteNotFoundException;
|
|
}
|
|
|
|
return view('news/post', compact('post'));
|
|
}
|
|
}
|