Card.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, $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 function resetFields(){
  14. $this->name = '';
  15. $this->next_day_expire = null;
  16. $this->next_month_expire = null;
  17. $this->enabled = true;
  18. $this->use_for_user_check = true;
  19. }
  20. public function render()
  21. {
  22. $this->records = \App\Models\Card::get();
  23. return view('livewire.card');
  24. }
  25. public function add()
  26. {
  27. $this->resetFields();
  28. $this->add = true;
  29. $this->update = false;
  30. }
  31. public function store()
  32. {
  33. $this->validate();
  34. try {
  35. \App\Models\Card::create([
  36. 'name' => $this->name,
  37. 'next_day_expire' => $this->next_day_expire,
  38. 'next_month_expire' => $this->next_month_expire,
  39. 'enabled' => $this->enabled,
  40. 'use_for_user_check' => $this->use_for_user_check
  41. ]);
  42. session()->flash('success','Tessera creata');
  43. $this->resetFields();
  44. $this->add = false;
  45. } catch (\Exception $ex) {
  46. session()->flash('error','Errore');
  47. }
  48. }
  49. public function edit($id){
  50. try {
  51. $card = \App\Models\Card::findOrFail($id);
  52. if( !$card) {
  53. session()->flash('error','Tessera non trovata');
  54. } else {
  55. $this->name = $card->name;
  56. $this->enabled = $card->enabled;
  57. $this->use_for_user_check = $card->use_for_user_check;
  58. $this->next_day_expire = $card->next_day_expire;
  59. $this->next_month_expire = $card->next_month_expire;
  60. $this->dataId = $card->id;
  61. $this->update = true;
  62. $this->add = false;
  63. }
  64. } catch (\Exception $ex) {
  65. session()->flash('error','Errore');
  66. }
  67. }
  68. public function update()
  69. {
  70. $this->validate();
  71. try {
  72. \App\Models\Card::whereId($this->dataId)->update([
  73. 'name' => $this->name,
  74. 'next_day_expire' => $this->next_day_expire,
  75. 'next_month_expire' => $this->next_month_expire,
  76. 'enabled' => $this->enabled,
  77. 'use_for_user_check' => $this->use_for_user_check
  78. ]);
  79. session()->flash('success','Tessera aggiornata');
  80. $this->resetFields();
  81. $this->update = false;
  82. } catch (\Exception $ex) {
  83. session()->flash('success','Errore');
  84. }
  85. }
  86. public function cancel()
  87. {
  88. $this->add = false;
  89. $this->update = false;
  90. $this->resetFields();
  91. }
  92. public function delete($id)
  93. {
  94. try{
  95. \App\Models\Card::find($id)->delete();
  96. session()->flash('success',"Tessera eliminata");
  97. }catch(\Exception $e){
  98. session()->flash('error',"Errore");
  99. }
  100. }
  101. }