Court.php 3.4 KB

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