| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace App\Http\Livewire\Traits;
- use App\Models\AllegatiGalleryType;
- use App\Models\ReportAllegatiGallery;
- use Livewire\WithFileUploads;
- trait HasAllegato
- {
- 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'];
- public function getAllegatoType($type)
- {
- if ($type > 0) {
- $ret = AllegatiGalleryType::findOrFail($type);
- return $ret->name;
- }
- return "";
- }
- public function addAllegato($type)
- {
- $this->allegatoId = 0;
- $this->allegatoType = $type;
- $this->allegatoGallery = 0;
- $this->allegatoName = '';
- $this->allegatoVisible = false;
- $this->allegatoFiles = '';
- $this->allegatiFiles = [];
- $this->emit('attachments', "");
- }
- public function saveAllegato()
- {
- if ($this->allegatoGallery == 1) {
- foreach ($this->allegatiFiles as $file) {
- $extension = pathinfo($file, PATHINFO_EXTENSION);
- if (!in_array(strtolower($extension), $this->imageExtensions)) {
- session()->flash('error', 'I File devono essere immagini per Tipologia: foto sinistro');
- return;
- }
- }
- }
- $files = implode("|", $this->allegatiFiles);
- 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
- ]);
- }
- $this->resetAllegatoFields();
- $this->refreshAllegatiCollections();
- $this->emit('close-modal');
- }
- private function resetAllegatoFields()
- {
- $this->allegatoId = 0;
- $this->allegatoGallery = 0;
- $this->allegatoName = '';
- $this->allegatoVisible = false;
- $this->allegatoFiles = '';
- $this->allegatiFiles = [];
- $this->allegatoType = 0;
- }
- 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();
- }
- public function editAllegato($id)
- {
- $this->emit('attachments', "");
- $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->allegatoFiles = $a->files;
- $this->allegatiFiles = explode("|", $this->allegatoFiles);
- $this->emit('attachments', $this->allegatoFiles);
- }
- }
- public function removeAllegato($id)
- {
- ReportAllegatiGallery::findOrFail($id)->delete();
- $this->refreshAllegatiCollections();
- }
- public function updatedAllegati()
- {
- foreach ($this->allegati as $allegato) {
- $name = $allegato->getClientOriginalName();
- $allegato->storeAs('', $name, 'public');
- $this->allegatiFiles[] = $name;
- }
- $this->emit('attachments', implode("|", $this->allegatiFiles));
- $this->allegati = [];
- }
- }
|