Causals.php 2.6 KB

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