misuzu/public-legacy/search.php

98 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
2023-07-15 23:58:17 +00:00
use RuntimeException;
2023-07-15 17:02:46 +00:00
use Misuzu\Comments\CommentsCategory;
if(!$msz->isLoggedIn()) {
echo render_error(403);
return;
}
2022-09-13 13:14:49 +00:00
$searchQuery = !empty($_GET['q']) && is_string($_GET['q']) ? $_GET['q'] : '';
if(!empty($searchQuery)) {
$forumTopics = forum_topic_listing_search($searchQuery, $msz->getActiveUser()->getId());
2022-09-13 13:14:49 +00:00
$forumPosts = forum_post_search($searchQuery);
2023-07-15 17:02:46 +00:00
// this sure is an expansion
$news = $msz->getNews();
$users = $msz->getUsers();
2023-07-15 23:58:17 +00:00
$comments = $msz->getComments();
2023-07-15 17:02:46 +00:00
$newsPosts = [];
$newsPostInfos = $news->getPosts(searchQuery: $searchQuery);
2023-07-15 17:02:46 +00:00
$newsUserInfos = [];
$newsUserColours = [];
2023-07-15 17:02:46 +00:00
$newsCategoryInfos = [];
foreach($newsPostInfos as $postInfo) {
$userId = $postInfo->getUserId();
$categoryId = $postInfo->getCategoryId();
if(array_key_exists($userId, $newsUserInfos)) {
$userInfo = $newsUserInfos[$userId];
} else {
try {
$userInfo = $users->getUser($userId, 'id');
$newsUserColours[$userId] = $users->getUserColour($userInfo);
} catch(RuntimeException $ex) {
2023-07-15 17:02:46 +00:00
$userInfo = null;
}
$newsUserInfos[$userId] = $userInfo;
}
if(array_key_exists($categoryId, $newsCategoryInfos))
$categoryInfo = $newsCategoryInfos[$categoryId];
else
$newsCategoryInfos[$categoryId] = $categoryInfo = $news->getCategory(postInfo: $postInfo);
2023-07-15 17:02:46 +00:00
2023-07-15 23:58:17 +00:00
$commentsCount = $postInfo->hasCommentsCategoryId()
? $comments->countPosts(categoryInfo: $postInfo->getCommentsCategoryId(), deleted: false) : 0;
2023-07-15 17:02:46 +00:00
$newsPosts[] = [
'post' => $postInfo,
'category' => $categoryInfo,
'user' => $userInfo,
'user_colour' => $newsUserColours[$userId] ?? \Index\Colour\Colour::none(),
2023-07-15 17:02:46 +00:00
'comments_count' => $commentsCount,
];
}
2022-09-13 13:14:49 +00:00
$findUsers = DB::prepare('
SELECT u.`user_id`, u.`username`, u.`user_country`,
u.`user_created`, u.`user_active`, r.`role_id`,
COALESCE(u.`user_title`, r.`role_title`) AS `user_title`,
COALESCE(u.`user_colour`, r.`role_colour`) AS `user_colour`,
(
SELECT COUNT(`topic_id`)
FROM `msz_forum_topics`
WHERE `user_id` = u.`user_id`
AND `topic_deleted` IS NULL
) AS `user_count_topics`,
(
SELECT COUNT(`post_Id`)
FROM `msz_forum_posts`
WHERE `user_id` = u.`user_id`
AND `post_deleted` IS NULL
) AS `user_count_posts`
FROM `msz_users` AS u
LEFT JOIN `msz_roles` AS r
ON r.`role_id` = u.`display_role`
LEFT JOIN `msz_users_roles` AS ur
2022-09-13 13:14:49 +00:00
ON ur.`user_id` = u.`user_id`
WHERE u.`username` LIKE CONCAT("%%", :query, "%%")
2022-09-13 13:14:49 +00:00
GROUP BY u.`user_id`
');
$findUsers->bind('query', $searchQuery);
$userList = $findUsers->fetchAll();
2022-09-13 13:14:49 +00:00
}
Template::render('home.search', [
'search_query' => $searchQuery,
'forum_topics' => $forumTopics ?? [],
'forum_posts' => $forumPosts ?? [],
'users' => $userList ?? [],
2022-09-13 13:14:49 +00:00
'news_posts' => $newsPosts ?? [],
]);