public function moveTheme(int $themeId, int $newParentThemeId = null, int $position = SerialManager::POSITION_LAST) : Theme { $theme = $this->getThemeById($themeId); $themeIsMovingToAnotherTree = $theme->getParentId() !== $newParentThemeId; if ($themeIsMovingToAnotherTree) { $oldParentId = $theme->getParentId(); if ($newParentThemeId === null) { $theme->setParent(null); } else { $theme->setParent($this->getEntityManager()->getReference(Theme::class, $newParentThemeId)); } $sameLevelThemes = $this->getThemesByParentId($newParentThemeId); $serialManager = new SerialManager($sameLevelThemes); $serialManager->insertAs($theme, $position); $serialManager->normalize(); $this->normalizeTree($oldParentId); $this->getEntityManager()->flush(); } else { $sameLevelThemes = $this->getThemesByParentId($newParentThemeId); $serialManager = new SerialManager($sameLevelThemes); $serialManager->swap($serialManager->locate($position), $theme); $serialManager->normalize(); $this->getEntityManager()->flush(); } return $theme; }
public function normalize() { $serialManager = new SerialManager($this->items); $serialManager->normalize(); $reorder = []; for ($i = 1; $i <= count($this->items); $i++) { $reorder[] = $serialManager->locate($i); } if (count($reorder) !== count($this->items)) { throw new \Exception('SerialManager did something completely wrong.'); } $this->items = $reorder; foreach ($this->items as $subItem) { if ($subItem->sub()->hasChild()) { $subItem->sub()->normalize(); } } }