openvk/Web/Models/Repositories/Tickets.php

72 lines
2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-06-07 19:04:43 +03:00
namespace openvk\Web\Models\Repositories;
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
{
use \Nette\SmartObject;
2020-06-07 19:04:43 +03:00
private $context;
private $tickets;
public function __construct()
2020-06-07 19:04:43 +03:00
{
$this->context = DatabaseConnection::i()->getContext();
$this->tickets = $this->context->table("tickets");
}
2020-06-07 19:04:43 +03:00
private function toTicket(?ActiveRow $ar): ?Ticket
{
return is_null($ar) ? null : new Ticket($ar);
2020-06-07 19:04:43 +03:00
}
public function getTickets(int $state = 0, int $page = 1): \Traversable
2020-06-07 19:04:43 +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
}
public function getTicketCount(int $state = 0): int
2020-06-07 19:04:43 +03:00
{
return sizeof($this->tickets->where(["deleted" => 0, "type" => $state]));
}
public function getTicketsByUserId(int $userId, int $page = 1): \Traversable
2020-06-07 19:04:43 +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
}
public function getTicketsCountByUserId(int $userId, int $type = null): int
{
if (is_null($type)) {
2022-01-07 03:07:36 +03:00
return sizeof($this->tickets->where(["user_id" => $userId, "deleted" => 0]));
} else {
2022-01-07 03:07:36 +03:00
return sizeof($this->tickets->where(["user_id" => $userId, "deleted" => 0, "type" => $type]));
}
}
public function getRequestById(int $requestId): ?Ticket
2020-06-07 19:04:43 +03:00
{
$requests = $this->tickets->where(["id" => $requestId])->fetch();
if (!is_null($requests)) {
2020-06-07 19:04:43 +03:00
return new Req($requests);
} else {
return null;
}
2020-06-07 19:04:43 +03:00
}
public function get(int $id): ?Ticket
2020-06-07 19:04:43 +03:00
{
return $this->toTicket($this->tickets->get($id));
}
}