Ticket.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App;
  3. use App\Enums\TicketPriorities;
  4. use App\Enums\TicketStatuses;
  5. use Carbon\CarbonImmutable;
  6. /**
  7. * Class Ticket
  8. * @property int $id
  9. * @property int $location_id
  10. * @property int $creator_user_id
  11. * @property int $assigned_user_id
  12. * @property string $subject
  13. * @property string $contact_name
  14. * @property int $status
  15. * @property int $priority
  16. * @property int $rating
  17. * @property string $dts_due
  18. * @property string $dts_created
  19. * @property string $dts_updated
  20. * @property string $dts_deleted
  21. */
  22. class Ticket extends Model {
  23. protected static $table = "ticket";
  24. protected static $idField = "id";
  25. protected static $fillableFields = [
  26. "location_id", "creator_user_id", "assigned_user_id",
  27. "subject", "contact_name", "status", "priority", "rating",
  28. "dts_due",
  29. "dts_created", "dts_updated", "dts_deleted",
  30. ];
  31. public function getLocation(): Location {
  32. return Location::get($this->location_id);
  33. }
  34. public function getCreator(): User {
  35. return User::get($this->creator_user_id);
  36. }
  37. public function getAssignedTo(): ?User {
  38. return User::get($this->assigned_user_id);
  39. }
  40. public function getDateTimeCreated(): CarbonImmutable {
  41. return new CarbonImmutable($this->dts_created, "UTC");
  42. }
  43. public function getDateTimeUpdated(): ?CarbonImmutable {
  44. return $this->dts_updated ? new CarbonImmutable($this->dts_updated, "UTC") : null;
  45. }
  46. public function getDateTimeDue(): ?CarbonImmutable {
  47. return $this->dts_due ? new CarbonImmutable($this->dts_due, "UTC") : null;
  48. }
  49. /**
  50. * @return TicketComment[]
  51. */
  52. public function getComments(): array {
  53. return TicketComment::query(["ticket_id" => $this->id], ["sort" => "id"]);
  54. }
  55. public function getDisplayCreator(): string {
  56. $creator = $this->getCreator();
  57. return $creator->getDisplayName() . " ($creator->email)";
  58. }
  59. public function getDisplayAssignedTo(): string {
  60. $assigned_to = $this->getAssignedTo();
  61. return $assigned_to ? $assigned_to->getDisplayName() . " ($assigned_to->email)" : __("Unassigned");
  62. }
  63. public function getDisplayStatus(): string {
  64. return TicketStatuses::enumToText($this->status);
  65. }
  66. public function getPriorityBadgeHtml(): string {
  67. return TicketPriorities::getBadgeHtml($this->priority);
  68. }
  69. public function getDisplayCreatedDateTime(): string {
  70. return $this->getDateTimeCreated()->format("d/m/Y H:i");
  71. }
  72. public function getDisplayUpdatedDateTime(): string {
  73. $dt_updated = $this->getDateTimeUpdated();
  74. return $dt_updated ? $dt_updated->format("d/m/Y H:i") : __("Never");
  75. }
  76. public function getUrl(): string {
  77. return sprintf("/tickets/%d", $this->id);
  78. }
  79. /**
  80. * @param User $user
  81. * @return bool
  82. * @throws \Exception
  83. */
  84. public function assign(User $user): bool {
  85. $this->assigned_user_id = $user->id;
  86. return $this->commit();
  87. }
  88. /**
  89. * @return bool
  90. * @throws \Exception
  91. */
  92. public function unassign(): bool {
  93. $this->assigned_user_id = null;
  94. return $this->commit();
  95. }
  96. }