extract autocomplete code for later api-isation
This commit is contained in:
parent
b03880c11d
commit
3a9fd38cb0
@ -12,58 +12,66 @@ class AutoComplete extends Extension
|
|||||||
|
|
||||||
public function onPageRequest(PageRequestEvent $event)
|
public function onPageRequest(PageRequestEvent $event)
|
||||||
{
|
{
|
||||||
global $cache, $page, $database;
|
global $page;
|
||||||
|
|
||||||
if ($event->page_matches("api/internal/autocomplete")) {
|
if ($event->page_matches("api/internal/autocomplete")) {
|
||||||
if (!isset($_GET["s"])) {
|
$limit = $_GET["limit"] ?? 0;
|
||||||
return;
|
$s = $_GET["s"] ?? null;
|
||||||
}
|
|
||||||
|
$res = $this->complete($s, $limit);
|
||||||
|
|
||||||
$page->set_mode(PageMode::DATA);
|
$page->set_mode(PageMode::DATA);
|
||||||
$page->set_mime(MimeType::JSON);
|
$page->set_mime(MimeType::JSON);
|
||||||
|
$page->set_data(json_encode($res));
|
||||||
$s = strtolower($_GET["s"]);
|
|
||||||
if (
|
|
||||||
$s == '' ||
|
|
||||||
$s[0] == '_' ||
|
|
||||||
$s[0] == '%' ||
|
|
||||||
strlen($s) > 32
|
|
||||||
) {
|
|
||||||
$page->set_data("{}");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//$limit = 0;
|
$this->theme->build_autocomplete($page);
|
||||||
$cache_key = "autocomplete-$s";
|
}
|
||||||
|
|
||||||
|
private function complete(string $search, int $limit): array {
|
||||||
|
global $cache, $database;
|
||||||
|
|
||||||
|
if (!$search) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$search = strtolower($search);
|
||||||
|
if (
|
||||||
|
$search == '' ||
|
||||||
|
$search[0] == '_' ||
|
||||||
|
$search[0] == '%' ||
|
||||||
|
strlen($search) > 32
|
||||||
|
) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache_key = "autocomplete-$search";
|
||||||
$limitSQL = "";
|
$limitSQL = "";
|
||||||
$s = str_replace('_', '\_', $s);
|
$search = str_replace('_', '\_', $search);
|
||||||
$s = str_replace('%', '\%', $s);
|
$search = str_replace('%', '\%', $search);
|
||||||
$SQLarr = ["search"=>"$s%"]; #, "cat_search"=>"%:$s%"];
|
$SQLarr = ["search"=>"$search%"]; #, "cat_search"=>"%:$search%"];
|
||||||
if (isset($_GET["limit"]) && $_GET["limit"] !== 0) {
|
if ($limit !== 0) {
|
||||||
$limitSQL = "LIMIT :limit";
|
$limitSQL = "LIMIT :limit";
|
||||||
$SQLarr['limit'] = $_GET["limit"];
|
$SQLarr['limit'] = $limit;
|
||||||
$cache_key .= "-" . $_GET["limit"];
|
$cache_key .= "-" . $limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$res = $cache->get($cache_key);
|
$res = $cache->get($cache_key);
|
||||||
if (!$res) {
|
if (!$res) {
|
||||||
$res = $database->get_pairs(
|
$res = $database->get_pairs("
|
||||||
"
|
|
||||||
SELECT tag, count
|
SELECT tag, count
|
||||||
FROM tags
|
FROM tags
|
||||||
WHERE LOWER(tag) LIKE LOWER(:search)
|
WHERE LOWER(tag) LIKE LOWER(:search)
|
||||||
-- OR LOWER(tag) LIKE LOWER(:cat_search)
|
-- OR LOWER(tag) LIKE LOWER(:cat_search)
|
||||||
AND count > 0
|
AND count > 0
|
||||||
ORDER BY count DESC
|
ORDER BY count DESC
|
||||||
$limitSQL",
|
$limitSQL
|
||||||
|
",
|
||||||
$SQLarr
|
$SQLarr
|
||||||
);
|
);
|
||||||
$cache->set($cache_key, $res, 600);
|
$cache->set($cache_key, $res, 600);
|
||||||
}
|
}
|
||||||
|
|
||||||
$page->set_data(json_encode($res));
|
return $res;
|
||||||
}
|
|
||||||
|
|
||||||
$this->theme->build_autocomplete($page);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user