<?php
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav'],
'items' => \app\modules\manager\models\MenuItemsModel::viewMenuItems(),
]);
NavBar::end();
?>
private static function getMenuItems()
{
$items = [];
$code = 'top-menu';
$query_menu = MenuModel::find()
->andWhere(['code' => $code, 'status' => 1])
//->andWhere([])
->one();
$query = self::find()
->andWhere([
'menu_id' => $query_menu->id,
'status' => 1
])
->all();
foreach ($query as $item)
{
if ( empty($items[$item->parent_id]) )
{
$items[$items->parent_id] = [];
}
$items[$item->parent_id][] = $item->attributes;
}
return $items;
}
/*
* @inheritdoc
*/
public static function viewMenuItems($parentId = 0)
{
$array = self::getMenuItems();
if ( empty($array[$parentId]) ) { return; }
for ( $i = 0; $i < count($array[$parentId]); $i++ )
{
$result[] = [
'label' => $array[$parentId][$i]['name'],
'url' => $array[$parentId][$i]['url_item'],
'items' => self::viewMenuItems($array[$parentId][$i]['id'])
];
}
return $result;
}
$code = 'top-menu';
if ( Choice_Menu::viewMenuItemsTop('top-menu') ) {
echo Nav::widget([
'options' => ['class' => 'navbar-nav'],
'items' => Choice_Menu::viewMenuItemsTop('top-menu'),
]);
}
namespace app\modules\manager\models;
use app\modules\manager\models\MenuModel;
use app\modules\manager\models\MenuItemsModel;
/**
* Description of Choice_Menu
*
* @author Максимус
*/
class Choice_Menu
{
/**
* @inheritdoc
*/
private static function getMenuItemsTop($nameMenu)
{
$items = [];
$code = $nameMenu;
$query_menu = MenuModel::find()
->andWhere(['code' => $code, 'status' => 1])
->one();
$query = MenuItemsModel::find()
->andWhere([
'menu_id' => $query_menu->id,
'status' => 1
])
->all();
foreach ($query as $item)
{
if ( empty($items[$item->parent_id]) )
{
$items[$items->parent_id] = [];
}
$items[$item->parent_id][] = $item->attributes;
}
return $items;
}
/*
* @inheritdoc
*/
public static function viewMenuItemsTop($nameMenu, $parentId = 0)
{
$array = self::getMenuItemsTop($nameMenu);
if ( empty($array[$parentId]) ) { return; }
for ( $i = 0; $i < count($array[$parentId]); $i++ )
{
$result[] = [
'label' => $array[$parentId][$i]['name'],
'url' => [$array[$parentId][$i]['url_item'].'/index'],
'items' => self::viewMenuItemsTop($array[$parentId][$i]['id'], $nameMenu)
];
}
return $result;
}
}