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/app/Controllers/NewsController.php

94 lines
2.2 KiB
PHP
Raw Normal View History

2016-03-28 14:47:43 +00:00
<?php
/**
* Holds the news controller.
* @package Sakura
*/
namespace Sakura\Controllers;
2016-12-21 19:55:17 +00:00
use Illuminate\Database\Query\JoinClause;
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
2016-12-12 22:22:09 +00:00
use Sakura\DB;
2016-03-28 14:47:43 +00:00
use Sakura\Config;
use Sakura\News\Category;
use Sakura\News\Post;
2016-12-21 19:55:17 +00:00
use stdClass;
2016-03-28 14:47:43 +00:00
/**
* News controller.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class NewsController extends Controller
{
2016-12-18 22:22:41 +00:00
public function __construct()
{
parent::__construct();
// pretend these pages don't exist on testii for now
if (!config('dev.show_errors')) {
throw new HttpRouteNotFoundException;
}
}
2016-12-12 22:22:09 +00:00
/**
* Shows all news posts in any category.
* @return string
*/
public function index(): string
{
2016-12-21 19:55:17 +00:00
$categories = DB::table('news_categories')
->where('category_hidden', 0)
->get();
2016-12-12 22:22:09 +00:00
2016-12-21 19:55:17 +00:00
$posts = DB::table('news_posts')
->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'));
2016-12-12 22:22:09 +00:00
}
2016-08-05 02:35:37 +00:00
/**
* Shows all posts in a specific category.
2016-12-12 22:22:09 +00:00
* @param int $catId
2016-12-04 16:33:52 +00:00
* @throws HttpRouteNotFoundException
2016-08-05 02:35:37 +00:00
* @return string
*/
2016-12-21 19:55:17 +00:00
public function category(int $catId): string
2016-03-28 14:47:43 +00:00
{
2016-12-12 22:22:09 +00:00
$category = new Category($catId);
2016-03-28 14:47:43 +00:00
2016-12-12 22:22:09 +00:00
if ($category->id === 0) {
2016-12-04 16:33:52 +00:00
throw new HttpRouteNotFoundException;
2016-03-28 14:47:43 +00:00
}
return view('news/category', compact('category'));
2016-03-28 14:47:43 +00:00
}
2016-08-05 02:35:37 +00:00
/**
* Returns a news post.
* @param int $id
2016-12-04 16:33:52 +00:00
* @throws HttpRouteNotFoundException
2016-08-05 02:35:37 +00:00
* @return string
*/
2016-12-04 16:33:52 +00:00
public function post(int $id = 0): string
2016-03-28 14:47:43 +00:00
{
// Create the post object
$post = new Post($id);
if (!$post->id) {
2016-12-04 16:33:52 +00:00
throw new HttpRouteNotFoundException;
2016-03-28 14:47:43 +00:00
}
return view('news/post', compact('post'));
2016-03-28 14:47:43 +00:00
}
}