Causals.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 function boot()
  19. {
  20. app(TenantMiddleware::class)->setupTenantConnection();
  21. }
  22. public function mount($type, $idx, $causal_id)
  23. {
  24. $this->type = $type;
  25. $this->idx = $idx;
  26. $this->causal_id = $causal_id;
  27. if ($this->causal_id != null && $this->causal_id != 0) {
  28. $c = \App\Models\Causal::findOrFail($this->causal_id);
  29. $ids = array_reverse($c->recursiveParent($c->parent_id, [$c->id]));
  30. foreach($ids as $ii => $i) {
  31. if ($ii == 0) {
  32. $this->level_1_id = $i;
  33. }
  34. if ($ii == 1) {
  35. $this->level_2_id = $i;
  36. }
  37. if ($ii == 2) {
  38. $this->level_3_id = $i;
  39. }
  40. }
  41. }
  42. }
  43. public function updatedLevel1Id()
  44. {
  45. $this->emit($this->emit, null, $this->idx);
  46. $this->level_2_id = 0;
  47. $this->level_2 = [];
  48. $this->level_3_id = 0;
  49. $this->level_3 = [];
  50. }
  51. public function updatedLevel2Id()
  52. {
  53. $this->emit($this->emit, null, $this->idx);
  54. $this->level_3_id = 0;
  55. $this->level_3 = [];
  56. }
  57. public function updatedLevel3Id()
  58. {
  59. $this->emit($this->emit, null, $this->idx);
  60. }
  61. public function render()
  62. {
  63. $reset = false;
  64. // Build query with visibility filter
  65. $visibilityFilter = function($query) {
  66. return $query;
  67. };
  68. if ($this->show_hidden == false) {
  69. $visibilityFilter = function($query) {
  70. return $query->where('hidden', false);
  71. };
  72. }
  73. if ($this->level_1_id > 0) {
  74. $this->level_2 = \App\Models\Causal::where('parent_id', $this->level_1_id)
  75. ->where('type', $this->type)
  76. ->where(function($query) use ($visibilityFilter) {
  77. return $visibilityFilter($query);
  78. })
  79. ->orderBy('name')
  80. ->get();
  81. if (sizeof($this->level_2) == 0) {
  82. $this->emit($this->emit, $this->level_1_id, $this->idx);
  83. $reset = true;
  84. }
  85. }
  86. if ($this->level_2_id > 0) {
  87. $this->level_3 = \App\Models\Causal::where('parent_id', $this->level_2_id)
  88. ->where('type', $this->type)
  89. ->where(function($query) use ($visibilityFilter) {
  90. return $visibilityFilter($query);
  91. })
  92. ->orderBy('name')
  93. ->get();
  94. if (sizeof($this->level_3) == 0) {
  95. $this->emit($this->emit, $this->level_2_id, $this->idx);
  96. $reset = true;
  97. }
  98. }
  99. if ($this->level_3_id > 0) {
  100. $this->emit($this->emit, $this->level_3_id, $this->idx);
  101. $reset = true;
  102. }
  103. $this->level_1 = \App\Models\Causal::where('parent_id', null)
  104. ->where('type', $this->type)
  105. ->where(function($query) use ($visibilityFilter) {
  106. return $visibilityFilter($query);
  107. })
  108. ->orderBy('name')
  109. ->get();
  110. return view('livewire.causals');
  111. }
  112. }