/** * Добавить эементы структуры для * указанного родителя и местоположения * @param Structure[] $structureTree * @param Menu $menuParent * @param string $location */ protected function addMenuTree($structureTree, $menuParent, $location) { $menuRepository = $this->entityManager->getRepository('OctavaMenuBundle:Menu'); /** @var Menu[] $menuChildren */ $menuChildren = $menuRepository->getByParentIdAndLocation($menuParent ? $menuParent->getId() : 0, $location); $existStructureIds = []; foreach ($menuChildren as $menuItem) { if ($menuItem->getStructure()) { $structureId = $menuItem->getStructure()->getId(); $existStructureIds[$structureId] = $structureId; } } $structureParentId = $menuParent && $menuParent->getStructure() ? $menuParent->getStructure()->getId() : null; $structureAddElements = []; /** @var Structure $structureItem */ foreach ($structureTree as $structureItem) { $currentParentId = $structureItem->getParent() && $structureItem->getParent()->getId() ? $structureItem->getParent()->getId() : null; if ($currentParentId == $structureParentId && !isset($existStructureIds[$structureItem->getId()])) { $structureAddElements[] = $structureItem; } } if (count($structureAddElements)) { $translationRepository = $this->entityManager->getRepository('Gedmo\\Translatable\\Entity\\Translation'); foreach ($structureAddElements as $structureItem) { /** @var Structure $structureItem */ $menu = new Menu(); $menu->setState(true); $menu->setLocation($location); $menu->setStructure($structureItem); $menu->setTitle($structureItem->getTitle()); $menu->setLink($structureItem->getPreparedPath()); if ($menuParent) { $menu->setParent($menuParent); } $this->entityManager->persist($menu); $structureTranslations = $translationRepository->findTranslations($structureItem); foreach ($this->localeManager->getAllAliases() as $localeAlias) { if (!isset($structureTranslations[$localeAlias])) { continue; } $translation = $structureTranslations[$localeAlias]; $path = empty($translation['path']) ? $structureItem->getPath() : $translation['path']; $title = empty($translation['title']) ? $structureItem->getTitle() : $translation['title']; $translationRepository->translate($menu, 'title', $localeAlias, $title); $translationRepository->translate($menu, 'proxyTitle', $localeAlias, true); $translationRepository->translate($menu, 'link', $localeAlias, $structureItem->getType() == Structure::TYPE_STRUCTURE_EMPTY ? '' : $path); $translationRepository->translate($menu, 'proxyLink', $localeAlias, true); } $menuChildren[] = $menu; $this->updatedItems[] = $menu; } $this->entityManager->flush(); } foreach ($menuChildren as $value) { if ($value->getStructure()) { $this->addMenuTree($structureTree, $value, $location); } } }
/** * @param Menu $menuItem */ protected function updateItem($menuItem) { if (in_array($menuItem->getId(), $this->updatedItems)) { return; } $this->updatedItems[] = $menuItem->getId(); $translationRepository = $this->entityManager->getRepository('Gedmo\\Translatable\\Entity\\Translation'); $structureItem = $menuItem->getStructure(); if (is_null($structureItem)) { return; } $structureTranslations = $translationRepository->findTranslations($structureItem); $translations = $translationRepository->findTranslations($menuItem); foreach ($translations as $locale => $translation) { if (!empty($translation['proxyTitle'])) { $title = empty($structureTranslations[$this->defaultLocale]['title']) ? $structureItem->getTitle() : $structureTranslations[$this->defaultLocale]['title']; if (!empty($structureTranslations[$locale]['title'])) { $title = $structureTranslations[$locale]['title']; } $translationRepository->translate($menuItem, 'title', $locale, $title); } if (!empty($translation['proxyLink'])) { $link = empty($structureTranslations[$this->defaultLocale]['path']) ? $structureItem->getPath() : $structureTranslations[$this->defaultLocale]['path']; if (!empty($structureTranslations[$locale]['path'])) { $link = $structureTranslations[$locale]['path']; } $translationRepository->translate($menuItem, 'link', $locale, $link); } } }