Ad

Output Nested Set For Google Visualisation

I am using Baum for Laravel to retrieve a nested set, but I can't begin to think how to get it into the format that my Google organisation chart needs it.

The outputted JSON:

{  
   "1":{  
      "id":1,
      "parent_id":null,
      "lft":1,
      "rgt":10,
      "depth":0,
      "name":"Jing's Team",
      "created_at":"-0001-11-30 00:00:00",
      "updated_at":"2016-01-24 18:32:22",
      "children":[  
         {  
            "id":2,
            "parent_id":1,
            "lft":2,
            "rgt":3,
            "depth":1,
            "name":"Ann's Team",
            "created_at":"-0001-11-30 00:00:00",
            "updated_at":"2016-01-24 18:32:22",
            "children":[  

            ]
         },
         {  
            "id":3,
            "parent_id":1,
            "lft":4,
            "rgt":9,
            "depth":1,
            "name":"Mike's Team",
            "created_at":"-0001-11-30 00:00:00",
            "updated_at":"2016-01-24 18:32:22",
            "children":[  
               {  
                  "id":4,
                  "parent_id":3,
                  "lft":5,
                  "rgt":8,
                  "depth":2,
                  "name":"Laidy's Team",
                  "created_at":"-0001-11-30 00:00:00",
                  "updated_at":"2016-01-24 18:32:22",
                  "children":[  
                     {  
                        "id":5,
                        "parent_id":4,
                        "lft":6,
                        "rgt":7,
                        "depth":3,
                        "name":"Steve's Team",
                        "created_at":"-0001-11-30 00:00:00",
                        "updated_at":"2016-01-24 18:32:22",
                        "children":[  

                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
}

Which is generated with:

    Team::rebuild(true);
    $nodes = Team::where('id', Auth::user()->team_id)->first();
    $nodes = $nodes->getDescendantsAndSelf()->toHierarchy();

    return view('team.index', ['nodes' => $nodes]);

The way that Google organisation takes it:

data.addRows([
    [{v:'Mike', f:'Mike<div style="color:red; font-style:italic">President</div>'},
        '', 'The President'],
    [{v:'Jim', f:'Jim<div style="color:red; font-style:italic">Vice President</div>'},
        'Mike', 'VP'],
    ['Sam', 'Mike', ''],
    ['Bob', 'Jim', 'Bob Sponge'],
    ['Phil', 'Jim', 'Bob Sponge'],
    ['Carol', 'Bob', '']
]);

['NAME', 'PARENT_NAME', 'HOVER OVER TITLE']

How can parse the JSON to get it into the format that Google needs it?


So far I have:

public function index()
{
    Team::rebuild(true);
    $nodes = Team::where('id', Auth::user()->team_id)->first();
    $nodes = $nodes->getDescendantsAndSelf()->toHierarchy();

    $data = [];
    foreach($nodes as $node) {
        $data[] = [$node->name, '', ''];
        $data = $this->parseChildNode($node, $data);
    }

    return view('team.index', ['nodes' => json_encode($data)]);
}
protected $i = 0;
protected function parseChildNode($node, $data)
{
    if ($node->children()->count() > 0) {
        foreach ($node->children as $child) {
            $data[] = [$child->name, $data[$this->i][0], ''];
            $data = $this->parseChildNode($child, $data);
        }
        $this->i++;
        return $data;
    }
    return $data;
}

But it only goes down two levels and no further :(


I am now getting (everything is under "Jing's Team" which is incorrect):

[["Jing's Team","",""],["Ann's Team","Jing's Team",""],["Mike's Team","Jing's Team",""],["Laidy's Team","Jing's Team",""],["Steve's Team","Jing's Team",""]]

With the above altered code.

Ad

Answer

The following code seems to works with your use case, biggest issue was $data[$this->i][0]

public function index()
{
    Team::rebuild(true);
    $node = Team::where('id', Auth::user()->team_id)->first();
    $nodes = $nodes->getDescendantsAndSelf()->toHierarchy();

    $data = [];
    foreach($nodes as $node) {
        $data[] = [$node->name, '', ''];
        $data = $this->parseNodesChildren($node, $data);
    }

    return view('team.index', ['nodes' => json_encode($data)]);
}
protected function parseNodesChildren($node, &$data)
{       
    if ($node->children->count() > 0) {
        foreach ($node->children as $child) {
            $data[] = [$child->name, $node->name, ''];
            $this->parseNodesChildren($child, $data);
        }
    }
}
Ad
source: stackoverflow.com
Ad