CourseSubscription.php 3.7 KB

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