HasAllegato.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace App\Http\Livewire\Traits;
  3. use App\Models\AllegatiGalleryType;
  4. use App\Models\ReportAllegatiGallery;
  5. use Livewire\WithFileUploads;
  6. use Illuminate\Support\Facades\Log;
  7. trait HasAllegato
  8. {
  9. use WithFileUploads;
  10. public $allegatoId = 0;
  11. public $allegatoType = 0;
  12. public $allegatoGallery = 0;
  13. public $allegatoName = '';
  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. protected function getAllegatiRules()
  22. {
  23. return [
  24. 'allegati.*' => 'file|max:2048',// 2MB max per file
  25. ];
  26. }
  27. public function updatedAllegati()
  28. {
  29. $this->resetErrorBag('allegati');
  30. Log::info('File upload started', [
  31. 'files_count' => is_array($this->allegati) ? count($this->allegati) : 'No files',
  32. ]);
  33. try {
  34. $this->validate($this->getAllegatiRules(), [
  35. 'allegati.*.file' => 'Il file deve essere valido',
  36. 'allegati.*.max' => 'Il file non può superare 2MB',
  37. ]);
  38. if (!empty($this->allegati)) {
  39. foreach ($this->allegati as $index => $allegato) {
  40. try {
  41. $size = $allegato->getSize();
  42. Log::info('Processing file', [
  43. 'index' => $index,
  44. 'original_name' => $allegato->getClientOriginalName(),
  45. 'size' => $size,
  46. 'size_mb' => round($size / 1024 / 1024, 2) . 'MB',
  47. ]);
  48. $name = $allegato->getClientOriginalName();
  49. $allegato->storeAs('', $name, 'public');
  50. $this->allegatiFiles[] = $name;
  51. Log::info('File stored successfully', [
  52. 'name' => $name
  53. ]);
  54. } catch (\Exception $e) {
  55. Log::error('File upload error', [
  56. 'index' => $index,
  57. 'error' => $e->getMessage(),
  58. 'trace' => $e->getTraceAsString()
  59. ]);
  60. session()->flash('error', 'Errore durante il caricamento del file: ' . $e->getMessage());
  61. }
  62. }
  63. $this->emit('attachments', implode("|", $this->allegatiFiles));
  64. $this->allegati = [];
  65. Log::info('All files processed', [
  66. 'total_files' => count($this->allegatiFiles)
  67. ]);
  68. }
  69. } catch (\Exception $e) {
  70. Log::error('Validation error', [
  71. 'error' => $e->getMessage(),
  72. 'trace' => $e->getTraceAsString()
  73. ]);
  74. session()->flash('error', 'Errore di validazione: ' . $e->getMessage());
  75. }
  76. }
  77. public function updatedAllegatoGallery()
  78. {
  79. $this->resetErrorBag('allegatoGallery');
  80. }
  81. public function updatedAllegatoName()
  82. {
  83. $this->resetErrorBag('allegatoName');
  84. }
  85. public function getAllegatoType($type)
  86. {
  87. if ($type > 0) {
  88. $ret = AllegatiGalleryType::findOrFail($type);
  89. return $ret->name;
  90. }
  91. return "";
  92. }
  93. public function addAllegato($type)
  94. {
  95. $this->resetAllegatoFields();
  96. $this->allegatoType = $type;
  97. $this->emit('attachments', "");
  98. $this->emit('open-allegati-modal');
  99. Log::info('Add allegato modal opened', [
  100. 'type' => $type
  101. ]);
  102. }
  103. public function saveAllegato()
  104. {
  105. Log::info('Save allegato started', [
  106. 'allegatoName' => $this->allegatoName,
  107. 'allegatoGallery' => $this->allegatoGallery,
  108. 'files_count' => count($this->allegatiFiles)
  109. ]);
  110. $this->validate([
  111. 'allegatoName' => 'required',
  112. 'allegatoGallery' => 'required',
  113. ], [
  114. 'allegatoName.required' => 'Il nome è obbligatorio',
  115. 'allegatoGallery.required' => 'La tipologia è obbligatoria',
  116. ]);
  117. if ($this->allegatoGallery == 1 && !empty($this->allegatiFiles)) {
  118. foreach ($this->allegatiFiles as $file) {
  119. $extension = pathinfo($file, PATHINFO_EXTENSION);
  120. if (!in_array(strtolower($extension), $this->imageExtensions)) {
  121. Log::warning('Invalid file type', [
  122. 'file' => $file,
  123. 'extension' => $extension,
  124. 'allowed_extensions' => $this->imageExtensions
  125. ]);
  126. session()->flash('error', 'I File devono essere immagini per Tipologia: Foto sinistro');
  127. return;
  128. }
  129. }
  130. }
  131. if (empty($this->allegatiFiles)) {
  132. $this->addError('allegati', 'È necessario caricare almeno un file');
  133. Log::warning('No files uploaded');
  134. return;
  135. }
  136. $files = implode("|", $this->allegatiFiles);
  137. try {
  138. if ($this->allegatoId > 0) {
  139. Log::info('Updating existing allegato', [
  140. 'id' => $this->allegatoId
  141. ]);
  142. ReportAllegatiGallery::where('id', $this->allegatoId)->update([
  143. 'file_type' => $this->allegatoType,
  144. 'gallery_type' => $this->allegatoGallery,
  145. 'name' => $this->allegatoName,
  146. 'is_visible' => $this->allegatoVisible,
  147. 'files' => $files,
  148. 'state' => 0
  149. ]);
  150. } else {
  151. Log::info('Creating new allegato');
  152. ReportAllegatiGallery::create([
  153. 'report_id' => $this->dataId,
  154. 'file_type' => $this->allegatoType,
  155. 'gallery_type' => $this->allegatoGallery,
  156. 'name' => $this->allegatoName,
  157. 'is_visible' => $this->allegatoVisible,
  158. 'files' => $files,
  159. 'state' => 0,
  160. 'created' => date("Y-m-d H:i:s"),
  161. 'created_by' => 0
  162. ]);
  163. }
  164. Log::info('Allegato saved successfully');
  165. session()->flash('success', 'Allegato salvato con successo');
  166. $this->resetAllegatoFields();
  167. $this->refreshAllegatiCollections();
  168. $this->emit('close-modal');
  169. } catch (\Exception $e) {
  170. Log::error('Save error', [
  171. 'error' => $e->getMessage(),
  172. 'trace' => $e->getTraceAsString()
  173. ]);
  174. session()->flash('error', 'Errore durante il salvataggio: ' . $e->getMessage());
  175. }
  176. }
  177. private function resetAllegatoFields()
  178. {
  179. $this->allegatoId = 0;
  180. $this->allegatoGallery = 0;
  181. $this->allegatoName = '';
  182. $this->allegatoVisible = false;
  183. $this->allegatoFiles = '';
  184. $this->allegatiFiles = [];
  185. $this->allegatoType = 0;
  186. $this->allegati = [];
  187. $this->resetErrorBag();
  188. Log::info('Allegato fields reset');
  189. }
  190. private function refreshAllegatiCollections()
  191. {
  192. $this->allegatiImmagini = ReportAllegatiGallery::where('report_id', $this->dataId)
  193. ->where('file_type', 0)
  194. ->orderBy('name')
  195. ->get();
  196. $this->allegatiDocumenti = ReportAllegatiGallery::where('report_id', $this->dataId)
  197. ->where('file_type', 1)
  198. ->orderBy('name')
  199. ->get();
  200. Log::info('Allegati collections refreshed', [
  201. 'immagini_count' => $this->allegatiImmagini->count(),
  202. 'documenti_count' => $this->allegatiDocumenti->count()
  203. ]);
  204. }
  205. public function editAllegato($id)
  206. {
  207. $this->resetErrorBag();
  208. $this->emit('attachments', "");
  209. Log::info('Edit allegato started', [
  210. 'id' => $id
  211. ]);
  212. $a = ReportAllegatiGallery::where('id', $id)->first();
  213. if ($a != null) {
  214. $this->allegatoId = $id;
  215. $this->allegatoType = $a->file_type;
  216. $this->allegatoGallery = $a->gallery_type;
  217. $this->allegatoName = $a->name;
  218. $this->allegatoVisible = $a->is_visible;
  219. $this->allegatoFiles = $a->files;
  220. $this->allegatiFiles = explode("|", $this->allegatoFiles);
  221. Log::info('Allegato loaded for editing', [
  222. 'name' => $this->allegatoName,
  223. 'files_count' => count($this->allegatiFiles)
  224. ]);
  225. $this->emit('attachments', $this->allegatoFiles);
  226. $this->emit('open-allegati-modal');
  227. } else {
  228. Log::warning('Allegato not found', [
  229. 'id' => $id
  230. ]);
  231. }
  232. }
  233. public function removeAllegato($id)
  234. {
  235. try {
  236. Log::info('Remove allegato started', [
  237. 'id' => $id
  238. ]);
  239. ReportAllegatiGallery::findOrFail($id)->delete();
  240. Log::info('Allegato removed successfully');
  241. session()->flash('success', 'Allegato eliminato con successo');
  242. $this->refreshAllegatiCollections();
  243. } catch (\Exception $e) {
  244. Log::error('Remove error', [
  245. 'error' => $e->getMessage(),
  246. 'trace' => $e->getTraceAsString()
  247. ]);
  248. session()->flash('error', 'Errore durante l\'eliminazione: ' . $e->getMessage());
  249. }
  250. }
  251. public function removeFile($filename)
  252. {
  253. Log::info('Remove file started', [
  254. 'filename' => $filename
  255. ]);
  256. $index = array_search($filename, $this->allegatiFiles);
  257. if ($index !== false) {
  258. unset($this->allegatiFiles[$index]);
  259. $this->allegatiFiles = array_values($this->allegatiFiles);
  260. $this->allegatoFiles = implode("|", $this->allegatiFiles);
  261. Log::info('File removed from list', [
  262. 'remaining_files' => count($this->allegatiFiles)
  263. ]);
  264. $this->emit('attachments', $this->allegatoFiles);
  265. } else {
  266. Log::warning('File not found in list', [
  267. 'filename' => $filename,
  268. 'current_files' => $this->allegatiFiles
  269. ]);
  270. }
  271. }
  272. public function closeAllegatiModal()
  273. {
  274. Log::info('Closing allegati modal');
  275. $this->resetAllegatoFields();
  276. $this->emit('close-modal');
  277. }
  278. }