/** * JSON tree structure for jsTree * * @param Request $request * @return JsonResponse */ public function jsonTreeSourceAction(Request $request) { if (false === $this->admin->isGranted('LIST')) { throw new AccessDeniedException(); } if (!$request->isXmlHttpRequest()) { throw $this->createNotFoundException(); } $root = PageQuery::create()->findRoot(); if (!$root) { $root = new Page(); $root->setLocale($request->getLocale()); $root->setTitle('Home'); $root->makeRoot(); $root->save(); } $root->setLocale($request->getLocale()); $buildTree = function (Page $root, &$buildTree) { $array_nodes = array(); /** @var Page $node */ foreach ($root->getChildren() as $node) { $node->setLocale($root->getLocale()); $array_node = array(); $array_node['id'] = $node->getId(); $array_node['text'] = $node->getTitle(); if ($node->getModule()) { $array_node['type'] = 'module'; } else { $array_node['type'] = 'default'; } if ($node->hasChildren()) { $array_node['children'] = $buildTree($node, $buildTree); } $array_nodes[] = $array_node; } return $array_nodes; }; $data_tree = array('id' => $root->getId(), 'text' => $root->getTitle(), 'type' => 'root', 'children' => $buildTree($root, $buildTree)); return new JsonResponse($data_tree); }
/** * Return array of siblings for menu generation * * @param Page $page * @param Router $router * @return array */ public function getSubMenu(Page $page, Router $router) { $childrens = $page->getChildren($this->getShowMenuCriteria()); $menu = array(); /** @var Page $children */ foreach ($childrens as $children) { $children->setLocale($page->getLocale()); $item = array('page' => $children, 'title' => $children->getTitle(), 'link' => $this->generatePageLink($children, $router), 'active' => $this->isPageActive($children), 'subactive' => $this->isPageSubActive($children)); $menu[] = $item; } return $menu; }