| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App;
- use App\Enums\TicketPriorities;
- use App\Enums\TicketStatuses;
- use App\Traits\DateTimeTrait;
- use App\Traits\SoftDeleteTrait;
- use Carbon\CarbonImmutable;
- /**
- * Class Ticket
- * @property int $id
- * @property int $location_id
- * @property int $creator_user_id
- * @property int $assigned_user_id
- * @property string $subject
- * @property string $contact_name
- * @property int $status
- * @property int $priority
- * @property int $rating
- * @property string $dts_due
- */
- class Ticket extends Model {
- protected static $table = "ticket";
- protected static $idField = "id";
- protected static $fillableFields = [
- "location_id", "creator_user_id", "assigned_user_id",
- "subject", "contact_name", "status", "priority", "rating",
- "dts_due",
- "dts_created", "dts_updated", "dts_deleted",
- ];
- use DateTimeTrait;
- use SoftDeleteTrait;
- public function getLocation(): Location {
- return Location::get($this->location_id);
- }
- public function getCreator(): User {
- return User::get($this->creator_user_id);
- }
- public function getAssignedTo(): ?User {
- return User::get($this->assigned_user_id);
- }
- public function getDateTimeDue(): ?CarbonImmutable {
- return $this->dts_due ? new CarbonImmutable($this->dts_due, "UTC") : null;
- }
- /**
- * @return TicketComment[]
- */
- public function getComments(): array {
- return TicketComment::query(["ticket_id" => $this->id], ["sort" => "id"]);
- }
- public function getDisplayCreator(): string {
- $creator = $this->getCreator();
- return $creator->getDisplayName() . " ($creator->email)";
- }
- public function getDisplayAssignedTo(): string {
- $assigned_to = $this->getAssignedTo();
- return $assigned_to ? $assigned_to->getDisplayName() . " ($assigned_to->email)" : __("Unassigned");
- }
- public function getDisplayStatus(): string {
- return TicketStatuses::enumToText($this->status);
- }
- public function getPriorityBadgeHtml(): string {
- return TicketPriorities::getBadgeHtml($this->priority);
- }
- public function getDisplayCreatedDateTime(): string {
- return $this->getDateTimeCreated()->format("d/m/Y H:i");
- }
- public function getDisplayUpdatedDateTime(): string {
- $dt_updated = $this->getDateTimeUpdated();
- return $dt_updated ? $dt_updated->format("d/m/Y H:i") : __("Never");
- }
- public function getUrl(): string {
- return sprintf("/tickets/%d", $this->id);
- }
- /**
- * @param User $user
- * @return bool
- * @throws \Exception
- */
- public function assign(User $user): bool {
- $this->assigned_user_id = $user->id;
- return $this->commit();
- }
- /**
- * @return bool
- * @throws \Exception
- */
- public function unassign(): bool {
- $this->assigned_user_id = null;
- return $this->commit();
- }
- }
|