Card.php 4.4 KB

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