Fixed menu sort issue

This commit is contained in:
jitendra 2018-12-12 16:26:20 +05:30
parent 7f65c3327a
commit 15135d64d7
2 changed files with 52 additions and 8 deletions

View File

@ -68,23 +68,22 @@ class AdminServiceProvider extends ServiceProvider
$menu = current(Event::fire('admin.menu.create'));
$keys = explode('.', $menu->currentKey);
$subMenus = $tabs = [];
if (count($keys) > 1) {
$subMenus = [
'items' => $menu->sortItems(array_get($menu->items, current($keys) . '.children')),
'current' => $menu->current,
'currentKey' => $menu->currentKey
];
if (count($keys) > 2) {
$tabs = [
'items' => $menu->sortItems(array_get($menu->items, implode('.children.', array_slice($keys, 0, 2)) . '.children')),
'current' => $menu->current,
'currentKey' => $menu->currentKey
];
}
}
$menu->items = $menu->sortItems($menu->items);
$view->with('menu', $menu)->with('subMenus', $subMenus)->with('tabs', $tabs);
});
}

View File

@ -26,7 +26,6 @@ class Menu {
public static function create($callback) {
$menu = new Menu();
$callback($menu);
$menu->sortItems($menu->items);
return $menu;
}
@ -58,7 +57,7 @@ class Menu {
}
$children = str_replace('.', '.children.', $key);
array_set($this->items, $children, $item);
$this->array_set($this->items, $children, $item);
}
/**
@ -67,9 +66,12 @@ class Menu {
* @return void
*/
public function sortItems($items) {
if(!$items) {
return;
foreach ($items as &$item) {
if(count($item['children'])) {
$item['children'] = $this->sortItems($item['children']);
}
}
usort($items, function($a, $b) {
if ($a['sort'] == $b['sort']) {
return 0;
@ -81,6 +83,49 @@ class Menu {
return $items;
}
public function array_set(&$array, $key, $value)
{
if (is_null($key)) {
return $array = $value;
}
$keys = explode('.', $key);
$count = count($keys);
while (count($keys) > 1) {
$key = array_shift($keys);
if (! isset($array[$key]) || ! is_array($array[$key])) {
$array[$key] = [];
}
$array = &$array[$key];
}
$finalKey = array_shift($keys);
if(isset($array[$finalKey])) {
$array[$finalKey] = $this->arrayMerge($array[$finalKey], $value);
} else {
$array[$finalKey] = $value;
}
return $array;
}
protected function arrayMerge(array &$array1, array &$array2)
{
$merged = $array1;
foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->arrayMerge($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}
return $merged;
}
/**
* Method to find the active links
*