Ticket.php 2.6 KB

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