HasAllegato.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 $allegatoFotoSinistri = 0;
  12. public $allegatoName = '';
  13. public $allegatoDidascalia = '';
  14. public $allegatoVisible = false;
  15. public $allegatoFiles = '';
  16. public $allegatiFiles = [];
  17. public $allegatiImmagini;
  18. public $allegatiDocumenti;
  19. public $allegati = [];
  20. protected $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'];
  21. public function getAllegatoType($type)
  22. {
  23. if ($type > 0) {
  24. $ret = AllegatiGalleryType::findOrFail($type);
  25. return $ret->name;
  26. }
  27. return "";
  28. }
  29. public function addAllegato($type)
  30. {
  31. $this->allegatoId = 0;
  32. $this->allegatoType = $type;
  33. $this->allegatoGallery = 0;
  34. $this->allegatoFotoSinistri = 0;
  35. $this->allegatoName = '';
  36. $this->allegatoDidsacalia = '';
  37. $this->allegatoVisible = false;
  38. $this->allegatoFiles = '';
  39. $this->allegatiFiles = [];
  40. $this->emit('attachments', "");
  41. }
  42. public function saveAllegato()
  43. {
  44. if ($this->allegatoGallery == 1) {
  45. foreach ($this->allegatiFiles as $file) {
  46. $extension = pathinfo($file, PATHINFO_EXTENSION);
  47. if (!in_array(strtolower($extension), $this->imageExtensions)) {
  48. session()->flash('error', 'I File devono essere immagini per Tipologia: Foto sinistro');
  49. return;
  50. }
  51. }
  52. }
  53. $files = implode("|", $this->allegatiFiles);
  54. if ($this->allegatoId > 0) {
  55. ReportAllegatiGallery::where('id', $this->allegatoId)->update([
  56. 'file_type' => $this->allegatoType,
  57. 'gallery_type' => $this->allegatoGallery,
  58. 'foto_sinistri' => $this->allegatoFotoSinistri,
  59. 'didascalia' => $this->allegatoDidascalia,
  60. 'name' => $this->allegatoName,
  61. 'is_visible' => $this->allegatoVisible,
  62. 'files' => $files,
  63. 'state' => 0
  64. ]);
  65. } else {
  66. ReportAllegatiGallery::create([
  67. 'report_id' => $this->dataId,
  68. 'file_type' => $this->allegatoType,
  69. 'gallery_type' => $this->allegatoGallery,
  70. 'foto_sinistri' => $this->allegatoFotoSinistri,
  71. 'didascalia' => $this->allegatoDidascalia,
  72. 'name' => $this->allegatoName,
  73. 'is_visible' => $this->allegatoVisible,
  74. 'files' => $files,
  75. 'state' => 0,
  76. 'created' => date("Y-m-d H:i:s"),
  77. 'created_by' => 0
  78. ]);
  79. }
  80. $this->resetAllegatoFields();
  81. $this->refreshAllegatiCollections();
  82. $this->emit('close-modal');
  83. }
  84. private function resetAllegatoFields()
  85. {
  86. $this->allegatoId = 0;
  87. $this->allegatoGallery = 0;
  88. $this->allegatoFotoSinistri = 0;
  89. $this->allegatoDidascalia = '';
  90. $this->allegatoName = '';
  91. $this->allegatoVisible = false;
  92. $this->allegatoFiles = '';
  93. $this->allegatiFiles = [];
  94. $this->allegatoType = 0;
  95. }
  96. private function refreshAllegatiCollections()
  97. {
  98. $this->allegatiImmagini = ReportAllegatiGallery::where('report_id', $this->dataId)
  99. ->where('file_type', 0)
  100. ->orderBy('name')
  101. ->get();
  102. $this->allegatiDocumenti = ReportAllegatiGallery::where('report_id', $this->dataId)
  103. ->where('file_type', 1)
  104. ->orderBy('name')
  105. ->get();
  106. }
  107. public function editAllegato($id)
  108. {
  109. $this->emit('attachments', "");
  110. $a = ReportAllegatiGallery::where('id', $id)->first();
  111. if ($a != null) {
  112. $this->allegatoId = $id;
  113. $this->allegatoType = $a->file_type;
  114. $this->allegatoGallery = $a->gallery_type;
  115. $this->allegatoFotoSinistri = $a->foto_sinistri;
  116. $this->allegatoDidascalia = $a->didascalia;
  117. $this->allegatoName = $a->name;
  118. $this->allegatoFiles = $a->files;
  119. $this->allegatiFiles = explode("|", $this->allegatoFiles);
  120. $this->emit('attachments', $this->allegatoFiles);
  121. }
  122. }
  123. public function removeAllegato($id)
  124. {
  125. ReportAllegatiGallery::findOrFail($id)->delete();
  126. $this->refreshAllegatiCollections();
  127. }
  128. public function updatedAllegati()
  129. {
  130. foreach ($this->allegati as $allegato) {
  131. $size = $allegato->getSize();
  132. if ($size < env('FILE_SIZE', 10000000))
  133. {
  134. $name = $allegato->getClientOriginalName();
  135. $allegato->storeAs('', $name, 'public');
  136. $this->allegatiFiles[] = $name;
  137. }
  138. else
  139. {
  140. $this->emit('fileSize', 'Superato il limite di ' . number_format($size / 1000 / 1000, 0) . "MB");
  141. }
  142. }
  143. $this->emit('attachments', implode("|", $this->allegatiFiles));
  144. $this->allegati = [];
  145. }
  146. public function removeFile($filename)
  147. {
  148. $index = array_search($filename, $this->allegatiFiles);
  149. if ($index !== false) {
  150. unset($this->allegatiFiles[$index]);
  151. $this->allegatiFiles = array_values($this->allegatiFiles);
  152. $this->emit('attachments', implode("|", $this->allegatiFiles));
  153. }
  154. }
  155. }