Пример #1
0
 public function getPostsWithVotes($postId = false)
 {
     $sql = 'SELECT p.*, pr.usersId as editorId,
         (SELECT SUM(v.positive) FROM  vote v  WHERE p.id = v.objectId AND v.object = ?) AS positive,
         (SELECT SUM(v.negative) FROM  vote v  WHERE p.id = v.objectId AND v.object = ?) AS negative
         FROM postsReply p
         LEFT JOIN postsReplyHistory pr ON p.id = pr.postsReplyId
         WHERE p.postsId= ? AND p.deleted = 0
         GROUP BY p.id
         ORDER BY p.id DESC';
     $postsReply = new PostsReply();
     $params = [Vote::OBJECT_POSTS_REPLY, Vote::OBJECT_POSTS_REPLY, $postId ? $postId : $this->getId()];
     $pdoResult = $postsReply->getReadConnection()->query($sql, $params);
     return (new Resultset(null, $postsReply, $pdoResult))->toArray();
 }
Пример #2
0
 public function indexAction($user)
 {
     d($user);
     if (!($user = Users::findFirstByUsername($user))) {
         $this->flashSession->error(t('The User dosen\'t exits'));
         return $this->indexRedirect();
     }
     $tab = $this->request->getQuery('tab', 'string');
     if ($tab == "answers") {
         $join = ['type' => 'join', 'model' => 'Phanbook\\Models\\PostsReply', 'on' => 'r.postsId = p.id', 'alias' => 'r'];
         list($itemBuilder, $totalBuilder) = $this->prepareQueries($join, false, self::POSTS_IN_PAGE);
         $itemBuilder->groupBy(array('p.id'));
     } else {
         list($itemBuilder, $totalBuilder) = $this->prepareQueries('', false, self::POSTS_IN_PAGE);
     }
     $params = [];
     switch ($tab) {
         case 'questions':
             $this->tag->setTitle('Questions');
             $questionConditions = 'p.type = "questions"';
             $itemBuilder->where($questionConditions);
             break;
         case 'tips':
             $this->tag->setTitle('Tips');
             $questionConditions = 'p.type = "tips"';
             $itemBuilder->where($questionConditions);
             break;
         case 'hackernews':
             $this->tag->setTitle('Hackernews');
             $questionConditions = 'p.type = "hackernews"';
             $itemBuilder->where($questionConditions);
             break;
         case 'answers':
             $this->tag->setTitle('My Answers');
             $answersConditions = 'r.usersId = ?0';
             $itemBuilder->where($answersConditions);
             //$totalBuilder->where($answersConditions);
             break;
         default:
             $this->tag->setTitle('All Questions and Tips');
             break;
     }
     $conditions = 'p.deleted = 0 and p.usersId = ?0';
     if ($tab == 'answers') {
         $conditions = 'p.deleted = 0';
     }
     $itemBuilder->andWhere($conditions);
     $totalBuilder->andWhere($conditions);
     $params = array($user->getId());
     //get all reply
     $parametersNumberReply = ['group' => 'postsId', 'usersId = ?0', 'bind' => [$user->getId()]];
     $paramTips = ['usersId = ?0 and type = "tips" and deleted = 0', 'bind' => [$user->getId()]];
     $paramQuestions = ['usersId = ?0 and type = "questions" and deleted = 0', 'bind' => [$user->getId()]];
     $paramHackernews = ['usersId = ?0 and type = "hackernews" and deleted = 0', 'bind' => [$user->getId()]];
     $this->view->setVars(['user' => $user, 'posts' => $itemBuilder->getQuery()->execute($params), 'totalTips' => Posts::count($paramTips), 'totalQuestions' => Posts::count($paramQuestions), 'totalHackernews' => Posts::count($paramHackernews), 'totalReply' => PostsReply::find($parametersNumberReply)->count(), 'currentOrder' => $tab]);
 }
Пример #3
0
 /**
  * Method for voting a task
  *
  * @return mixed
  */
 public function voteAction()
 {
     $this->view->disable();
     if (!$this->request->isPost()) {
         return $this->response->redirect($this->router->getControllerName());
     }
     $way = 'positive';
     if ($this->request->getPost('way') == 'negative') {
         $way = 'negative';
     }
     $objectId = $this->request->getPost('objectId');
     $object = $this->request->getPost('object');
     $vote = Vote::vote($objectId, $object, $way);
     $user = Users::findFirstById($this->auth->getAuth()['id']);
     $this->setJsonResponse();
     if (!$user) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'You need to login first'];
         return $this->jsonMessages;
     }
     if ($object == Vote::OBJECT_POSTS) {
         if (!($post = Posts::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post does not exist'];
             return $this->jsonMessages;
         }
         $this->setPointPost($way, $user, $post);
         //Adding notification when you have receive vote on the post, and not for now for post replies
         if ($user->getId() != $post->getUsersId()) {
             $this->setActivityNotifications($user, $post);
         }
     }
     if ($object == Vote::OBJECT_POSTS_REPLY) {
         if (!($postReply = PostsReply::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post reply does not exist'];
             return $this->jsonMessages;
         }
         //Set karam Voting someone else's post (positive or negative) on posts reply
         $this->setPointReply($way, $user, $postReply);
     }
     if (!$vote) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Vote have a problem :)'];
         return $this->jsonMessages;
     }
     if ($user->getVote() <= 0) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => t('You don\'t have enough votes available :)')];
         return $this->jsonMessages;
     }
     //checking the user have already voted this post yet
     if (is_array($vote)) {
         $this->jsonMessages['messages'][] = $vote;
         return $this->jsonMessages;
     }
     if ($this->request->isAjax()) {
         $vote = (new Vote())->getVotes($objectId, $object);
         return ['data' => $vote['positive'] - $vote['negative']];
     }
     echo 0;
     return 0;
 }
