/** * @param \Doctrine\Common\Persistence\ObjectManager $manager * @param $locale */ public function createMenuItems(ObjectManager $manager, $locale) { $menuRoot = new MenuItem(); $menuRoot->setName('Main menu'); $menuRoot->setLocale($locale); $menuRoot->setIsRoot(true); $menuRoot->setLvl(1); $menuRoot->setLft(1); $menuRoot->setRgt(2); $manager->persist($menuRoot); $manager->flush(); $homePageMenu = new MenuItem(); $homePageMenu->setName('Homepage'); $homePageMenu->setPage($this->getReference('homepage_' . $locale)); $homePageMenu->setParent($menuRoot); $manager->persist($homePageMenu); $manager->flush(); $footerRoot = new MenuItem(); $footerRoot->setName('Footer menu'); $footerRoot->setLocale($locale); $footerRoot->setIsRoot(true); $footerRoot->setLvl(1); $footerRoot->setLft(1); $footerRoot->setRgt(2); $manager->persist($footerRoot); $manager->flush(); }
/** * @param \Symfony\Component\HttpFoundation\Request $request * @param $rootId * @param $pageId * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * @return \Symfony\Component\HttpFoundation\RedirectResponse */ public function createFromPageAction(Request $request, $rootId, $pageId) { $rootNode = $this->admin->getObject($rootId); if (!$rootNode) { throw new NotFoundHttpException(sprintf('unable to find the Menu with id : %s', $rootId)); } $page = $this->get('networking_init_cms.page_manager')->find($pageId); if (!$page) { throw new NotFoundHttpException(sprintf('unable to find the Page with id : %s', $pageId)); } $menuItem = new MenuItem(); $menuItem->setPage($page); $menuItem->setRoot($rootNode); $menuItem->setParent($rootNode); $menuItem->setName($page->getTitle()); $em = $this->getDoctrine()->getManager(); $em->persist($menuItem); $em->flush(); $this->admin->createObjectSecurity($menuItem); return $this->redirect($this->admin->generateUrl('list', array('page_id' => $page->getId(), 'menu_id' => $menuItem->getId())) . '#end'); }
/** * Add a menu item node at the correct place in the menu * * @param Menu $menu * @param MenuItem $node * @param $startDepth * @return bool|\Knp\Menu\ItemInterface */ public function addNodeToMenu(Menu $menu, MenuItem $node, $startDepth) { if ($node->getLvl() < $startDepth) { return false; } if ($node->getLvl() > $startDepth) { $menu = $this->getParentMenu($menu, $node, $startDepth); } if (is_object($menu)) { $knpMenuNode = $this->createFromMenuItem($node); if (!is_null($knpMenuNode)) { $menu->addChild($knpMenuNode); $knpMenuNode->setAttribute('class', $node->getLinkClass()); if ($node->getVisibility() != MenuItem::VISIBILITY_PUBLIC && !$this->isLoggedIn) { $knpMenuNode->setDisplay(false); } return $knpMenuNode; } } return false; }
/** * {@inheritdoc} */ protected function configureFormFields(FormMapper $formMapper) { if (!($locale = $this->getRequest()->get('locale'))) { $locale = $this->getRequest()->getLocale(); } $uniqId = $this->getUniqid(); if ($postArray = $this->getRequest()->get($uniqId)) { if (array_key_exists('locale', $postArray)) { $locale = $postArray['locale']; } } $er = $this->getContainer()->get('Doctrine')->getRepository('NetworkingInitCmsBundle:MenuItem'); $id = $this->getRequest()->get('id'); if ($id) { $menuItem = $er->find($id); $locale = $menuItem->getLocale(); } if ($rootId = $this->getRequest()->get('root_id')) { $root = $er->find($rootId); } elseif ($id) { $root = $er->find($this->getSubject()->getRoot()); } else { $root = $er->findOneBy(array('isRoot' => 1, 'locale' => $locale)); } if ($this->getRequest()->get('subclass') && $this->getRequest()->get('subclass') == 'menu') { $this->isRoot = true; } elseif ($this->getSubject()->getIsRoot()) { $this->isRoot = true; } $formMapper->add('locale', 'hidden', array('data' => $locale))->add('name', null, array('horizontal' => true)); if ($this->isRoot) { $formMapper->add('description', null, array('horizontal' => true))->add('isRoot', 'hidden', array('data' => true)); } else { // start group page_or_url $formMapper->with('form.legend_page_or_url', array('collapsed' => false, 'description' => $this->translator->trans('form.legend_page_or_url', array(), $this->translationDomain), 'horizontal' => true)); $pageAdmin = $this->configurationPool->getAdminByAdminCode('networking_init_cms.admin.page'); $pageClass = $pageAdmin->getClass(); $formMapper->add('page', 'networking_type_autocomplete', array('attr' => array('style' => "width:220px"), 'class' => $pageClass, 'required' => false, 'horizontal' => true, 'property' => 'AdminTitle', 'query_builder' => function (EntityRepository $er) use($locale) { $qb = $er->createQueryBuilder('p'); $qb->where('p.locale = :locale')->orderBy('p.path', 'asc')->setParameter(':locale', $locale); return $qb; })); $formMapper->add('redirect_url', 'url', array('required' => false, 'help_block' => 'help.redirect_url', 'horizontal' => true)); $formMapper->add('internal_url', 'text', array('required' => false, 'help_block' => 'help.internal_url', 'horizontal' => true)); $formMapper->end(); // start group optionals $formMapper->with('Options', array('collapsed' => false, 'description' => $this->translator->trans('form.legend_options', array(), $this->translationDomain), 'horizontal' => true))->add('visibility', 'sonata_type_translatable_choice', array('horizontal' => true, 'help_block' => 'visibility.helper.text', 'choices' => MenuItem::getVisibilityList(), 'catalogue' => $this->translationDomain))->add('link_target', 'choice', array('horizontal' => true, 'choices' => $this->getTranslatedLinkTargets(), 'required' => false))->add('link_class', 'text', array('horizontal' => true, 'required' => false))->add('link_rel', 'text', array('horizontal' => true, 'required' => false))->add('hidden', null, array('horizontal' => true, 'required' => false))->end(); $entityManager = $this->getContainer()->get('Doctrine')->getManager(); $transformer = new MenuItemToNumberTransformer($entityManager); $menuField = $formMapper->getFormBuilder()->create('menu', 'hidden', array('data' => $root, 'data_class' => null)); $menuField->addModelTransformer($transformer); $formMapper->add($menuField, 'hidden'); } }