2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Presenters;
|
|
|
|
use openvk\Web\Models\Repositories\Users;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use openvk\Web\Models\Repositories\Clubs;
|
|
|
|
use openvk\Web\Models\Entities\Club;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
final class SearchPresenter extends OpenVKPresenter
|
|
|
|
{
|
|
|
|
private $users;
|
|
|
|
private $clubs;
|
|
|
|
|
|
|
|
function __construct(Users $users, Clubs $clubs)
|
|
|
|
{
|
|
|
|
$this->users = $users;
|
|
|
|
$this->clubs = $clubs;
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderIndex(): void
|
|
|
|
{
|
|
|
|
$query = $this->queryParam("query") ?? "";
|
|
|
|
$type = $this->queryParam("type") ?? "users";
|
|
|
|
$page = (int) ($this->queryParam("p") ?? 1);
|
|
|
|
|
2022-04-04 15:10:30 +03:00
|
|
|
$this->willExecuteWriteAction();
|
|
|
|
if($query != "")
|
|
|
|
$this->assertUserLoggedIn();
|
|
|
|
|
2022-05-08 13:06:26 +03:00
|
|
|
# https://youtu.be/pSAWM5YuXx8
|
2020-06-07 19:04:43 +03:00
|
|
|
|
2020-11-22 13:29:27 +03:00
|
|
|
$repos = [ "groups" => "clubs", "users" => "users" ];
|
|
|
|
$repo = $repos[$type] or $this->throwError(400, "Bad Request", "Invalid search entity $type.");
|
|
|
|
|
|
|
|
$results = $this->{$repo}->find($query);
|
|
|
|
$iterator = $results->page($page);
|
|
|
|
$count = $results->size();
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
$this->template->iterator = iterator_to_array($iterator);
|
|
|
|
$this->template->count = $count;
|
|
|
|
$this->template->type = $type;
|
|
|
|
$this->template->page = $page;
|
|
|
|
}
|
|
|
|
}
|