Example #1
0
 protected function handle()
 {
     $request = $this->getRequest();
     $parent_node_id = $request->query->get('parent_form_id');
     $parent_forum = ForumModel::getForum($parent_node_id);
     if (!$parent_forum) {
         throw new \Exception("论坛分组或者版块不存在");
     }
     if ($request->getMethod() == 'POST') {
         try {
             // 检查参数
             $forum_name = $request->request->get('name');
             if (!$forum_name) {
                 throw new \Exception('论坛分组名称不能为空');
             }
             $forum_status = $request->request->get('forum_status');
             $forum_status = intval($forum_status);
             $now = time();
             $parent_forum->createChildNode(array('name' => $forum_name, 'forum_type' => 'forum', 'forum_status' => $forum_status, 'create_timestamp' => $now, 'update_timestamp' => $now));
         } catch (\Exception $e) {
             $session = $this->getSession();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_forum_group_manage'));
     }
     return $this->render('forum/forum-add.html.twig');
 }
Example #2
0
 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));
 }
Example #3
0
 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));
 }
Example #4
0
 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));
 }
Example #5
0
 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);
     }
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $forums_data = $request->request->get('forums');
         if (!$forums_data) {
             $forums_data = array();
         }
         $db = ForumModel::getDb();
         try {
             $db->transaction();
             foreach ($forums_data as $forum_id => $info) {
                 $sort_number = $info['sort'];
                 $forum_name = $info['name'];
                 $forum = ForumModel::getForum($forum_id);
                 if ($forum) {
                     $forum->name = $forum_name;
                     $forum->displayOrder = $sort_number;
                     ForumModel::saveForum($forum);
                 }
             }
             $db->commit();
         } catch (\Exception $e) {
             $db->rollback();
             throw $e;
         }
     }
     /** @var ForumModel[] $sub_tree */
     $sub_tree = $root_node->getSubTree();
     $list = array();
     foreach ($sub_tree as $i => $node) {
         if ($i == 0) {
             continue;
         }
         $list[$node->forumId] = $node;
     }
     ForumBusiness::preOrderTree2RecursiveTree($list, $root_node);
     return $this->render('forum/group-manage.html.twig', array('root_node' => $root_node));
 }
Example #6
0
 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));
 }
Example #7
0
 public function __invoke()
 {
     // 论坛首页
     /** @var ForumModel $root_node */
     $root_node = ForumModel::getRootNode();
     /** @var ForumModel[] $sub_tree */
     $sub_tree = $root_node->getSubTree();
     $list = array();
     foreach ($sub_tree as $i => $node) {
         if ($i == 0) {
             continue;
         }
         $list[$node->forumId] = $node;
     }
     ForumBusiness::preOrderTree2RecursiveTree($list, $root_node);
     return $this->render('forum/homepage.html.twig', array('root_node' => $root_node));
 }
Example #8
0
 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));
 }
Example #9
0
 protected function handle()
 {
     $request = $this->getRequest();
     $forum_id = $request->query->get('forum_id');
     $forum = ForumModel::getForum($forum_id);
     if (!$forum) {
         throw new \Exception('论坛不存在');
     }
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         try {
             $forum->remove();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_forum_group_manage'));
     }
     return $this->render('forum/forum-delete.html.twig', array('forum' => $forum));
 }
Example #10
0
 protected function handle()
 {
     $request = $this->getRequest();
     $forum_id = $request->query->get('forum_id');
     $forum = ForumModel::getForum($forum_id);
     // 检查论坛是否存在
     try {
         if (!$forum) {
             throw new \Exception('论坛不存在');
         }
     } catch (\Exception $e) {
         $session = $this->getSession();
         $session->addFlash('error', $e->getMessage());
         return new RedirectResponse($this->generateUrl('admin_forum_gruop_manage'));
     }
     if ($request->getMethod() == 'POST') {
         // 修改数据
         $session = $this->getSession();
         try {
             $forum_name = $request->request->get('name');
             $forum_description = $request->request->get('description');
             $forum_icon = $request->request->get('icon');
             if (!$forum_name) {
                 throw new \Exception('版块名称不能为空');
             }
             $forum->name = $forum_name;
             $forum->description = $forum_description;
             $forum->icon = $forum_icon;
             ForumModel::saveForum($forum);
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_forum_forum_edit', array('forum_id' => $forum_id)));
     }
     return $this->render('forum/forum-edit.html.twig', array('forum' => $forum));
 }
Example #11
0
 /**
  * 保存
  * @param ForumModel $forum
  * @return ForumModel
  * @throws \Exception
  */
 public static function saveForum(ForumModel $forum)
 {
     return self::editForum($forum->toArray());
 }