misuzu/public/news.php

160 lines
5.1 KiB
PHP
Raw Normal View History

2018-04-14 02:58:53 +00:00
<?php
2018-05-16 02:58:21 +00:00
use Misuzu\Database;
2018-04-14 02:58:53 +00:00
require_once __DIR__ . '/../misuzu.php';
2018-05-16 02:58:21 +00:00
$db = Database::connection();
$templating = $app->getTemplating();
2018-05-27 00:20:35 +00:00
$categoryId = isset($_GET['c']) ? (int)$_GET['c'] : null;
$postId = isset($_GET['p']) ? (int)$_GET['p'] : (isset($_GET['n']) ? (int)$_GET['n'] : null);
$postsOffset = (int)($_GET['o'] ?? 0);
$postsTake = 5;
2018-05-16 20:48:33 +00:00
2018-05-27 00:20:35 +00:00
$templating->vars([
'posts_offset' => $postsOffset,
'posts_take' => $postsTake,
]);
2018-04-14 02:58:53 +00:00
2018-05-27 00:20:35 +00:00
if ($postId !== null) {
2018-05-16 02:58:21 +00:00
$getPost = $db->prepare('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
2018-07-06 01:28:06 +00:00
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`
2018-05-16 02:58:21 +00:00
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE `post_id` = :post_id
');
2018-05-27 00:20:35 +00:00
$getPost->bindValue(':post_id', $postId, PDO::PARAM_INT);
2018-05-16 02:58:21 +00:00
$post = $getPost->execute() ? $getPost->fetch() : false;
2018-04-14 02:58:53 +00:00
2018-05-16 02:58:21 +00:00
if ($post === false) {
echo render_error(404);
2018-04-14 02:58:53 +00:00
return;
}
echo $templating->render('news.post', compact('post'));
2018-04-14 02:58:53 +00:00
return;
}
2018-05-27 00:20:35 +00:00
if ($categoryId !== null) {
2018-05-16 02:58:21 +00:00
$getCategory = $db->prepare('
SELECT
2018-05-16 20:48:33 +00:00
c.`category_id`, c.`category_name`, c.`category_description`,
COUNT(p.`post_id`) AS `posts_count`
FROM `msz_news_categories` as c
LEFT JOIN `msz_news_posts` as p
ON c.`category_id` = p.`category_id`
WHERE c.`category_id` = :category_id
GROUP BY c.`category_id`
2018-05-16 02:58:21 +00:00
');
2018-05-27 00:20:35 +00:00
$getCategory->bindValue(':category_id', $categoryId, PDO::PARAM_INT);
2018-05-16 02:58:21 +00:00
$category = $getCategory->execute() ? $getCategory->fetch() : false;
2018-04-14 02:58:53 +00:00
2018-05-27 00:20:35 +00:00
if ($category === false || $postsOffset < 0 || $postsOffset >= $category['posts_count']) {
echo render_error(404);
2018-04-14 02:58:53 +00:00
return;
}
2018-05-16 02:58:21 +00:00
$getPosts = $db->prepare('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
2018-07-06 01:28:06 +00:00
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`
2018-05-16 02:58:21 +00:00
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE p.`category_id` = :category_id
ORDER BY `created_at` DESC
2018-05-16 20:48:33 +00:00
LIMIT :offset, :take
2018-05-16 02:58:21 +00:00
');
2018-05-27 00:20:35 +00:00
$getPosts->bindValue('offset', $postsOffset);
$getPosts->bindValue('take', $postsTake);
2018-05-16 02:58:21 +00:00
$getPosts->bindValue('category_id', $category['category_id'], PDO::PARAM_INT);
$posts = $getPosts->execute() ? $getPosts->fetchAll() : false;
2018-04-16 00:33:54 +00:00
2018-05-16 02:58:21 +00:00
$getFeatured = $db->prepare('
SELECT `post_id`, `post_title`
FROM `msz_news_posts`
WHERE `category_id` = :category_id
AND `is_featured` = true
ORDER BY `created_at` DESC
LIMIT 10
');
$getFeatured->bindValue('category_id', $category['category_id'], PDO::PARAM_INT);
$featured = $getFeatured->execute() ? $getFeatured->fetchAll() : [];
2018-05-16 20:48:33 +00:00
echo $templating->render('news.category', compact('category', 'posts', 'featured'));
2018-04-14 02:58:53 +00:00
return;
}
2018-05-16 02:58:21 +00:00
$getCategories = $db->prepare('
SELECT
c.`category_id`, c.`category_name`,
COUNT(p.`post_id`) AS count
FROM `msz_news_categories` as c
LEFT JOIN `msz_news_posts` as p
ON c.`category_id` = p.`category_id`
WHERE `is_hidden` = false
GROUP BY c.`category_id`
HAVING count > 0
');
$categories = $getCategories->execute() ? $getCategories->fetchAll() : [];
2018-05-16 20:48:33 +00:00
$postsCount = (int)$db->query('
SELECT COUNT(p.`post_id`) as `posts_count`
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
WHERE p.`is_featured` = true
AND c.`is_hidden` = false
')->fetchColumn();
$templating->var('posts_count', $postsCount);
2018-05-27 00:20:35 +00:00
if ($postsOffset < 0 || $postsOffset >= $postsCount) {
echo render_error(404);
2018-05-16 20:48:33 +00:00
return;
}
2018-05-16 02:58:21 +00:00
$getPosts = $db->prepare('
SELECT
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`,
c.`category_id`, c.`category_name`,
u.`user_id`, u.`username`,
2018-07-06 01:28:06 +00:00
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour`
2018-05-16 02:58:21 +00:00
FROM `msz_news_posts` as p
LEFT JOIN `msz_news_categories` as c
ON p.`category_id` = c.`category_id`
LEFT JOIN `msz_users` as u
ON p.`user_id` = u.`user_id`
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
WHERE p.`is_featured` = true
AND c.`is_hidden` = false
ORDER BY p.`created_at` DESC
2018-05-16 20:48:33 +00:00
LIMIT :offset, :take
2018-05-16 02:58:21 +00:00
');
2018-05-27 00:20:35 +00:00
$getPosts->bindValue('offset', $postsOffset);
$getPosts->bindValue('take', $postsTake);
2018-05-16 02:58:21 +00:00
$posts = $getPosts->execute() ? $getPosts->fetchAll() : [];
2018-05-16 20:48:33 +00:00
if (!$posts) {
echo render_error(404);
2018-04-16 00:33:54 +00:00
return;
}
2018-04-14 02:58:53 +00:00
2018-05-16 20:48:33 +00:00
echo $templating->render('news.index', compact('categories', 'posts'));