Causals.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. {
  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. {
  32. if ($ii == 0)
  33. {
  34. $this->level_1_id = $i;
  35. }
  36. if ($ii == 1)
  37. {
  38. $this->level_2_id = $i;
  39. }
  40. if ($ii == 2)
  41. {
  42. $this->level_3_id = $i;
  43. }
  44. }
  45. }
  46. }
  47. public function updatedLevel1Id() {
  48. $this->emit($this->emit, null, $this->idx);
  49. $this->level_2_id = 0;
  50. $this->level_2 = [];
  51. $this->level_3_id = 0;
  52. $this->level_3 = [];
  53. }
  54. public function updatedLevel2Id() {
  55. $this->emit($this->emit, null, $this->idx);
  56. $this->level_3_id = 0;
  57. $this->level_3 = [];
  58. }
  59. public function updatedLevel3Id() {
  60. $this->emit($this->emit, null, $this->idx);
  61. }
  62. public function render()
  63. {
  64. $reset = false;
  65. if ($this->level_1_id > 0)
  66. {
  67. $this->level_2 = \App\Models\Causal::where('parent_id', $this->level_1_id)->where('type', $this->type)->orderBy('name')->get();
  68. if (sizeof($this->level_2) == 0)
  69. {
  70. $this->emit($this->emit, $this->level_1_id, $this->idx);
  71. $reset = true;
  72. }
  73. }
  74. if ($this->level_2_id > 0)
  75. {
  76. $this->level_3 = \App\Models\Causal::where('parent_id', $this->level_2_id)->where('type', $this->type)->orderBy('name')->get();
  77. if (sizeof($this->level_3) == 0)
  78. {
  79. $this->emit($this->emit, $this->level_2_id, $this->idx);
  80. $reset = true;
  81. }
  82. }
  83. if ($this->level_3_id > 0)
  84. {
  85. $this->emit($this->emit, $this->level_3_id, $this->idx);
  86. $reset = true;
  87. }
  88. $this->level_1 = \App\Models\Causal::where('parent_id', null)->where('type', $this->type)->orderBy('name')->get();
  89. return view('livewire.causals');
  90. }
  91. }