Added basic search page.
This commit is contained in:
parent
9d5376926f
commit
85c41b4d86
4 changed files with 121 additions and 0 deletions
53
public/search.php
Normal file
53
public/search.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
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);
|
||||
|
||||
$findForumPosts = db_prepare('
|
||||
SELECT fp.`post_id`, fp.`post_text`, ft.`topic_title`, u.`username`
|
||||
FROM `msz_forum_posts` AS fp
|
||||
LEFT JOIN `msz_forum_topics` AS ft
|
||||
ON ft.`topic_id` = fp.`topic_id`
|
||||
LEFT JOIN `msz_users` AS u
|
||||
ON u.`user_id` = fp.`user_id`
|
||||
WHERE MATCH(fp.`post_text`)
|
||||
AGAINST (:query IN NATURAL LANGUAGE MODE);
|
||||
');
|
||||
$findForumPosts->bindValue('query', $searchQuery);
|
||||
$forumPosts = db_fetch_all($findForumPosts);
|
||||
|
||||
$findUsers = db_prepare('
|
||||
SELECT `user_id`, `username`
|
||||
FROM `msz_users`
|
||||
WHERE LOWER(`username`) LIKE CONCAT("%", LOWER(:query), "%");
|
||||
');
|
||||
$findUsers->bindValue('query', $searchQuery);
|
||||
$users = db_fetch_all($findUsers);
|
||||
|
||||
$findNewsPosts = db_prepare('
|
||||
SELECT `post_id`, `post_title`, `post_text`
|
||||
FROM `msz_news_posts`
|
||||
WHERE MATCH(`post_title`, `post_text`)
|
||||
AGAINST (:query IN NATURAL LANGUAGE MODE);
|
||||
');
|
||||
$findNewsPosts->bindValue('query', $searchQuery);
|
||||
$newsPosts = db_fetch_all($findNewsPosts);
|
||||
}
|
||||
|
||||
echo tpl_render('home.search', [
|
||||
'search_query' => $searchQuery,
|
||||
'forum_topics' => $forumTopics ?? [],
|
||||
'forum_posts' => $forumPosts ?? [],
|
||||
'users' => $users ?? [],
|
||||
'news_posts' => $newsPosts ?? [],
|
||||
]);
|
|
@ -12,6 +12,9 @@ define('MSZ_URLS', [
|
|||
'info' => ['/info.php/<title>'],
|
||||
'media-proxy' => ['/proxy.php/<hash>/<url>'],
|
||||
|
||||
'search-index' => ['/search.php'],
|
||||
'search-query' => ['/search.php', ['q' => '<query>', 't' => '<type>']],
|
||||
|
||||
'auth-login' => ['/auth/login.php', ['username' => '<username>', 'redirect' => '<redirect>']],
|
||||
'auth-login-welcome' => ['/auth/login.php', ['welcome' => '1', 'username' => '<username>']],
|
||||
'auth-register' => ['/auth/register.php'],
|
||||
|
|
|
@ -54,6 +54,12 @@
|
|||
'url': url('settings-index'),
|
||||
'icon': 'fas fa-cog fa-fw',
|
||||
},
|
||||
{
|
||||
'title': 'Search',
|
||||
'url': url('search-index'),
|
||||
'icon': 'fas fa-search fa-fw',
|
||||
'display': current_user.general_perms|default(0)|perms_check(constant('MSZ_PERM_GENERAL_TESTER'))
|
||||
},
|
||||
{
|
||||
'title': 'Return to Site',
|
||||
'url': site_link|default(url('index')),
|
||||
|
|
59
templates/home/search.twig
Normal file
59
templates/home/search.twig
Normal file
|
@ -0,0 +1,59 @@
|
|||
{% extends 'home/master.twig' %}
|
||||
{% from 'macros.twig' import container_title %}
|
||||
{% from '_layout/input.twig' import input_text %}
|
||||
|
||||
{% set canonical_url = url('search-query', {'query': search_query}) %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container container--lazy">
|
||||
{{ container_title('Search') }}
|
||||
<form method="get" action="{{ url('search-index') }}" style="margin:0">
|
||||
{{ input_text('q', '', search_query, 'text', 'What are you looking for?', false, null, 1, true) }}
|
||||
<button class="input__button" tabindex="2">Search</button>
|
||||
</form>
|
||||
<a href="#topics" class="link">Topics ({{ forum_topics|length|number_format }})</a>
|
||||
<a href="#posts" class="link">Posts ({{ forum_posts|length|number_format }})</a>
|
||||
<a href="#users" class="link">Users ({{ users|length|number_format }})</a>
|
||||
<a href="#news" class="link">News ({{ news_posts|length|number_format }})</a>
|
||||
</div>
|
||||
|
||||
{% if search_query|length > 0 %}
|
||||
<div id="topics" class="container container--lazy">
|
||||
{{ container_title('Topics (%s)'|format(forum_topics|length|number_format)) }}
|
||||
{% for topic in forum_topics %}
|
||||
<a href="{{ url('forum-topic', {'topic': topic.topic_id}) }}" class="link">
|
||||
{{ topic.topic_title }}
|
||||
</a><br>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<div id="posts" class="container container--lazy">
|
||||
{{ container_title('Posts (%s)'|format(forum_posts|length|number_format)) }}
|
||||
|
||||
{% for post in forum_posts %}
|
||||
<a href="{{ url('forum-post', {'post': post.post_id, 'post_fragment': 'p' ~ post.post_id}) }}" class="link">
|
||||
Reply by {{ post.username }} in "{{ post.topic_title }}"
|
||||
</a><br>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<div id="users" class="container container--lazy">
|
||||
{{ container_title('Users (%s)'|format(users|length|number_format)) }}
|
||||
{% for user in users %}
|
||||
<a href="{{ url('user-profile', {'user': user.user_id}) }}" class="link">
|
||||
{{ user.username }}
|
||||
</a><br>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<div id="news" class="container container--lazy">
|
||||
{{ container_title('News (%s)'|format(news_posts|length|number_format)) }}
|
||||
{% for post in news_posts %}
|
||||
<a href="{{ url('news-post', {'post': post.post_id}) }}" class="link">
|
||||
{{ post.post_title }}
|
||||
</a><br>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endblock %}
|
||||
|
Loading…
Add table
Reference in a new issue