protected function handle() { $forum_id = $this->forumId; $forum = ForumModel::getForum($forum_id); $request = $this->getRequest(); $session = $this->getSession(); if (!$forum) { if ($request->isXmlHttpRequest()) { throw new \Exception('论坛不存在'); } else { $session->addFlash('error', '论坛不存在'); return new RedirectResponse($this->generateUrl('admin_forum_index')); } } if ($request->getMethod() == 'POST') { $posts = $request->request; $db = ForumDatabase::getDb(); $db->transaction(); try { $name = $posts->get('name'); if (!$name) { throw new \Exception('分组名称不能为空'); } $description = $posts->get('description'); $search = $posts->get('search'); if ($search) { // 检查重复性 $g = GroupModel::search($search, $forum_id); if ($g) { throw new \Exception('URL占位符已被使用'); } } else { $search = null; } $group = new GroupModel(); $group->name = $name; $group->search = $search; $group->description = $description; $group->forumId = $forum_id; $now = time(); $group->createTimestamp = $now; $group->updateTimestamp = $now; // 更改论坛数据 $forum->groupCount += 1; // 保存 GroupModel::createGroup($group); ForumModel::saveForum($forum); $db->commit(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_forum_group_index', array('forum_id' => $forum_id))); } return $this->render('group/add.html.twig', array('forum' => $forum)); }
protected function handle() { $forum_id = $this->id; $forum = ForumModel::getForum($forum_id); if (!$forum) { $session = $this->getSession(); $session->addFlash('error', '论坛不存在'); return new RedirectResponse($this->generateUrl('admin_forum_index')); } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $session = $this->getSession(); try { // 检查 $name = $posts->get('name'); if (!$name) { throw new \Exception('论坛名称不能为空'); } if (strlen(trim($name)) < 2) { throw new \Exception('论坛名称至少2个字符'); } $description = $posts->get('description'); $search = $posts->get('search'); if ($search) { // 检查重复 $f = ForumModel::search($search); if ($f && $f->id != $forum->id) { throw new \Exception("URL占位符为 {$search} 的论坛已存在"); } } else { $search = null; } $icon = $posts->get('icon'); $logo = $posts->get('logo'); $slogan = $posts->get('slogan'); // 更新 $forum->name = $name; $forum->description = $description; $forum->search = $search; $forum->icon = $icon; $forum->logo = $logo; $forum->slogan = $slogan; $forum->updateTimestamp = time(); // 保存 ForumModel::saveForum($forum); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_forum_edit', array('id' => $forum_id))); } return $this->render('forum/edit.html.twig', array('forum' => $forum)); }
protected function handle() { $group_id = $this->groupId; $group = GroupModel::getGroup($group_id); if (!$group) { throw new \Exception('论坛分组不存在'); } $forum = ForumModel::getForum($group->forumId); $board_id = intval($this->boardId); $parent_board = null; if ($board_id) { $parent_board = BoardModel::getBoard($board_id); if (!$parent_board) { throw new \Exception('父论坛版块不存在'); } } $request = $this->getRequest(); $session = $this->getSession(); if ($request->getMethod() == 'POST') { $posts = $request->request; $db = WWWDatabase::getDb(); $db->transaction(); try { $name = $posts->get('name'); if (!$name) { throw new \Exception('版块名称不能为空'); } $description = $posts->get('description'); $search = $posts->get('search'); if ($search) { // 检查重复性 $g = BoardModel::search($search, $group->forumId, $group->id); if ($g) { throw new \Exception('URL占位符已被使用'); } } else { $search = null; } $icon = $posts->get('icon'); // 创建 $board = new BoardModel(); $board->name = $name; $board->search = $search; $board->description = $description; $board->icon = $icon; $now = time(); $board->createTimestamp = $now; $board->updateTimestamp = $now; $board->forumId = $group->forumId; $board->groupId = $group->id; $board->parentId = $board_id; // 路径 $path = array(); while ($parent_board != null) { $path[] = $parent_board->id; if ($parent_board->parentId != 0) { $parent_board = BoardModel::getBoard($parent_board->parentId); } else { $parent_board = null; } } $board->path = implode(',', array_reverse($path)); // 保存 BoardModel::createBoard($board); $group->boardCount += 1; GroupModel::saveGroup($group); $forum->boardCount += 1; ForumModel::saveForum($forum); $db->commit(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', '操作失败'); } if ($board_id) { return new RedirectResponse($this->generateUrl('admin_forum_board_index', array('id' => $board_id))); } else { return new RedirectResponse($this->generateUrl('admin_forum_group_index', array('forum_id' => $group->forumId))); } } return $this->render('board/add.html.twig', array('group_id' => $group_id, 'board_id' => $board_id)); }