User.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. use Hash;
  6. use Illuminate\Support\Facades\Hash as FacadesHash;
  7. class User extends Component
  8. {
  9. use WithPagination;
  10. protected $paginationTheme = 'bootstrap';
  11. public $title = 'Utenti';
  12. public $groups = [];
  13. public $search, $firstname, $lastname, $username, $email, $group_id, $password, $old_password, $dataId, $update = false, $add = false;
  14. protected $rules = [
  15. 'firstname' => 'required',
  16. 'lastname' => 'required',
  17. 'username' => 'required',
  18. 'email' => 'required'
  19. ];
  20. protected $messages = [
  21. 'firstname.required' => 'Il nome è obbligatorio',
  22. 'lastname.required' => 'Il cognome è obbligatorio',
  23. 'username.required' => 'Lo username è obbligatorio',
  24. 'email.required' => 'Il nome è obbligatorio',
  25. ];
  26. public function resetFields(){
  27. $this->firstname = '';
  28. $this->lastname = '';
  29. $this->username = '';
  30. $this->email = '';
  31. $this->password = '';
  32. $this->old_password = '';
  33. $this->group_id = null;
  34. }
  35. public function updatingSearch()
  36. {
  37. $this->resetPage();
  38. }
  39. public function render()
  40. {
  41. $this->groups = \App\Models\UserGroup::orderBy('name')->get();
  42. $rows = \App\Models\User::where('firstname', 'like', '%'.$this->search.'%')->orWhere('lastname', 'like', '%'.$this->search.'%')->orderBy('firstname')->paginate(10);
  43. return view('livewire.users', ['records' => $rows]);
  44. }
  45. public function add()
  46. {
  47. $this->resetFields();
  48. $this->add = true;
  49. $this->update = false;
  50. }
  51. public function store()
  52. {
  53. $this->validate();
  54. try {
  55. $u = \App\Models\User::create([
  56. 'firstname' => $this->firstname,
  57. 'lastname' => $this->lastname,
  58. 'username' => $this->username,
  59. 'email' => $this->email,
  60. 'password' => FacadesHash::make($this->password),
  61. ]);
  62. \App\Models\UserUserGroup::create([
  63. 'group_id' => $this->group_id,
  64. 'user_id' => $u->id
  65. ]);
  66. session()->flash('success','Record creato');
  67. $this->resetFields();
  68. $this->add = false;
  69. } catch (\Exception $ex) {
  70. session()->flash('error','Errore in fase di salvataggio' . $ex->getMessage());
  71. }
  72. }
  73. public function edit($id){
  74. try {
  75. $record = \App\Models\User::findOrFail($id);
  76. if( !$record) {
  77. session()->flash('error','Record non trovato');
  78. } else {
  79. $this->firstname = $record->firstname;
  80. $this->lastname = $record->lastname;
  81. $this->username = $record->username;
  82. $this->email = $record->email;
  83. $this->old_password = $record->password;
  84. $this->dataId = $record->id;
  85. $this->update = true;
  86. $this->add = false;
  87. $this->group_id = \App\Models\UserUserGroup::where('user_id', $this->dataId)->first()->group_id;
  88. }
  89. } catch (\Exception $ex) {
  90. session()->flash('error','Errore');
  91. }
  92. }
  93. public function update()
  94. {
  95. $this->validate();
  96. try {
  97. $updateData = [
  98. 'firstname' => $this->firstname,
  99. 'lastname' => $this->lastname,
  100. 'username' => $this->username,
  101. 'email' => $this->email,
  102. ];
  103. if ($this->password !== '') {
  104. $updateData['password'] = FacadesHash::make($this->password);
  105. }
  106. \App\Models\User::whereId($this->dataId)->update($updateData);
  107. \App\Models\UserUserGroup::where('user_id', $this->dataId)->delete();
  108. \App\Models\UserUserGroup::create([
  109. 'group_id' => $this->group_id,
  110. 'user_id' => $this->dataId
  111. ]);
  112. session()->flash('success','Record aggiornato');
  113. $this->resetFields();
  114. $this->update = false;
  115. } catch (\Exception $ex) {
  116. session()->flash('error','Errore ' . $ex->getMessage());
  117. }
  118. }
  119. public function cancel()
  120. {
  121. $this->add = false;
  122. $this->update = false;
  123. $this->resetFields();
  124. }
  125. public function delete($id)
  126. {
  127. try{
  128. \App\Models\User::find($id)->delete();
  129. session()->flash('success',"Record eliminato");
  130. }catch(\Exception $e){
  131. session()->flash('error',"Errore");
  132. }
  133. }
  134. }