diff --git a/Web/Models/Repositories/Applications.php b/Web/Models/Repositories/Applications.php index 3aa6e5be..0687856e 100644 --- a/Web/Models/Repositories/Applications.php +++ b/Web/Models/Repositories/Applications.php @@ -66,4 +66,12 @@ class Applications { return sizeof($this->appRels->where("user", $user->getId())); } + + function find(string $query, array $pars = [], string $sort = "id"): Util\EntityStream + { + $query = "%$query%"; + $result = $this->apps->where("CONCAT_WS(' ', name, description) LIKE ?", $query)->where("enabled", 1); + + return new Util\EntityStream("Application", $result->order("$sort")); + } } \ No newline at end of file diff --git a/Web/Models/Repositories/Clubs.php b/Web/Models/Repositories/Clubs.php index 8e97f3eb..04bb30ab 100644 --- a/Web/Models/Repositories/Clubs.php +++ b/Web/Models/Repositories/Clubs.php @@ -43,12 +43,12 @@ class Clubs return $this->toClub($this->clubs->get($id)); } - function find(string $query, int $page = 1, ?int $perPage = NULL): \Traversable + function find(string $query, array $pars = [], string $sort = "id DESC", int $page = 1, ?int $perPage = NULL): \Traversable { $query = "%$query%"; $result = $this->clubs->where("name LIKE ? OR about LIKE ?", $query, $query); - return new Util\EntityStream("Club", $result); + return new Util\EntityStream("Club", $result->order($sort)); } function getCount(): int diff --git a/Web/Models/Repositories/Comments.php b/Web/Models/Repositories/Comments.php index 4774cf35..4b4dfc57 100644 --- a/Web/Models/Repositories/Comments.php +++ b/Web/Models/Repositories/Comments.php @@ -59,4 +59,27 @@ class Comments "deleted" => false, ])); } + + function find(string $query = "", array $pars = [], string $sort = "id"): Util\EntityStream + { + $query = "%$query%"; + + $notNullParams = []; + + foreach($pars as $paramName => $paramValue) { + if($paramName != "before" && $paramName != "after") + $paramValue != NULL ? $notNullParams+=["$paramName" => "%$paramValue%"] : NULL; + else + $paramValue != NULL ? $notNullParams+=["$paramName" => "$paramValue"] : NULL; + } + + $result = $this->comments->where("content LIKE ?", $query)->where("deleted", 0); + $nnparamsCount = sizeof($notNullParams); + + if($nnparamsCount > 0) { + !is_null($notNullParams["before"]) ? $result->where("created < ?", $notNullParams["before"]) : NULL; + !is_null($notNullParams["after"]) ? $result->where("created > ?", $notNullParams["after"]) : NULL; + } + return new Util\EntityStream("Comment", $result->order("$sort")); + } } diff --git a/Web/Models/Repositories/Posts.php b/Web/Models/Repositories/Posts.php index 7aadc0bb..8056264a 100644 --- a/Web/Models/Repositories/Posts.php +++ b/Web/Models/Repositories/Posts.php @@ -99,7 +99,30 @@ class Posts return NULL; } - + + function find(string $query = "", array $pars = [], string $sort = "id"): Util\EntityStream + { + $query = "%$query%"; + + $notNullParams = []; + + foreach($pars as $paramName => $paramValue) { + if($paramName != "before" && $paramName != "after") + $paramValue != NULL ? $notNullParams+=["$paramName" => "%$paramValue%"] : NULL; + else + $paramValue != NULL ? $notNullParams+=["$paramName" => "$paramValue"] : NULL; + } + + $result = $this->posts->where("content LIKE ?", $query)->where("deleted", 0); + $nnparamsCount = sizeof($notNullParams); + + if($nnparamsCount > 0) { + !is_null($notNullParams["before"]) ? $result->where("created < ?", $notNullParams["before"]) : NULL; + !is_null($notNullParams["after"]) ? $result->where("created > ?", $notNullParams["after"]) : NULL; + } + return new Util\EntityStream("Post", $result->order("$sort")); + } + function getPostCountOnUserWall(int $user): int { return sizeof($this->posts->where(["wall" => $user, "deleted" => 0])); diff --git a/Web/Models/Repositories/Users.php b/Web/Models/Repositories/Users.php index 63e77df0..c98c5a47 100644 --- a/Web/Models/Repositories/Users.php +++ b/Web/Models/Repositories/Users.php @@ -49,12 +49,46 @@ class Users return $this->toUser($this->users->where("user", $user->getId())->fetch()); } - function find(string $query): Util\EntityStream + function find(string $query, array $pars = [], string $sort = "id DESC"): Util\EntityStream { $query = "%$query%"; $result = $this->users->where("CONCAT_WS(' ', first_name, last_name, pseudo, shortcode) LIKE ?", $query)->where("deleted", 0); - return new Util\EntityStream("User", $result); + $notNullParams = []; + $nnparamsCount = 0; + + foreach($pars as $paramName => $paramValue) { + if($paramName != "before" && $paramName != "after" && $paramName != "gender") + $paramValue != NULL ? $notNullParams += ["$paramName" => "%$paramValue%"] : NULL; + else + $paramValue != NULL ? $notNullParams += ["$paramName" => "$paramValue"] : NULL; + } + + $nnparamsCount = sizeof($notNullParams); + + if($nnparamsCount > 0) { + !is_null($notNullParams["hometown"]) ? $result->where("hometown LIKE ?", $notNullParams["hometown"]) : NULL; + !is_null($notNullParams["city"]) ? $result->where("city LIKE ?", $notNullParams["city"]) : NULL; + !is_null($notNullParams["maritalstatus"]) ? $result->where("marital_status LIKE ?", $notNullParams["maritalstatus"]) : NULL; + !is_null($notNullParams["status"]) ? $result->where("status LIKE ?", $notNullParams["status"]) : NULL; + !is_null($notNullParams["politViews"]) ? $result->where("polit_views LIKE ?", $notNullParams["politViews"]) : NULL; + !is_null($notNullParams["email"]) ? $result->where("email_contact LIKE ?", $notNullParams["email"]) : NULL; + !is_null($notNullParams["telegram"]) ? $result->where("telegram LIKE ?", $notNullParams["telegram"]) : NULL; + !is_null($notNullParams["site"]) ? $result->where("website LIKE ?", $notNullParams["site"]) : NULL; + !is_null($notNullParams["address"]) ? $result->where("address LIKE ?", $notNullParams["address"]) : NULL; + !is_null($notNullParams["is_online"]) ? $result->where("online >= ?", time() - 900) : NULL; + !is_null($notNullParams["interests"]) ? $result->where("interests LIKE ?", $notNullParams["interests"]) : NULL; + !is_null($notNullParams["fav_mus"]) ? $result->where("fav_music LIKE ?", $notNullParams["fav_mus"]) : NULL; + !is_null($notNullParams["fav_films"]) ? $result->where("fav_films LIKE ?", $notNullParams["fav_films"]) : NULL; + !is_null($notNullParams["fav_shows"]) ? $result->where("fav_shows LIKE ?", $notNullParams["fav_shows"]) : NULL; + !is_null($notNullParams["fav_books"]) ? $result->where("fav_books LIKE ?", $notNullParams["fav_books"]) : NULL; + !is_null($notNullParams["fav_quote"]) ? $result->where("fav_quote LIKE ?", $notNullParams["fav_quote"]) : NULL; + !is_null($notNullParams["before"]) ? $result->where("UNIX_TIMESTAMP(since) < ?", $notNullParams["before"]) : NULL; + !is_null($notNullParams["after"]) ? $result->where("UNIX_TIMESTAMP(since) > ?", $notNullParams["after"]) : NULL; + !is_null($notNullParams["gender"]) ? $result->where("sex ?", $notNullParams["gender"]) : NULL; + # !is_null($notNullParams["has_avatar"]) ? $result->related(): NULL; + } + return new Util\EntityStream("User", $result->order($sort)); } function getStatistics(): object diff --git a/Web/Models/Repositories/Videos.php b/Web/Models/Repositories/Videos.php index 5701b44e..92ebc11c 100644 --- a/Web/Models/Repositories/Videos.php +++ b/Web/Models/Repositories/Videos.php @@ -45,4 +45,27 @@ class Videos { return sizeof($this->videos->where("owner", $user->getId())->where(["deleted" => 0, "unlisted" => 0])); } + + function find(string $query = "", array $pars = [], string $sort = "id"): Util\EntityStream + { + $query = "%$query%"; + + $notNullParams = []; + + foreach($pars as $paramName => $paramValue) { + if($paramName != "before" && $paramName != "after") + $paramValue != NULL ? $notNullParams+=["$paramName" => "%$paramValue%"] : NULL; + else + $paramValue != NULL ? $notNullParams+=["$paramName" => "$paramValue"] : NULL; + } + + $result = $this->videos->where("name OR description LIKE ?", $query)->where("deleted", 0); + $nnparamsCount = sizeof($notNullParams); + + if($nnparamsCount > 0) { + !is_null($notNullParams["before"]) ? $result->where("created < ?", $notNullParams["before"]) : NULL; + !is_null($notNullParams["after"]) ? $result->where("created > ?", $notNullParams["after"]) : NULL; + } + return new Util\EntityStream("Video", $result->order("$sort")); + } } diff --git a/Web/Presenters/SearchPresenter.php b/Web/Presenters/SearchPresenter.php index 2171297c..f029edc8 100644 --- a/Web/Presenters/SearchPresenter.php +++ b/Web/Presenters/SearchPresenter.php @@ -1,18 +1,28 @@ users = $users; - $this->clubs = $clubs; + $this->users = $users; + $this->clubs = $clubs; + $this->posts = new Posts; + $this->comments = new Comments; + $this->videos = new Videos; + $this->apps = new Applications; + $this->notes = new Notes; parent::__construct(); } @@ -21,6 +31,8 @@ final class SearchPresenter extends OpenVKPresenter { $query = $this->queryParam("query") ?? ""; $type = $this->queryParam("type") ?? "users"; + $sorter = $this->queryParam("sort") ?? "id"; + $invert = $this->queryParam("invert") == 1 ? "ASC" : "DESC"; $page = (int) ($this->queryParam("p") ?? 1); $this->willExecuteWriteAction(); @@ -28,11 +40,61 @@ final class SearchPresenter extends OpenVKPresenter $this->assertUserLoggedIn(); # https://youtu.be/pSAWM5YuXx8 - - $repos = [ "groups" => "clubs", "users" => "users" ]; + + $repos = [ + "groups" => "clubs", + "users" => "users", + "posts" => "posts", + "comments" => "comments", + "videos" => "videos", + "audios" => "posts", + "apps" => "apps", + "notes" => "notes" + ]; + + switch($sorter) { + default: + case "id": + $sort = "id " . $invert; + break; + case "random": + $sort = "RAND()"; + break; + case "name": + $sort = "first_name " . $invert; + break; + case "rating": + $sort = "rating " . $invert; + break; + } + + $parameters = [ + "type" => $this->queryParam("type"), + "city" => $this->queryParam("city") != "" ? $this->queryParam("city") : NULL, + "maritalstatus" => $this->queryParam("maritalstatus") != 0 ? $this->queryParam("maritalstatus") : NULL, + "with_photo" => $this->queryParam("with_photo"), + "status" => $this->queryParam("status") != "" ? $this->queryParam("status") : NULL, + "politViews" => $this->queryParam("politViews") != 0 ? $this->queryParam("politViews") : NULL, + "email" => $this->queryParam("email"), + "telegram" => $this->queryParam("telegram"), + "site" => $this->queryParam("site") != "" ? "https://".$this->queryParam("site") : NULL, + "address" => $this->queryParam("address"), + "is_online" => $this->queryParam("is_online") == 1 ? 1 : NULL, + "interests" => $this->queryParam("interests") != "" ? $this->queryParam("interests") : NULL, + "fav_mus" => $this->queryParam("fav_mus") != "" ? $this->queryParam("fav_mus") : NULL, + "fav_films" => $this->queryParam("fav_films") != "" ? $this->queryParam("fav_films") : NULL, + "fav_shows" => $this->queryParam("fav_shows") != "" ? $this->queryParam("fav_shows") : NULL, + "fav_books" => $this->queryParam("fav_books") != "" ? $this->queryParam("fav_books") : NULL, + "fav_quote" => $this->queryParam("fav_quote") != "" ? $this->queryParam("fav_quote") : NULL, + "hometown" => $this->queryParam("hometown") != "" ? $this->queryParam("hometown") : NULL, + "before" => $this->queryParam("datebefore") != "" ? strtotime($this->queryParam("datebefore")) : NULL, + "after" => $this->queryParam("dateafter") != "" ? strtotime($this->queryParam("dateafter")) : NULL, + "gender" => $this->queryParam("gender") != "" && $this->queryParam("gender") != 2 ? $this->queryParam("gender") : NULL + ]; + $repo = $repos[$type] or $this->throwError(400, "Bad Request", "Invalid search entity $type."); - $results = $this->{$repo}->find($query); + $results = $this->{$repo}->find($query, $parameters, $sort); $iterator = $results->page($page); $count = $results->size(); diff --git a/Web/Presenters/templates/@layout.xml b/Web/Presenters/templates/@layout.xml index f211c2c1..ed593490 100644 --- a/Web/Presenters/templates/@layout.xml +++ b/Web/Presenters/templates/@layout.xml @@ -70,28 +70,60 @@ {_header_log_out} {else} -