diff --git a/components/MenuManagerBundle/src/bundle/Controller/AdminController.php b/components/MenuManagerBundle/src/bundle/Controller/AdminController.php index 2ed1658e8..3ffd27c96 100644 --- a/components/MenuManagerBundle/src/bundle/Controller/AdminController.php +++ b/components/MenuManagerBundle/src/bundle/Controller/AdminController.php @@ -162,6 +162,7 @@ public function editAction(Request $request, Menu $menu) if ($form->isSubmitted() && $form->isValid()) { /** @var Menu $menu */ $menu = $form->getData(); + $menu->assignPositions(); $this->em->persist($menu); $this->em->flush(); diff --git a/components/MenuManagerBundle/src/bundle/Entity/Menu.php b/components/MenuManagerBundle/src/bundle/Entity/Menu.php index 7d520429d..e416364e7 100644 --- a/components/MenuManagerBundle/src/bundle/Entity/Menu.php +++ b/components/MenuManagerBundle/src/bundle/Entity/Menu.php @@ -178,4 +178,15 @@ public function __toString() { return (string) $this->id; } + + public function assignPositions(): void + { + $rootItems = $this->getItemsByParent(); + $position = 0; + foreach ($rootItems as $item) { + $item->setPosition($position); + $item->assignPositions(); + ++$position; + } + } } diff --git a/components/MenuManagerBundle/src/bundle/Entity/MenuItem.php b/components/MenuManagerBundle/src/bundle/Entity/MenuItem.php index bb7f0e88e..872250cad 100644 --- a/components/MenuManagerBundle/src/bundle/Entity/MenuItem.php +++ b/components/MenuManagerBundle/src/bundle/Entity/MenuItem.php @@ -13,6 +13,7 @@ namespace Novactive\EzMenuManagerBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Mapping as ORM; use Novactive\EzMenuManager\Traits\IdentityTrait; @@ -203,7 +204,10 @@ public function hasChildrens(): bool */ public function getChildrens() { - return $this->childrens; + $criteria = new Criteria(); + $criteria->orderBy(['position' => Criteria::ASC]); + + return $this->childrens->matching($criteria); } /** @@ -303,7 +307,20 @@ public function __toString() public function update(array $properties): void { foreach ($properties as $property => $value) { - $this->$property = $value; + if ($this->$property !== $value) { + $this->$property = $value; + } + } + } + + public function assignPositions(): void + { + $childrens = $this->getChildrens(); + $position = 0; + foreach ($childrens as $child) { + $child->setPosition($position); + $child->assignPositions(); + ++$position; } } } diff --git a/components/MenuManagerBundle/src/lib/Form/Type/FieldType/FieldValueTransformer.php b/components/MenuManagerBundle/src/lib/Form/Type/FieldType/FieldValueTransformer.php index 934161716..ac4f93a78 100644 --- a/components/MenuManagerBundle/src/lib/Form/Type/FieldType/FieldValueTransformer.php +++ b/components/MenuManagerBundle/src/lib/Form/Type/FieldType/FieldValueTransformer.php @@ -78,7 +78,7 @@ public function reverseTransform($value) } $parent = $menuItemRepo->find($hashItem['parentId']); if ($hashItem['parentId'] && $parent) { - $menuItem->setParent($parent); + $parent->addChildren($menuItem); } $menuItem->setMenu($menuRepo->find($hashItem['menuId'])); $menuItem->setPosition($hashItem['position'] ?? 0); diff --git a/components/MenuManagerBundle/src/lib/MenuItem/Type/DefaultMenuItemType.php b/components/MenuManagerBundle/src/lib/MenuItem/Type/DefaultMenuItemType.php index 1ccd79394..632232d28 100644 --- a/components/MenuManagerBundle/src/lib/MenuItem/Type/DefaultMenuItemType.php +++ b/components/MenuManagerBundle/src/lib/MenuItem/Type/DefaultMenuItemType.php @@ -76,7 +76,7 @@ public function fromHash($hash): ?MenuItem 'name' => $hash['name'] ?? false, 'url' => $hash['url'] ?? false, 'target' => $hash['target'] ?? false, - 'position' => $hash['position'] ?? 0, + 'position' => (int) $hash['position'] ?? 0, ]; $menuItem->update(array_filter($updateData)); @@ -85,11 +85,12 @@ public function fromHash($hash): ?MenuItem $menuItem->setOption($option, $value); } - if (isset($hash['parentId']) && $hash['parentId']) { + $currentParent = $menuItem->getParent(); + if ($currentParent && (!isset($hash['parentId']) || null === $hash['parentId'])) { + $currentParent->removeChildren($menuItem); + } elseif ($hash['parentId'] && (!$currentParent || $currentParent->getId() !== $hash['parentId'])) { $parent = $menuItemRepo->find($hash['parentId']); - $menuItem->setParent($parent); - } else { - $menuItem->setParent(null); + $parent->addChildren($menuItem); } if (isset($hash['menuId'])) {