From 9704bd3b5443c26450a3c49581ff8ed8b7858a48 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 1 May 2019 22:31:59 +0200 Subject: [PATCH] Update topic look on search page. --- assets/less/search/anchor.less | 4 +- public/search.php | 9 +--- src/Forum/topic.php | 81 ++++++++++++++++++++++++++++++++++ templates/home/search.twig | 18 +++----- 4 files changed, 90 insertions(+), 22 deletions(-) diff --git a/assets/less/search/anchor.less b/assets/less/search/anchor.less index f12f980a..6dd83b6d 100644 --- a/assets/less/search/anchor.less +++ b/assets/less/search/anchor.less @@ -1,4 +1,4 @@ .search__anchor { - position: absolute; - top: -100px; + position: relative; + top: -94px; } diff --git a/public/search.php b/public/search.php index 51809b3c..1b192649 100644 --- a/public/search.php +++ b/public/search.php @@ -4,14 +4,7 @@ require_once '../misuzu.php'; $searchQuery = !empty($_GET['q']) && is_string($_GET['q']) ? $_GET['q'] : ''; if (!empty($searchQuery)) { - $findForumTopics = db_prepare(' - SELECT `topic_id`, `topic_title` - FROM `msz_forum_topics` - WHERE MATCH(`topic_title`) - AGAINST (:query IN NATURAL LANGUAGE MODE); - '); - $findForumTopics->bindValue('query', $searchQuery); - $forumTopics = db_fetch_all($findForumTopics); + $forumTopics = forum_topic_listing_search($searchQuery, user_session_current('user_id', 0)); $findForumPosts = db_prepare(' SELECT fp.`post_id`, fp.`post_text`, ft.`topic_title`, u.`username` diff --git a/src/Forum/topic.php b/src/Forum/topic.php index 05554f66..fdc85dda 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -383,6 +383,87 @@ function forum_topic_listing_user(int $authorId, int $userId, int $offset = 0, i return db_fetch_all($getTopics); } +function forum_topic_listing_search(string $query, int $userId): array +{ + $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` + AND `post_deleted` IS NULL + ) AS `topic_count_posts`, + ( + SELECT CEIL(COUNT(`post_id`) / %2$d) + FROM `msz_forum_posts` + WHERE `topic_id` = t.`topic_id` + AND `post_deleted` IS NULL + ) 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` + AND `post_deleted` IS NULL + 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 MATCH(`topic_title`) + AGAINST (:query IN NATURAL LANGUAGE MODE) + AND t.`topic_deleted` IS NULL + ORDER BY FIELD(t.`topic_type`, %1$s) DESC, t.`topic_bumped` DESC + ', + implode(',', array_reverse(MSZ_TOPIC_TYPE_ORDER)), + MSZ_FORUM_POSTS_PER_PAGE + )); + $getTopics->bindValue('query', $query); + $getTopics->bindValue('user_id', $userId); + + return db_fetch_all($getTopics); +} + function forum_topic_lock(int $topicId): bool { if ($topicId < 1) { diff --git a/templates/home/search.twig b/templates/home/search.twig index 310e8025..ced27017 100644 --- a/templates/home/search.twig +++ b/templates/home/search.twig @@ -2,6 +2,7 @@ {% from 'macros.twig' import container_title %} {% from '_layout/input.twig' import input_text %} {% from 'user/macros.twig' import user_card %} +{% from 'forum/macros.twig' import forum_topic_listing %} {% set title = search_query|length < 1 ? 'Search' : 'Looking for ' ~ search_query %} {% set canonical_url = url('search-query', {'query': search_query}) %} @@ -68,20 +69,13 @@ {% endif %} {% if forum_topics|length > 0 %} -
-
- {{ container_title(' Topics (%s)'|format(forum_topics|length|number_format)) }} - {% for topic in forum_topics %} - - {{ topic.topic_title }} -
- {% endfor %} -
+
+ {{ forum_topic_listing(forum_topics) }} {% endif %} {% if forum_posts|length > 0 %} +
-
{{ container_title(' Posts (%s)'|format(forum_posts|length|number_format)) }} {% for post in forum_posts %} @@ -93,8 +87,8 @@ {% endif %} {% if users|length > 0 %} +
-
{{ container_title(' Users (%s)'|format(users|length|number_format)) }}
@@ -108,8 +102,8 @@ {% endif %} {% if news_posts|length > 0 %} +
-
{{ container_title(' News (%s)'|format(news_posts|length|number_format)) }} {% for post in news_posts %}