Card.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 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->next_day_expire = null;
  28. $this->next_month_expire = null;
  29. $this->enabled = true;
  30. $this->use_for_user_check = true;
  31. $this->one_year_expire = false;
  32. }
  33. public function render()
  34. {
  35. $this->records = \App\Models\Card::orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
  36. return view('livewire.card');
  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. if ($this->one_year_expire)
  48. {
  49. $this->next_day_expire = 0;
  50. $this->next_month_expire = 0;
  51. }
  52. try {
  53. \App\Models\Card::create([
  54. 'name' => $this->name,
  55. 'next_day_expire' => $this->next_day_expire,
  56. 'next_month_expire' => $this->next_month_expire,
  57. 'enabled' => $this->enabled,
  58. 'use_for_user_check' => $this->use_for_user_check,
  59. 'one_year_expire' => $this->one_year_expire
  60. ]);
  61. session()->flash('success','Tessera creata');
  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. $card = \App\Models\Card::findOrFail($id);
  71. if( !$card) {
  72. session()->flash('error','Tessera non trovata');
  73. } else {
  74. $this->name = $card->name;
  75. $this->enabled = $card->enabled;
  76. $this->use_for_user_check = $card->use_for_user_check;
  77. $this->one_year_expire = $card->one_year_expire;
  78. $this->next_day_expire = $card->next_day_expire;
  79. $this->next_month_expire = $card->next_month_expire;
  80. $this->dataId = $card->id;
  81. $this->update = true;
  82. $this->add = false;
  83. }
  84. } catch (\Exception $ex) {
  85. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  86. }
  87. }
  88. public function update()
  89. {
  90. $this->validate();
  91. if ($this->one_year_expire)
  92. {
  93. $this->next_day_expire = 0;
  94. $this->next_month_expire = 0;
  95. }
  96. try {
  97. \App\Models\Card::whereId($this->dataId)->update([
  98. 'name' => $this->name,
  99. 'next_day_expire' => $this->next_day_expire,
  100. 'next_month_expire' => $this->next_month_expire,
  101. 'enabled' => $this->enabled,
  102. 'use_for_user_check' => $this->use_for_user_check,
  103. 'one_year_expire' => $this->one_year_expire
  104. ]);
  105. session()->flash('success','Tessera aggiornata');
  106. $this->resetFields();
  107. $this->update = false;
  108. } catch (\Exception $ex) {
  109. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  110. }
  111. }
  112. public function cancel()
  113. {
  114. $this->add = false;
  115. $this->update = false;
  116. $this->resetFields();
  117. }
  118. public function delete($id)
  119. {
  120. try{
  121. \App\Models\Card::find($id)->delete();
  122. session()->flash('success',"Tessera eliminata");
  123. }catch(\Exception $e){
  124. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  125. }
  126. }
  127. }