Motivation.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. class Motivation extends Component
  6. {
  7. public $records, $name, $type, $enabled, $show_in_member_presences, $dataId, $update = false, $add = false;
  8. protected $rules = [
  9. 'name' => 'required'
  10. ];
  11. protected $messages = [
  12. 'name.required' => 'Il nome è obbligatorio'
  13. ];
  14. public $sortField ='name';
  15. public $sortAsc = true;
  16. public function boot()
  17. {
  18. app(TenantMiddleware::class)->setupTenantConnection();
  19. }
  20. public function mount(){
  21. if(\Auth::user()->level != env('LEVEL_ADMIN', 0))
  22. return redirect()->to('/dashboard');
  23. }
  24. public function sortBy($field)
  25. {
  26. if($this->sortField === $field)
  27. {
  28. $this->sortAsc = ! $this->sortAsc;
  29. } else {
  30. $this->sortAsc = true;
  31. }
  32. $this->sortField = $field;
  33. }
  34. public function resetFields(){
  35. $this->name = '';
  36. $this->type = '';
  37. $this->enabled = true;
  38. $this->emit('load-data-table');
  39. }
  40. public function render()
  41. {
  42. $this->records = \App\Models\Motivation::select('id', 'name', 'type', 'show_in_member_presences', 'enabled')->get();
  43. return view('livewire.motivation');
  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. \App\Models\Motivation::create([
  56. 'name' => $this->name,
  57. 'type' => $this->type,
  58. 'enabled' => $this->enabled,
  59. 'show_in_member_presences' => $this->show_in_member_presences,
  60. ]);
  61. session()->flash('success','Campo creata');
  62. $this->resetFields();
  63. $this->add = false;
  64. } catch (\Exception $ex) {
  65. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  66. }
  67. }
  68. public function edit($id){
  69. try {
  70. $motivation = \App\Models\Motivation::findOrFail($id);
  71. if( !$motivation) {
  72. session()->flash('error','Motivazione non trovata');
  73. } else {
  74. $this->name = $motivation->name;
  75. $this->type = $motivation->type;
  76. $this->enabled = $motivation->enabled;
  77. $this->show_in_member_presences = $motivation->show_in_member_presences;
  78. $this->dataId = $motivation->id;
  79. $this->update = true;
  80. $this->add = false;
  81. }
  82. } catch (\Exception $ex) {
  83. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  84. }
  85. }
  86. public function update()
  87. {
  88. $this->validate();
  89. try {
  90. \App\Models\Motivation::whereId($this->dataId)->update([
  91. 'name' => $this->name,
  92. 'type' => $this->type,
  93. 'enabled' => $this->enabled,
  94. 'show_in_member_presences' => $this->show_in_member_presences,
  95. ]);
  96. session()->flash('success','Motivazione aggiornata');
  97. $this->resetFields();
  98. $this->update = false;
  99. } catch (\Exception $ex) {
  100. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  101. }
  102. }
  103. public function cancel()
  104. {
  105. $this->add = false;
  106. $this->update = false;
  107. $this->resetFields();
  108. }
  109. public function delete($id)
  110. {
  111. try{
  112. \App\Models\Motivation::find($id)->delete();
  113. session()->flash('success',"Motivazione eliminata");
  114. return redirect(request()->header('Referer'));
  115. }catch(\Exception $e){
  116. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  117. }
  118. }
  119. }