|
|
@@ -5,12 +5,9 @@ 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;
|
|
|
@@ -24,80 +21,6 @@ trait HasAllegato
|
|
|
|
|
|
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) {
|
|
|
@@ -109,97 +32,56 @@ trait HasAllegato
|
|
|
|
|
|
public function addAllegato($type)
|
|
|
{
|
|
|
- $this->resetAllegatoFields();
|
|
|
+ $this->allegatoId = 0;
|
|
|
$this->allegatoType = $type;
|
|
|
+ $this->allegatoGallery = 0;
|
|
|
+ $this->allegatoName = '';
|
|
|
+ $this->allegatoVisible = false;
|
|
|
+ $this->allegatoFiles = '';
|
|
|
+ $this->allegatiFiles = [];
|
|
|
$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)) {
|
|
|
+ if ($this->allegatoGallery == 1) {
|
|
|
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()
|
|
|
+ if ($this->allegatoId > 0) {
|
|
|
+ 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 {
|
|
|
+ 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
|
|
|
]);
|
|
|
- session()->flash('error', 'Errore durante il salvataggio: ' . $e->getMessage());
|
|
|
}
|
|
|
+
|
|
|
+ $this->resetAllegatoFields();
|
|
|
+ $this->refreshAllegatiCollections();
|
|
|
+ $this->emit('close-modal');
|
|
|
}
|
|
|
|
|
|
private function resetAllegatoFields()
|
|
|
@@ -211,10 +93,6 @@ trait HasAllegato
|
|
|
$this->allegatoFiles = '';
|
|
|
$this->allegatiFiles = [];
|
|
|
$this->allegatoType = 0;
|
|
|
- $this->allegati = [];
|
|
|
- $this->resetErrorBag();
|
|
|
-
|
|
|
- Log::info('Allegato fields reset');
|
|
|
}
|
|
|
|
|
|
private function refreshAllegatiCollections()
|
|
|
@@ -228,22 +106,11 @@ trait HasAllegato
|
|
|
->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) {
|
|
|
@@ -251,75 +118,26 @@ trait HasAllegato
|
|
|
$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());
|
|
|
- }
|
|
|
+ ReportAllegatiGallery::findOrFail($id)->delete();
|
|
|
+ $this->refreshAllegatiCollections();
|
|
|
}
|
|
|
|
|
|
- public function removeFile($filename)
|
|
|
+ public function updatedAllegati()
|
|
|
{
|
|
|
- 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
|
|
|
- ]);
|
|
|
+ foreach ($this->allegati as $allegato) {
|
|
|
+ $name = $allegato->getClientOriginalName();
|
|
|
+ $allegato->storeAs('', $name, 'public');
|
|
|
+ $this->allegatiFiles[] = $name;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- public function closeAllegatiModal()
|
|
|
- {
|
|
|
- Log::info('Closing allegati modal');
|
|
|
- $this->resetAllegatoFields();
|
|
|
- $this->emit('close-modal');
|
|
|
+ $this->emit('attachments', implode("|", $this->allegatiFiles));
|
|
|
+ $this->allegati = [];
|
|
|
}
|
|
|
}
|