1
0

TicketComment.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App;
  3. use Carbon\CarbonImmutable;
  4. use Exception;
  5. /**
  6. * Class TicketComment
  7. * @property int $id
  8. * @property int $ticket_id
  9. * @property int $user_id
  10. * @property string $content
  11. * @property int $internal
  12. * @property string $dts_created
  13. * @property string $dts_deleted
  14. */
  15. class TicketComment extends Model {
  16. protected static $table = "ticket_comment";
  17. protected static $idField = "id";
  18. protected static $fillableFields = [
  19. "ticket_id", "user_id",
  20. "content",
  21. "internal",
  22. "dts_created", "dts_deleted",
  23. ];
  24. /**
  25. * @return Ticket
  26. * @throws Exception
  27. */
  28. public function getTicket(): Ticket {
  29. return Ticket::get($this->ticket_id);
  30. }
  31. /**
  32. * @return User
  33. * @throws Exception
  34. */
  35. public function getUser(): User {
  36. return User::get($this->user_id);
  37. }
  38. public function getDateTime(): CarbonImmutable {
  39. return new CarbonImmutable($this->dts_created, "UTC");
  40. }
  41. /**
  42. * @return TicketCommentAttachment[]
  43. */
  44. public function getAttachments(): array {
  45. return TicketCommentAttachment::query([
  46. "ticket_comment_id" => $this->id,
  47. ], [
  48. "sort" => "id",
  49. "order" => "desc"
  50. ]);
  51. }
  52. public function getHtml(): string {
  53. return nl2br(htmlspecialchars($this->content));
  54. }
  55. public function getDisplayDateTimeCreated(): string {
  56. return $this->getDateTime()->format("d/m/Y H:i");
  57. }
  58. /**
  59. * @param array $form_file
  60. * @return TicketCommentAttachment
  61. * @throws Exception
  62. */
  63. public function addAttachment(array $form_file): TicketCommentAttachment {
  64. $attachment = new TicketCommentAttachment();
  65. $attachment->ticket_comment_id = $this->id;
  66. $attachment->mime_type = $form_file["type"];
  67. $attachment->file_name = $form_file["name"];
  68. $attachment->file_size = $form_file["size"];
  69. $attachment->commit();
  70. $path = $attachment->getPath();
  71. move_uploaded_file($form_file["tmp_name"], $path);
  72. return $attachment;
  73. }
  74. }