public function __invoke() { /** @var StructureModel $root_node */ $root_node = StructureModel::getRootNode(); if (!$root_node) { // 没有根分类,需要自动创建 $node = new StructureModel(); $node->name = '根'; $node->description = '根'; $now = time(); $node->createTimestamp = $now; $node->updateTimestamp = $now; $node->preOrderTreeLeft = 0; $node->preOrderTreeRight = 1; $root_node = StructureModel::createStructure($node); } /** @var StructureModel[] $sub_tree */ $sub_tree = $root_node->getSubTree(); $list = array(); $path_node = null; foreach ($sub_tree as $i => $node) { if ($i == 0) { continue; } $list[$node->id] = $node; try { if ($node->type == 'menu') { if ($node->metadata) { $path = $node->metadata; } else { $path = '/cate/' . ($node->search ? $node->search : $node->id); } if ($path == $this->getRequest()->getPathInfo()) { $path_node = $node; } } } catch (\Exception $e) { // do nothing } } StructureModelBusiness::preOrderTree2RecursiveTree($list, $root_node); $breadcrumbs = array(); if ($path_node) { /** @var StructureModel[] $nodes */ $nodes = $path_node->getNodeSinglePath(); array_shift($nodes); foreach ($nodes as $node) { $breadcrumbs[$node->id] = $node->name; } } return $this->render('fragment/navigation.html.twig', array('navigation' => $root_node, 'breadcrumbs' => $breadcrumbs)); }
protected function handle() { /** @var StructureModel $root_node */ $root_node = StructureModel::getRootNode(); if (!$root_node) { // 没有根分类,需要自动创建 $node = new StructureModel(); $node->name = '根'; $node->description = '根'; $now = time(); $node->createTimestamp = $now; $node->updateTimestamp = $now; $node->preOrderTreeLeft = 0; $node->preOrderTreeRight = 1; $root_node = StructureModel::createStructure($node); } /** @var StructureModel[] $sub_tree */ $sub_tree = $root_node->getSubTree(); $list = array(); foreach ($sub_tree as $i => $node) { if ($i == 0) { continue; } $list[$node->id] = $node; } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $session = $this->getSession(); try { $root_category = $root_node->toArray(); if (isset($root_category['depth'])) { unset($root_category['depth']); } if (isset($root_category['width'])) { unset($root_category['width']); } if (isset($root_category['children'])) { unset($root_category['children']); } // 保存根菜单 StructureModel::editStructure($root_category); $serialized_output = $request->request->get('serialized_output'); $serialized_output = trim($serialized_output); $serialized_output = json_decode($serialized_output, true); $tree = StructureModelBusiness::formatTree($serialized_output, $list); $root_node->children = $tree; $list = StructureModelBusiness::recursiveTree2PreOrderTree($root_node); foreach ($list as $category) { $category = $category->toArray(); if (isset($category['depth'])) { unset($category['depth']); } if (isset($category['width'])) { unset($category['width']); } if (isset($category['children'])) { unset($category['children']); } // 保存根菜单 StructureModel::editStructure($category); } $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_www_structure')); } StructureModelBusiness::preOrderTree2RecursiveTree($list, $root_node); return $this->render('structure/index.html.twig', array('structure' => $root_node)); }