From c025a2c88e97e93ecd6bba180047820036704ad6 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 30 Dec 2018 04:23:04 +0100 Subject: [PATCH] Allow editing of the topic name when editing the original post. --- public/forum/posting.php | 17 +++++++++++------ src/Forum/post.php | 34 ++++++++++++++++++++++------------ src/Forum/topic.php | 16 ++++++++++++++++ templates/forum/macros.twig | 5 ++--- templates/forum/posting.twig | 17 ++++++++++++++++- templates/forum/topic.twig | 2 +- 6 files changed, 68 insertions(+), 23 deletions(-) diff --git a/public/forum/posting.php b/public/forum/posting.php index 7493719d..d356feb9 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -110,16 +110,15 @@ if (!empty($_POST)) { $notices[] = 'Could not verify request.'; } else { $topicTitle = $_POST['post']['title'] ?? ''; - $topicTitleValidate = forum_validate_title($topicTitle); + $setTopicTitle = empty($topic) || ($mode === 'edit' && $post['is_opening_post'] && $topicTitle !== $topic['topic_title']); $postText = $_POST['post']['text'] ?? ''; - $postTextValidate = forum_validate_post($postText); $postParser = (int)($_POST['post']['parser'] ?? MSZ_PARSER_BBCODE); if (!parser_is_valid($postParser)) { $notices[] = 'Invalid parser selected.'; } - switch ($postTextValidate) { + switch (forum_validate_post($postText)) { case 'too-short': $notices[] = 'Post content was too short.'; break; @@ -129,8 +128,8 @@ if (!empty($_POST)) { break; } - if (empty($topic)) { - switch ($topicTitleValidate) { + if ($setTopicTitle) { + switch (forum_validate_title($topicTitle)) { case 'too-short': $notices[] = 'Topic title was too short.'; break; @@ -162,9 +161,15 @@ if (!empty($_POST)) { break; case 'edit': - if (!forum_post_edit($postId, ip_remote_address(), $postText, $postParser)) { + if (!forum_post_update($postId, ip_remote_address(), $postText, $postParser)) { $notices[] = 'Post edit failed.'; } + + if ($setTopicTitle) { + if (!forum_topic_update($topicId, $topicTitle)) { + $notices[] = 'Topic update failed.'; + } + } break; } diff --git a/src/Forum/post.php b/src/Forum/post.php index 6472d61a..43e0ba09 100644 --- a/src/Forum/post.php +++ b/src/Forum/post.php @@ -23,7 +23,7 @@ function forum_post_create( return $createPost->execute() ? db_last_insert_id() : 0; } -function forum_post_edit( +function forum_post_update( int $postId, string $ipAddress, string $text, @@ -78,22 +78,27 @@ function forum_post_get(int $postId, bool $allowDeleted = false): array SELECT p.`post_id`, p.`post_text`, p.`post_created`, p.`post_parse`, p.`topic_id`, p.`post_deleted`, p.`post_edited`, - INET6_NTOA(p.`post_ip`) as `post_ip`, - u.`user_id` as `poster_id`, - u.`username` as `poster_name`, - u.`user_created` as `poster_joined`, - u.`user_country` as `poster_country`, - COALESCE(u.`user_colour`, r.`role_colour`) as `poster_colour`, + INET6_NTOA(p.`post_ip`) AS `post_ip`, + u.`user_id` AS `poster_id`, + u.`username` AS `poster_name`, + u.`user_created` AS `poster_joined`, + u.`user_country` AS `poster_country`, + COALESCE(u.`user_colour`, r.`role_colour`) AS `poster_colour`, ( SELECT COUNT(`post_id`) FROM `msz_forum_posts` WHERE `user_id` = p.`user_id` AND `post_deleted` IS NULL - ) as `poster_post_count` - FROM `msz_forum_posts` as p - LEFT JOIN `msz_users` as u + ) AS `poster_post_count`, + ( + SELECT MIN(`post_id`) = p.`post_id` + FROM `msz_forum_posts` + WHERE `topic_id` = p.`topic_id` + ) AS `is_opening_post` + FROM `msz_forum_posts` AS p + LEFT JOIN `msz_users` AS u ON u.`user_id` = p.`user_id` - LEFT JOIN `msz_roles` as r + LEFT JOIN `msz_roles` AS r ON r.`role_id` = u.`display_role` WHERE `post_id` = :post_id %1$s @@ -125,7 +130,12 @@ function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $ FROM `msz_forum_posts` WHERE `user_id` = p.`user_id` AND `post_deleted` IS NULL - ) as `poster_post_count` + ) as `poster_post_count`, + ( + SELECT MIN(`post_id`) = p.`post_id` + FROM `msz_forum_posts` + WHERE `topic_id` = p.`topic_id` + ) AS `is_opening_post` FROM `msz_forum_posts` as p LEFT JOIN `msz_users` as u ON u.`user_id` = p.`user_id` diff --git a/src/Forum/topic.php b/src/Forum/topic.php index eb62b222..038db331 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -23,6 +23,22 @@ function forum_topic_create(int $forumId, int $userId, string $title): int return $createTopic->execute() ? (int)db_last_insert_id() : 0; } +function forum_topic_update(int $topicId, string $title): bool +{ + if ($topicId < 1 || empty($title)) { + return false; + } + + $updateTopic = db_prepare(' + UPDATE `msz_forum_topics` + SET `topic_title` = :topic_title + WHERE `topic_id` = :topic_id + '); + $updateTopic->bindValue('topic_id', $topicId); + $updateTopic->bindValue('topic_title', $title); + return $updateTopic->execute(); +} + function forum_topic_fetch(int $topicId): array { $getTopic = db_prepare(' diff --git a/templates/forum/macros.twig b/templates/forum/macros.twig index 87d6c3f5..3f76539a 100644 --- a/templates/forum/macros.twig +++ b/templates/forum/macros.twig @@ -327,13 +327,12 @@ {% endmacro %} -{% macro forum_post_listing(posts, opening_post_id, user_id, perms) %} +{% macro forum_post_listing(posts, user_id, perms) %} {% from _self import forum_post_entry %} {% for post in posts %} {{ forum_post_entry( post, - post.post_id == opening_post_id, perms|perms_check(constant('MSZ_FORUM_PERM_CREATE_POST')), perms|perms_check(constant(user_id == post.poster_id ? 'MSZ_FORUM_PERM_EDIT_POST' : 'MSZ_FORUM_PERM_EDIT_ANY_POST')), perms|perms_check(constant(user_id == post.poster_id ? 'MSZ_FORUM_PERM_DELETE_POST' : 'MSZ_FORUM_PERM_DELETE_ANY_POST')) @@ -341,7 +340,7 @@ {% endfor %} {% endmacro %} -{% macro forum_post_entry(post, is_original_post, can_post, can_edit, can_delete) %} +{% macro forum_post_entry(post, can_post, can_edit, can_delete) %} {% set is_deleted = post.post_deleted is not null %}
diff --git a/templates/forum/posting.twig b/templates/forum/posting.twig index 9fe6ac4a..3d79a4b7 100644 --- a/templates/forum/posting.twig +++ b/templates/forum/posting.twig @@ -10,7 +10,22 @@ {{ input_hidden('post[' ~ (is_reply ? 'topic' : 'forum') ~ ']', is_reply ? posting_topic.topic_id : posting_forum.forum_id) }} {{ input_hidden('post[mode]', posting_mode) }} {{ input_csrf('forum_post') }} - {{ forum_header(is_reply ? posting_topic.topic_title : input_text('post[title]', 'forum__header__input', '', 'text', 'Enter your title here...'), posting_breadcrumbs, false, is_reply ? '/forum/topic.php?t=' ~ posting_topic.topic_id : '') }} + {{ forum_header( + is_reply and not posting_post.is_opening_post|default(false) + ? posting_topic.topic_title + : input_text( + 'post[title]', + 'forum__header__input', + posting_topic.topic_title|default(''), + 'text', + 'Enter your title here...' + ), + posting_breadcrumbs, + false, + is_reply and not posting_post.is_opening_post|default(false) + ? '/forum/topic.php?t=' ~ posting_topic.topic_id + : '' + ) }} {% if posting_post is defined %} {{ input_hidden('post[id]', posting_post.post_id) }} diff --git a/templates/forum/topic.twig b/templates/forum/topic.twig index f6526690..ef59e4c5 100644 --- a/templates/forum/topic.twig +++ b/templates/forum/topic.twig @@ -22,6 +22,6 @@ {{ forum_header(topic_info.topic_title, topic_breadcrumbs) }} {{ forum_topic_locked(topic_info.topic_locked, topic_info.topic_archived) }} {{ topic_tools }} - {{ forum_post_listing(topic_posts, topic_info.topic_first_post_id, current_user.user_id, topic_perms) }} + {{ forum_post_listing(topic_posts, current_user.user_id, topic_perms) }} {{ topic_tools }} {% endblock %}