2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-08-11 16:50:19 +03:00
|
|
|
namespace openvk\Web\Models\Repositories;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2023-08-11 16:50:19 +03:00
|
|
|
use openvk\Web\Models\Entities\Report;
|
|
|
|
use Nette\Database\Table\ActiveRow;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
class Reports
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
use \Nette\SmartObject;
|
2023-08-11 16:50:19 +03:00
|
|
|
private $context;
|
|
|
|
private $reports;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function __construct()
|
2023-08-11 16:50:19 +03:00
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->reports = $this->context->table("reports");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2023-08-11 16:50:19 +03:00
|
|
|
private function toReport(?ActiveRow $ar): ?Report
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
return is_null($ar) ? null : new Report($ar);
|
2023-08-11 16:50:19 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getReports(int $state = 0, int $page = 1, ?string $type = null, ?bool $pagination = true): \Traversable
|
2023-08-11 16:50:19 +03:00
|
|
|
{
|
|
|
|
$filter = ["deleted" => 0];
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($type) {
|
|
|
|
$filter["type"] = $type;
|
|
|
|
}
|
2023-08-11 16:50:19 +03:00
|
|
|
|
|
|
|
$reports = $this->reports->where($filter)->order("created DESC")->group("target_id, type");
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($pagination) {
|
2023-08-11 16:50:19 +03:00
|
|
|
$reports = $reports->page($page, 15);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2023-08-11 16:50:19 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($reports as $t) {
|
2023-08-11 16:50:19 +03:00
|
|
|
yield new Report($t);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2023-08-11 16:50:19 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getReportsCount(int $state = 0): int
|
2023-08-11 16:50:19 +03:00
|
|
|
{
|
|
|
|
return sizeof($this->reports->where(["deleted" => 0, "type" => $state])->group("target_id, type"));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function get(int $id): ?Report
|
2023-08-11 16:50:19 +03:00
|
|
|
{
|
|
|
|
return $this->toReport($this->reports->get($id));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getByContentId(int $id): ?Report
|
2023-08-11 16:50:19 +03:00
|
|
|
{
|
|
|
|
$post = $this->reports->where(["deleted" => 0, "content_id" => $id])->fetch();
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($post) {
|
2023-08-11 16:50:19 +03:00
|
|
|
return new Report($post);
|
2025-01-31 18:20:13 +03:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-11 16:50:19 +03:00
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getDuplicates(string $type, int $target_id, ?int $orig = null, ?int $user_id = null): \Traversable
|
2023-08-11 16:50:19 +03:00
|
|
|
{
|
|
|
|
$filter = ["deleted" => 0, "type" => $type, "target_id" => $target_id];
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($orig) {
|
|
|
|
$filter[] = "id != $orig";
|
|
|
|
}
|
|
|
|
if ($user_id) {
|
|
|
|
$filter["user_id"] = $user_id;
|
|
|
|
}
|
2023-08-11 16:50:19 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($this->reports->where($filter) as $report) {
|
2023-08-11 16:50:19 +03:00
|
|
|
yield new Report($report);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2023-08-11 16:50:19 +03:00
|
|
|
}
|
|
|
|
}
|