Member.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Member extends Component
  5. {
  6. public $members, $first_name, $last_name, $status, $birth_city_id, $birth_province_id, $birth_nation_id, $birth_date, $gender, $fiscal_code, $address, $zip_code, $nation_id, $province_id, $city_id, $phone, $email, $enabled, $updateMember = false, $addMember = false;
  7. protected $listeners = [
  8. 'deleteMemberListner'=>'deleteMember'
  9. ];
  10. protected $rules = [
  11. 'first_name' => 'required',
  12. 'last_name' => 'required',
  13. ];
  14. public function resetFields(){
  15. $this->first_name = '';
  16. $this->last_name = '';
  17. $this->status = '';
  18. $this->birth_city_id = null;
  19. $this->birth_province_id = null;
  20. $this->birth_nation_id = null;
  21. $this->birth_date = null;
  22. $this->gender = '';
  23. $this->fiscal_code = '';
  24. $this->address = '';
  25. $this->zip_code = '';
  26. $this->nation_id = null;
  27. $this->province_id = null;
  28. $this->city_id = null;
  29. $this->phone = '';
  30. $this->email = '';
  31. $this->enabled = true;
  32. }
  33. public function render()
  34. {
  35. $this->members = Member::select('id', 'first_name', 'last_name', 'status', 'enabled')->get();
  36. return view('livewire.member');
  37. }
  38. public function addMember()
  39. {
  40. $this->resetFields();
  41. $this->addMember = true;
  42. $this->updateMember = false;
  43. }
  44. public function storeMember()
  45. {
  46. $this->validate();
  47. try {
  48. Member::create([
  49. 'first_name' => $this->first_name,
  50. 'last_name' => $this->last_name,
  51. 'status' => $this->status,
  52. 'birth_city_id' => $this->birth_city_id,
  53. 'birth_province_id' => $this->birth_province_id,
  54. 'birth_nation_id' => $this->birth_nation_id,
  55. 'birth_date' => $this->birth_date,
  56. 'gender' => $this->gender,
  57. 'fiscal_code' => $this->fiscal_code,
  58. 'address' => $this->address,
  59. 'zip_code' => $this->zip_code,
  60. 'nation_id' => $this->nation_id,
  61. 'province_id' => $this->province_id,
  62. 'city_id' => $this->city_id,
  63. 'phone' => $this->phone,
  64. 'email' => $this->email,
  65. 'enabled' => $this->enabled
  66. ]);
  67. session()->flash('success, $Tesserato creato');
  68. $this->resetFields();
  69. $this->addMember = false;
  70. } catch (\Exception $ex) {
  71. session()->flash('error','Errore in fase di salvataggio');
  72. }
  73. }
  74. public function editMember($id){
  75. try {
  76. $member = Member::findOrFail($id);
  77. if( !$member) {
  78. session()->flash('error','Tesserato non trovato');
  79. } else {
  80. $this->first_name = $member->first_name;
  81. $this->last_name = $member->last_name;
  82. $this->status = $member->status;
  83. $this->birth_city_id = $member->birth_city_id;
  84. $this->birth_province_id = $member->birth_province_id;
  85. $this->birth_nation_id = $member->birth_nation_id;
  86. $this->birth_date = $member->birth_date;
  87. $this->gender = $member->gender;
  88. $this->fiscal_code = $member->fiscal_code;
  89. $this->address = $member->address;
  90. $this->zip_code = $member->zip_code;
  91. $this->nation_id = $member->nation_id;
  92. $this->province_id = $member->province_id;
  93. $this->city_id = $member->city_id;
  94. $this->phone = $member->phone;
  95. $this->email = $member->email;
  96. $this->enabled = $member->enabled;
  97. $this->memberId = $member->id;
  98. $this->updateMember = true;
  99. $this->addMember = false;
  100. }
  101. } catch (\Exception $ex) {
  102. session()->flash('error','Errore');
  103. }
  104. }
  105. public function updateMember()
  106. {
  107. $this->validate();
  108. try {
  109. Member::whereId($this->memberId)->update([
  110. 'first_name' => $this->first_name,
  111. 'last_name' => $this->last_name,
  112. 'status' => $this->status,
  113. 'birth_city_id' => $this->birth_city_id,
  114. 'birth_province_id' => $this->birth_province_id,
  115. 'birth_nation_id' => $this->birth_nation_id,
  116. 'birth_date' => $this->birth_date,
  117. 'gender' => $this->gender,
  118. 'fiscal_code' => $this->fiscal_code,
  119. 'address' => $this->address,
  120. 'zip_code' => $this->zip_code,
  121. 'nation_id' => $this->nation_id,
  122. 'province_id' => $this->province_id,
  123. 'city_id' => $this->city_id,
  124. 'phone' => $this->phone,
  125. 'email' => $this->email,
  126. 'enabled' => $this->enabled
  127. ]);
  128. session()->flash('success','Tesserato aggiornato');
  129. $this->resetFields();
  130. $this->updateMember = false;
  131. } catch (\Exception $ex) {
  132. session()->flash('success','Errore');
  133. }
  134. }
  135. public function cancelMember()
  136. {
  137. $this->addMember = false;
  138. $this->updateMember = false;
  139. $this->resetFields();
  140. }
  141. public function deleteMember($id)
  142. {
  143. try{
  144. Member::find($id)->delete();
  145. session()->flash('success',"Tesserato eliminato");
  146. }catch(\Exception $e){
  147. session()->flash('error',"Errore");
  148. }
  149. }
  150. }