| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- use App\Http\Middleware\TenantMiddleware;
- use Livewire\WithPagination;
- use Illuminate\Support\Facades\Auth;
- class CourseType extends Component
- {
- public $records, $name, $duration, $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->duration = 0;
- $this->enabled = true;
- $this->emit('load-data-table');
- }
- public function render()
- {
- $this->records = \App\Models\CourseType::select('id', 'name', 'duration', 'enabled')->get();
- return view('livewire.course_type');
- }
- public function add()
- {
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- }
- public function store()
- {
- $this->validate();
- try {
- \App\Models\CourseType::create([
- 'name' => $this->name,
- 'duration' => $this->duration,
- '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 {
- $course_type = \App\Models\CourseType::findOrFail($id);
- if( !$course_type) {
- session()->flash('error','Dato non trovato');
- } else {
- $this->name = $course_type->name;
- $this->duration = $course_type->duration;
- $this->enabled = $course_type->enabled;
- $this->dataId = $course_type->id;
- $this->update = true;
- $this->add = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->validate();
- try {
- \App\Models\CourseType::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'duration' => $this->duration,
- '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\CourseType::find($id)->delete();
- session()->flash('success',"Dato eliminato");
- return redirect(request()->header('Referer'));
- }catch(\Exception $e){
- session()->flash('error','Errore (' . $e->getMessage() . ')');
- }
- }
- }
|