protected function handle() { $tree = StructureModel::getTree(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { try { // 检查参数 $name = $request->request->get('name'); if (!$name) { throw new \Exception("结构名称不能为空"); } // 检查search是否重复 $search = $request->request->get('search'); if ($search) { $structure = StructureModel::search($search); if ($structure) { throw new \Exception("键名为 {$search} 的目录结构已存在"); } } else { $search = null; } $description = $request->request->get('description'); // 类型 $type = $request->request->get('type'); if (!$type) { throw new \Exception("结构的类型不能为空"); } if (!StructureTypeBusiness::isValid($type)) { throw new \Exception("结构类型错误"); } $parent_structure_id = $request->request->get('parent_id'); if (!$parent_structure_id) { throw new \Exception("请选择父结构"); } $parent_structure = StructureModel::getStructure($parent_structure_id); if (!$parent_structure) { throw new \Exception("父结构不存在"); } $metadata = $request->request->get('metadata'); $now = time(); $parent_structure->createChildNode(array('name' => $name, 'search' => $search, 'description' => $description, 'type' => $type, 'metadata' => $metadata, 'create_timestamp' => $now, 'update_timestamp' => $now)); } catch (\Exception $e) { $session = $this->getSession(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_www_structure')); } return $this->render('structure/add.html.twig', array('tree' => $tree)); }
protected function handle() { $structure = StructureModel::getStructure($this->id); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $session = $this->getSession(); try { $name = $posts->get('name'); // 检查search是否重复 $search = $request->request->get('search'); if ($search) { $s = StructureModel::search($search); if ($s->id != $this->id) { throw new \Exception("键名为 {$search} 的目录结构已存在"); } } else { $search = null; } $description = $posts->get('description'); if (!$name) { throw new \Exception("结构名称不能为空"); } $type = $posts->get('type'); if (!$type) { throw new \Exception("结构类型不能为空"); } if (!StructureTypeBusiness::isValid($type)) { throw new \Exception("结构类型错误"); } $metadata = $posts->get('metadata'); $structure->name = $name; $structure->search = $search; $structure->description = $description; $structure->type = $type; $structure->metadata = $metadata; $structure->updateTimestamp = time(); StructureModel::saveStructure($structure); $session->addFlash('success', '编辑成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_www_structure')); } return $this->render('structure/edit.html.twig', array('structure' => $structure)); }