first upload all files
This commit is contained in:
43
Modules/Menu/Services/MenuItemOrdener.php
Normal file
43
Modules/Menu/Services/MenuItemOrdener.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Menu\Services;
|
||||
|
||||
use Modules\Menu\Entities\MenuItem;
|
||||
|
||||
class MenuItemOrdener
|
||||
{
|
||||
/**
|
||||
* Order the menu items.
|
||||
*
|
||||
* @param array $rootItem
|
||||
* @return void
|
||||
*/
|
||||
public function order(array $rootItem)
|
||||
{
|
||||
$this->orderChildren($rootItem['id'], $rootItem['children']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Order child menu items recursively.
|
||||
*
|
||||
* @param int $parentId
|
||||
* @param array $childItems
|
||||
* @return void
|
||||
*/
|
||||
private function orderChildren($parentId, array $childItems)
|
||||
{
|
||||
foreach ($childItems as $position => $childItem) {
|
||||
$menuItem = MenuItem::find($childItem['id']);
|
||||
|
||||
if (is_null($menuItem)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$menuItem->update(['parent_id' => $parentId, 'position' => $position]);
|
||||
|
||||
if (isset($childItem['children'])) {
|
||||
$this->orderChildren($childItem['id'], $childItem['children']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user