mirror of
https://github.com/openvk/openvk
synced 2025-02-02 21:15:42 +03:00
Alexander Minkin
6ec54a379d
* feat(lint): add php-cs-fixer for linting Removing previous CODE_STYLE as it was not enforced anyway and using PER-CS 2.0. This is not the reformatting commit. * style: format code according to PER-CS 2.0 with php-cs-fixer * ci(actions): add lint action Resolves #1132.
80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?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
|
|
{
|
|
use \Nette\SmartObject;
|
|
private $context;
|
|
private $reports;
|
|
|
|
public 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);
|
|
}
|
|
|
|
public function getReports(int $state = 0, int $page = 1, ?string $type = null, ?bool $pagination = true): \Traversable
|
|
{
|
|
$filter = ["deleted" => 0];
|
|
if ($type) {
|
|
$filter["type"] = $type;
|
|
}
|
|
|
|
$reports = $this->reports->where($filter)->order("created DESC")->group("target_id, type");
|
|
if ($pagination) {
|
|
$reports = $reports->page($page, 15);
|
|
}
|
|
|
|
foreach ($reports as $t) {
|
|
yield new Report($t);
|
|
}
|
|
}
|
|
|
|
public function getReportsCount(int $state = 0): int
|
|
{
|
|
return sizeof($this->reports->where(["deleted" => 0, "type" => $state])->group("target_id, type"));
|
|
}
|
|
|
|
public function get(int $id): ?Report
|
|
{
|
|
return $this->toReport($this->reports->get($id));
|
|
}
|
|
|
|
public function getByContentId(int $id): ?Report
|
|
{
|
|
$post = $this->reports->where(["deleted" => 0, "content_id" => $id])->fetch();
|
|
|
|
if ($post) {
|
|
return new Report($post);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function getDuplicates(string $type, int $target_id, ?int $orig = null, ?int $user_id = null): \Traversable
|
|
{
|
|
$filter = ["deleted" => 0, "type" => $type, "target_id" => $target_id];
|
|
if ($orig) {
|
|
$filter[] = "id != $orig";
|
|
}
|
|
if ($user_id) {
|
|
$filter["user_id"] = $user_id;
|
|
}
|
|
|
|
foreach ($this->reports->where($filter) as $report) {
|
|
yield new Report($report);
|
|
}
|
|
}
|
|
}
|