feat: add small search hooks (to be improved later, presumably)

This commit is contained in:
David Bailey 2025-01-06 22:44:09 +01:00
parent 2fe4d20187
commit fc0d38e118
2 changed files with 49 additions and 1 deletions

View file

@ -17,10 +17,13 @@ if(isset($REQUEST_QUERY['page'])) {
$ajax_args['page'] = $adapter->get_post($REQUEST_QUERY['page']);
}
if(isset($REQUEST_QUERY['search'])) {
$ajax_args['search_results'] = $post->handler->search_posts($REQUEST_QUERY['search']);
}
$ajax_args['fa'] = $FONT_AWESOME_ARRAY;
$ajax_args['page'] ??= $SITE_CONFIG['site_defaults'];
$ajax_args['post'] ??= $ajax_args['page'];
echo $twig->render($AJAX_REQUEST_TEMPLATE, $ajax_args);

View file

@ -57,6 +57,51 @@ if($REQUEST_PATH == '/upload') {
render_root_template('upload.html');
die();
}
if($REQUEST_PATH == '/search') {
$search_results = [];
$display_type = 'none';
$search_query = [
'user_input' => $_GET['query'] ?? null,
'user_type' => $_GET['search_type'] ?? 'all',
'types' => [['All', 'all'], ['Images', 'image'], ['Blog Posts', 'blog']]
];
if(isset($_GET['query']) && strlen($_GET['query']) > 0) {
$search_request['query'] = $_GET['query'];
if(isset($_GET['search_type']) && $_GET['search_type'] != 'all') {
$search_request['tags'] []= 'type:' . $_GET['search_type'];
}
$search_results = $adapter->search_posts($search_request);
$type_count = [];
if(count($search_results) > 0) {
foreach($search_results as $result) {
$type_count[$result['type']] ??= 0;
$type_count[$result['type']] += 1;
}
$display_type = array_search(max($type_count), $type_count);
}
} else {
$display_type = 'no_search';
}
$search_page = $SITE_CONFIG['site_defaults'];
$search_page['path'] = '/search';
$search_page['title'] = 'Search';
$search_page['basename'] = 'search';
render_root_template('search.html', [
'search_results' => $search_results,
'page' => $search_page,
'display_type' => $display_type,
'search_query' => $search_query
]);
die();
}
$post = $adapter->get_post($REQUEST_PATH);
render_post($post);