Category.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. use Illuminate\Support\Facades\Auth;
  6. class Category extends Component
  7. {
  8. public $records, $parent_id, $name, $enabled, $dataId, $update = false, $add = false;
  9. public $parent = '';
  10. protected $rules = [
  11. 'name' => 'required'
  12. ];
  13. protected $messages = [
  14. 'name.required' => 'Il nome è obbligatorio'
  15. ];
  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. }
  24. public function resetFields(){
  25. $this->name = '';
  26. $this->parent_id = null;
  27. $this->parent = '';
  28. $this->enabled = true;
  29. }
  30. public function render()
  31. {
  32. $this->records = \App\Models\Category::where('parent_id', null)->get();
  33. return view('livewire.category');
  34. }
  35. public function add()
  36. {
  37. $this->resetFields();
  38. $this->add = true;
  39. $this->update = false;
  40. }
  41. public function addLevel($parent_id)
  42. {
  43. $this->resetFields();
  44. $this->parent_id = $parent_id;
  45. $this->parent = \App\Models\Category::findOrFail($parent_id)->name;
  46. $this->add = true;
  47. $this->update = false;
  48. }
  49. public function store()
  50. {
  51. $this->validate();
  52. try {
  53. \App\Models\Category::create([
  54. 'name' => $this->name,
  55. 'parent_id' => $this->parent_id,
  56. 'enabled' => $this->enabled
  57. ]);
  58. session()->flash('success','Categoria creata');
  59. $this->resetFields();
  60. $this->add = false;
  61. } catch (\Exception $ex) {
  62. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  63. }
  64. }
  65. public function edit($id){
  66. try {
  67. $category = \App\Models\Category::findOrFail($id);
  68. if( !$category) {
  69. session()->flash('error','Categoria non trovata');
  70. } else {
  71. $this->name = $category->name;
  72. $this->enabled = $category->enabled;
  73. $this->parent_id = $category->parent_id;
  74. $this->dataId = $category->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\Category::whereId($this->dataId)->update([
  87. 'name' => $this->name,
  88. 'parent_id' => $this->parent_id,
  89. 'enabled' => $this->enabled
  90. ]);
  91. session()->flash('success','Categoria 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\Category::find($id)->delete();
  108. session()->flash('success',"Categoria eliminata");
  109. return redirect(request()->header('Referer'));
  110. }catch(\Exception $e){
  111. session()->flash('error','Errore (' . $e->getMessage() . ')');
  112. }
  113. }
  114. }