Exemplo n.º 1
0
 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));
 }
Exemplo n.º 2
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));
 }
Exemplo n.º 3
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));
 }
Exemplo n.º 4
0
 protected function handle()
 {
     $request = $this->getRequest();
     $user_id = $request->query->get('user_id');
     if (empty($user_id)) {
         throw new \Exception('参数错误');
     }
     $user = UserModel::getUser($user_id);
     if (!$user) {
         throw new \Exception('用户不存在');
     }
     if ($request->getMethod() == 'POST') {
         $session = $this->getSession();
         $posts = $request->request;
         try {
             $password = $posts->get('password');
             $repeat_password = $posts->get('repeat_password');
             $nickname = $posts->get('nickname');
             if (empty($nickname) || strlen($nickname) < 2) {
                 throw new \Exception("昵称不能为空,必需至少2个字符");
             }
             if (!empty($password) && strlen($password) < 6) {
                 throw new \Exception("登录密码必需至少6个字符");
             }
             if (!empty($repeat_password) && strlen($repeat_password) < 6) {
                 throw new \Exception("重复密码必需至少6个字符");
             }
             if (!empty($password) && !empty($repeat_password) && $password != $repeat_password) {
                 throw new \Exception("重复密码与登录密码不一致");
             }
             $email = $posts->get('email');
             $mobile = $posts->get('mobile');
             $user->nickname = $nickname;
             if (!empty($password)) {
                 $user->password = $password;
             }
             $user->email = $email;
             $user->mobile = $mobile;
             $user->updateTimestamp = time();
             UserModel::saveUser($user);
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_forum_user_list'));
     }
     return $this->render('user/user-edit.html.twig', array('user' => $user));
 }
Exemplo n.º 5
0
 protected function handle()
 {
     $request = $this->getRequest();
     if ($request->getMethod() == 'POST') {
         $ids = $request->request->get('ids');
         $db = UserModel::getDb();
         $session = $this->getSession();
         try {
             $db->transaction();
             foreach ($ids as $id) {
                 $user = UserModel::getUser($id);
                 if ($user) {
                     UserModel::removeUser($user);
                 }
             }
             $db->commit();
             $session->addFlash('success', '操作成功');
         } catch (\Exception $e) {
             $db->rollback();
             $session->addFlash('error', $e->getMessage());
         }
         return new RedirectResponse($this->generateUrl('admin_forum_user_list'));
     } else {
         $ids = $request->query->get('ids');
         $ids = json_decode($ids);
         $users = array();
         foreach ($ids as $user_id) {
             $user = UserModel::getUser($user_id);
             if (!$user) {
                 throw new \Exception("用户'{$user_id}'不存在");
             } else {
                 $users[] = $user;
             }
         }
         return $this->render('user/user-delete.html.twig', array('users' => $users));
     }
 }