HasAllegato.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Livewire\Traits;
  3. use App\Models\AllegatiGalleryType;
  4. use App\Models\ReportAllegatiGallery;
  5. use Livewire\WithFileUploads;
  6. trait HasAllegato
  7. {
  8. public $allegatoId = 0;
  9. public $allegatoType = 0;
  10. public $allegatoGallery = 0;
  11. public $allegatoName = '';
  12. public $allegatoVisible = false;
  13. public $allegatoFiles = '';
  14. public $allegatiFiles = [];
  15. public $allegatiImmagini;
  16. public $allegatiDocumenti;
  17. public $allegati = [];
  18. protected $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'];
  19. public function getAllegatoType($type)
  20. {
  21. if ($type > 0) {
  22. $ret = AllegatiGalleryType::findOrFail($type);
  23. return $ret->name;
  24. }
  25. return "";
  26. }
  27. public function addAllegato($type)
  28. {
  29. $this->allegatoId = 0;
  30. $this->allegatoType = $type;
  31. $this->allegatoGallery = 0;
  32. $this->allegatoName = '';
  33. $this->allegatoVisible = false;
  34. $this->allegatoFiles = '';
  35. $this->allegatiFiles = [];
  36. $this->emit('attachments', "");
  37. }
  38. public function saveAllegato()
  39. {
  40. if ($this->allegatoGallery == 1) {
  41. foreach ($this->allegatiFiles as $file) {
  42. $extension = pathinfo($file, PATHINFO_EXTENSION);
  43. if (!in_array(strtolower($extension), $this->imageExtensions)) {
  44. session()->flash('error', 'I File devono essere immagini per Tipologia: Foto sinistro');
  45. return;
  46. }
  47. }
  48. }
  49. $files = implode("|", $this->allegatiFiles);
  50. if ($this->allegatoId > 0) {
  51. ReportAllegatiGallery::where('id', $this->allegatoId)->update([
  52. 'file_type' => $this->allegatoType,
  53. 'gallery_type' => $this->allegatoGallery,
  54. 'name' => $this->allegatoName,
  55. 'is_visible' => $this->allegatoVisible,
  56. 'files' => $files,
  57. 'state' => 0
  58. ]);
  59. } else {
  60. ReportAllegatiGallery::create([
  61. 'report_id' => $this->dataId,
  62. 'file_type' => $this->allegatoType,
  63. 'gallery_type' => $this->allegatoGallery,
  64. 'name' => $this->allegatoName,
  65. 'is_visible' => $this->allegatoVisible,
  66. 'files' => $files,
  67. 'state' => 0,
  68. 'created' => date("Y-m-d H:i:s"),
  69. 'created_by' => 0
  70. ]);
  71. }
  72. $this->resetAllegatoFields();
  73. $this->refreshAllegatiCollections();
  74. $this->emit('close-modal');
  75. }
  76. private function resetAllegatoFields()
  77. {
  78. $this->allegatoId = 0;
  79. $this->allegatoGallery = 0;
  80. $this->allegatoName = '';
  81. $this->allegatoVisible = false;
  82. $this->allegatoFiles = '';
  83. $this->allegatiFiles = [];
  84. $this->allegatoType = 0;
  85. }
  86. private function refreshAllegatiCollections()
  87. {
  88. $this->allegatiImmagini = ReportAllegatiGallery::where('report_id', $this->dataId)
  89. ->where('file_type', 0)
  90. ->orderBy('name')
  91. ->get();
  92. $this->allegatiDocumenti = ReportAllegatiGallery::where('report_id', $this->dataId)
  93. ->where('file_type', 1)
  94. ->orderBy('name')
  95. ->get();
  96. }
  97. public function editAllegato($id)
  98. {
  99. $this->emit('attachments', "");
  100. $a = ReportAllegatiGallery::where('id', $id)->first();
  101. if ($a != null) {
  102. $this->allegatoId = $id;
  103. $this->allegatoType = $a->file_type;
  104. $this->allegatoGallery = $a->gallery_type;
  105. $this->allegatoName = $a->name;
  106. $this->allegatoFiles = $a->files;
  107. $this->allegatiFiles = explode("|", $this->allegatoFiles);
  108. $this->emit('attachments', $this->allegatoFiles);
  109. }
  110. }
  111. public function removeAllegato($id)
  112. {
  113. ReportAllegatiGallery::findOrFail($id)->delete();
  114. $this->refreshAllegatiCollections();
  115. }
  116. public function updatedAllegati()
  117. {
  118. foreach ($this->allegati as $allegato) {
  119. $name = $allegato->getClientOriginalName();
  120. $allegato->storeAs('', $name, 'public');
  121. $this->allegatiFiles[] = $name;
  122. }
  123. $this->emit('attachments', implode("|", $this->allegatiFiles));
  124. $this->allegati = [];
  125. }
  126. }