public function __invoke($group_slug = null, $board_slug = null, $thread_id = null) { // 检查登录 $session = $this->getSession(); if (!isset($session['auth'])) { $current_url = $this->getCurrentUrl(); return new RedirectResponse($this->generateUrl('forum_login') . '?redirection=' . $current_url); } $thread = ThreadModel::getThread($thread_id); $post = PostModel::getPostByThreadId($thread_id); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $session = $this->getSession(); $db = ForumDatabase::getDb(); $db->transaction(); try { $comment = $posts->get('comment'); if (!$comment) { throw new \Exception('回复内容不能为空'); } // 创建 $reply = new PostModel(); $reply->content = $comment; $now = time(); $reply->createTimestamp = $now; $reply->updateTimestamp = $now; $reply->forumId = $thread->forumId; $reply->groupId = $thread->groupId; $reply->boardId = $thread->boardId; $reply->threadId = $thread->id; $reply->postId = $post->id; $reply->authorId = $session['auth']['id']; $reply->authorName = $session['profile']['nickname']; // 保存 PostModel::createPost($reply); // 更新 $post->commentCount += 1; PostModel::savePost($post); // 主题 $thread->replyAuthorId = $session['auth']['id']; $thread->replyAuthorName = $session['profile']['nickname']; $thread->replyCount += 1; $thread->replyTimestamp = $now; ThreadModel::saveThread($thread); $db->commit(); $session->addFlash('success', '回复成功'); return new JsonResponse(array('status' => 1, 'data' => '回复成功')); } catch (\Exception $e) { $db->rollback(); return new JsonResponse(array('status' => 0, 'data' => $e->getMessage())); } } else { $forum = $this->getForum(); // 优先search $group = GroupModel::search($group_slug, $forum->id); if (!$group) { $group = GroupModel::getGroup($group_slug); } $board = BoardModel::search($board_slug, $forum->id, $group->id); if (!$board) { $board = BoardModel::getBoard($board_slug); } $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' => '回复RE: ' . $thread->title); // 获得论坛的分组信息 $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'); }); $post = PostModel::getPostByThreadId($thread->id); return $this->render('thread/reply.html.twig', array('forum' => $forum, 'groups' => $groups, 'group' => $group, 'board' => $board, 'thread' => $thread, 'post' => $post, 'breadcrumbs' => $breadcrumbs)); } }