From 6d39c6e3c9e84f2b9dae51aafdec7bbe78e033d2 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 10 Oct 2018 11:21:57 +0200 Subject: [PATCH] News but everything is a function. --- public/index.php | 20 +--- public/manage/news.php | 29 ++++++ public/news.php | 144 ++++---------------------- src/Net/geoip.php | 9 +- src/news.php | 209 ++++++++++++++++++++++++++++++++++++++ templates/news/index.twig | 2 +- 6 files changed, 263 insertions(+), 150 deletions(-) create mode 100644 public/manage/news.php diff --git a/public/index.php b/public/index.php index 42337b5c..93ea2d6b 100644 --- a/public/index.php +++ b/public/index.php @@ -10,25 +10,7 @@ if (config_get_default(false, 'Site', 'embed_linked_data')) { ]); } -$news = db_query(' - SELECT - p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, - u.`user_id`, u.`username`, - 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` - FROM `msz_news_posts` as p - 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 - ORDER BY p.`created_at` DESC - LIMIT 5 -')->fetchAll(PDO::FETCH_ASSOC); +$news = news_posts_get(0, 5, null, true); $statistics = cache_get('index:stats:v1', function () { return [ diff --git a/public/manage/news.php b/public/manage/news.php new file mode 100644 index 00000000..0c64e9ef --- /dev/null +++ b/public/manage/news.php @@ -0,0 +1,29 @@ +bindValue(':post_id', $postId, PDO::PARAM_INT); - $post = $getPost->execute() ? $getPost->fetch() : false; + $post = news_post_get($postId); - if ($post === false) { + if (!$post) { echo render_error(404); return; } @@ -40,14 +24,10 @@ if ($postId !== null) { if ($commentsInfo) { $post['comment_section_id'] = $commentsInfo['category_id']; - db_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'], - ]); + news_post_comments_set( + $post['post_id'], + $post['comment_section_id'] = $commentsInfo['category_id'] + ); } } else { $commentsInfo = comments_category_info($post['comment_section_id']); @@ -63,87 +43,27 @@ if ($postId !== null) { } if ($categoryId !== null) { - $getCategory = db_prepare(' - SELECT - 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` - '); - $getCategory->bindValue(':category_id', $categoryId, PDO::PARAM_INT); - $category = $getCategory->execute() ? $getCategory->fetch() : false; + $category = news_categories_single($categoryId, true); - if ($category === false || $postsOffset < 0 || $postsOffset >= $category['posts_count']) { + if (!$category || $postsOffset < 0 || $postsOffset >= $category['posts_count']) { echo render_error(404); return; } - $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`, - 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` - 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 - LIMIT :offset, :take - '); - $getPosts->bindValue('offset', $postsOffset); - $getPosts->bindValue('take', $postsTake); - $getPosts->bindValue('category_id', $category['category_id'], PDO::PARAM_INT); - $posts = $getPosts->execute() ? $getPosts->fetchAll() : false; + $posts = news_posts_get( + $postsOffset, + $postsTake, + $category['category_id'] + ); - $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() : []; + $featured = news_posts_get(0, 10, $category['category_id'], true); echo tpl_render('news.category', compact('category', 'posts', 'featured')); return; } -$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() : []; - -$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(); +$categories = news_categories_get(0, 0, true); +$postsCount = news_posts_count(null, true); tpl_var('posts_count', $postsCount); @@ -152,32 +72,12 @@ if ($postsOffset < 0 || $postsOffset >= $postsCount) { return; } -$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`, - 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` - 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 - LIMIT :offset, :take -'); -$getPosts->bindValue('offset', $postsOffset); -$getPosts->bindValue('take', $postsTake); -$posts = $getPosts->execute() ? $getPosts->fetchAll() : []; +$posts = news_posts_get( + $postsOffset, + $postsTake, + null, + true +); if (!$posts) { echo render_error(404); diff --git a/src/Net/geoip.php b/src/Net/geoip.php index 5004650f..18617a0e 100644 --- a/src/Net/geoip.php +++ b/src/Net/geoip.php @@ -24,14 +24,7 @@ function geoip_cache(string $section, string $ipAddress, callable $value) function geoip_country(string $ipAddress) { - return geoip_cache('country', $ipAddress, function () { + return geoip_cache('country', $ipAddress, function () use ($ipAddress) { return $GLOBALS[MSZ_GEOIP_INSTANCE_STORE]->country($ipAddress); }); } - -function geoip_anonymous(string $ipAddress) -{ - return geoip_cache('anonymous', $ipAddress, function () { - return $GLOBALS[MSZ_GEOIP_INSTANCE_STORE]->anonymousIp($ipAddress); - }); -} diff --git a/src/news.php b/src/news.php index 36674bf0..a83ec526 100644 --- a/src/news.php +++ b/src/news.php @@ -1,3 +1,212 @@ bindValue('offset', $offset); + $getCats->bindValue('take', $take); + } + + $cats = $getCats->execute() ? $getCats->fetchAll(PDO::FETCH_ASSOC) : false; + return $cats ? $cats : []; +} + +function news_categories_single( + int $category, + bool $includePostCount = false, + bool $featuredOnly = false, + bool $exposeScheduled = false, + bool $excludeDeleted = true +): array { + if ($includePostCount) { + $query = sprintf( + ' + SELECT + 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 %1$s %2$s %3$s + GROUP BY c.`category_id` + ', + $featuredOnly ? 'AND p.`is_featured` = 1' : '', + $exposeScheduled ? '' : 'AND p.`scheduled_for` < NOW()', + $excludeDeleted ? 'AND p.`deleted_at` IS NULL' : '' + ); + } else { + $query = ' + SELECT + c.`category_id`, c.`category_name`, c.`category_description`, + FROM `msz_news_categories` as c + WHERE c.`category_id` = :category + GROUP BY c.`category_id` + '; + } + + $getCategory = db_prepare($query); + $getCategory->bindValue('category', $category); + $category = $getCategory->execute() ? $getCategory->fetch(PDO::FETCH_ASSOC) : false; + return $category ? $category : []; +} + +function news_posts_count( + ?int $category = null, + bool $featuredOnly = false, + bool $exposeScheduled = false, + bool $excludeDeleted = true +): int { + $hasCategory= $category !== null; + + $countPosts = db_prepare(sprintf( + ' + SELECT COUNT(`post_id`) + FROM `msz_news_posts` + WHERE %1$s %2$s %3$s %4$s + ', + $hasCategory ? '`category_id` = :category' : '1', + $featuredOnly ? 'AND `is_featured` = 1' : '', + $exposeScheduled ? '' : 'AND `scheduled_for` < NOW()', + $excludeDeleted ? 'AND `deleted_at` IS NULL' : '' + )); + + if ($hasCategory) { + $countPosts->bindValue('category', $category); + } + + return $countPosts->execute() ? (int)$countPosts->fetchColumn() : 0; +} + +function news_posts_get( + int $offset, + int $take, + ?int $category = null, + bool $featuredOnly = false, + bool $exposeScheduled = false, + bool $excludeDeleted = true +): array { + $getAll = $offset < 0 || $take < 1; + $hasCategory= $category !== null; + + $getPosts = db_prepare(sprintf( + ' + SELECT + p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, + c.`category_id`, c.`category_name`, + u.`user_id`, u.`username`, + 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` + 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 %5$s %2$s %3$s %4$s + ORDER BY p.`created_at` DESC + %1$s + ', + $getAll ? '' : 'LIMIT :offset, :take', + $featuredOnly ? 'AND p.`is_featured` = 1' : '', + $exposeScheduled ? '' : 'AND p.`scheduled_for` < NOW()', + $excludeDeleted ? 'AND p.`deleted_at` IS NULL' : '', + $hasCategory ? 'p.`category_id` = :category' : '1' + )); + + if ($hasCategory) { + $getPosts->bindValue('category', $category); + } + + if (!$getAll) { + $getPosts->bindValue('take', $take); + $getPosts->bindValue('offset', $offset); + } + + $posts = $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : false; + return $posts ? $posts : []; +} + +function news_post_comments_set(int $postId, int $sectionId): void +{ + db_prepare(' + UPDATE `msz_news_posts` + SET `comment_section_id` = :comment_section_id + WHERE `post_id` = :post_id + ')->execute([ + 'comment_section_id' => $sectionId, + 'post_id' => $postId, + ]); +} + +function news_post_get(int $postId): array +{ + $getPost = db_prepare(' + SELECT + p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, p.`comment_section_id`, + c.`category_id`, c.`category_name`, + u.`user_id`, u.`username`, + COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour` + 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 + '); + + $getPost->bindValue(':post_id', $postId); + $post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false; + + return $post ? $post : []; +} diff --git a/templates/news/index.twig b/templates/news/index.twig index d7ee36bf..1d42aee4 100644 --- a/templates/news/index.twig +++ b/templates/news/index.twig @@ -25,7 +25,7 @@ {{ category.category_name }}
- {{ category.count }} post{{ category.count == 1 ? '' : 's' }} + {{ category.posts_count }} post{{ category.posts_count == 1 ? '' : 's' }}
{% endfor %}