mirror of
https://github.com/openvk/openvk
synced 2024-12-25 18:11:07 +03:00
Reports: [INDEV] Undone implementation of reports
This commit is contained in:
parent
517ce5ccb1
commit
75c0aecf47
3 changed files with 208 additions and 0 deletions
87
Web/Models/Entities/Report.php
Normal file
87
Web/Models/Entities/Report.php
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace openvk\Web\Models\Entities;
|
||||
use openvk\Web\Util\DateTime;
|
||||
use Nette\Database\Table\ActiveRow;
|
||||
use openvk\Web\Models\RowModel;
|
||||
use Chandler\Database\DatabaseConnection;
|
||||
use openvk\Web\Models\Repositories\Users;
|
||||
use openvk\Web\Models\Repositories\Posts;
|
||||
use Chandler\Database\DatabaseConnection as DB;
|
||||
use Nette\InvalidStateException as ISE;
|
||||
use Nette\Database\Table\Selection;
|
||||
|
||||
class Report extends RowModel
|
||||
{
|
||||
protected $tableName = "reports";
|
||||
|
||||
function getId(): int
|
||||
{
|
||||
return $this->getRecord()->id;
|
||||
}
|
||||
|
||||
function getStatus(): int
|
||||
{
|
||||
return $this->getRecord()->status;
|
||||
}
|
||||
|
||||
function getContentType(): string
|
||||
{
|
||||
return $this->getRecord()->type;
|
||||
}
|
||||
|
||||
function getReason(): string
|
||||
{
|
||||
return $this->getRecord()->reason;
|
||||
}
|
||||
|
||||
function getTime(): DateTime
|
||||
{
|
||||
return new DateTime($this->getRecord()->date);
|
||||
}
|
||||
|
||||
function isDeleted(): bool
|
||||
{
|
||||
if ($this->getRecord()->deleted === 0)
|
||||
{
|
||||
return false;
|
||||
} elseif ($this->getRecord()->deleted === 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function authorId(): int
|
||||
{
|
||||
return $this->getRecord()->user_id;
|
||||
}
|
||||
|
||||
function getUser(): user
|
||||
{
|
||||
return (new Users)->get($this->getRecord()->user_id);
|
||||
}
|
||||
|
||||
function getContentId(): int
|
||||
{
|
||||
return $this->getRecord()->target_id;
|
||||
}
|
||||
|
||||
function getContentObject()
|
||||
{
|
||||
if ($this->getContentType() == "post") return (new Posts)->get($this->getContentId());
|
||||
else return null;
|
||||
}
|
||||
|
||||
// TODO: Localize that
|
||||
function banUser()
|
||||
{
|
||||
$this->getUser()->ban("Banned by report. Ask Technical support for ban reason");
|
||||
}
|
||||
|
||||
function deleteContent()
|
||||
{
|
||||
$this->getUser()->adminNotify("Ваш контент, который вы опубликовали " . $this->getContentObject()->getPublicationTime() . " был удалён модераторами инстанса. За повторные или серьёзные нарушения вас могут заблокировать.");
|
||||
$this->getContentObject()->delete();
|
||||
$this->setDeleted(1);
|
||||
$this->unwire();
|
||||
$this->save();
|
||||
}
|
||||
}
|
40
Web/Models/Repositories/Reports.php
Normal file
40
Web/Models/Repositories/Reports.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace openvk\Web\Models\Repositories;
|
||||
use openvk\Web\Models\Entities\Report;
|
||||
use Nette\Database\Table\ActiveRow;
|
||||
use Chandler\Database\DatabaseConnection;
|
||||
|
||||
class Reports
|
||||
{
|
||||
private $context;
|
||||
private $reports;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->context = DatabaseConnection::i()->getContext();
|
||||
$this->reports = $this->context->table("reports");
|
||||
}
|
||||
|
||||
private function toReport(?ActiveRow $ar): ?Report
|
||||
{
|
||||
return is_null($ar) ? NULL : new Report($ar);
|
||||
}
|
||||
|
||||
function getReports(int $state = 0, int $page = 1): \Traversable
|
||||
{
|
||||
foreach($this->reports->where(["deleted" => 0])->page($page, 15) as $t)
|
||||
yield new Ticket($t);
|
||||
}
|
||||
|
||||
function getReportsCount(int $state = 0): int
|
||||
{
|
||||
return sizeof($this->tickets->where(["deleted" => 0, "type" => $state]));
|
||||
}
|
||||
|
||||
function get(int $id): ?Ticket
|
||||
{
|
||||
return $this->toTicket($this->tickets->get($id));
|
||||
}
|
||||
|
||||
use \Nette\SmartObject;
|
||||
}
|
81
Web/Presenters/ReportPresenter.php
Normal file
81
Web/Presenters/ReportPresenter.php
Normal file
|
@ -0,0 +1,81 @@
|
|||
<?php declare(strict_types=1);
|
||||
namespace openvk\Web\Presenters;
|
||||
use openvk\Web\Models\Repositories\Users;
|
||||
use openvk\Web\Models\Repositories\Reports;
|
||||
use openvk\Web\Models\Entities\Report;
|
||||
|
||||
final class ReportPresenter extends OpenVKPresenter
|
||||
{
|
||||
private $reports;
|
||||
|
||||
function __construct(Reports $reports)
|
||||
{
|
||||
$this->reports = $reports;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
function renderList(): void
|
||||
{
|
||||
$this->template->reports = $this->reports->getReports(0, (int)($this->queryParam("p") ?? 1));
|
||||
$this->template->count = $this->notes->getReportsCount();
|
||||
$this->template->paginatorConf = (object) [
|
||||
"count" => $this->template->count,
|
||||
"page" => $this->queryParam("p") ?? 1,
|
||||
"amount" => NULL,
|
||||
"perPage" => 15,
|
||||
];
|
||||
}
|
||||
|
||||
function renderView(int $id): void
|
||||
{
|
||||
$report = $this->reports->get($id);
|
||||
if(!$report || $note->isDeleted())
|
||||
$this->notFound();
|
||||
|
||||
$this->template->report = $report;
|
||||
}
|
||||
|
||||
function renderCreate(): void
|
||||
{
|
||||
$this->assertUserLoggedIn();
|
||||
$this->willExecuteWriteAction();
|
||||
|
||||
// ЛАПСКИЙ Я НЕ ДО КОНЦА ДОДЕЛАЛ Я ПРОСТО МЫТЬСЯ ПОШЁЛ
|
||||
if(!$id)
|
||||
$this->notFound();
|
||||
|
||||
if($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
if(empty($this->postParam("name"))) {
|
||||
$this->flashFail("err", tr("error"), tr("error_segmentation"));
|
||||
}
|
||||
|
||||
$note = new Note;
|
||||
$note->setOwner($this->user->id);
|
||||
$note->setCreated(time());
|
||||
$note->setName($this->postParam("name"));
|
||||
$note->setSource($this->postParam("html"));
|
||||
$note->save();
|
||||
|
||||
$this->redirect("/note" . $this->user->id . "_" . $note->getId());
|
||||
}
|
||||
}
|
||||
|
||||
function renderDelete(int $owner, int $id): void
|
||||
{
|
||||
$this->assertUserLoggedIn();
|
||||
$this->willExecuteWriteAction();
|
||||
$this->assertNoCSRF();
|
||||
|
||||
$note = $this->notes->get($id);
|
||||
if(!$note) $this->notFound();
|
||||
if($note->getOwner()->getId() . "_" . $note->getId() !== $owner . "_" . $id || $note->isDeleted()) $this->notFound();
|
||||
if(is_null($this->user) || !$note->canBeModifiedBy($this->user->identity))
|
||||
$this->flashFail("err", "Ошибка доступа", "Недостаточно прав для модификации данного ресурса.");
|
||||
|
||||
$name = $note->getName();
|
||||
$note->delete();
|
||||
$this->flash("succ", "Заметка удалена", "Заметка \"$name\" была успешно удалена.");
|
||||
$this->redirect("/notes" . $this->user->id);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue