2015-02-01 00:47:09 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class AutoComplete extends Extension
|
|
|
|
{
|
|
|
|
public function get_priority(): int
|
|
|
|
{
|
|
|
|
return 30;
|
|
|
|
} // before Home
|
|
|
|
|
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2019-10-02 09:49:32 +00:00
|
|
|
global $cache, $page, $database;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
if ($event->page_matches("api/internal/autocomplete")) {
|
|
|
|
if (!isset($_GET["s"])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_type("application/json");
|
|
|
|
|
|
|
|
$s = strtolower($_GET["s"]);
|
|
|
|
if (
|
|
|
|
$s == '' ||
|
|
|
|
$s[0] == '_' ||
|
|
|
|
$s[0] == '%' ||
|
|
|
|
strlen($s) > 32
|
|
|
|
) {
|
|
|
|
$page->set_data("{}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//$limit = 0;
|
|
|
|
$cache_key = "autocomplete-$s";
|
|
|
|
$limitSQL = "";
|
2019-09-29 13:30:55 +00:00
|
|
|
$s = str_replace('_', '\_', $s);
|
|
|
|
$s = str_replace('%', '\%', $s);
|
2019-07-06 10:27:05 +00:00
|
|
|
$SQLarr = ["search"=>"$s%"]; #, "cat_search"=>"%:$s%"];
|
2019-05-28 16:59:38 +00:00
|
|
|
if (isset($_GET["limit"]) && $_GET["limit"] !== 0) {
|
|
|
|
$limitSQL = "LIMIT :limit";
|
|
|
|
$SQLarr['limit'] = $_GET["limit"];
|
|
|
|
$cache_key .= "-" . $_GET["limit"];
|
|
|
|
}
|
|
|
|
|
2019-10-02 09:49:32 +00:00
|
|
|
$res = $cache->get($cache_key);
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!$res) {
|
|
|
|
$res = $database->get_pairs(
|
|
|
|
$database->scoreql_to_sql("
|
2016-06-18 12:19:25 +00:00
|
|
|
SELECT tag, count
|
|
|
|
FROM tags
|
2019-12-15 16:07:46 +00:00
|
|
|
WHERE LOWER(tag) LIKE LOWER(:search)
|
|
|
|
-- OR LOWER(tag) LIKE LOWER(:cat_search)
|
2016-06-18 12:19:25 +00:00
|
|
|
AND count > 0
|
|
|
|
ORDER BY count DESC
|
2019-05-28 16:59:38 +00:00
|
|
|
$limitSQL"),
|
|
|
|
$SQLarr
|
|
|
|
);
|
2019-10-02 09:49:32 +00:00
|
|
|
$cache->set($cache_key, $res, 600);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2015-02-01 00:47:09 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_data(json_encode($res));
|
|
|
|
}
|
2015-02-01 00:47:09 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->build_autocomplete($page);
|
|
|
|
}
|
2015-02-01 00:47:09 +00:00
|
|
|
}
|