2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2024-06-21 17:29:26 +00:00
|
|
|
use function MicroHTML\LINK;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class BrowserSearch extends Extension
|
|
|
|
{
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string("search_suggestions_results_order", 'a');
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $database, $page;
|
|
|
|
|
|
|
|
// Add in header code to let the browser know that the search plugin exists
|
|
|
|
// We need to build the data for the header
|
2019-08-02 19:40:03 +00:00
|
|
|
$search_title = $config->get_string(SetupConfig::TITLE);
|
2020-02-04 01:45:45 +00:00
|
|
|
$search_file_url = make_link('browser_search.xml');
|
2024-06-21 17:29:26 +00:00
|
|
|
$page->add_html_header(LINK([
|
|
|
|
'rel' => 'search',
|
|
|
|
'type' => 'application/opensearchdescription+xml',
|
|
|
|
'title' => $search_title,
|
|
|
|
'href' => $search_file_url
|
|
|
|
]));
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// The search.xml file that is generated on the fly
|
2020-02-04 01:45:45 +00:00
|
|
|
if ($event->page_matches("browser_search.xml")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
// First, we need to build all the variables we'll need
|
2019-08-02 19:40:03 +00:00
|
|
|
$search_title = $config->get_string(SetupConfig::TITLE);
|
2023-08-18 12:38:55 +00:00
|
|
|
$search_form_url = search_link(['{searchTerms}']);
|
2019-05-28 16:59:38 +00:00
|
|
|
$suggenton_url = make_link('browser_search/')."{searchTerms}";
|
2024-02-20 00:22:25 +00:00
|
|
|
$icon_b64 = base64_encode(\Safe\file_get_contents("ext/static_files/static/favicon.ico"));
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// Now for the XML
|
|
|
|
$xml = "
|
2007-10-27 22:17:53 +00:00
|
|
|
<SearchPlugin xmlns='http://www.mozilla.org/2006/browser/search/' xmlns:os='http://a9.com/-/spec/opensearch/1.1/'>
|
|
|
|
<os:ShortName>$search_title</os:ShortName>
|
|
|
|
<os:InputEncoding>UTF-8</os:InputEncoding>
|
2009-07-19 17:04:52 +00:00
|
|
|
<os:Image width='16' height='16'>data:image/x-icon;base64,$icon_b64</os:Image>
|
2007-10-27 22:17:53 +00:00
|
|
|
<SearchForm>$search_form_url</SearchForm>
|
|
|
|
<os:Url type='text/html' method='GET' template='$search_form_url'>
|
|
|
|
<os:Param name='search' value='{searchTerms}'/>
|
|
|
|
</os:Url>
|
|
|
|
<Url type='application/x-suggestions+json' template='$suggenton_url'/>
|
|
|
|
</SearchPlugin>
|
|
|
|
";
|
2009-01-04 19:18:37 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// And now to send it to the browser
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2020-06-14 16:05:55 +00:00
|
|
|
$page->set_mime(MimeType::XML);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_data($xml);
|
2024-02-11 11:34:09 +00:00
|
|
|
} elseif ($event->page_matches("browser_search/{tag_search}")) {
|
2020-02-04 01:45:45 +00:00
|
|
|
$suggestions = $config->get_string("search_suggestions_results_order");
|
|
|
|
if ($suggestions == "n") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// We have to build some json stuff
|
2024-02-11 11:34:09 +00:00
|
|
|
$tag_search = $event->get_arg('tag_search');
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// Now to get DB results
|
2020-02-04 01:45:45 +00:00
|
|
|
if ($suggestions == "a") {
|
|
|
|
$order = "tag ASC";
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2020-02-04 01:45:45 +00:00
|
|
|
$order = "count DESC";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2020-02-04 01:45:45 +00:00
|
|
|
$tags = $database->get_col(
|
|
|
|
"SELECT tag FROM tags WHERE tag LIKE :tag AND count > 0 ORDER BY $order LIMIT 30",
|
2023-11-11 21:49:12 +00:00
|
|
|
['tag' => $tag_search."%"]
|
2020-02-04 01:45:45 +00:00
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// And to do stuff with it. We want our output to look like:
|
|
|
|
// ["shimmie",["shimmies","shimmy","shimmie","21 shimmies","hip shimmies","skea shimmies"],[],[]]
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2024-02-20 00:22:25 +00:00
|
|
|
$page->set_data(\Safe\json_encode([$tag_search, $tags, [], []]));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$sort_by = [];
|
|
|
|
$sort_by['Alphabetical'] = 'a';
|
|
|
|
$sort_by['Tag Count'] = 't';
|
2020-02-04 01:45:45 +00:00
|
|
|
$sort_by['Disabled'] = 'n';
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Browser Search");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_choice_option("search_suggestions_results_order", $sort_by, "Sort the suggestions by:");
|
|
|
|
}
|
2007-10-27 22:17:53 +00:00
|
|
|
}
|