openvk/Web/Models/Repositories/BugtrackerReports.php

76 lines
2.4 KiB
PHP
Raw Normal View History

2022-08-20 20:30:04 +03:00
<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
2022-08-23 02:40:11 +03:00
use openvk\Web\Models\Entities\{BugReport, User};
use openvk\Web\Models\Repositories\{BugtrackerProducts};
2022-08-20 20:30:04 +03:00
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection;
class BugtrackerReports
{
private $context;
private $reports;
private $reportsPerPage = 5;
function __construct()
{
$this->context = DatabaseConnection::i()->getContext();
$this->reports = $this->context->table("bugs");
}
private function toReport(?ActiveRow $ar)
{
return is_null($ar) ? NULL : new BugReport($ar);
}
function get(int $id): ?BugReport
{
return $this->toReport($this->reports->get($id));
}
2022-08-23 02:40:11 +03:00
function getAllReports(User $user, int $page = 1): \Traversable
2022-08-20 20:30:04 +03:00
{
2022-08-23 02:40:11 +03:00
$reports = $this->reports->where(["deleted" => NULL])->order("created DESC")->page($page, 5);
foreach($reports as $report)
2022-08-20 20:30:04 +03:00
yield new BugReport($report);
}
2022-08-23 02:40:11 +03:00
function getReports(int $product_id = 0, int $priority = 0, int $page = 1, User $user = NULL): \Traversable
2022-08-20 20:30:04 +03:00
{
2022-08-22 12:36:35 +03:00
$filter = ["deleted" => NULL];
$product_id && $filter["product_id"] = $product_id;
$priority && $filter["priority"] = $priority;
2022-08-23 02:40:11 +03:00
$product = (new BugtrackerProducts)->get($product_id);
if (!$product->hasAccess($user))
return false;
2022-08-22 12:36:35 +03:00
foreach($this->reports->where($filter)->order("created DESC")->page($page, 5) as $report)
2022-08-20 20:30:04 +03:00
yield new BugReport($report);
}
2022-08-22 12:36:35 +03:00
function getReportsCount(int $product_id = 0, int $priority = 0): int
2022-08-20 20:30:04 +03:00
{
2022-08-22 12:36:35 +03:00
$filter = ["deleted" => NULL];
$product_id && $filter["product_id"] = $product_id;
$priority && $filter["priority"] = $priority;
return sizeof($this->reports->where($filter));
2022-08-20 20:30:04 +03:00
}
function getByReporter(int $reporter_id, int $page = 1): \Traversable
{
foreach($this->reports->where(["deleted" => NULL, "reporter" => $reporter_id])->order("created DESC")->page($page, 5) as $report)
yield new BugReport($report);
}
2022-08-22 02:11:48 +03:00
function getCountByReporter(int $reporter_id): ?int
2022-08-20 20:30:04 +03:00
{
return sizeof($this->reports->where(["deleted" => NULL, "reporter" => $reporter_id]));
}
2022-08-22 02:11:48 +03:00
function getSuccCountByReporter(int $reporter_id): ?int
2022-08-20 20:30:04 +03:00
{
return sizeof($this->reports->where(["deleted" => NULL, "reporter" => $reporter_id, "status" => "<= 4"]));
}
}