Motivation.php 3.7 KB

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