creocoder / yii2-nested-sets

The nested sets behavior for the Yii framework.
Other
446 stars 129 forks source link

How to create hierarchical Html::Menu() with nested sets? #100

Open just-greg-me opened 8 years ago

just-greg-me commented 8 years ago
private static function getTreeInline($categories, $left = 0, $right = null, $lvl = 1){

    $names = '';

    foreach ($categories as $index => $category) {
        if ($category->lft >= $left + 1 && (is_null($right) || $category->rgt <= $right) && $category->lvl == $lvl) {
            $childName = self::getTreeInline($categories, $category->lft, $category->rgt, $category->lvl + 1);
            $adding = '';
            if(!empty($childName)){
                $adding = ' - '.$childName;
            }
            $names .= $category->name.$adding;
        }
    }
    return $names;

}

public static function getFullTreeInline(){

    $roots = Category::find()->roots()->addOrderBy('root, lft')->all();
    $tree = [];
    foreach ($roots as $root){
        $tree [] = [
            'id' => $root->id,
            'title' => $root->name.' - '.self::getTreeInline($root->children()->all()),
        ];
    }

    return $tree;
}