2018-04-14 02:58:53 +00:00
|
|
|
<?php
|
|
|
|
use Misuzu\News\NewsCategory;
|
|
|
|
use Misuzu\News\NewsPost;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../misuzu.php';
|
|
|
|
|
2018-04-16 00:33:54 +00:00
|
|
|
$category_id = isset($_GET['c']) ? (int)$_GET['c'] : null;
|
|
|
|
$post_id = isset($_GET['n']) ? (int)$_GET['n'] : null;
|
|
|
|
$page_id = (int)($_GET['p'] ?? 1);
|
2018-04-14 02:58:53 +00:00
|
|
|
|
|
|
|
if ($post_id !== null) {
|
|
|
|
$post = NewsPost::find($post_id);
|
|
|
|
|
|
|
|
if ($post === null) {
|
|
|
|
http_response_code(404);
|
2018-04-24 22:55:46 +00:00
|
|
|
echo $app->getTemplating()->render('errors.404');
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:55:46 +00:00
|
|
|
echo $app->getTemplating()->render('news.post', compact('post'));
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($category_id !== null) {
|
|
|
|
$category = NewsCategory::find($category_id);
|
|
|
|
|
|
|
|
if ($category === null) {
|
|
|
|
http_response_code(404);
|
2018-04-24 22:55:46 +00:00
|
|
|
echo $app->getTemplating()->render('errors.404');
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-16 00:33:54 +00:00
|
|
|
$posts = $category->posts()->orderBy('created_at', 'desc')->paginate(5, ['*'], 'p', $page_id);
|
|
|
|
|
|
|
|
if (!is_valid_page($posts, $page_id)) {
|
|
|
|
http_response_code(404);
|
2018-04-24 22:55:46 +00:00
|
|
|
echo $app->getTemplating()->render('errors.404');
|
2018-04-16 00:33:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$featured = $category->posts()->where('is_featured', 1)->orderBy('created_at', 'desc')->take(10)->get();
|
2018-04-24 22:55:46 +00:00
|
|
|
echo $app->getTemplating()->render('news.category', compact('category', 'posts', 'featured', 'page_id'));
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$categories = NewsCategory::where('is_hidden', false)->get();
|
2018-04-16 00:33:54 +00:00
|
|
|
$posts = NewsPost::where('is_featured', true)->orderBy('created_at', 'desc')->paginate(5, ['*'], 'p', $page_id);
|
|
|
|
|
|
|
|
if (!is_valid_page($posts, $page_id)) {
|
|
|
|
http_response_code(404);
|
2018-04-24 22:55:46 +00:00
|
|
|
echo $app->getTemplating()->render('errors.404');
|
2018-04-16 00:33:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-14 02:58:53 +00:00
|
|
|
|
2018-04-24 22:55:46 +00:00
|
|
|
echo $app->getTemplating()->render('news.index', compact('categories', 'posts', 'page_id'));
|