<?php

namespace common\models;
 
use backend\models\Menu;
use yii\helpers\Url;

class MenuTree extends Menu
{   
    var $mass = array (); 
    
    public function build ($location_name)
    {
        if ($this->mass = parent::getMenuList ($location_name))
        {
            return $this->getMenuRecrusive (0);
        }
    }

    public function findChild ($id)
    {
        $mass = array ();
    
        foreach ($this->mass as $row)
        {
            if ($row['menu_pid'] == $id)
            {
                $mass[] = $row;
            }
        }
    
        return $mass;
    }
    
    public function getMenuRecrusive ($menu_id)
    { 
        $items = $this->findChild($menu_id);

        if (! empty ($items))
        { 
            $result = [];
        
            foreach ($items as $row) 
            {
                $result[] = [
                    'label' => $row['termin_title'],
                    'url' => ['/'.$row['termin_alias']],
                    //'url' => [$row['template_file']],
                    //'url' => Url::toRoute($row['template_file']),
                    'items' => $this->getMenuRecrusive($row['menu_id']),
                    '<li class="divider"></li>',
                ];
            }
 
            return $result;
        }

    }
}
