CourseSubscription.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 CourseSubscription extends Component
  7. {
  8. public $records, $name, $months, $month_day, $when_start, $enabled, $dataId, $update = false, $add = false;
  9. protected $rules = [
  10. 'name' => 'required'
  11. ];
  12. protected $messages = [
  13. 'name.required' => 'Il nome è obbligatorio'
  14. ];
  15. public $sortField ='name';
  16. public $sortAsc = true;
  17. public function boot()
  18. {
  19. app(TenantMiddleware::class)->setupTenantConnection();
  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->months = 0;
  34. $this->month_day = 0;
  35. $this->when_start = '';
  36. $this->enabled = true;
  37. $this->emit('load-data-table');
  38. }
  39. public function render()
  40. {
  41. $this->records = \App\Models\CourseSubscription::select('id', 'name', 'enabled', 'months')->get();
  42. return view('livewire.course_subscription');
  43. }
  44. public function add()
  45. {
  46. $this->resetFields();
  47. $this->add = true;
  48. $this->update = false;
  49. }
  50. public function store()
  51. {
  52. $this->validate();
  53. try {
  54. \App\Models\CourseSubscription::create([
  55. 'name' => $this->name,
  56. 'months' => $this->months,
  57. 'month_day' => $this->month_day,
  58. 'when_start' => $this->when_start,
  59. 'enabled' => $this->enabled
  60. ]);
  61. session()->flash('success','Dato creato');
  62. $this->resetFields();
  63. $this->add = false;
  64. } catch (\Exception $ex) {
  65. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  66. }
  67. }
  68. public function edit($id){
  69. try {
  70. $course_subscription = \App\Models\CourseSubscription::findOrFail($id);
  71. if( !$course_subscription) {
  72. session()->flash('error','Dato non trovato');
  73. } else {
  74. $this->name = $course_subscription->name;
  75. $this->months = $course_subscription->months;
  76. $this->month_day = $course_subscription->month_day;
  77. $this->when_start = $course_subscription->when_start;
  78. $this->enabled = $course_subscription->enabled;
  79. $this->dataId = $course_subscription->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\CourseSubscription::whereId($this->dataId)->update([
  92. 'name' => $this->name,
  93. 'months' => $this->months,
  94. 'month_day' => $this->month_day,
  95. 'when_start' => $this->when_start,
  96. 'enabled' => $this->enabled
  97. ]);
  98. session()->flash('success','Dato aggiornato');
  99. $this->resetFields();
  100. $this->update = false;
  101. } catch (\Exception $ex) {
  102. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  103. }
  104. }
  105. public function cancel()
  106. {
  107. $this->add = false;
  108. $this->update = false;
  109. $this->resetFields();
  110. }
  111. public function delete($id)
  112. {
  113. try{
  114. \App\Models\CourseSubscription::find($id)->delete();
  115. session()->flash('success',"Dato eliminato");
  116. }catch(\Exception $e){
  117. session()->flash('error','Errore (' . $e->getMessage() . ')');
  118. }
  119. }
  120. }