Motivation.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $this->enabled = true;
  24. $this->show_in_member_presences = false;
  25. }
  26. public function sortBy($field)
  27. {
  28. if($this->sortField === $field)
  29. {
  30. $this->sortAsc = ! $this->sortAsc;
  31. } else {
  32. $this->sortAsc = true;
  33. }
  34. $this->sortField = $field;
  35. }
  36. public function resetFields(){
  37. $this->name = '';
  38. $this->type = '';
  39. $this->enabled = true;
  40. $this->show_in_member_presences = false;
  41. $this->emit('load-data-table');
  42. }
  43. public function render()
  44. {
  45. $this->records = \App\Models\Motivation::select('id', 'name', 'type', 'show_in_member_presences', 'enabled')->get();
  46. return view('livewire.motivation');
  47. }
  48. public function add()
  49. {
  50. $this->resetFields();
  51. $this->add = true;
  52. $this->update = false;
  53. }
  54. public function store()
  55. {
  56. $this->validate();
  57. try {
  58. \App\Models\Motivation::create([
  59. 'name' => $this->name,
  60. 'type' => $this->type,
  61. 'enabled' => $this->enabled,
  62. 'show_in_member_presences' => $this->show_in_member_presences,
  63. ]);
  64. session()->flash('success','Campo creata');
  65. $this->resetFields();
  66. $this->add = false;
  67. } catch (\Exception $ex) {
  68. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  69. }
  70. }
  71. public function edit($id){
  72. try {
  73. $motivation = \App\Models\Motivation::findOrFail($id);
  74. if( !$motivation) {
  75. session()->flash('error','Motivazione non trovata');
  76. } else {
  77. $this->name = $motivation->name;
  78. $this->type = $motivation->type;
  79. $this->enabled = $motivation->enabled;
  80. $this->show_in_member_presences = $motivation->show_in_member_presences;
  81. $this->dataId = $motivation->id;
  82. $this->update = true;
  83. $this->add = false;
  84. }
  85. } catch (\Exception $ex) {
  86. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  87. }
  88. }
  89. public function update()
  90. {
  91. $this->validate();
  92. try {
  93. \App\Models\Motivation::whereId($this->dataId)->update([
  94. 'name' => $this->name,
  95. 'type' => $this->type,
  96. 'enabled' => $this->enabled,
  97. 'show_in_member_presences' => $this->show_in_member_presences,
  98. ]);
  99. session()->flash('success','Motivazione aggiornata');
  100. $this->resetFields();
  101. $this->update = false;
  102. } catch (\Exception $ex) {
  103. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  104. }
  105. }
  106. public function cancel()
  107. {
  108. $this->add = false;
  109. $this->update = false;
  110. $this->resetFields();
  111. }
  112. public function delete($id)
  113. {
  114. try{
  115. \App\Models\Motivation::find($id)->delete();
  116. session()->flash('success',"Motivazione eliminata");
  117. return redirect(request()->header('Referer'));
  118. }catch(\Exception $e){
  119. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  120. }
  121. }
  122. }