diff --git a/ServiceAPI/Search.php b/ServiceAPI/Search.php new file mode 100644 index 00000000..46def4f4 --- /dev/null +++ b/ServiceAPI/Search.php @@ -0,0 +1,76 @@ +user = $user; + $this->users = new Users; + $this->clubs = new Clubs; + $this->videos = new Videos; + } + + function fastSearch(string $query, string $type = "users", callable $resolve, callable $reject) + { + if($query == "" || strlen($query) < 3) + $reject(12, "No input or input < 3"); + + $repo; + $sort; + + switch($type) { + default: + case "users": + $repo = (new Users); + $sort = "rating DESC"; + + break; + case "groups": + $repo = (new Clubs); + $sort = "id ASC"; + + break; + case "videos": + $repo = (new Videos); + $sort = "created ASC"; + + break; + } + + $res = $repo->find($query, ["doNotSearchMe" => $this->user->getId()], $sort); + + $results = array_slice(iterator_to_array($res), 0, 5); + + $count = sizeof($results); + + $arr = [ + "count" => $count, + "items" => [] + ]; + + if(sizeof($results) < 1) { + $reject(2, "No results"); + } + + foreach($results as $res) { + $arr["items"][] = [ + "id" => $res->getId(), + "name" => $type == "users" ? $res->getCanonicalName() : $res->getName(), + "avatar" => $type != "videos" ? $res->getAvatarUrl() : $res->getThumbnailURL(), + "url" => $type != "videos" ? $res->getUrl() : "/video".$res->getPrettyId(), + "description" => ovk_proc_strtr($res->getDescription() ?? "...", 40) + ]; + } + + $resolve($arr); + } +} diff --git a/Web/Models/Repositories/Users.php b/Web/Models/Repositories/Users.php index 0eec3306..6c165aa3 100644 --- a/Web/Models/Repositories/Users.php +++ b/Web/Models/Repositories/Users.php @@ -58,7 +58,7 @@ class Users $nnparamsCount = 0; foreach($pars as $paramName => $paramValue) - if($paramName != "before" && $paramName != "after" && $paramName != "gender" && $paramName != "maritalstatus" && $paramName != "politViews") + if($paramName != "before" && $paramName != "after" && $paramName != "gender" && $paramName != "maritalstatus" && $paramName != "politViews" && $paramName != "doNotSearchMe") $paramValue != NULL ? $notNullParams += ["$paramName" => "%$paramValue%"] : NULL; else $paramValue != NULL ? $notNullParams += ["$paramName" => "$paramValue"] : NULL; @@ -125,6 +125,9 @@ class Users case "gender": $result->where("sex ?", $paramValue); break; + case "doNotSearchMe": + $result->where("id !=", $paramValue); + break; } } } diff --git a/Web/Presenters/templates/@layout.xml b/Web/Presenters/templates/@layout.xml index 7bb7e194..cd23d47a 100644 --- a/Web/Presenters/templates/@layout.xml +++ b/Web/Presenters/templates/@layout.xml @@ -93,8 +93,8 @@ {if !$atSearch}
+