From 0d6b1be094f90deb1debadfe42669d7dcab8ecba Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 30 Dec 2018 19:58:30 +0100 Subject: [PATCH] Added global announcement post type. --- src/Forum/forum.php | 5 ++++ src/Forum/topic.php | 53 ++++++++++++++++++++++++++++++------- templates/forum/macros.twig | 20 +++++++------- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/src/Forum/forum.php b/src/Forum/forum.php index 1f5fd68b..24e955ef 100644 --- a/src/Forum/forum.php +++ b/src/Forum/forum.php @@ -63,6 +63,11 @@ define('MSZ_FORUM_ROOT_DATA', [ // should be compatible with the data fetched in 'forum_colour' => null, ]); +function forum_is_valid_type(int $type): bool +{ + return in_array($type, MSZ_FORUM_TYPES, true); +} + function forum_may_have_children(int $forumType): bool { return in_array($forumType, MSZ_FORUM_MAY_HAVE_CHILDREN); diff --git a/src/Forum/topic.php b/src/Forum/topic.php index 038db331..4ad007b3 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -2,40 +2,70 @@ define('MSZ_TOPIC_TYPE_DISCUSSION', 0); define('MSZ_TOPIC_TYPE_STICKY', 1); define('MSZ_TOPIC_TYPE_ANNOUNCEMENT', 2); +define('MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT', 3); define('MSZ_TOPIC_TYPES', [ MSZ_TOPIC_TYPE_DISCUSSION, MSZ_TOPIC_TYPE_STICKY, MSZ_TOPIC_TYPE_ANNOUNCEMENT, + MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT, ]); -function forum_topic_create(int $forumId, int $userId, string $title): int +define('MSZ_TOPIC_TYPE_ORDER', [ // in which order to display topics, only add types here that should appear above others + MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT, + MSZ_TOPIC_TYPE_ANNOUNCEMENT, + MSZ_TOPIC_TYPE_STICKY, +]); + +function forum_topic_is_valid_type(int $type): bool { + return in_array($type, MSZ_TOPIC_TYPES, true); +} + +function forum_topic_create(int $forumId, int $userId, string $title, int $type = MSZ_TOPIC_TYPE_DISCUSSION): int +{ + if (empty($title) || !forum_topic_is_valid_type($type)) { + return 0; + } + $createTopic = db_prepare(' INSERT INTO `msz_forum_topics` - (`forum_id`, `user_id`, `topic_title`) + (`forum_id`, `user_id`, `topic_title`, `topic_type`) VALUES - (:forum_id, :user_id, :topic_title) + (:forum_id, :user_id, :topic_title, :topic_type) '); $createTopic->bindValue('forum_id', $forumId); $createTopic->bindValue('user_id', $userId); $createTopic->bindValue('topic_title', $title); + $createTopic->bindValue('topic_type', $type); return $createTopic->execute() ? (int)db_last_insert_id() : 0; } -function forum_topic_update(int $topicId, string $title): bool +function forum_topic_update(int $topicId, ?string $title, ?int $type = null): bool { - if ($topicId < 1 || empty($title)) { + if ($topicId < 1) { + return false; + } + + // make sure it's null and not some other kinda empty + if (empty($title)) { + $title = null; + } + + if ($type !== null && !forum_topic_is_valid_type($type)) { return false; } $updateTopic = db_prepare(' UPDATE `msz_forum_topics` - SET `topic_title` = :topic_title + SET `topic_title` = COALESCE(:topic_title, `topic_title`) + `topic_type` = COALESCE(:topic_type, `topic_type`) WHERE `topic_id` = :topic_id '); $updateTopic->bindValue('topic_id', $topicId); $updateTopic->bindValue('topic_title', $title); + $updateTopic->bindValue('topic_type', $type); + return $updateTopic->execute(); } @@ -152,13 +182,18 @@ function forum_topic_listing(int $forumId, int $userId, int $offset = 0, int $ta ON lu.`user_id` = lp.`user_id` LEFT JOIN `msz_roles` as lr ON lr.`role_id` = lu.`display_role` - WHERE t.`forum_id` = :forum_id + WHERE ( + t.`forum_id` = :forum_id + OR t.`topic_type` = %3$d + ) %1$s - ORDER BY t.`topic_type` DESC, t.`topic_bumped` DESC + ORDER BY FIELD(t.`topic_type`, %4$s) DESC, t.`topic_bumped` DESC %2$s ', $showDeleted ? '' : 'AND t.`topic_deleted` IS NULL', - $hasPagination ? 'LIMIT :offset, :take' : '' + $hasPagination ? 'LIMIT :offset, :take' : '', + MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT, + implode(',', array_reverse(MSZ_TOPIC_TYPE_ORDER)) )); $getTopics->bindValue('forum_id', $forumId); $getTopics->bindValue('user_id', $userId); diff --git a/templates/forum/macros.twig b/templates/forum/macros.twig index 3f76539a..0fecb801 100644 --- a/templates/forum/macros.twig +++ b/templates/forum/macros.twig @@ -115,10 +115,10 @@ {% if forum_type is null %} {% if forum.forum_archived is defined and forum.forum_archived %} {% set forum_type = 'fas fa-archive' %} - {% elseif forum.forum_type is defined and forum.forum_type != 0 %} - {% if forum.forum_type == 2 %} + {% elseif forum.forum_type is defined and forum.forum_type != constant('MSZ_FORUM_TYPE_DISCUSSION') %} + {% if forum.forum_type == constant('MSZ_FORUM_TYPE_LINK') %} {% set forum_type = 'fas fa-link' %} - {% elseif forum.forum_type == 1 %} + {% elseif forum.forum_type == constant('MSZ_FORUM_TYPE_CATEGORY') %} {% set forum_type = 'fas fa-folder' %} {% endif %} {% else %} @@ -155,22 +155,22 @@ {% endif %} - {% if forum.forum_type == 2 %} + {% if forum.forum_type == constant('MSZ_FORUM_TYPE_LINK') %} {% if forum.forum_link_clicks is not null %}
{{ forum.forum_link_clicks|number_format }}
{% endif %} - {% elseif forum.forum_type != 1 %} + {% elseif forum.forum_type != constant('MSZ_FORUM_TYPE_CATEGORY') %}
{{ forum.forum_topic_count|number_format }}
{{ forum.forum_post_count|number_format }}
{% endif %} - {% if forum.forum_type == 0 or forum.forum_link_clicks is not null %} + {% if forum.forum_type == constant('MSZ_FORUM_TYPE_DISCUSSION') or forum.forum_link_clicks is not null %}