Courses.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use App\Http\Middleware\TenantMiddleware;
  5. use Livewire\WithPagination;
  6. use Illuminate\Support\Facades\Auth;
  7. class Courses extends Component
  8. {
  9. public $level_1 = [];
  10. public $level_2 = [];
  11. public $level_3 = [];
  12. public $level_1_id = 0;
  13. public $level_2_id = 0;
  14. public $level_3_id = 0;
  15. public $course_id = null;
  16. public function boot()
  17. {
  18. app(TenantMiddleware::class)->setupTenantConnection();
  19. }
  20. public function mount($course_id)
  21. {
  22. $this->course_id = $course_id;
  23. if ($this->course_id != null)
  24. {
  25. $c = \App\Models\Course::findOrFail($this->course_id);
  26. $ids = array_reverse($c->recursiveParent($c->parent_id, [$c->id]));
  27. foreach($ids as $ii => $i)
  28. {
  29. if ($ii == 0)
  30. {
  31. $this->level_1_id = $i;
  32. }
  33. if ($ii == 1)
  34. {
  35. $this->level_2_id = $i;
  36. }
  37. if ($ii == 2)
  38. {
  39. $this->level_3_id = $i;
  40. }
  41. }
  42. }
  43. }
  44. public function updatedLevel1Id() {
  45. $this->emit('setCourse', null);
  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. $this->emit('setCourse', null);
  53. $this->level_3_id = 0;
  54. $this->level_3 = [];
  55. }
  56. public function updatedLevel3Id() {
  57. $this->emit('setCourse', null);
  58. }
  59. public function render()
  60. {
  61. $reset = false;
  62. if ($this->level_1_id > 0)
  63. {
  64. $this->level_2 = \App\Models\Course::where('parent_id', $this->level_1_id)->orderBy('name')->get();
  65. if (sizeof($this->level_2) == 0)
  66. {
  67. $this->emit('setCourse', $this->level_1_id);
  68. $reset = true;
  69. }
  70. }
  71. if ($this->level_2_id > 0)
  72. {
  73. $this->level_3 = \App\Models\Course::where('parent_id', $this->level_2_id)->orderBy('name')->get();
  74. if (sizeof($this->level_3) == 0)
  75. {
  76. $this->emit('setCourse', $this->level_2_id);
  77. $reset = true;
  78. }
  79. }
  80. if ($this->level_3_id > 0)
  81. {
  82. $this->emit('setCourse', $this->level_3_id);
  83. $reset = true;
  84. }
  85. $this->level_1 = \App\Models\Course::where('parent_id', null)->orderBy('name')->get();
  86. return view('livewire.courses');
  87. }
  88. }