Motivation.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Motivation extends Component
  5. {
  6. public $records, $name, $type, $enabled, $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', '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. ]);
  55. session()->flash('success','Campo creata');
  56. $this->resetFields();
  57. $this->add = false;
  58. } catch (\Exception $ex) {
  59. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  60. }
  61. }
  62. public function edit($id){
  63. try {
  64. $motivation = \App\Models\Motivation::findOrFail($id);
  65. if( !$motivation) {
  66. session()->flash('error','Campo non trovata');
  67. } else {
  68. $this->name = $motivation->name;
  69. $this->type = $motivation->type;
  70. $this->enabled = $motivation->enabled;
  71. $this->dataId = $motivation->id;
  72. $this->update = true;
  73. $this->add = false;
  74. }
  75. } catch (\Exception $ex) {
  76. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  77. }
  78. }
  79. public function update()
  80. {
  81. $this->validate();
  82. try {
  83. \App\Models\Motivation::whereId($this->dataId)->update([
  84. 'name' => $this->name,
  85. 'type' => $this->type,
  86. 'enabled' => $this->enabled
  87. ]);
  88. session()->flash('success','Campo aggiornata');
  89. $this->resetFields();
  90. $this->update = false;
  91. } catch (\Exception $ex) {
  92. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  93. }
  94. }
  95. public function cancel()
  96. {
  97. $this->add = false;
  98. $this->update = false;
  99. $this->resetFields();
  100. }
  101. public function delete($id)
  102. {
  103. try{
  104. \App\Models\Motivation::find($id)->delete();
  105. session()->flash('success',"Campo eliminata");
  106. return redirect(request()->header('Referer'));
  107. }catch(\Exception $e){
  108. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  109. }
  110. }
  111. }