protected function handle() { $session = $this->getSession(); $thread_id = $this->threadId; $thread = ThreadModel::getThread($thread_id); if (!$thread) { $session->addFlash('error', '帖子不存在'); return new RedirectResponse($this->generateUrl('admin_forum_thread_manage')); } $request = $this->getRequest(); $page = $request->query->get('page'); if (!$page) { $page = 1; } $size = 10; $posts_pager = PostModel::listForumPosts($page, $size, function (QueryBuilder $qb) use($thread) { /** @var ThreadModel $thread */ $qb->andWhere($qb->expr()->eq('forum_id', ':forum_id'))->setParameter(':forum_id', $thread->forumId); $qb->andWhere($qb->expr()->eq('thread_id', ':thread_id'))->setParameter(':thread_id', $thread->threadId); $qb->addOrderBy('create_timestamp'); }); $data = $posts_pager->getData(); foreach ($data as $key => $post) { if ($post['reply_post_id']) { $reply_post = PostModel::getPost($post['reply_post_id']); if ($reply_post) { $data[$key]['reply_post'] = $reply_post->toArray(); } } } $posts_pager->setData($data); return $this->render('thread/thread-view.html.twig', array('thread' => $thread, 'posts_pager' => $posts_pager)); }
public function __invoke($post_id) { // 检查登录 $session = $this->getSession(); if (empty($session['user'])) { $session->addFlash('error', '请先登录'); return new RedirectResponse($this->generateUrl('user_login')); } $user = UserModel::getUser($session['user']['id']); // 检查帖子是否存在 $post = PostModel::getPost($post_id); if (!$post) { $session->addFlash('error', '帖子不存在'); return new RedirectResponse($this->getRequest()->server->get('HTTP_REFERER')); } // 检查主题 $thread = ThreadModel::getThread($post->threadId); if (!$thread) { $session->addFlash('error', '主题不存在'); return new RedirectResponse($this->generateUrl('forum_board', array('forum_id' => $post->forumId))); } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $db = ForumDatabase::getInstance(); $db->transaction(); try { $content = $posts->get('content'); $pictures = $posts->get('thumbnails'); if (!$pictures) { $pictures = array(); } else { $pictures = explode('<>', $pictures); } if (empty($content) && empty($pictures)) { throw new \Exception("内容和图片不能都为空"); } $reply = new PostModel(); $reply->threadId = $post->threadId; $reply->forumId = $post->forumId; $reply->replyPostId = $post->postId; $reply->content = $content; $reply->thumbnails = implode('<>', $pictures); $reply->createTimestamp = time(); $reply->authorId = $user->id; $reply->authorName = $user->username; // 保存 PostModel::createPost($reply); // 增加主题回复数 $thread->replies += 1; ThreadModel::saveThread($thread); $db->commit(); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('forum_thread_view', array('thread_id' => $post->threadId))); } return $this->render('post/reply.html.twig', array('post' => $post)); }
public function __invoke($thread_id) { // 主题 $thread = ThreadModel::getThread($thread_id); if (!$thread) { $session = $this->getSession(); $session->addFlash('error', '帖子主题不存在'); return new RedirectResponse($this->getRequest()->server->get('HTTP_REFERER')); } $forum = ForumModel::getForum($thread->forumId); if (!$forum) { $session = $this->getSession(); $session->addFlash('error', '论坛版块不存在'); return new RedirectResponse($this->getRequest()->server->get('HTTP_REFERER')); } // 增加浏览数 $thread->views += 1; $thread = ThreadModel::saveThread($thread); $request = $this->getRequest(); $page = $request->query->get('page'); if (!$page) { $page = 1; } $size = 10; $posts_pager = PostModel::listForumPosts($page, $size, function (QueryBuilder $qb) use($thread) { /** @var ThreadModel $thread */ $qb->andWhere($qb->expr()->eq('forum_id', ':forum_id'))->setParameter(':forum_id', $thread->forumId); $qb->andWhere($qb->expr()->eq('thread_id', ':thread_id'))->setParameter(':thread_id', $thread->threadId); $qb->addOrderBy('create_timestamp'); }); $data = $posts_pager->getData(); foreach ($data as $key => $post) { if ($post['reply_post_id']) { $reply_post = PostModel::getPost($post['reply_post_id']); if ($reply_post) { $data[$key]['reply_post'] = $reply_post->toArray(); } } } $posts_pager->setData($data); return $this->render('thread/view.html.twig', array('forum' => $forum, 'thread' => $thread, 'posts_pager' => $posts_pager)); }