Update.php
1 <?php 2 3 namespace App\Http\Livewire\Documents\Document; 4 5 use App\Http\Services\DocumentService; 6 use App\Http\Services\InboxFileService; 7 use App\Models\Document; 8 use App\Models\InboxFile; 9 use App\Helpers\InboxFileSaver; 10 use Illuminate\Foundation\Auth\Access\AuthorizesRequests; 11 use Livewire\Component; 12 use Livewire\WithFileUploads; 13 use WireUi\Traits\Actions; 14 15 class Update extends Component 16 { 17 use AuthorizesRequests, Actions, WithFileUploads; 18 19 public int $entityId = 0; 20 public ?Document $document = null; 21 public $file = null; 22 public $name = null; 23 public $version = null; 24 public ?string $description = null; 25 public bool $btnIsDisabled = false; 26 27 public function mount() 28 { 29 $this->document = Document::find($this->entityId); 30 $this->authorize('update', $this->document); 31 $this->description = $this->document?->head?->description; 32 $this->name = $this->document?->head?->name; 33 } 34 35 public function update() 36 { 37 $this->authorize('update', $this->document); 38 $this->validate([ 39 'file' => 'nullable|file', 40 'description' => 'nullable|string|max:255', 41 ]); 42 43 try { 44 if ($this->file) { 45 $path = $this->file->storeAs('documents', $this->file->getClientOriginalName(), 'local'); 46 $data['path'] = $path; 47 $data['description'] = $this->description; 48 $data['name'] = $this->name; 49 50 $revision = DocumentService::addRevision($this->entityId, $data, $this->document->head->version + 1); 51 52 if ($inboxfile = $this->document->inboxfile) { 53 $newVersion = extract_pol_version($this->file->getClientOriginalName()); 54 $existingVersion = extract_pol_version($inboxfile->name); 55 throw_if(!$newVersion && $existingVersion, __('inbox-files.insert_version')); 56 57 InboxFileService::newVersion( 58 $inboxfile, 59 auth()->id(), 60 $this->name, 61 $revision->path, 62 $newVersion 63 ); 64 } 65 } else { 66 $revision = Document::find($this->entityId)->head; 67 DocumentService::updateRevision($revision->id, $this->name, $this->description); 68 } 69 70 // UI Trick for delaying spinner 71 if (config('app.env') !== 'testing') { 72 sleep(1); 73 } 74 75 $this->emitUp('closePanel'); 76 $this->emit('documentUpdated'); 77 78 $this->notification()->success( 79 __('documents.notifications.restore.success.title'), 80 __('documents.notifications.restore.success.message') 81 ); 82 } catch (\Exception $e) { 83 $this->notification()->error( 84 'Error', 85 $e->getMessage() 86 ); 87 } 88 } 89 90 public function updatedFile() 91 { 92 if ($this->file) { 93 $this->name = $this->file->getClientOriginalName(); 94 } 95 } 96 97 98 public function render() 99 { 100 return view('livewire.documents.document.update'); 101 } 102 }