TicketCommentAttachment.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App;
  3. use Exception;
  4. use RuntimeException;
  5. /**
  6. * Class TicketCommentAttachment
  7. * @property int $id
  8. * @property int $ticket_comment_id
  9. * @property string $mime_type
  10. * @property string $file_name
  11. * @property int $file_size
  12. */
  13. class TicketCommentAttachment extends Model {
  14. protected static $table = "ticket_comment_attachment";
  15. protected static $idField = "id";
  16. protected static $fillableFields = [
  17. "ticket_comment_id",
  18. "mime_type", "file_name", "file_size"
  19. ];
  20. /**
  21. * @return TicketComment
  22. * @throws Exception
  23. */
  24. public function getTicketComment(): TicketComment {
  25. return TicketComment::get($this->ticket_comment_id);
  26. }
  27. private function getDirectory(): string {
  28. $subfolder = bcdiv($this->id, 1000);
  29. $folder = $_ENV["ROOT_DIR"] . "/files/ticket_attachments/$subfolder";
  30. if(!file_exists($folder) && !mkdir($folder, 0777, true) && !is_dir($folder)) {
  31. throw new RuntimeException(sprintf('Directory "%s" was not created', $folder));
  32. }
  33. return $folder;
  34. }
  35. public function getPath(): string {
  36. return $this->getDirectory() . "/" . $this->id;
  37. }
  38. public function renderButton(): void {
  39. ?>
  40. <a class="btn btn-sm btn-outline-primary" href="/download-file/ticket-comment-attachment/<?=$this->id?>" target="_blank">
  41. <i class="fa fa-fw fa-paperclip"></i>
  42. <?=htmlspecialchars($this->file_name)?>
  43. </a>
  44. <?php
  45. }
  46. }