View.php
1 <?php 2 3 namespace App\Http\Livewire\Documents\Document; 4 5 use App\Enums\ReviewTypeEnum; 6 use App\Helpers\DocGen\DocumentDownloader; 7 use App\Helpers\DocxToPdfConverter; 8 use App\Helpers\PdfSigner; 9 use App\Http\Services\DocumentService; 10 use App\Models\Document; 11 use Closure; 12 use Exception; 13 use finfo; 14 use Illuminate\Foundation\Auth\Access\AuthorizesRequests; 15 use Illuminate\Support\Arr; 16 use Illuminate\Support\Facades\Auth; 17 use Illuminate\Support\Facades\Gate; 18 use Illuminate\Support\Facades\Storage; 19 use Livewire\Component; 20 use Livewire\WithFileUploads; 21 use WireUi\Traits\Actions; 22 23 24 class View extends Component 25 { 26 use AuthorizesRequests, Actions, WithFileUploads; 27 28 public $entityId; 29 /** 30 * @var Document|null 31 */ 32 public $doc = null; 33 34 public array $uploadDocx = [ 35 'file' => null, 36 'description' => '', 37 ]; 38 39 public bool $btnIsDisabled = false; 40 41 protected function getListeners() 42 { 43 return ['refresh' => '$refresh']; 44 } 45 46 public function mount() 47 { 48 $this->doc = Document::findOrFail($this->entityId); 49 $this->authorize('view', $this->doc); 50 } 51 52 public function download() 53 { 54 return response()->download(Storage::path($this->doc->head->path), $this->doc->downloadName); 55 } 56 57 public function approve() 58 { 59 Gate::allowIf($this->doc->roleCanApprove(auth()->user())); 60 61 if (!$this->checkGpg()) { 62 return; 63 } 64 65 //$this->emitUp('closePanel'); 66 67 $this->emit( 68 'openPanel', 69 __('documents.approval.panel.title'), 70 'documents.document.review', 71 $this->entityId, 72 '', 73 'approval' 74 ); 75 } 76 77 public function review() 78 { 79 Gate::allowIf($this->doc->roleCanReview(auth()->user())); 80 81 if (!$this->checkGpg()) { 82 return; 83 } 84 85 $this->emitUp('closePanel'); 86 87 $this->emit( 88 'openPanel', 89 __('documents.review.panel.title'), 90 'documents.document.review', 91 $this->entityId, 92 '', 93 'review' 94 ); 95 } 96 97 public function confirmDraft() 98 { 99 $this->dialog()->confirm([ 100 'title' => __('documents.confirm_draft.title'), 101 'description' => __('documents.confirm_draft.description',), 102 'acceptLabel' => __('documents.confirm_draft.accept'), 103 'rejectLabel' => __('documents.confirm_draft.reject'), 104 'method' => 'confirmedToUndraft', 105 ]); 106 } 107 108 public function confirmedToUndraft() 109 { 110 DocumentService::removeDraft($this->doc); 111 $this->notification()->success( 112 __('documents.notifications.remove_draft.success.title'), 113 __('documents.notifications.remove_draft.success.message') 114 ); 115 $this->doc->refresh(); 116 } 117 118 public function canUndraft() 119 { 120 return (Auth::id() == $this->doc->created_by) && $this->doc->isDraft(); 121 } 122 123 public function checkGpg() 124 { 125 if (auth()->user()->activeKey) { 126 return true; 127 } else { 128 $this->notification()->error( 129 'Error', 130 __('documents.review.panel.errors.no-gpg') 131 ); 132 return false; 133 } 134 } 135 136 public function readJson() 137 { 138 return redirect()->to(route('jsons.view', ['id' => (int)$this->doc->head->path])); 139 } 140 141 public function uploadDocx() 142 { 143 $this->validate([ 144 'uploadDocx.file' => 'required|file', 145 ]); 146 147 try { 148 DocumentService::uploadDocx($this->doc, $this->uploadDocx); 149 $this->notification()->success( 150 __('documents.notifications.upload.success.title'), 151 __('documents.notifications.upload.success.message') 152 ); 153 $this->emitSelf('refresh'); 154 } catch (\Exception $e) { 155 $this->doc->refresh(); 156 157 logger()->error($e); 158 $this->notification()->error( 159 'Error', 160 $e->getMessage() 161 ); 162 } 163 } 164 165 public function confirmCreatePdf() 166 { 167 if ($this->doc->hasDeletedRevisions()) { 168 $this->dialog()->confirm([ 169 'title' => __('documents.confirm_pdf.title'), 170 'description' => __('documents.confirm_pdf.description',), 171 'acceptLabel' => __('documents.confirm_pdf.accept'), 172 'rejectLabel' => __('documents.confirm_pdf.reject'), 173 'method' => 'createPDF', 174 ]); 175 } else { 176 $this->createPDF(); 177 } 178 } 179 180 public function createPDF() 181 { 182 try { 183 DocumentService::createPDF($this->doc); 184 $this->notification()->success( 185 __('documents.notifications.create_pdf.success.title'), 186 __('documents.notifications.create_pdf.success.message') 187 ); 188 189 $this->emitSelf('refresh'); 190 $this->emit('documentConvertedToPdf'); 191 } catch (\Exception $e) { 192 $this->doc->refresh(); 193 194 logger()->error($e); 195 $this->notification()->error( 196 'Error', 197 $e->getMessage() 198 ); 199 } 200 } 201 202 public function redirectVersions() 203 { 204 set_dossier_in_session_for_breadcrumb(); 205 return redirect()->route('document.versions.index', ['document' => $this->entityId]); 206 } 207 208 public function redirectOutPuts() 209 { 210 set_dossier_in_session_for_breadcrumb(); 211 return redirect()->route('document.out-puts.index', ['document' => $this->entityId]); 212 } 213 214 public function render() 215 { 216 return view('livewire.documents.document.view'); 217 } 218 }