|
@@ -0,0 +1,132 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Http\Livewire;
|
|
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
|
|
+use App\Http\Middleware\TenantMiddleware;
|
|
|
|
|
+use Livewire\Component;
|
|
|
|
|
+
|
|
|
|
|
+class Court extends Component
|
|
|
|
|
+{
|
|
|
|
|
+ public $records, $name, $enabled, $dataId, $update = false, $add = false;
|
|
|
|
|
+
|
|
|
|
|
+ protected $rules = [
|
|
|
|
|
+ 'name' => 'required'
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ protected $messages = [
|
|
|
|
|
+ 'name.required' => 'Il nome è obbligatorio'
|
|
|
|
|
+ ];
|
|
|
|
|
+ public function boot()
|
|
|
|
|
+ {
|
|
|
|
|
+ app(TenantMiddleware::class)->setupTenantConnection();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function mount(){
|
|
|
|
|
+
|
|
|
|
|
+ if(Auth::user()->level != env('LEVEL_ADMIN', 0))
|
|
|
|
|
+ return redirect()->to('/dashboard');
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public $sortField ='name';
|
|
|
|
|
+ public $sortAsc = true;
|
|
|
|
|
+
|
|
|
|
|
+ public function sortBy($field)
|
|
|
|
|
+ {
|
|
|
|
|
+ if($this->sortField === $field)
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->sortAsc = ! $this->sortAsc;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->sortAsc = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->sortField = $field;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function resetFields(){
|
|
|
|
|
+ $this->name = '';
|
|
|
|
|
+ $this->enabled = true;
|
|
|
|
|
+ $this->emit('load-data-table');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function render()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->records = \App\Models\Court::select('id', 'name', 'enabled')->get();
|
|
|
|
|
+ return view('livewire.court');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function add()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->resetFields();
|
|
|
|
|
+ $this->add = true;
|
|
|
|
|
+ $this->update = false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function store()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->validate();
|
|
|
|
|
+ try {
|
|
|
|
|
+ \App\Models\Court::create([
|
|
|
|
|
+ 'name' => $this->name,
|
|
|
|
|
+ 'enabled' => $this->enabled
|
|
|
|
|
+ ]);
|
|
|
|
|
+ session()->flash('success','Dato creato');
|
|
|
|
|
+ $this->resetFields();
|
|
|
|
|
+ $this->add = false;
|
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
|
+ session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function edit($id){
|
|
|
|
|
+ try {
|
|
|
|
|
+ $court = \App\Models\Court::findOrFail($id);
|
|
|
|
|
+ if( !$court) {
|
|
|
|
|
+ session()->flash('error','Dato non trovato');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $this->name = $court->name;
|
|
|
|
|
+ $this->enabled = $court->enabled;
|
|
|
|
|
+ $this->dataId = $court->id;
|
|
|
|
|
+ $this->update = true;
|
|
|
|
|
+ $this->add = false;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
|
+ session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function update()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->validate();
|
|
|
|
|
+ try {
|
|
|
|
|
+ \App\Models\Court::whereId($this->dataId)->update([
|
|
|
|
|
+ 'name' => $this->name,
|
|
|
|
|
+ 'enabled' => $this->enabled
|
|
|
|
|
+ ]);
|
|
|
|
|
+ session()->flash('success','Dato aggiornato');
|
|
|
|
|
+ $this->resetFields();
|
|
|
|
|
+ $this->update = false;
|
|
|
|
|
+ } catch (\Exception $ex) {
|
|
|
|
|
+ session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function cancel()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->add = false;
|
|
|
|
|
+ $this->update = false;
|
|
|
|
|
+ $this->resetFields();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function delete($id)
|
|
|
|
|
+ {
|
|
|
|
|
+ try{
|
|
|
|
|
+ \App\Models\CourseLevel::find($id)->delete();
|
|
|
|
|
+ session()->flash('success',"Dato eliminato");
|
|
|
|
|
+ return redirect(request()->header('Referer'));
|
|
|
|
|
+ //return redirect()->to(\Illuminate\Http\Request::url());
|
|
|
|
|
+ }catch(\Exception $e){
|
|
|
|
|
+ session()->flash('error','Errore (' . $e->getMessage() . ')');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|