CourseDuration.php 3.7 KB

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