From 8c7aad6d16fea6870f29a39b29dcfa40608b2e4d Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 30 Dec 2018 20:51:32 +0100 Subject: [PATCH] Added dropdown for selecting topic type in editor. --- assets/less/classes/forum/post.less | 2 +- public/forum/posting.php | 77 +++++++++++++++++++---------- src/Forum/topic.php | 2 +- src/array.php | 13 +++++ templates/forum/posting.twig | 10 ++-- 5 files changed, 74 insertions(+), 30 deletions(-) diff --git a/assets/less/classes/forum/post.less b/assets/less/classes/forum/post.less index e288f69c..99483658 100644 --- a/assets/less/classes/forum/post.less +++ b/assets/less/classes/forum/post.less @@ -176,7 +176,7 @@ } } - &__parser { + &__dropdown { margin: 0; } diff --git a/public/forum/posting.php b/public/forum/posting.php index d356feb9..8ef4396a 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -47,13 +47,7 @@ if (!empty($postId)) { } if (!empty($topicId)) { - $getTopic = db_prepare(' - SELECT `topic_id`, `forum_id`, `topic_title`, `topic_locked` - FROM `msz_forum_topics` - WHERE `topic_id` = :topic_id - '); - $getTopic->bindValue('topic_id', $topicId); - $topic = $getTopic->execute() ? $getTopic->fetch(PDO::FETCH_ASSOC) : false; + $topic = forum_topic_fetch($topicId); if (isset($topic['forum_id'])) { $forumId = (int)$topic['forum_id']; @@ -78,7 +72,7 @@ if (empty($forum)) { $perms = forum_perms_get_user(MSZ_FORUM_PERMS_GENERAL, $forum['forum_id'], user_session_current('user_id')); if ($forum['forum_archived'] - || !empty($topic['topic_locked']) + || (!empty($topic['topic_locked']) && !perms_check($perms, MSZ_FORUM_PERM_LOCK_TOPIC)) || !perms_check($perms, MSZ_FORUM_PERM_VIEW_FORUM | MSZ_FORUM_PERM_CREATE_POST) || (empty($topic) && !perms_check($perms, MSZ_FORUM_PERM_CREATE_TOPIC))) { echo render_error(403); @@ -90,6 +84,22 @@ if (!forum_may_have_topics($forum['forum_type'])) { return; } +$topicTypes = []; + +if ($mode === 'create' || $mode === 'edit') { + $topicTypes[MSZ_TOPIC_TYPE_DISCUSSION] = 'Normal discussion'; + + if (perms_check($perms, MSZ_FORUM_PERM_STICKY_TOPIC)) { + $topicTypes[MSZ_TOPIC_TYPE_STICKY] = 'Sticky topic'; + } + if (perms_check($perms, MSZ_FORUM_PERM_ANNOUNCE_TOPIC)) { + $topicTypes[MSZ_TOPIC_TYPE_ANNOUNCEMENT] = 'Announcement'; + } + if (perms_check($perms, MSZ_FORUM_PERM_GLOBAL_ANNOUNCE_TOPIC)) { + $topicTypes[MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT] = 'Global Announcement'; + } +} + // edit mode stuff if ($mode === 'edit') { if (empty($post)) { @@ -109,8 +119,31 @@ if (!empty($_POST)) { if (!csrf_verify('forum_post', $_POST['csrf'] ?? '')) { $notices[] = 'Could not verify request.'; } else { - $topicTitle = $_POST['post']['title'] ?? ''; - $setTopicTitle = empty($topic) || ($mode === 'edit' && $post['is_opening_post'] && $topicTitle !== $topic['topic_title']); + $isEditingTopic = empty($topic) || ($mode === 'edit' && $post['is_opening_post']); + + if ($isEditingTopic) { + $topicTitle = $_POST['post']['title'] ?? ''; + $originalTopicTitle = $topic['topic_title'] ?? null; + $topicTitleChanged = $topicTitle !== $originalTopicTitle; + $topicType = (int)($_POST['post']['type'] ?? array_key_first($topicTypes)); + $originalTopicType = (int)($topic['topic_type'] ?? 0); + $topicTypeChanged = $topicType !== $originalTopicType; + + switch (forum_validate_title($topicTitle)) { + case 'too-short': + $notices[] = 'Topic title was too short.'; + break; + + case 'too-long': + $notices[] = 'Topic title was too long.'; + break; + } + + if (!array_key_exists($topicType, $topicTypes) && $topicTypeChanged) { + $notices[] = 'You are not allowed to set this topic type.'; + } + } + $postText = $_POST['post']['text'] ?? ''; $postParser = (int)($_POST['post']['parser'] ?? MSZ_PARSER_BBCODE); @@ -128,25 +161,18 @@ if (!empty($_POST)) { break; } - if ($setTopicTitle) { - switch (forum_validate_title($topicTitle)) { - case 'too-short': - $notices[] = 'Topic title was too short.'; - break; - - case 'too-long': - $notices[] = 'Topic title was too long.'; - break; - } - } - if (empty($notices)) { switch ($mode) { case 'create': if (!empty($topic)) { forum_topic_bump($topic['topic_id']); } else { - $topicId = forum_topic_create($forum['forum_id'], user_session_current('user_id', 0), $topicTitle); + $topicId = forum_topic_create( + $forum['forum_id'], + user_session_current('user_id', 0), + $topicTitle, + $topicType + ); } $postId = forum_post_create( @@ -165,8 +191,8 @@ if (!empty($_POST)) { $notices[] = 'Post edit failed.'; } - if ($setTopicTitle) { - if (!forum_topic_update($topicId, $topicTitle)) { + if ($isEditingTopic && ($topicTitleChanged || $topicTypeChanged)) { + if (!forum_topic_update($topicId, $topicTitle, $topicType)) { $notices[] = 'Topic update failed.'; } } @@ -209,4 +235,5 @@ echo tpl_render('forum.posting', [ 'posting_info' => $displayInfo, 'posting_notices' => $notices, 'posting_mode' => $mode, + 'posting_types' => $topicTypes, ]); diff --git a/src/Forum/topic.php b/src/Forum/topic.php index 4ad007b3..5ce9524f 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -58,7 +58,7 @@ function forum_topic_update(int $topicId, ?string $title, ?int $type = null): bo $updateTopic = db_prepare(' UPDATE `msz_forum_topics` - SET `topic_title` = COALESCE(:topic_title, `topic_title`) + SET `topic_title` = COALESCE(:topic_title, `topic_title`), `topic_type` = COALESCE(:topic_type, `topic_type`) WHERE `topic_id` = :topic_id '); diff --git a/src/array.php b/src/array.php index 366d97da..fb5adf48 100644 --- a/src/array.php +++ b/src/array.php @@ -18,3 +18,16 @@ function array_apply(array $array, callable $func): array return $array; } + +if (!function_exists('array_key_first')) { + // https://secure.php.net/manual/en/function.array-key-first.php#123301 + function array_key_first(array $array) + { + if (count($array)) { + reset($array); + return key($array); + } + + return null; + } +} diff --git a/templates/forum/posting.twig b/templates/forum/posting.twig index 3d79a4b7..133467ec 100644 --- a/templates/forum/posting.twig +++ b/templates/forum/posting.twig @@ -4,6 +4,7 @@ {% set title = 'Posting' %} {% set is_reply = posting_topic is defined %} +{% set is_opening = not is_reply or posting_post.is_opening_post|default(false) %} {% block content %}
@@ -11,7 +12,7 @@ {{ input_hidden('post[mode]', posting_mode) }} {{ input_csrf('forum_post') }} {{ forum_header( - is_reply and not posting_post.is_opening_post|default(false) + is_reply and not is_opening ? posting_topic.topic_title : input_text( 'post[title]', @@ -22,7 +23,7 @@ ), posting_breadcrumbs, false, - is_reply and not posting_post.is_opening_post|default(false) + is_reply and not is_opening ? '/forum/topic.php?t=' ~ posting_topic.topic_id : '' ) }} @@ -65,7 +66,10 @@
- {{ input_select('post[parser]', constant('MSZ_PARSERS_NAMES'), posting_post.post_parse|default(constant('MSZ_PARSER_BBCODE')), null, null, null, 'forum__post__parser') }} + {{ input_select('post[parser]', constant('MSZ_PARSERS_NAMES'), posting_post.post_parse|default(constant('MSZ_PARSER_BBCODE')), null, null, null, 'forum__post__dropdown') }} + {% if is_opening and posting_types|length > 1 %} + {{ input_select('post[type]', posting_types, posting_topic.topic_type|default(posting_types|keys|first), null, null, null, 'forum__post__dropdown') }} + {% endif %}