public function listAction($pageId = false, $menuId = false, $ajaxTemplate = 'menu_tabs', $rootMenuId = null)
{
if (false === $this->admin->isGranted('LIST')) {
throw new AccessDeniedException();
}
/** @var Request $request */
$request = $this->get('request_stack')->getCurrentRequest();
if ($request->get('page_id')) {
$pageId = $request->get('page_id');
}
if ($request->get('menu_id')) {
$menuId = $request->get('menu_id');
if ($menuId) {
$this->get('session')->set('MenuItem.last_edited', $menuId);
}
}
$this->get('session')->set('admin/last_page_id', $pageId);
if ($request->get('show_now_confirm_dialog')) {
$user = $this->getUser();
$user->setAdminSetting('menuAdmin.show_now_confirm_dialog', true);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
$menus = array();
/** @var MenuItemManager $menuItemManager */
$menuItemManager = $this->get('networking_init_cms.menu_item_manager');
/*
* if the list was filtered or a new menu entry was posted,
* use the submitted locale for list, else set the locale
* to the users default within the availble CMS frontend languages
*/
if ($menuId) {
$menu = $menuItemManager->find($menuId);
if ($menu) {
$this->currentMenuLanguage = $menu->getLocale();
}
}
if ($postArray = $request->get($this->admin->getUniqid())) {
// posted locale
if (array_key_exists('locale', $request->get($this->admin->getUniqid()))) {
$this->currentMenuLanguage = $postArray['locale'];
}
} elseif ($filterParameters = $this->admin->getFilterParameters()) {
// filter locale
if (array_key_exists('locale', $filterParameters) && $filterParameters['locale']['value']) {
$this->currentMenuLanguage = $filterParameters['locale']['value'];
}
}
if (!$this->currentMenuLanguage) {
// fallback to default
$this->currentMenuLanguage = $this->admin->getDefaultLocale();
}
if (!$this->currentMenuLanguage) {
throw new \Sonata\AdminBundle\Exception\NoValueException('No locale has been provided to generate a menu list');
}
//use one of the locale to filter the list of menu entries (see above
$rootNodes = $menuItemManager->getRootNodesByLocale($this->currentMenuLanguage, 'id');
$childOpen = function ($node) {
return sprintf('<li class="table-row-style" id="listItem_%s">', $node['id']);
};
$admin = $this->admin;
$controller = $this;
/** @var ArrayCollection $menuAllItems */
$menuAllItems = new ArrayCollection($menuItemManager->findAllJoinPage());
$nodeDecorator = function ($node) use($admin, $controller, $menuItemManager, $menuAllItems) {
$items = $menuAllItems->filter(function ($menuItem) use($node) {
if ($menuItem->getId() == $node['id']) {
return $menuItem;
}
});
$node = $items->first();
return $controller->renderView('NetworkingInitCmsBundle:MenuItemAdmin:menu_list_item.html.twig', array('admin' => $admin, 'node' => $node));
};
foreach ($rootNodes as $rootNode) {
if ($rootMenuId) {
if ($rootMenuId == $rootNode->getId()) {
$menus = array('rootNode' => $rootNode, 'navigation' => $this->createPlacementNavigation($rootNode, $admin, $controller, $menuItemManager));
break;
}
} else {
$navigation = $menuItemManager->childrenHierarchy($rootNode, null, array('rootOpen' => '<ul class="table-row-style">', 'decorate' => true, 'childOpen' => $childOpen, 'childClose' => '</li>', 'nodeDecorator' => $nodeDecorator), false);
$menus[] = array('rootNode' => $rootNode, 'navigation' => $navigation);
}
}
$datagrid = $this->admin->getDatagrid();
if ($menuId) {
$menu = $menuItemManager->find($menuId);
if ($menu) {
$locale = $menu->getLocale();
$datagrid->setValue('locale', null, $locale);
$datagrid->buildPager();
}
}
$formView = $datagrid->getForm()->createView();
// set the theme for the current Admin Form
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
if ($this->isXmlHttpRequest()) {
if ($request->get('render')) {
return $this->render('NetworkingInitCmsBundle:MenuItemAdmin:' . $ajaxTemplate . '.html.twig', array('menus' => $menus, 'admin' => $this->admin, 'page_id' => $pageId));
} else {
return $this->renderView('NetworkingInitCmsBundle:MenuItemAdmin:' . $ajaxTemplate . '.html.twig', array('menus' => $menus, 'admin' => $this->admin, 'page_id' => $pageId));
}
}
$lastEdited = $this->get('session')->get('MenuItem.last_edited');
return $this->render($this->admin->getTemplate('list'), array('action' => 'navigation', 'form' => $formView, 'datagrid' => $datagrid, 'menus' => $menus, 'last_edited' => $lastEdited, 'page_id' => $pageId, 'menu_id' => $menuId));
}