User.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Illuminate\Support\Facades\Log;
  4. use Livewire\Component;
  5. class User extends Component
  6. {
  7. public $records, $name,$cognome, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
  8. protected $rules = [
  9. 'name' => 'required',
  10. 'cognome' => 'required',
  11. 'email' => 'required',
  12. 'password' => 'required'
  13. ];
  14. protected $messages = [
  15. 'name.required' => 'Il nome è obbligatorio',
  16. 'cognome.required' => 'Il cognome è obbligatorio',
  17. 'email.required' => 'La mail è obbligatoria',
  18. 'password.required' => 'La password è obbligatoria',
  19. ];
  20. public function resetFields(){
  21. $this->name = '';
  22. $this->cognome = '';
  23. $this->email = '';
  24. $this->password = '';
  25. $this->oldPassword = '';
  26. $this->level = 0;
  27. $this->enabled = true;
  28. $this->emit('load-data-table');
  29. }
  30. public function render()
  31. {
  32. $this->records = \App\Models\User::select('id', 'name','cognome' ,'email', 'password', 'level', 'enabled')->get();
  33. return view('livewire.user');
  34. }
  35. public function add()
  36. {
  37. $this->resetFields();
  38. $this->add = true;
  39. $this->update = false;
  40. }
  41. public function store()
  42. {
  43. Log::info('User store', [
  44. 'name' => $this->name,
  45. 'cognome' => $this->cognome,
  46. 'email' => $this->email,
  47. 'level' => $this->level,
  48. 'enabled' => $this->enabled
  49. ]);
  50. $this->validate();
  51. Log::info('User store', [
  52. 'name' => $this->name,
  53. 'cognome' => $this->cognome,
  54. 'email' => $this->email,
  55. 'level' => $this->level,
  56. 'enabled' => $this->enabled
  57. ]);
  58. try {
  59. \App\Models\User::create([
  60. 'name' => $this->name,
  61. 'cognome' => $this->cognome,
  62. 'email' => $this->email,
  63. 'password' => bcrypt($this->password),
  64. 'level' => $this->level,
  65. 'enabled' => $this->enabled
  66. ]);
  67. Log::info('User created', [
  68. 'name' => $this->name,
  69. 'cognome' => $this->cognome,
  70. 'email' => $this->email,
  71. 'level' => $this->level,
  72. 'enabled' => $this->enabled
  73. ]);
  74. session()->flash('success','Dato creato');
  75. $this->resetFields();
  76. $this->add = false;
  77. } catch (\Exception $ex) {
  78. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  79. }
  80. }
  81. public function edit($id){
  82. try {
  83. $user = \App\Models\User::findOrFail($id);
  84. if( !$user) {
  85. session()->flash('error','Dato non trovato');
  86. } else {
  87. $this->name = $user->name;
  88. $this->cognome = $user->cognome;
  89. $this->email = $user->email;
  90. $this->password = $user->password;
  91. $this->level = $user->level;
  92. $this->dataId = $user->id;
  93. $this->update = true;
  94. $this->add = false;
  95. }
  96. Log::info('User edit', [
  97. 'name' => $this->name,
  98. 'cognome' => $this->cognome,
  99. 'email' => $this->email,
  100. 'level' => $this->level
  101. ]);
  102. } catch (\Exception $ex) {
  103. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  104. }
  105. }
  106. public function update()
  107. {
  108. $this->validate();
  109. try {
  110. if ($this->pa)
  111. \App\Models\User::whereId($this->dataId)->update([
  112. 'name' => $this->name,
  113. 'cognome' => $this->cognome,
  114. 'email' => $this->email,
  115. 'password' => bcrypt($this->password),
  116. 'level' => $this->level,
  117. 'enabled' => $this->enabled
  118. ]);
  119. Log::info('User updated', [
  120. 'name' => $this->name,
  121. 'cognome' => $this->cognome,
  122. 'email' => $this->email,
  123. 'level' => $this->level,
  124. 'enabled' => $this->enabled
  125. ]);
  126. session()->flash('success','Dato aggiornato');
  127. $this->resetFields();
  128. $this->update = false;
  129. } catch (\Exception $ex) {
  130. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  131. }
  132. }
  133. public function cancel()
  134. {
  135. $this->add = false;
  136. $this->update = false;
  137. $this->resetFields();
  138. }
  139. public function delete($id)
  140. {
  141. try{
  142. \App\Models\User::find($id)->delete();
  143. session()->flash('success',"Dato eliminato");
  144. }catch(\Exception $e){
  145. session()->flash('error','Errore (' . $e->getMessage() . ')');
  146. }
  147. }
  148. }