Card.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Illuminate\Support\Facades\Auth;
  4. use Livewire\Component;
  5. use App\Http\Middleware\TenantMiddleware;
  6. class Card extends Component
  7. {
  8. public $records, $name, $enabled, $use_for_user_check, $one_year_expire, $next_day_expire, $next_month_expire, $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 mount(){
  22. if(Auth::user()->level != env('LEVEL_ADMIN', 0))
  23. return redirect()->to('/dashboard');
  24. }
  25. public function sortBy($field)
  26. {
  27. if($this->sortField === $field)
  28. {
  29. $this->sortAsc = ! $this->sortAsc;
  30. } else {
  31. $this->sortAsc = true;
  32. }
  33. $this->sortField = $field;
  34. }
  35. public function resetFields(){
  36. $this->name = '';
  37. $this->next_day_expire = null;
  38. $this->next_month_expire = null;
  39. $this->enabled = true;
  40. $this->use_for_user_check = true;
  41. $this->one_year_expire = false;
  42. $this->emit('load-data-table');
  43. }
  44. public function render()
  45. {
  46. $this->records = \App\Models\Card::get(); //orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
  47. return view('livewire.card');
  48. }
  49. public function add()
  50. {
  51. $this->resetFields();
  52. $this->add = true;
  53. $this->update = false;
  54. }
  55. public function store()
  56. {
  57. $this->validate();
  58. if ($this->one_year_expire)
  59. {
  60. $this->next_day_expire = 0;
  61. $this->next_month_expire = 0;
  62. }
  63. try {
  64. \App\Models\Card::create([
  65. 'name' => $this->name,
  66. 'next_day_expire' => $this->next_day_expire,
  67. 'next_month_expire' => $this->next_month_expire,
  68. 'enabled' => $this->enabled,
  69. 'use_for_user_check' => $this->use_for_user_check,
  70. 'one_year_expire' => $this->one_year_expire
  71. ]);
  72. session()->flash('success','Tessera creata');
  73. $this->resetFields();
  74. $this->add = false;
  75. } catch (\Exception $ex) {
  76. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  77. }
  78. }
  79. public function edit($id){
  80. try {
  81. $card = \App\Models\Card::findOrFail($id);
  82. if( !$card) {
  83. session()->flash('error','Tessera non trovata');
  84. } else {
  85. $this->name = $card->name;
  86. $this->enabled = $card->enabled;
  87. $this->use_for_user_check = $card->use_for_user_check;
  88. $this->one_year_expire = $card->one_year_expire;
  89. $this->next_day_expire = $card->next_day_expire;
  90. $this->next_month_expire = $card->next_month_expire;
  91. $this->dataId = $card->id;
  92. $this->update = true;
  93. $this->add = false;
  94. }
  95. } catch (\Exception $ex) {
  96. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  97. }
  98. }
  99. public function update()
  100. {
  101. $this->validate();
  102. if ($this->one_year_expire)
  103. {
  104. $this->next_day_expire = 0;
  105. $this->next_month_expire = 0;
  106. }
  107. try {
  108. \App\Models\Card::whereId($this->dataId)->update([
  109. 'name' => $this->name,
  110. 'next_day_expire' => $this->next_day_expire,
  111. 'next_month_expire' => $this->next_month_expire,
  112. 'enabled' => $this->enabled,
  113. 'use_for_user_check' => $this->use_for_user_check,
  114. 'one_year_expire' => $this->one_year_expire
  115. ]);
  116. session()->flash('success','Tessera aggiornata');
  117. $this->resetFields();
  118. $this->update = false;
  119. } catch (\Exception $ex) {
  120. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  121. }
  122. }
  123. public function cancel()
  124. {
  125. $this->add = false;
  126. $this->update = false;
  127. $this->resetFields();
  128. }
  129. public function delete($id)
  130. {
  131. try{
  132. \App\Models\Card::find($id)->delete();
  133. session()->flash('success',"Tessera eliminata");
  134. return redirect(request()->header('Referer'));
  135. }catch(\Exception $e){
  136. session()->flash('error','Errore (' . $e->getMessage() . ')');
  137. }
  138. }
  139. }