PolSettings.php
1 <?php 2 3 namespace App\Http\Livewire\App; 4 5 use App\Http\Services\PolService; 6 use Closure; 7 use Illuminate\Support\Facades\Storage; 8 use Livewire\Component; 9 use Livewire\WithFileUploads; 10 use WireUi\Traits\Actions; 11 12 class PolSettings extends Component 13 { 14 use Actions, WithFileUploads; 15 16 public $polFile; 17 18 public bool $btnIsDisabled = false; 19 20 public function getIsTemplateProperty() 21 { 22 return !Storage::exists('settings/POL_json/POL.json'); 23 } 24 25 public function resetPol() 26 { 27 $this->dialog()->confirm([ 28 'title' => __('settings.pol.actions.resetConfirmation'), 29 'acceptLabel' => __('inbox-files.dialog.confirm.submit'), 30 'rejectLabel' => __('inbox-files.dialog.confirm.cancel'), 31 'method' => 'resetPolConfirmed', 32 ]); 33 } 34 35 public function resetPolConfirmed() 36 { 37 Storage::delete('settings/POL_json/POL.json'); 38 $this->notification()->success(__('settings.pol.reseted')); 39 } 40 41 public function downloadPol() 42 { 43 if (Storage::exists('settings/POL_json/POL.json')) { 44 return Storage::download('settings/POL_json/POL.json'); 45 } 46 47 return Storage::download('templates/POL_json/POL.json'); 48 } 49 50 public function uploadPol() 51 { 52 $this->validate([ 53 'polFile' => [ 54 'required', 'file', 55 function (string $attribute, mixed $value, Closure $fail) { 56 $content = file_get_contents($value->getPath() . '/' . $value->getFilename()); 57 $json = json_decode($content, true); 58 if ($json === null) $fail(__('settings.pol.errors.json', ['attribute' => $attribute])); 59 }, 60 ], 61 ]); 62 $this->resetErrorBag('polFile'); 63 $this->polFile->storeAs('settings/POL_json', 'POL.json'); 64 $this->notification()->success(__('settings.pol.success')); 65 $this->dispatchBrowserEvent('reset-file'); 66 } 67 68 public function render() 69 { 70 return view('livewire.app.pol-settings'); 71 } 72 }