CourseType.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. use Livewire\WithPagination;
  6. use Illuminate\Support\Facades\Auth;
  7. class CourseType extends Component
  8. {
  9. public $records, $name, $duration, $enabled, $dataId, $update = false, $add = false;
  10. protected $rules = [
  11. 'name' => 'required'
  12. ];
  13. protected $messages = [
  14. 'name.required' => 'Il nome è obbligatorio'
  15. ];
  16. public function boot()
  17. {
  18. app(TenantMiddleware::class)->setupTenantConnection();
  19. }
  20. public function mount(){
  21. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  22. return redirect()->to('/dashboard');
  23. }
  24. public $sortField ='name';
  25. public $sortAsc = true;
  26. public function sortBy($field)
  27. {
  28. if($this->sortField === $field)
  29. {
  30. $this->sortAsc = ! $this->sortAsc;
  31. } else {
  32. $this->sortAsc = true;
  33. }
  34. $this->sortField = $field;
  35. }
  36. public function resetFields(){
  37. $this->name = '';
  38. $this->duration = 0;
  39. $this->enabled = true;
  40. $this->emit('load-data-table');
  41. }
  42. public function render()
  43. {
  44. $this->records = \App\Models\CourseType::select('id', 'name', 'duration', 'enabled')->get();
  45. return view('livewire.course_type');
  46. }
  47. public function add()
  48. {
  49. $this->resetFields();
  50. $this->add = true;
  51. $this->update = false;
  52. }
  53. public function store()
  54. {
  55. $this->validate();
  56. try {
  57. \App\Models\CourseType::create([
  58. 'name' => $this->name,
  59. 'duration' => $this->duration,
  60. 'enabled' => $this->enabled
  61. ]);
  62. session()->flash('success','Dato creato');
  63. $this->resetFields();
  64. $this->add = false;
  65. } catch (\Exception $ex) {
  66. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  67. }
  68. }
  69. public function edit($id){
  70. try {
  71. $course_type = \App\Models\CourseType::findOrFail($id);
  72. if( !$course_type) {
  73. session()->flash('error','Dato non trovato');
  74. } else {
  75. $this->name = $course_type->name;
  76. $this->duration = $course_type->duration;
  77. $this->enabled = $course_type->enabled;
  78. $this->dataId = $course_type->id;
  79. $this->update = true;
  80. $this->add = false;
  81. }
  82. } catch (\Exception $ex) {
  83. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  84. }
  85. }
  86. public function update()
  87. {
  88. $this->validate();
  89. try {
  90. \App\Models\CourseType::whereId($this->dataId)->update([
  91. 'name' => $this->name,
  92. 'duration' => $this->duration,
  93. 'enabled' => $this->enabled
  94. ]);
  95. session()->flash('success','Dato aggiornato');
  96. $this->resetFields();
  97. $this->update = false;
  98. } catch (\Exception $ex) {
  99. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  100. }
  101. }
  102. public function cancel()
  103. {
  104. $this->add = false;
  105. $this->update = false;
  106. $this->resetFields();
  107. }
  108. public function delete($id)
  109. {
  110. try{
  111. \App\Models\CourseType::find($id)->delete();
  112. session()->flash('success',"Dato eliminato");
  113. return redirect(request()->header('Referer'));
  114. }catch(\Exception $e){
  115. session()->flash('error','Errore (' . $e->getMessage() . ')');
  116. }
  117. }
  118. }