コード例 #1
0
ファイル: ActionDelete.php プロジェクト: aeshion/ZeroPHP
 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));
 }
コード例 #2
0
ファイル: ActionReply.php プロジェクト: aeshion/ZeroPHP
 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));
 }
コード例 #3
0
ファイル: ActionPost.php プロジェクト: aeshion/ZeroPHP
 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));
 }