Causals.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Causals extends Component
  5. {
  6. public $level_1 = [];
  7. public $level_2 = [];
  8. public $level_3 = [];
  9. public $level_1_id = 0;
  10. public $level_2_id = 0;
  11. public $level_3_id = 0;
  12. public $type = '';
  13. public $emit = 'setCausal';
  14. public $idx = -1;
  15. public $causal_id = null;
  16. public function mount($type, $idx, $causal_id)
  17. {
  18. $this->type = $type;
  19. $this->idx = $idx;
  20. $this->causal_id = $causal_id;
  21. if ($this->causal_id != null)
  22. {
  23. $c = \App\Models\Causal::findOrFail($this->causal_id);
  24. $ids = array_reverse($c->recursiveParent($c->parent_id, [$c->id]));
  25. foreach($ids as $ii => $i)
  26. {
  27. if ($ii == 0)
  28. {
  29. $this->level_1_id = $i;
  30. }
  31. if ($ii == 1)
  32. {
  33. $this->level_2_id = $i;
  34. }
  35. if ($ii == 2)
  36. {
  37. $this->level_3_id = $i;
  38. }
  39. }
  40. }
  41. }
  42. public function updatedLevel1Id() {
  43. $this->emit($this->emit, null, $this->idx);
  44. $this->level_2_id = 0;
  45. $this->level_2 = [];
  46. $this->level_3_id = 0;
  47. $this->level_3 = [];
  48. }
  49. public function updatedLevel2Id() {
  50. $this->emit($this->emit, null, $this->idx);
  51. $this->level_3_id = 0;
  52. $this->level_3 = [];
  53. }
  54. public function updatedLevel3Id() {
  55. $this->emit($this->emit, null, $this->idx);
  56. }
  57. public function render()
  58. {
  59. $reset = false;
  60. if ($this->level_1_id > 0)
  61. {
  62. $this->level_2 = \App\Models\Causal::where('parent_id', $this->level_1_id)->where('type', $this->type)->orderBy('name')->get();
  63. if (sizeof($this->level_2) == 0)
  64. {
  65. $this->emit($this->emit, $this->level_1_id, $this->idx);
  66. $reset = true;
  67. }
  68. }
  69. if ($this->level_2_id > 0)
  70. {
  71. $this->level_3 = \App\Models\Causal::where('parent_id', $this->level_2_id)->where('type', $this->type)->orderBy('name')->get();
  72. if (sizeof($this->level_3) == 0)
  73. {
  74. $this->emit($this->emit, $this->level_2_id, $this->idx);
  75. $reset = true;
  76. }
  77. }
  78. if ($this->level_3_id > 0)
  79. {
  80. $this->emit($this->emit, $this->level_3_id, $this->idx);
  81. $reset = true;
  82. }
  83. $this->level_1 = \App\Models\Causal::where('parent_id', null)->where('type', $this->type)->orderBy('name')->get();
  84. return view('livewire.causals');
  85. }
  86. }