From 624cba9d32cec8c69481ff0af6851ce5b53f8981 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 9 Jan 2019 21:22:54 +0100 Subject: [PATCH] Added button to mark a forum or the entire thing as read. --- assets/less/classes/forum/actions.less | 4 ++ public/forum/index.php | 65 ++++++++++++++++---------- src/Forum/forum.php | 35 ++++++++++++++ templates/forum/forum.twig | 2 +- templates/forum/index.twig | 14 +++--- templates/forum/macros.twig | 9 ++-- 6 files changed, 94 insertions(+), 35 deletions(-) diff --git a/assets/less/classes/forum/actions.less b/assets/less/classes/forum/actions.less index 11f2d01e..dbace11f 100644 --- a/assets/less/classes/forum/actions.less +++ b/assets/less/classes/forum/actions.less @@ -24,4 +24,8 @@ display: flex; align-items: stretch; } + + &__button { + margin-right: 5px; + } } diff --git a/public/forum/index.php b/public/forum/index.php index 8f3a0f09..1279fb82 100644 --- a/public/forum/index.php +++ b/public/forum/index.php @@ -1,32 +1,49 @@ $forumId]))); + break; -foreach ($categories as $key => $category) { - $categories[$key]['forum_subforums'] = forum_get_children( - $category['forum_id'], - user_session_current('user_id', 0), - perms_check($category['forum_permissions'], MSZ_FORUM_PERM_DELETE_TOPIC | MSZ_FORUM_PERM_DELETE_ANY_POST) - ); + case 'new': + break; - foreach ($categories[$key]['forum_subforums'] as $skey => $sub) { - if (!forum_may_have_children($sub['forum_type'])) { - continue; + case 'your': + break; + + default: + $categories = forum_get_root_categories(user_session_current('user_id', 0)); + $blankForum = count($categories) <= 1 && $categories[0]['forum_children'] < 1; + + foreach ($categories as $key => $category) { + $categories[$key]['forum_subforums'] = forum_get_children( + $category['forum_id'], + user_session_current('user_id', 0), + perms_check($category['forum_permissions'], MSZ_FORUM_PERM_DELETE_TOPIC | MSZ_FORUM_PERM_DELETE_ANY_POST) + ); + + foreach ($categories[$key]['forum_subforums'] as $skey => $sub) { + if (!forum_may_have_children($sub['forum_type'])) { + continue; + } + + $categories[$key]['forum_subforums'][$skey]['forum_subforums'] + = forum_get_children( + $sub['forum_id'], + user_session_current('user_id', 0), + perms_check($sub['forum_permissions'], MSZ_FORUM_PERM_DELETE_TOPIC | MSZ_FORUM_PERM_DELETE_ANY_POST), + true + ); + } } - $categories[$key]['forum_subforums'][$skey]['forum_subforums'] - = forum_get_children( - $sub['forum_id'], - user_session_current('user_id', 0), - perms_check($sub['forum_permissions'], MSZ_FORUM_PERM_DELETE_TOPIC | MSZ_FORUM_PERM_DELETE_ANY_POST), - true - ); - } + echo tpl_render('forum.index', [ + 'forum_categories' => $categories, + 'forum_empty' => $blankForum, + ]); + break; } - -echo tpl_render('forum.index', [ - 'forum_categories' => $categories, - 'forum_empty' => $blankForum, -]); diff --git a/src/Forum/forum.php b/src/Forum/forum.php index d3eb2e62..7b3ed293 100644 --- a/src/Forum/forum.php +++ b/src/Forum/forum.php @@ -393,3 +393,38 @@ function forum_timeout(int $forumId, int $userId): int return (int)($checkTimeout->execute() ? $checkTimeout->fetchColumn() : 0); } + +// $forumId == null marks all forums as read +function forum_mark_read(?int $forumId, int $userId): bool +{ + if (($forumId !== null && $forumId < 1) || $userId < 1) { + return false; + } + + $entireForum = $forumId === null; + $doMark = db_prepare(sprintf( + ' + REPLACE INTO `msz_forum_topics_track` + (`user_id`, `topic_id`, `forum_id`, `track_last_read`) + SELECT u.`user_id`, t.`topic_id`, t.`forum_id`, NOW() + FROM `msz_forum_topics` AS t + LEFT JOIN `msz_users` AS u + ON u.`user_id` = :user + WHERE t.`topic_deleted` IS NULL + AND t.`topic_bumped` >= NOW() - INTERVAL 1 MONTH + %1$s + GROUP BY t.`topic_id` + HAVING ((%2$s) & %3$d) > 0 + ', + $entireForum ? '' : 'AND t.`forum_id` = :forum', + forum_perms_get_user_sql(MSZ_FORUM_PERMS_GENERAL, 't.`forum_id`', 'u.`user_id`', 'u.`user_id`'), + MSZ_FORUM_PERM_SET_READ + )); + $doMark->bindValue('user', $userId); + + if (!$entireForum) { + $doMark->bindValue('forum', $forumId); + } + + return $doMark->execute(); +} diff --git a/templates/forum/forum.twig b/templates/forum/forum.twig index e9125145..a640ae60 100644 --- a/templates/forum/forum.twig +++ b/templates/forum/forum.twig @@ -15,7 +15,7 @@ {% endif %} {% if forum_may_have_topics %} - {% set category_tools = forum_category_tools(forum_info, forum_perms, forum_pagination) %} + {% set category_tools = forum_category_tools(forum_info, forum_perms, forum_pagination, current_user|default(false)) %} {{ category_tools }} {{ forum_topic_listing(forum_topics) }} {{ category_tools }} diff --git a/templates/forum/index.twig b/templates/forum/index.twig index 1621af35..e2f7e58b 100644 --- a/templates/forum/index.twig +++ b/templates/forum/index.twig @@ -20,13 +20,13 @@ {% endif %} {% endfor %} - {# -
- Mark All Read - Unanswered Posts - New Posts - Your Posts -
#} + {% if current_user is defined %} +
+ Mark All Read + {#Unread Posts#} + {#Your Posts#} +
+ {% endif %} {% else %}
{{ container_title(' Forums') }} diff --git a/templates/forum/macros.twig b/templates/forum/macros.twig index 5dcdae28..b9eccebf 100644 --- a/templates/forum/macros.twig +++ b/templates/forum/macros.twig @@ -53,18 +53,21 @@
{% endmacro %} -{% macro forum_category_tools(info, perms, pagination_info) %} +{% macro forum_category_tools(info, perms, pagination_info, user) %} {% from 'macros.twig' import pagination %} {% set is_locked = info.forum_archived != 0 %} {% set can_topic = not is_locked and perms|perms_check(constant('MSZ_FORUM_PERM_CREATE_TOPIC')) %} {% set pag = pagination(pagination_info, '/forum/forum.php', null, {'f': info.forum_id}) %} - {% if can_topic or pag|trim|length > 0 %} + {% if user or can_topic or pag|trim|length > 0 %}
{% if can_topic %} - New Topic + New Topic + {% endif %} + {% if user %} + Mark As Read {% endif %}