public static function buildTree($elements, $parentId = 0, $author = null, $tag = null, $keyword = null)
 {
     $branch = array();
     foreach ($elements as $element) {
         $element = BmoocJsonController::search($element, $author, $tag, $keyword);
         if ($element['parent_id'] == $parentId) {
             $children = BmoocJsonController::buildTree($element->children, $element['id'], $author, $tag, $keyword);
             if ($children) {
                 //$element['children'] = $children;
             }
             $branch[] = $element;
         }
     }
     return $branch;
 }
 public function tree(Request $request)
 {
     // LIST OF TOPICS
     $topics = DB::table('artefacts')->orderBy('updated_at', 'desc')->whereNull('parent_id')->get();
     $topic = Input::get('topic');
     if ($topic == "all") {
         $topic = null;
     } else {
         if (!is_numeric($topic)) {
             $topic = $topics[0]->thread;
         }
     }
     // BUILD TREE
     $parent = DB::table('artefacts')->select('id')->where('thread', 'LIKE', $topic)->where('parent_id', '=', NULL)->get();
     $parent = Artefact::all()->find($parent[0]->id);
     $tree = $parent;
     $parent->children = BmoocJsonController::buildTree($parent->children, $parent->id);
     // GET PLAIN LIST
     $list = DB::table('artefacts')->where('thread', 'LIKE', $topic)->get();
     $tags = DB::table('artefacts_tags')->select('artefact_id', 'tag_id')->leftJoin('artefacts', 'artefact_id', '=', 'artefacts.id')->where('thread', 'LIKE', $topic)->get();
     $user = Auth::user();
     if ($user && $user->role == "editor") {
         return view('admin.data.tree', ['topics' => $topics, 'topic' => $topic, 'tree' => $tree, 'list' => $list, 'tags' => $tags]);
     } else {
         App::abort(401, 'Not authenticated');
     }
 }