Card.php 4.2 KB

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