Пример #4
0
 public function editAnswerAction($id)
 {
     $auth = $this->auth->getAuth();
     $postReply = PostsReply::findFirstById($id);
     if (!$auth) {
         $this->flashSession->error(t('You must be logged in first to post answer'));
         return $this->currentRedirect();
     }
     if (!$this->auth->isTrustModeration() && $auth['id'] != $postReply->getUsersId()) {
         $this->flashSession->error(t('You don\'t have permission'));
         return $this->currentRedirect();
     }
     if (!$postReply) {
         $this->flashSession->error(t('The posts replies not exist!'));
         return $this->currentRedirect();
     }
     if ($this->request->isPost()) {
         //save history  postreplies table, it just for admin or moderator
         if ($this->auth->isTrustModeration() && $auth['id'] != $postReply->getUsersId()) {
             $postReplyHistory = new PostsReplyHistory();
             $postReplyHistory->setContent($this->request->getPost('content'));
             $postReplyHistory->setPostsReplyId($id);
             $postReplyHistory->setUsersId($auth['id']);
             if (!$postReplyHistory->save()) {
                 $this->saveLoger($postReplyHistory->getMessages());
             }
         }
         //Update replies post
         $postReply->setContent($this->request->getPost('content'));
         if (!$postReply->save()) {
             $this->saveLoger($postReply->getMessages());
         }
         $this->flashSession->success(t('Data was successfully saved'));
         return $this->response->redirect('/posts/' . $postReply->getPostsId() . '/editpost');
     }
     $this->view->hotPosts = Posts::getHotPosts();
     $this->view->form = new ReplyForm($postReply);
     $this->tag->setTitle(t('Edit answer'));
     return $this->view->pickCustom('replies/editAnswer');
 }
Пример #5
0
 public function detailAction($user)
 {
     if (!($user = Users::findFirstByUsername($user))) {
         $this->flashSession->error(t('The User dosen\'t exits'));
         return $this->indexRedirect();
     }
     $tab = $this->request->getQuery('tab');
     $page = isset($_GET['page']) ? (int) $_GET['page'] : $this->numberPage;
     $perPage = isset($_GET['perPage']) ? (int) $_GET['perPage'] : $this->perPage;
     $where = '';
     if ($tab == "answers") {
         $join = ['type' => 'join', 'model' => 'PostsReply', 'on' => 'r.postsId = p.id', 'alias' => 'r'];
         list($itemBuilder, $totalBuilder) = ModelBase::prepareQueriesPosts($join, $where, $this->perPage);
         $itemBuilder->groupBy(array('p.id'));
     } else {
         list($itemBuilder, $totalBuilder) = ModelBase::prepareQueriesPosts('', $where, $this->perPage);
     }
     $params = [];
     switch ($tab) {
         case 'questions':
             $this->tag->setTitle('Questions');
             $questionConditions = 'p.type = "questions"';
             $itemBuilder->where($questionConditions);
             break;
         case 'answers':
             $this->tag->setTitle('My Answers');
             $answersConditions = 'r.usersId = ?0';
             $itemBuilder->where($answersConditions);
             //$totalBuilder->where($answersConditions);
             break;
         default:
             $this->tag->setTitle('All Questions');
             break;
     }
     $conditions = 'p.deleted = 0 and p.usersId = ?0';
     if ($tab == 'answers') {
         $conditions = 'p.deleted = 0';
     }
     $itemBuilder->andWhere($conditions);
     $totalBuilder->andWhere($conditions);
     $params = array($user->getId());
     //get all reply
     $parametersNumberReply = ['group' => 'postsId', 'usersId = ?0', 'bind' => [$user->getId()]];
     $paramQuestions = ['usersId = ?0 and type = "questions" and deleted = 0', 'bind' => [$user->getId()]];
     $totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute($params);
     $totalPages = ceil($totalPosts->count / $perPage);
     $offset = ($page - 1) * $perPage + 1;
     if ($page > 1) {
         $itemBuilder->offset($offset);
     }
     $this->view->setVars(['user' => $user, 'posts' => $itemBuilder->getQuery()->execute($params), 'totalQuestions' => Posts::count($paramQuestions), 'totalReply' => PostsReply::find($parametersNumberReply)->count(), 'tab' => $tab, 'totalPages' => $totalPages, 'currentPage' => $page]);
 }
Пример #6
0
 public static function totalReply()
 {
     return PostsReply::count();
 }