Causals.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. class Causals extends Component
  6. {
  7. public $level_1 = [];
  8. public $level_2 = [];
  9. public $level_3 = [];
  10. public $level_1_id = 0;
  11. public $level_2_id = 0;
  12. public $level_3_id = 0;
  13. public $type = '';
  14. public $emit = 'setCausal';
  15. public $idx = -1;
  16. public $causal_id = null;
  17. public $show_hidden = true;
  18. public $show_course_causals = true;
  19. public function boot()
  20. {
  21. app(TenantMiddleware::class)->setupTenantConnection();
  22. }
  23. public function mount($type, $idx, $causal_id)
  24. {
  25. $this->type = $type;
  26. $this->idx = $idx;
  27. $this->causal_id = $causal_id;
  28. if ($this->causal_id != null && $this->causal_id != 0) {
  29. $c = \App\Models\Causal::findOrFail($this->causal_id);
  30. $ids = array_reverse($c->recursiveParent($c->parent_id, [$c->id]));
  31. foreach($ids as $ii => $i) {
  32. if ($ii == 0) {
  33. $this->level_1_id = $i;
  34. }
  35. if ($ii == 1) {
  36. $this->level_2_id = $i;
  37. }
  38. if ($ii == 2) {
  39. $this->level_3_id = $i;
  40. }
  41. }
  42. }
  43. }
  44. public function updatedLevel1Id()
  45. {
  46. $this->emit($this->emit, null, $this->idx);
  47. $this->level_2_id = 0;
  48. $this->level_2 = [];
  49. $this->level_3_id = 0;
  50. $this->level_3 = [];
  51. }
  52. public function updatedLevel2Id()
  53. {
  54. $this->emit($this->emit, null, $this->idx);
  55. $this->level_3_id = 0;
  56. $this->level_3 = [];
  57. }
  58. public function updatedLevel3Id()
  59. {
  60. $this->emit($this->emit, null, $this->idx);
  61. }
  62. public function render()
  63. {
  64. $reset = false;
  65. $visibilityFilter = function($query) {
  66. return $query;
  67. };
  68. $courseFilter = function($query) {
  69. return $query;
  70. };
  71. if ($this->show_hidden == false) {
  72. $visibilityFilter = function($query) {
  73. return $query->where('hidden', false);
  74. };
  75. }
  76. if ($this->show_course_causals == false) {
  77. $courseFilter = function($query) {
  78. return $query->whereNotIn('name', ['PAGAMENTO CORSO', 'PAGAMENTO ISCRIZIONE']);
  79. };
  80. }
  81. if ($this->level_1_id > 0) {
  82. $this->level_2 = \App\Models\Causal::where('parent_id', $this->level_1_id)
  83. ->where('type', $this->type)
  84. ->where(function($query) use ($visibilityFilter) {
  85. return $visibilityFilter($query);
  86. })
  87. ->where(function($query) use ($courseFilter) {
  88. return $courseFilter($query);
  89. })
  90. ->orderBy('name')
  91. ->get();
  92. if (sizeof($this->level_2) == 0) {
  93. $this->emit($this->emit, $this->level_1_id, $this->idx);
  94. $reset = true;
  95. }
  96. }
  97. if ($this->level_2_id > 0) {
  98. $this->level_3 = \App\Models\Causal::where('parent_id', $this->level_2_id)
  99. ->where('type', $this->type)
  100. ->where(function($query) use ($visibilityFilter) {
  101. return $visibilityFilter($query);
  102. })
  103. ->where(function($query) use ($courseFilter) {
  104. return $courseFilter($query);
  105. })
  106. ->orderBy('name')
  107. ->get();
  108. if (sizeof($this->level_3) == 0) {
  109. $this->emit($this->emit, $this->level_2_id, $this->idx);
  110. $reset = true;
  111. }
  112. }
  113. if ($this->level_3_id > 0) {
  114. $this->emit($this->emit, $this->level_3_id, $this->idx);
  115. $reset = true;
  116. }
  117. $this->level_1 = \App\Models\Causal::where('parent_id', null)
  118. ->where('type', $this->type)
  119. ->where(function($query) use ($visibilityFilter) {
  120. return $visibilityFilter($query);
  121. })
  122. ->where(function($query) use ($courseFilter) {
  123. return $courseFilter($query);
  124. })
  125. ->orderBy('name')
  126. ->get();
  127. return view('livewire.causals');
  128. }
  129. }