Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions components/MenuManagerBundle/src/bundle/Entity/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
21 changes: 19 additions & 2 deletions components/MenuManagerBundle/src/bundle/Entity/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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'])) {
Expand Down
Loading