Causals.php 3.4 KB

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