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