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-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-08-15 01:12:58 +00:00
|
|
|
tpl_vars([
|
2018-05-27 00:20:35 +00:00
|
|
|
'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-07-15 22:59:14 +00:00
|
|
|
$getPost = Database::prepare('
|
2018-05-16 02:58:21 +00:00
|
|
|
SELECT
|
2018-08-10 04:20:54 +00:00
|
|
|
p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, p.`comment_section_id`,
|
2018-05-16 02:58:21 +00:00
|
|
|
c.`category_id`, c.`category_name`,
|
|
|
|
u.`user_id`, u.`username`,
|
2018-07-18 16:01:17 +00:00
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) 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) {
|
2018-05-26 20:33:05 +00:00
|
|
|
echo render_error(404);
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-10 04:20:54 +00:00
|
|
|
if ($post['comment_section_id'] === null) {
|
|
|
|
$commentsInfo = comments_category_create("news-{$post['post_id']}");
|
|
|
|
|
|
|
|
if ($commentsInfo) {
|
|
|
|
$post['comment_section_id'] = $commentsInfo['category_id'];
|
|
|
|
Database::prepare('
|
|
|
|
UPDATE `msz_news_posts`
|
|
|
|
SET `comment_section_id` = :comment_section_id
|
|
|
|
WHERE `post_id` = :post_id
|
|
|
|
')->execute([
|
|
|
|
'comment_section_id' => $post['comment_section_id'],
|
|
|
|
'post_id' => $post['post_id'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$commentsInfo = comments_category_info($post['comment_section_id']);
|
|
|
|
}
|
|
|
|
|
2018-08-15 01:12:58 +00:00
|
|
|
echo tpl_render('news.post', [
|
2018-08-10 04:20:54 +00:00
|
|
|
'post' => $post,
|
|
|
|
'comments_perms' => comments_get_perms($app->getUserId()),
|
|
|
|
'comments_category' => $commentsInfo,
|
|
|
|
'comments' => comments_category_get($commentsInfo['category_id'], $app->getUserId()),
|
|
|
|
]);
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-27 00:20:35 +00:00
|
|
|
if ($categoryId !== null) {
|
2018-07-15 22:59:14 +00:00
|
|
|
$getCategory = Database::prepare('
|
2018-05-16 02:58:21 +00:00
|
|
|
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']) {
|
2018-05-26 20:33:05 +00:00
|
|
|
echo render_error(404);
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:59:14 +00:00
|
|
|
$getPosts = Database::prepare('
|
2018-05-16 02:58:21 +00:00
|
|
|
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-08-10 21:54:40 +00:00
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`comment_id`)
|
|
|
|
FROM `msz_comments_posts`
|
|
|
|
WHERE `category_id` = `comment_section_id`
|
|
|
|
) as `post_comments`
|
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-07-15 22:59:14 +00:00
|
|
|
$getFeatured = Database::prepare('
|
2018-05-16 02:58:21 +00:00
|
|
|
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-08-15 01:12:58 +00:00
|
|
|
echo tpl_render('news.category', compact('category', 'posts', 'featured'));
|
2018-04-14 02:58:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:59:14 +00:00
|
|
|
$getCategories = Database::prepare('
|
2018-05-16 02:58:21 +00:00
|
|
|
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-07-15 22:59:14 +00:00
|
|
|
$postsCount = (int)Database::query('
|
2018-05-16 20:48:33 +00:00
|
|
|
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();
|
|
|
|
|
2018-08-15 01:12:58 +00:00
|
|
|
tpl_var('posts_count', $postsCount);
|
2018-05-16 20:48:33 +00:00
|
|
|
|
2018-05-27 00:20:35 +00:00
|
|
|
if ($postsOffset < 0 || $postsOffset >= $postsCount) {
|
2018-05-26 20:33:05 +00:00
|
|
|
echo render_error(404);
|
2018-05-16 20:48:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-15 22:59:14 +00:00
|
|
|
$getPosts = Database::prepare('
|
2018-05-16 02:58:21 +00:00
|
|
|
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-08-10 21:54:40 +00:00
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`,
|
|
|
|
(
|
|
|
|
SELECT COUNT(`comment_id`)
|
|
|
|
FROM `msz_comments_posts`
|
|
|
|
WHERE `category_id` = `comment_section_id`
|
|
|
|
) as `post_comments`
|
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) {
|
2018-05-26 20:33:05 +00:00
|
|
|
echo render_error(404);
|
2018-04-16 00:33:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-14 02:58:53 +00:00
|
|
|
|
2018-08-15 01:12:58 +00:00
|
|
|
echo tpl_render('news.index', compact('categories', 'posts'));
|