From c6826bd4f142c134c13f03c596c60d9000bbe78b Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 10 Mar 2019 22:18:33 +0100 Subject: [PATCH] Added profile topic listing (wip). --- public/profile.php | 41 ++++++++++++- src/Forum/post.php | 23 ++++++- src/Forum/topic.php | 109 ++++++++++++++++++++++++++++++++++ src/url.php | 2 + templates/profile/master.twig | 2 + templates/profile/posts.twig | 15 +++++ templates/profile/topics.twig | 23 +++++++ templates/user/macros.twig | 4 +- 8 files changed, 211 insertions(+), 8 deletions(-) create mode 100644 templates/profile/posts.twig create mode 100644 templates/profile/topics.twig diff --git a/public/profile.php b/public/profile.php index 1541f88f..ccc8c58c 100644 --- a/public/profile.php +++ b/public/profile.php @@ -289,7 +289,7 @@ switch ($mode) { tpl_vars([ 'title' => 'flash / following', - 'canonical_url' => url('user-profile-following', ['user' => $profile['user_id']]), + 'canonical_url' => url('user-profile-following', ['user' => $userId]), 'profile_users' => $following, 'profile_relation_pagination' => $followingPagination, ]); @@ -310,10 +310,45 @@ switch ($mode) { tpl_vars([ 'title' => 'flash / followers', - 'canonical_url' => url('user-profile-followers', ['user' => $profile['user_id']]), + 'canonical_url' => url('user-profile-followers', ['user' => $userId]), 'profile_users' => $followers, 'profile_relation_pagination' => $followerPagination, - ]); + ]); + break; + + case 'forum-topics': + $template = 'profile.topics'; + $topicsCount = forum_topic_count_user($userId, $currentUserId); + $topicsPagination = pagination_create($topicsCount, 20); + $topicsOffset = pagination_offset($topicsPagination, pagination_param()); + + if (!pagination_is_valid_offset($topicsOffset)) { + echo render_error(404); + return; + } + + $topics = forum_topic_listing_user($userId, $currentUserId, $topicsOffset, $topicsPagination['range']); + + tpl_vars([ + 'title' => 'flash / topics', + 'canonical_url' => url('user-profile-forum-topics', ['user' => $userId]), + 'profile_topics' => $topics, + 'profile_topics_pagination' => $topicsPagination, + ]); + break; + + case 'forum-posts': + $template = 'profile.posts'; + $postsCount = forum_post_count_user($userId); + $postsPagination = pagination_create($postsCount, 20); + $postsOffset = pagination_offset($postsPagination, pagination_param()); + + if (!pagination_is_valid_offset($postsOffset)) { + echo render_error(404); + return; + } + + $posts = forum_post_listing($userId, $postsOffset, $postsPagination['range'], false, true); break; case '': diff --git a/src/Forum/post.php b/src/Forum/post.php index 03cffff0..e695b1bb 100644 --- a/src/Forum/post.php +++ b/src/Forum/post.php @@ -129,7 +129,23 @@ function forum_post_get(int $postId, bool $allowDeleted = false): array return db_fetch($getPost); } -function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $showDeleted = false): array +function forum_post_count_user(int $userId, bool $showDeleted = false): int +{ + $getPosts = db_prepare(sprintf( + ' + SELECT COUNT(p.`post_id`) + FROM `msz_forum_posts` AS p + WHERE `user_id` = :user_id + %1$s + ', + $showDeleted ? '' : 'AND `post_deleted` IS NULL' + )); + $getPosts->bindValue('user_id', $userId); + + return (int)($getPosts->execute() ? $getPosts->fetchColumn() : 0); +} + +function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $showDeleted = false, bool $selectAuthor = false): array { $hasPagination = $offset >= 0 && $take > 0; $getPosts = db_prepare(sprintf( @@ -166,13 +182,14 @@ function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $ ON u.`user_id` = p.`user_id` LEFT JOIN `msz_roles` AS r ON r.`role_id` = u.`display_role` - WHERE `topic_id` = :topic_id + WHERE `%3$s` = :topic_id %1$s ORDER BY `post_id` %2$s ', $showDeleted ? '' : 'AND `post_deleted` IS NULL', - $hasPagination ? 'LIMIT :offset, :take' : '' + $hasPagination ? 'LIMIT :offset, :take' : '', + $selectAuthor ? 'topic_id' : 'user_id' )); $getPosts->bindValue('topic_id', $topicId); diff --git a/src/Forum/topic.php b/src/Forum/topic.php index 91d2f5df..db131471 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -270,6 +270,115 @@ function forum_topic_listing(int $forumId, int $userId, int $offset = 0, int $ta return db_fetch_all($getTopics); } +function forum_topic_count_user(int $authorId, int $userId, bool $showDeleted = false): int +{ + $getTopics = db_prepare(sprintf( + ' + SELECT COUNT(`topic_id`) + FROM `msz_forum_topics` AS t + WHERE t.`user_id` = :author_id + %1$s + ', + $showDeleted ? '' : 'AND t.`topic_deleted` IS NULL' + )); + $getTopics->bindValue('author_id', $authorId); + //$getTopics->bindValue('user_id', $userId); + + return (int)($getTopics->execute() ? $getTopics->fetchColumn() : 0); +} + +// Remove unneccesary stuff from the sql stmt +function forum_topic_listing_user(int $authorId, int $userId, int $offset = 0, int $take = 0, bool $showDeleted = false): array +{ + $hasPagination = $offset >= 0 && $take > 0; + $getTopics = db_prepare(sprintf( + ' + SELECT + :user_id AS `target_user_id`, + t.`topic_id`, t.`topic_title`, t.`topic_locked`, t.`topic_type`, t.`topic_created`, + t.`topic_bumped`, t.`topic_deleted`, t.`topic_count_views`, + au.`user_id` AS `author_id`, au.`username` AS `author_name`, + COALESCE(au.`user_colour`, ar.`role_colour`) AS `author_colour`, + lp.`post_id` AS `response_id`, + lp.`post_created` AS `response_created`, + lu.`user_id` AS `respondent_id`, + lu.`username` AS `respondent_name`, + COALESCE(lu.`user_colour`, lr.`role_colour`) AS `respondent_colour`, + ( + SELECT COUNT(`post_id`) + FROM `msz_forum_posts` + WHERE `topic_id` = t.`topic_id` + %5$s + ) AS `topic_count_posts`, + ( + SELECT CEIL(COUNT(`post_id`) / %6$d) + FROM `msz_forum_posts` + WHERE `topic_id` = t.`topic_id` + %5$s + ) AS `topic_pages`, + ( + SELECT + `target_user_id` > 0 + AND + t.`topic_bumped` > NOW() - INTERVAL 1 MONTH + AND ( + SELECT COUNT(ti.`topic_id`) < 1 + FROM `msz_forum_topics_track` AS tt + RIGHT JOIN `msz_forum_topics` AS ti + ON ti.`topic_id` = tt.`topic_id` + WHERE ti.`topic_id` = t.`topic_id` + AND tt.`user_id` = `target_user_id` + AND `track_last_read` >= `topic_bumped` + ) + ) AS `topic_unread`, + ( + SELECT COUNT(`post_id`) > 0 + FROM `msz_forum_posts` + WHERE `topic_id` = t.`topic_id` + AND `user_id` = `target_user_id` + LIMIT 1 + ) AS `topic_participated` + FROM `msz_forum_topics` AS t + LEFT JOIN `msz_users` AS au + ON t.`user_id` = au.`user_id` + LEFT JOIN `msz_roles` AS ar + ON ar.`role_id` = au.`display_role` + LEFT JOIN `msz_forum_posts` AS lp + ON lp.`post_id` = ( + SELECT `post_id` + FROM `msz_forum_posts` + WHERE `topic_id` = t.`topic_id` + %5$s + ORDER BY `post_id` DESC + LIMIT 1 + ) + LEFT JOIN `msz_users` AS lu + ON lu.`user_id` = lp.`user_id` + LEFT JOIN `msz_roles` AS lr + ON lr.`role_id` = lu.`display_role` + WHERE au.`user_id` = :author_id + %1$s + 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' : '', + MSZ_TOPIC_TYPE_GLOBAL_ANNOUNCEMENT, + implode(',', array_reverse(MSZ_TOPIC_TYPE_ORDER)), + $showDeleted ? '' : 'AND `post_deleted` IS NULL', + MSZ_FORUM_POSTS_PER_PAGE + )); + $getTopics->bindValue('author_id', $authorId); + $getTopics->bindValue('user_id', $userId); + + if ($hasPagination) { + $getTopics->bindValue('offset', $offset); + $getTopics->bindValue('take', $take); + } + + return db_fetch_all($getTopics); +} + function forum_topic_lock(int $topicId): bool { if ($topicId < 1) { diff --git a/src/url.php b/src/url.php index dfa6df03..901b21fa 100644 --- a/src/url.php +++ b/src/url.php @@ -58,6 +58,8 @@ define('MSZ_URLS', [ 'user-profile' => ['/profile.php', ['u' => '']], 'user-profile-following' => ['/profile.php', ['u' => '', 'm' => 'following']], 'user-profile-followers' => ['/profile.php', ['u' => '', 'm' => 'followers']], + 'user-profile-forum-topics' => ['/profile.php', ['u' => '', 'm' => 'forum-topics']], + 'user-profile-forum-posts' => ['/profile.php', ['u' => '', 'm' => 'forum-posts']], 'user-profile-edit' => ['/profile.php', ['u' => '', 'edit' => '1']], 'user-account-standing' => ['/profile.php', ['u' => ''], 'account-standing'], diff --git a/templates/profile/master.twig b/templates/profile/master.twig index 418ca3d6..3598cded 100644 --- a/templates/profile/master.twig +++ b/templates/profile/master.twig @@ -26,10 +26,12 @@ { 'title': 'Topics', 'value': profile.forum_topic_count, + 'url': url('user-profile-forum-topics', {'user': profile.user_id}), }, { 'title': 'Posts', 'value': profile.forum_post_count, + 'url': url('user-profile-forum-posts', {'user': profile.user_id}), }, { 'title': 'Comments', diff --git a/templates/profile/posts.twig b/templates/profile/posts.twig new file mode 100644 index 00000000..06155b7e --- /dev/null +++ b/templates/profile/posts.twig @@ -0,0 +1,15 @@ +{% extends 'profile/master.twig' %} +{% from 'macros.twig' import pagination %} +{% from 'forum/macros.twig' import forum_topic_listing %} + +{% block content %} +
+ {% include 'profile/_layout/header.twig' %} + + {# if profile_relation_pagination.pages > 1 %} +
+ {{ pagination(profile_relation_pagination, canonical_url) }} +
+ {% endif #} +
+{% endblock %} diff --git a/templates/profile/topics.twig b/templates/profile/topics.twig new file mode 100644 index 00000000..12773026 --- /dev/null +++ b/templates/profile/topics.twig @@ -0,0 +1,23 @@ +{% extends 'profile/master.twig' %} +{% from 'macros.twig' import pagination %} +{% from 'forum/macros.twig' import forum_topic_listing %} + +{% block content %} +
+ {% include 'profile/_layout/header.twig' %} + + {% set sp = profile_topics_pagination.pages > 1 + ? '
' ~ pagination(profile_topics_pagination, canonical_url) ~ '
' + : '' %} + + {% if sp is not empty %} + {{ sp|raw }} + {% endif %} + + {{ forum_topic_listing(profile_topics) }} + + {% if sp is not empty %} + {{ sp|raw }} + {% endif %} +
+{% endblock %} diff --git a/templates/user/macros.twig b/templates/user/macros.twig index b8f2fa4e..017cb2c1 100644 --- a/templates/user/macros.twig +++ b/templates/user/macros.twig @@ -58,7 +58,7 @@ {% endif %} {% if user.user_count_topics|default(0) > 0 %} - +
Topics
@@ -69,7 +69,7 @@ {% endif %} {% if user.user_count_posts|default(0) > 0 %} -
+
Posts