2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Repositories;
|
|
|
|
use openvk\Web\Models\Entities\Ticket;
|
|
|
|
use Nette\Database\Table\ActiveRow;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
class Tickets
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $tickets;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->tickets = $this->context->table("tickets");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function toTicket(?ActiveRow $ar): ?Ticket
|
|
|
|
{
|
|
|
|
return is_null($ar) ? NULL : new Ticket($ar);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTickets(int $state = 0, int $page = 1): \Traversable
|
|
|
|
{
|
2022-01-07 01:59:51 +03:00
|
|
|
foreach($this->tickets->where(["deleted" => 0, "type" => $state])->order("created DESC")->page($page, OPENVK_DEFAULT_PER_PAGE) as $ticket)
|
|
|
|
yield new Ticket($ticket);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTicketCount(int $state = 0): int
|
|
|
|
{
|
|
|
|
return sizeof($this->tickets->where(["deleted" => 0, "type" => $state]));
|
|
|
|
}
|
|
|
|
|
2022-01-07 01:59:51 +03:00
|
|
|
function getTicketsByUserId(int $userId, int $page = 1): \Traversable
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2022-01-07 01:59:51 +03:00
|
|
|
foreach($this->tickets->where(["user_id" => $userId, "deleted" => 0])->order("created DESC")->page($page, OPENVK_DEFAULT_PER_PAGE) as $ticket) yield new Ticket($ticket);
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
2021-11-27 12:24:34 +03:00
|
|
|
|
2022-01-07 01:59:51 +03:00
|
|
|
function getTicketsCountByUserId(int $userId, int $type = 0): int
|
2021-11-27 12:24:34 +03:00
|
|
|
{
|
2022-01-07 01:59:51 +03:00
|
|
|
return sizeof($this->tickets->where(["user_id" => $userId, "deleted" => 0, "type" => $type]));
|
2021-11-27 12:24:34 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
2022-01-07 01:59:51 +03:00
|
|
|
function getRequestById(int $requestId): ?Ticket
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
2022-01-07 01:59:51 +03:00
|
|
|
$requests = $this->tickets->where(["id" => $requestId])->fetch();
|
2020-06-07 19:04:43 +03:00
|
|
|
if(!is_null($requests))
|
|
|
|
return new Req($requests);
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function get(int $id): ?Ticket
|
|
|
|
{
|
|
|
|
return $this->toTicket($this->tickets->get($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
use \Nette\SmartObject;
|
|
|
|
}
|