protected function handle() { $thread_id = $this->threadId; $thread = ThreadModel::getThread($thread_id); if (!$thread) { throw new \Exception('帖子不存在'); } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $session = $this->getSession(); $db = ForumDatabase::getInstance(); $db->transaction(); try { // 删除帖子 ThreadModel::removeThread($thread); // 删除帖子的回复 PostModel::deletePosts(function (QueryBuilder $qb) use($thread_id) { $qb->andWhere($qb->expr()->eq('thread_id', ':thread_id'))->setParameter(':thread_id', $thread_id); }); $db->commit(); $session->addFlash('success', '删除成功'); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('admin_forum_thread_manage')); } return $this->render('thread/thread-delete.html.twig', array('thread' => $thread)); }
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($forum_id) { $session = $this->getSession(); $forum = ForumModel::getForum($forum_id); if (!$forum) { $session->addFlash('error', '论坛版块不存在'); return new RedirectResponse($this->generateUrl('forum_homepage')); } /** @var ForumModel[] $sub_tree */ $sub_tree = $forum->getSubTree(); $list = array(); foreach ($sub_tree as $i => $node) { if ($i == 0) { continue; } $list[$node->forumId] = $node; } ForumBusiness::preOrderTree2RecursiveTree($list, $forum); $breadcrumbs = $forum->getNodeSinglePath(); unset($breadcrumbs[0], $breadcrumbs[1]); $breadcrumbs = array_values($breadcrumbs); $request = $this->getRequest(); $page = $request->query->get('page'); if (!$page) { $page = 1; } $size = 20; $threads_pager = ThreadModel::listForumThreads($page, $size, function (QueryBuilder $qb) use($forum_id) { $qb->andWhere($qb->expr()->eq('forum_id', ':forum_id'))->setParameter(':forum_id', $forum_id); }); return $this->render('forum/board.html.twig', array('forum' => $forum, 'threads_pager' => $threads_pager, 'breadcrumbs' => $breadcrumbs)); }
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() { // 检查登录 $session = $this->getSession(); if (empty($session['user'])) { $session->addFlash('error', '请先登录'); return new RedirectResponse($this->generateUrl('user_login')); } $user = UserModel::getUser($session['user']['id']); $request = $this->getRequest(); $type = $request->query->get('type'); if (!$type) { $type = 'post'; } $page = $request->query->get('page'); if (!$page) { $page = 1; } $size = 10; $pager = null; if ($type == 'post') { $pager = ThreadModel::listForumThreads($page, $size, function (QueryBuilder $qb) use($user) { /** @var UserModel $user */ $qb->andWhere($qb->expr()->eq('author_id', ':author_id'))->setParameter(':author_id', $user->id); }); $data = $pager->getData(); foreach ($data as $key => $value) { $forum_id = $value['forum_id']; $forum = ForumModel::getForum($forum_id); $value['forum'] = $forum; $data[$key] = $value; } $pager->setData($data); } elseif ($type == 'reply') { $data = PostModel::getPostsWhere(function (QueryBuilder $qb) use($user) { /** @var UserModel $user */ $qb->andWhere($qb->expr()->eq('author_id', ':author_id'))->setParameter(':author_id', $user->id); }); $thread_ids = array(); foreach ($data as $key => $value) { $thread_ids[] = $value['thread_id']; } $thread_ids = array_unique($thread_ids); $pager = ThreadModel::listForumThreads($page, $size, function (QueryBuilder $qb) use($thread_ids) { /** @var UserModel $user */ $qb->andWhere($qb->expr()->in('thread_id', $thread_ids)); }); $data = $pager->getData(); foreach ($data as $key => $value) { $forum_id = $value['forum_id']; $forum = ForumModel::getForum($forum_id); $value['forum'] = $forum; $data[$key] = $value; } $pager->setData($data); } $pager->setQuery(array('type' => $type)); return $this->render('thread/my.html.twig', array('pager' => $pager)); }
public function __invoke($forum_id) { // 检查登录 $session = $this->getSession(); if (empty($session['user'])) { $session->addFlash('error', '请先登录'); return new RedirectResponse($this->generateUrl('user_login')); } $user = UserModel::getUser($session['user']['id']); // 检查版块是否存在 $forum = ForumModel::getForum($forum_id); if (!$forum) { $session->addFlash('error', '论坛版块不存在'); return new RedirectResponse($this->generateUrl('forum_homepage')); } $request = $this->getRequest(); if ($request->getMethod() == 'POST') { // 处理发布数据 $posts = $request->request; $db = ForumDatabase::getInstance(); try { $db->transaction(); $subject = $posts->get('subject'); $content = $posts->get('content'); $pictures = $posts->get('thumbnails'); if (!$pictures) { $pictures = array(); } else { $pictures = explode('<>', $pictures); } if (empty($subject)) { throw new \Exception('帖子标题不能为空'); } $summary = strip_tags($content); // 创建主题 $thread = new ThreadModel(); $thread->subject = $subject; $thread->summary = $summary; $thread->thumbnails = implode('<>', $pictures); $thread->content = $content; $thread->forumId = $forum_id; $thread->authorId = $user->id; $thread->authorName = $user->username; $now = time(); $thread->createTimestamp = $now; $thread->lastPostTimestamp = $now; $thread->lastPosterName = $user->username; // 保存 $thread = ThreadModel::createThread($thread); $db->commit(); return new RedirectResponse($this->generateUrl('forum_thread_view', array('thread_id' => $thread->threadId))); } catch (\Exception $e) { $db->rollback(); $session->addFlash('error', $e->getMessage()); return new RedirectResponse($this->generateUrl('forum_thread_post', array('forum_id' => $forum_id))); } } return $this->render('thread/post.html.twig', array('forum' => $forum)); }
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(); if ($request->getMethod() == 'POST') { $posts = $request->request; $subject = $posts->get('subject'); $content = $posts->get('content'); $thumbnails = $posts->get('thumbnails'); if (empty($thumbnails)) { $thumbnails = array(); } $reset_thumbnails = $posts->get('reset_thumbnails'); $highlight = $posts->get('highlight'); $digest = $posts->get('digest'); $cdn_url = $this->getContainer()->getParameter('cdn_url'); $cdn_url = str_replace('/', '\\/', $cdn_url); try { if (empty($subject)) { throw new \Exception('标题不能为空'); } $thread->subject = $subject; $new_thumbnails = array(); $pattern = "/<\\s*img\\s+[^>]*?src\\s*=\\s*(\\'|\")({$cdn_url}.*?)\\1[^>]*?\\/?\\s*>/i"; preg_match_all($pattern, $content, $new_thumbnails); $new_thumbnails = $new_thumbnails[0]; foreach ($new_thumbnails as $key => $thumbnail) { $matches = array(); preg_match('/src=\\"(.*?)\\"/', $thumbnail, $matches); $new_thumbnails[$key] = $matches[1]; } if ($reset_thumbnails) { $thumbnails = $new_thumbnails; } $thread->content = $content; $thread->thumbnails = implode('<>', $thumbnails); $thread->highlight = $highlight; $thread->digest = $digest; ThreadModel::saveThread($thread); $session->addFlash('success', '操作成功'); return new RedirectResponse($this->generateUrl('admin_forum_thread_edit', array('thread_id' => $thread_id))); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); return new RedirectResponse($this->generateUrl('admin_forum_thread_edit', array('thread_id' => $thread_id))); } } return $this->render('thread/thread-edit.html.twig', array('thread' => $thread)); }
protected function handle() { /** @var ForumModel $root_node */ $root_node = ForumModel::getRootNode(); if (!$root_node) { // 没有根分类,需要自动创建 $node = new ForumModel(); $node->name = '论坛首页'; $node->description = '论坛首页'; $node->forumStatus = 1; $node->forumType = 'group'; $now = time(); $node->createTimestamp = $now; $node->updateTimestamp = $now; $node->preOrderTreeLeft = 0; $node->preOrderTreeRight = 1; $root_node = ForumModel::createForumGroup($node); } /** @var ForumModel[] $root_tree */ $root_tree = $root_node->getSubTree(); $request = $this->getRequest(); $page = $request->query->get('page'); if (!$page) { $page = 1; } $size = 15; $forum_id = $request->query->get('forum_id'); if (!$forum_id) { $forum_id = $root_node->forumId; } $forum = ForumModel::getForum($forum_id); /** @var ForumModel[] $sub_tree */ $sub_tree = $forum->getSubTree(); $forum_ids = array(); foreach ($sub_tree as $item) { $forum_ids[] = $item->forumId; } $threads_pager = ThreadModel::listForumThreads($page, $size, function (QueryBuilder $qb) use($forum_ids) { $qb->andWhere($qb->expr()->in('forum_id', $forum_ids)); $qb->addOrderBy('create_timestamp', 'DESC'); }); $threads_pager->setQuery(array('forum_id' => $forum_id)); $data = $threads_pager->getData(); foreach ($data as $key => $item) { $id = $item['forum_id']; $forum = ForumModel::getForum($id); $data[$key]['forum'] = $forum->toArray(); } $threads_pager->setData($data); return $this->render('thread/thread-list.html.twig', array('tree' => $root_tree, 'threads_pager' => $threads_pager, 'forum_id' => $forum_id)); }
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)); }
/** * 保存 * @param ThreadModel $thread * @return ThreadModel * @throws \Exception */ public static function saveThread(ThreadModel $thread) { return self::editThread($thread->toArray()); }