Pārlūkot izejas kodu

rappresentazione livello

FabioFratini 9 mēneši atpakaļ
vecāks
revīzija
ebcdcdc521
1 mainītis faili ar 29 papildinājumiem un 5 dzēšanām
  1. 29 5
      app/Http/Livewire/RecordOUT.php

+ 29 - 5
app/Http/Livewire/RecordOUT.php

@@ -206,20 +206,44 @@ class RecordOUT extends Component
 
     public function loadImportCausals()
     {
-        $causals = \App\Models\Causal::select('id', 'name')
+        $causals = \App\Models\Causal::select('id', 'name', 'parent_id')
             ->where('type', 'OUT')
             ->orderBy('name')
             ->get();
 
         $this->importCausals = [];
+        $this->buildCausalTree($causals);
+    }
+
+    private function buildCausalTree($causals, $parentId = null, $level = 0)
+    {
         foreach ($causals as $causal) {
-            $this->importCausals[] = [
-                'id' => $causal->id,
-                'name' => $causal->getTree()
-            ];
+            if ($causal->parent_id == $parentId) {
+                $dotString = str_repeat('● ', $level);
+                $this->importCausals[] = [
+                    'id' => $causal->id,
+                    'name' => $dotString . $causal->name
+                ];
+
+                $this->buildCausalTree($causals, $causal->id, $level + 1);
+            }
         }
     }
 
+    private function getCausalLevel($causal, $level = 0)
+    {
+        if ($causal->parent_id == null) {
+            return $level;
+        }
+
+        $parent = \App\Models\Causal::find($causal->parent_id);
+        if (!$parent) {
+            return $level;
+        }
+
+        return $this->getCausalLevel($parent, $level + 1);
+    }
+
     public function getCausal($causal)
     {
         $ret = '';