You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

<?php
/**
* Created by Error202
* Date: 11.07.2018
*/
namespace backend\components\menu\widgets;
use core\entities\menu\Menu;
use core\entities\menu\MenuItem;
use yii\base\Widget;
class MenuEditorWidget extends Widget
{
3 years ago
public int $menu_id;
6 years ago
3 years ago
public function run(): string
6 years ago
{
$menu = Menu::findOne($this->menu_id);
$items = $menu->items;
6 years ago
return $this->render('menu', [
'items' => $this->getMenu($items),
'menu' => $menu,
]);
}
6 years ago
/**
3 years ago
* @param array $items
* @param null $paren_id
6 years ago
* @return array
*/
3 years ago
private function getMenu(array $items, $paren_id = null): array
6 years ago
{
$array = [];
foreach ($items as $item) {
3 years ago
/* @var $item MenuItem */
6 years ago
if ($item->parent_id != $paren_id) {
continue;
}
$array[$item->id]['item'] = $item;
if ($children = $item->children) {
$array[$item->id]['children'] = $this->getMenu($children, $item->id);
}
}
6 years ago
return $array;
}
}