| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- namespace App\Http\Livewire\Traits;
- use App\Models\AllegatiGalleryType;
- use App\Models\ReportAllegatiGallery;
- use Livewire\WithFileUploads;
- use Illuminate\Support\Facades\Log;
- trait HasAllegato
- {
- use WithFileUploads;
- public $allegatoId = 0;
- public $allegatoType = 0;
- public $allegatoGallery = 0;
- public $allegatoName = '';
- public $allegatoVisible = false;
- public $allegatoFiles = '';
- public $allegatiFiles = [];
- public $allegatiImmagini;
- public $allegatiDocumenti;
- public $allegati = [];
- protected $imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp'];
- protected function getAllegatiRules()
- {
- return [
- 'allegati.*' => 'file|max:2048',// 2MB max per file
- ];
- }
- public function updatedAllegati()
- {
- $this->resetErrorBag('allegati');
- Log::info('File upload started', [
- 'files_count' => is_array($this->allegati) ? count($this->allegati) : 'No files',
- ]);
- try {
- $this->validate($this->getAllegatiRules(), [
- 'allegati.*.file' => 'Il file deve essere valido',
- 'allegati.*.max' => 'Il file non può superare 2MB',
- ]);
- if (!empty($this->allegati)) {
- foreach ($this->allegati as $index => $allegato) {
- try {
- $size = $allegato->getSize();
- Log::info('Processing file', [
- 'index' => $index,
- 'original_name' => $allegato->getClientOriginalName(),
- 'size' => $size,
- 'size_mb' => round($size / 1024 / 1024, 2) . 'MB',
- ]);
- $name = $allegato->getClientOriginalName();
- $allegato->storeAs('', $name, 'public');
- $this->allegatiFiles[] = $name;
- Log::info('File stored successfully', [
- 'name' => $name
- ]);
- } catch (\Exception $e) {
- Log::error('File upload error', [
- 'index' => $index,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- session()->flash('error', 'Errore durante il caricamento del file: ' . $e->getMessage());
- }
- }
- $this->emit('attachments', implode("|", $this->allegatiFiles));
- $this->allegati = [];
- Log::info('All files processed', [
- 'total_files' => count($this->allegatiFiles)
- ]);
- }
- } catch (\Exception $e) {
- Log::error('Validation error', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- session()->flash('error', 'Errore di validazione: ' . $e->getMessage());
- }
- }
- public function updatedAllegatoGallery()
- {
- $this->resetErrorBag('allegatoGallery');
- }
- public function updatedAllegatoName()
- {
- $this->resetErrorBag('allegatoName');
- }
- public function getAllegatoType($type)
- {
- if ($type > 0) {
- $ret = AllegatiGalleryType::findOrFail($type);
- return $ret->name;
- }
- return "";
- }
- public function addAllegato($type)
- {
- $this->resetAllegatoFields();
- $this->allegatoType = $type;
- $this->emit('attachments', "");
- $this->emit('open-allegati-modal');
- Log::info('Add allegato modal opened', [
- 'type' => $type
- ]);
- }
- public function saveAllegato()
- {
- Log::info('Save allegato started', [
- 'allegatoName' => $this->allegatoName,
- 'allegatoGallery' => $this->allegatoGallery,
- 'files_count' => count($this->allegatiFiles)
- ]);
- $this->validate([
- 'allegatoName' => 'required',
- 'allegatoGallery' => 'required',
- ], [
- 'allegatoName.required' => 'Il nome è obbligatorio',
- 'allegatoGallery.required' => 'La tipologia è obbligatoria',
- ]);
- if ($this->allegatoGallery == 1 && !empty($this->allegatiFiles)) {
- foreach ($this->allegatiFiles as $file) {
- $extension = pathinfo($file, PATHINFO_EXTENSION);
- if (!in_array(strtolower($extension), $this->imageExtensions)) {
- Log::warning('Invalid file type', [
- 'file' => $file,
- 'extension' => $extension,
- 'allowed_extensions' => $this->imageExtensions
- ]);
- session()->flash('error', 'I File devono essere immagini per Tipologia: Foto sinistro');
- return;
- }
- }
- }
- if (empty($this->allegatiFiles)) {
- $this->addError('allegati', 'È necessario caricare almeno un file');
- Log::warning('No files uploaded');
- return;
- }
- $files = implode("|", $this->allegatiFiles);
- try {
- if ($this->allegatoId > 0) {
- Log::info('Updating existing allegato', [
- 'id' => $this->allegatoId
- ]);
- ReportAllegatiGallery::where('id', $this->allegatoId)->update([
- 'file_type' => $this->allegatoType,
- 'gallery_type' => $this->allegatoGallery,
- 'name' => $this->allegatoName,
- 'is_visible' => $this->allegatoVisible,
- 'files' => $files,
- 'state' => 0
- ]);
- } else {
- Log::info('Creating new allegato');
- ReportAllegatiGallery::create([
- 'report_id' => $this->dataId,
- 'file_type' => $this->allegatoType,
- 'gallery_type' => $this->allegatoGallery,
- 'name' => $this->allegatoName,
- 'is_visible' => $this->allegatoVisible,
- 'files' => $files,
- 'state' => 0,
- 'created' => date("Y-m-d H:i:s"),
- 'created_by' => 0
- ]);
- }
- Log::info('Allegato saved successfully');
- session()->flash('success', 'Allegato salvato con successo');
- $this->resetAllegatoFields();
- $this->refreshAllegatiCollections();
- $this->emit('close-modal');
- } catch (\Exception $e) {
- Log::error('Save error', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- session()->flash('error', 'Errore durante il salvataggio: ' . $e->getMessage());
- }
- }
- private function resetAllegatoFields()
- {
- $this->allegatoId = 0;
- $this->allegatoGallery = 0;
- $this->allegatoName = '';
- $this->allegatoVisible = false;
- $this->allegatoFiles = '';
- $this->allegatiFiles = [];
- $this->allegatoType = 0;
- $this->allegati = [];
- $this->resetErrorBag();
- Log::info('Allegato fields reset');
- }
- private function refreshAllegatiCollections()
- {
- $this->allegatiImmagini = ReportAllegatiGallery::where('report_id', $this->dataId)
- ->where('file_type', 0)
- ->orderBy('name')
- ->get();
- $this->allegatiDocumenti = ReportAllegatiGallery::where('report_id', $this->dataId)
- ->where('file_type', 1)
- ->orderBy('name')
- ->get();
- Log::info('Allegati collections refreshed', [
- 'immagini_count' => $this->allegatiImmagini->count(),
- 'documenti_count' => $this->allegatiDocumenti->count()
- ]);
- }
- public function editAllegato($id)
- {
- $this->resetErrorBag();
- $this->emit('attachments', "");
- Log::info('Edit allegato started', [
- 'id' => $id
- ]);
- $a = ReportAllegatiGallery::where('id', $id)->first();
- if ($a != null) {
- $this->allegatoId = $id;
- $this->allegatoType = $a->file_type;
- $this->allegatoGallery = $a->gallery_type;
- $this->allegatoName = $a->name;
- $this->allegatoVisible = $a->is_visible;
- $this->allegatoFiles = $a->files;
- $this->allegatiFiles = explode("|", $this->allegatoFiles);
- Log::info('Allegato loaded for editing', [
- 'name' => $this->allegatoName,
- 'files_count' => count($this->allegatiFiles)
- ]);
- $this->emit('attachments', $this->allegatoFiles);
- $this->emit('open-allegati-modal');
- } else {
- Log::warning('Allegato not found', [
- 'id' => $id
- ]);
- }
- }
- public function removeAllegato($id)
- {
- try {
- Log::info('Remove allegato started', [
- 'id' => $id
- ]);
- ReportAllegatiGallery::findOrFail($id)->delete();
- Log::info('Allegato removed successfully');
- session()->flash('success', 'Allegato eliminato con successo');
- $this->refreshAllegatiCollections();
- } catch (\Exception $e) {
- Log::error('Remove error', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- session()->flash('error', 'Errore durante l\'eliminazione: ' . $e->getMessage());
- }
- }
- public function removeFile($filename)
- {
- Log::info('Remove file started', [
- 'filename' => $filename
- ]);
- $index = array_search($filename, $this->allegatiFiles);
- if ($index !== false) {
- unset($this->allegatiFiles[$index]);
- $this->allegatiFiles = array_values($this->allegatiFiles);
- $this->allegatoFiles = implode("|", $this->allegatiFiles);
- Log::info('File removed from list', [
- 'remaining_files' => count($this->allegatiFiles)
- ]);
- $this->emit('attachments', $this->allegatoFiles);
- } else {
- Log::warning('File not found in list', [
- 'filename' => $filename,
- 'current_files' => $this->allegatiFiles
- ]);
- }
- }
- public function closeAllegatiModal()
- {
- Log::info('Closing allegati modal');
- $this->resetAllegatoFields();
- $this->emit('close-modal');
- }
- }
|