TicketCommentAttachment.php 1.3 KB

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