TicketComment.php 2.1 KB

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