Bank.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Bank extends Component
  5. {
  6. public $records, $name, $visibility, $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->visibility = 'IN';
  32. $this->enabled = true;
  33. $this->emit('load-data-table');
  34. }
  35. public function render()
  36. {
  37. //$this->records = \App\Models\Bank::select('id', 'name', 'enabled')->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')->get();
  38. $this->records = \App\Models\Bank::select('id', 'name', 'visibility', 'enabled')->get();
  39. return view('livewire.bank');
  40. }
  41. public function add()
  42. {
  43. $this->resetFields();
  44. $this->add = true;
  45. $this->update = false;
  46. }
  47. public function store()
  48. {
  49. $this->validate();
  50. try {
  51. \App\Models\Bank::create([
  52. 'name' => $this->name,
  53. 'visibility' => $this->visibility,
  54. 'enabled' => $this->enabled
  55. ]);
  56. session()->flash('success','Banca 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. $this->resetFields();
  65. try {
  66. $bank = \App\Models\Bank::findOrFail($id);
  67. if( !$bank) {
  68. session()->flash('error','Banca non trovata');
  69. } else {
  70. $this->name = $bank->name;
  71. $this->visibility = $bank->visibility;
  72. $this->enabled = $bank->enabled;
  73. $this->dataId = $bank->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\Bank::whereId($this->dataId)->update([
  86. 'name' => $this->name,
  87. 'visibility' => $this->visibility,
  88. 'enabled' => $this->enabled
  89. ]);
  90. session()->flash('success','Banca aggiornata');
  91. $this->resetFields();
  92. $this->update = false;
  93. } catch (\Exception $ex) {
  94. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  95. }
  96. }
  97. public function cancel()
  98. {
  99. $this->add = false;
  100. $this->update = false;
  101. $this->resetFields();
  102. }
  103. public function delete($id)
  104. {
  105. try{
  106. \App\Models\Bank::find($id)->delete();
  107. session()->flash('success',"Città eliminata");
  108. return redirect(request()->header('Referer'));
  109. }catch(\Exception $e){
  110. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  111. }
  112. }
  113. }