CourseDuration.php 3.5 KB

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