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() { $board_id = $this->boardId; $board = BoardModel::getBoard($board_id); $moderators = ModeratorModel::allModerator(function (QueryBuilder $qb) use($board) { $qb->andWhere($qb->expr()->eq('board_id', ':board_id'))->setParameter(':board_id', $board->id); $qb->andWhere($qb->expr()->eq('forum_id', ':forum_id'))->setParameter(':forum_id', $board->forumId); }); $session = $this->getSession(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $moderator_ids = $posts->get('moderators'); if (!$moderator_ids) { $moderator_ids = array(); } else { $moderator_ids = explode(',', $moderator_ids); } // 先删除所有的管理员, 再逐个添加 $db = ForumDatabase::getDb(); $db->transaction(); try { foreach ($moderators as $k => $moderator) { $moderator_id = $moderator['id']; ModeratorModel::deleteModerator($moderator_id); } foreach ($moderator_ids as $user_id) { $moderator = new ModeratorModel(); $moderator->forumId = $board->forumId; $moderator->boardId = $board->id; $moderator->userId = $user_id; ModeratorModel::createModerator($moderator); } $db->commit(); $session->addFlash('success', '操作成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new JsonResponse(array()); } $moderator_ids = array(); foreach ($moderators as $k => $moderator) { $moderator_ids[] = $moderator['user_id']; } $moderator_ids = implode(',', $moderator_ids); // 采用GraphQL的形式进行查询 $user_api_url = $this->getContainer()->getParameter('user_url') . '/api/graphql'; return $this->render('moderator/index.html.twig', array('moderators' => $moderator_ids, 'board' => $board, 'api' => $user_api_url)); }
public function __invoke($board_id) { // 检查登录 $session = $this->getSession(); if (!isset($session['auth'])) { $current_url = $this->getCurrentUrl(); return new RedirectResponse($this->generateUrl('forum_login') . '?redirection=' . $current_url); } $board = BoardModel::getBoard($board_id); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $db = ForumDatabase::getDb(); $db->transaction(); try { // 检查 $title = $posts->get('title'); if (!$title) { throw new \Exception('请输入帖子标题'); } $content = $posts->get('content'); // 创建 $thread = new ThreadModel(); $thread->title = $title; $thread->boardId = $board->id; $thread->groupId = $board->groupId; $thread->forumId = $board->forumId; $thread->authorId = $session['auth']['id']; $thread->authorName = $session['profile']['nickname']; $thread->replyAuthorId = $session['auth']['id']; $thread->replyAuthorName = $session['profile']['nickname']; $now = time(); $thread->replyTimestamp = $now; $thread->createTimestamp = $now; $thread->updateTimestamp = $now; // 保存 $thread = ThreadModel::createThread($thread); // 创建 $post = new PostModel(); $post->threadId = $thread->id; $post->boardId = $board->id; $post->groupId = $board->groupId; $post->forumId = $board->forumId; $post->authorId = $session['auth']['id']; $post->authorName = $session['profile']['nickname']; $post->postId = 0; $post->content = $content; $post->createTimestamp = $now; $post->updateTimestamp = $now; // 保存 $post = PostModel::createPost($post); $board->threadCount += 1; BoardModel::saveBoard($board); // 提交事务 $db->commit(); $session->addFlash('success', '发布成功'); return new JsonResponse(array('status' => 1, 'data' => $thread->id)); } catch (\Exception $e) { $db->rollback(); return new JsonResponse(array('status' => 0, 'data' => $e->getMessage())); } } else { $forum = $this->getForum(); // 获得论坛的分组信息 $groups = GroupModel::allGroup(function (QueryBuilder $qb) use($forum) { $qb->andWhere($qb->expr()->eq('forum_id', ':forum_id'))->setParameter(':forum_id', $forum->id); $qb->addOrderBy('weight', 'desc'); $qb->addOrderBy('create_timestamp'); }); // 优先search $group = GroupModel::getGroup($board->groupId); // 导航 $breadcrumbs = array(); $breadcrumbs[] = array('url' => $this->generateUrl('forum_group', array('slug' => $group->search ? $group->search : $group->id)), 'title' => $group->name); if ($board->path) { $board_ids = explode(',', $board->path); foreach ($board_ids as $board_id) { $b = BoardModel::getBoard($board_id); $breadcrumbs[] = array('url' => $this->generateUrl('forum_board', array('group_slug' => $group->search ? $group->search : $group->id, 'board_slug' => $b->search ? $b->search : $b->id)), 'title' => $b->name); } } // 自己 $breadcrumbs[] = array('url' => $this->generateUrl('forum_board', array('group_slug' => $group->search ? $group->search : $group->id, 'board_slug' => $board->search ? $board->search : $board->id)), 'title' => $board->name); // 发表帖子 $breadcrumbs[] = array('title' => '发表帖子'); return $this->render('thread/post.html.twig', array('forum' => $forum, 'groups' => $groups, 'group' => $group, 'board' => $board, 'breadcrumbs' => $breadcrumbs)); } }