openvk/Web/Presenters/AdminPresenter.php

138 lines
4 KiB
PHP
Raw Normal View History

2020-06-07 19:04:43 +03:00
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\User;
use openvk\Web\Models\Repositories\{Users, Clubs};
final class AdminPresenter extends OpenVKPresenter
{
private $users;
private $clubs;
function __construct(Users $users, Clubs $clubs)
{
$this->users = $users;
$this->clubs = $clubs;
parent::__construct();
}
private function searchResults(object $repo, &$count)
{
$query = $this->queryParam("q") ?? "";
$page = (int) ($this->queryParam("p") ?? 1);
$count = $repo->find($query)->size();
return $repo->find($query)->page($page, 20);
2020-06-07 19:04:43 +03:00
}
2020-06-15 21:25:55 +03:00
function onStartup(): void
{
2020-07-17 19:26:59 +03:00
parent::onStartup();
$this->assertPermission("admin", "access", -1);
2020-06-15 21:25:55 +03:00
}
2020-06-07 19:04:43 +03:00
function renderIndex(): void
{
}
function renderUsers(): void
{
$this->template->users = $this->searchResults($this->users, $this->template->count);
}
function renderUser(int $id): void
{
$user = $this->users->get($id);
if(!$user)
$this->notFound();
$this->template->user = $user;
if($_SERVER["REQUEST_METHOD"] !== "POST")
return;
switch($_POST["act"] ?? "info") {
default:
case "info":
2021-09-11 17:39:14 +03:00
$user->setFirst_Name($this->postParam("first_name"));
$user->setLast_Name($this->postParam("last_name"));
$user->setPseudo($this->postParam("nickname"));
$user->setStatus($this->postParam("status"));
$user->setVerified(empty($this->postParam("verify") ? 0 : 1));
if($user->onlineStatus() != $this->postParam("online")) $user->setOnline(intval($this->postParam("online")));
$user->save();
2020-06-07 19:04:43 +03:00
break;
}
}
function renderClubs(): void
{
$this->template->clubs = $this->searchResults($this->clubs, $this->template->count);
}
function renderClub(int $id): void
{
$club = $this->clubs->get($id);
if(!$club)
$this->notFound();
2021-09-13 16:47:10 +03:00
$this->template->mode = in_array($this->queryParam("act"), ["main", "ban", "followers"]) ? $this->queryParam("act") : "main";
2020-06-07 19:04:43 +03:00
$this->template->club = $club;
2021-09-11 22:36:17 +03:00
2021-09-13 16:47:10 +03:00
$this->template->followers = $this->template->club->getFollowers((int) ($this->queryParam("p") ?? 1));
2021-09-11 22:36:17 +03:00
if($_SERVER["REQUEST_METHOD"] !== "POST")
return;
2020-06-07 19:04:43 +03:00
2021-09-13 16:47:10 +03:00
switch($this->queryParam("act")) {
2021-09-11 22:36:17 +03:00
default:
2021-09-13 16:47:10 +03:00
case "main":
2021-09-11 22:36:17 +03:00
$club->setOwner($this->postParam("id_owner"));
$club->setName($this->postParam("name"));
$club->setAbout($this->postParam("about"));
$club->setShortCode($this->postParam("shortcode"));
$club->setVerified(empty($this->postParam("verify") ? 0 : 1));
2021-09-13 16:47:10 +03:00
$club->save();
break;
case "ban":
2021-09-11 22:36:17 +03:00
$club->setBlock_reason($this->postParam("ban_reason"));
$club->save();
break;
2020-06-07 19:04:43 +03:00
}
}
function renderFiles(): void
{
}
2020-07-17 19:26:59 +03:00
function renderQuickBan(int $id): void
{
$this->assertNoCSRF();
2020-07-17 19:26:59 +03:00
$user = $this->users->get($id);
if(!$user)
exit(json_encode([ "error" => "User does not exist" ]));
$user->ban($this->queryParam("reason"));
exit(json_encode([ "reason" => $this->queryParam("reason") ]));
}
2020-07-17 19:39:34 +03:00
function renderQuickWarn(int $id): void
{
$this->assertNoCSRF();
2020-07-17 19:39:34 +03:00
$user = $this->users->get($id);
if(!$user)
exit(json_encode([ "error" => "User does not exist" ]));
$user->adminNotify("⚠️ " . $this->queryParam("message"));
exit(json_encode([ "message" => $this->queryParam("message") ]));
}
2020-06-07 19:04:43 +03:00
}