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]);
 }
 /**
  * Retrieve a list of Posts for a specific tags id.
  *
  * @param int    $id       The Tags ID
  * @param string $slugTags
  *
  * @return array list of posts
  */
 public function postByTagAction($id, $slugName)
 {
     $join = ['type' => 'join', 'model' => 'PostsTags', 'on' => 'pt.postsId = p.id', 'alias' => 'pt'];
     /**@Todo later for security*/
     $where = 'p.deleted = 0 AND pt.tagsId = ' . $id;
     list($itemBuilder, $totalBuilder) = ModelBase::prepareQueriesPosts($join, $where, $this->perPage);
     //$itemBuilder->andWhere($conditions);
     $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
     $totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute();
     $totalPages = ceil($totalPosts->count / $this->perPage);
     $offset = ($page - 1) * $this->perPage + 1;
     if ($page > 1) {
         $itemBuilder->offset((int) $offset);
     }
     //@todo refacttor
     $this->view->setVars(['tab' => 'tags', 'type' => Posts::POST_ALL, 'posts' => $itemBuilder->getQuery()->execute(), 'totalPages' => $totalPages, 'currentPage' => $page, 'slugName' => $slugName, 'tags' => Tags::find()]);
     $this->tag->setTitle(t('These posts fillter by tags'));
     return $this->view->pick('post');
 }
Exemple #3
0
 /**
  * Default it will get all posts
  *
  */
 public function indexAction()
 {
     /* @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder */
     /* @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder */
     $tab = $this->request->getQuery('tab');
     $page = isset($_GET['page']) ? (int) $_GET['page'] : $this->numberPage;
     $perPage = isset($_GET['perPage']) ? (int) $_GET['perPage'] : $this->perPage;
     if ($tab == "answers") {
         $join = ['type' => 'join', 'model' => 'Phanbook\\Models\\PostsReply', 'on' => 'r.postsId = p.id', 'alias' => 'r'];
         list($itemBuilder, $totalBuilder) = ModelBase::prepareQueriesPosts($join, false, $perPage);
         $itemBuilder->groupBy(array('p.id'));
     } else {
         list($itemBuilder, $totalBuilder) = ModelBase::prepareQueriesPosts('', false, $perPage);
     }
     $userId = $this->auth->getAuth();
     /*
      * Create the conditions according to the parameter order
      */
     $params = null;
     switch ($tab) {
         case 'hot':
             $this->tag->setTitle('Hot Questions');
             $itemBuilder->orderBy('p.modifiedAt DESC');
             break;
         case 'unanswered':
             $this->tag->setTitle('Unanswered Questions');
             $unansweredConditions = 'p.numberReply = 0 AND p.acceptedAnswer <> "Y"';
             $itemBuilder->where($unansweredConditions);
             $totalBuilder->where($unansweredConditions);
             break;
         case 'week':
             $this->tag->setTitle('Hot Questions This Week');
             $lastWeek = new \DateTime();
             $lastWeek->modify('-1 week');
             $params = array($lastWeek->getTimestamp());
             $weekConditions = 'p.createdAt >= ?0';
             $itemBuilder->where($weekConditions);
             $totalBuilder->where($weekConditions);
             break;
         case 'month':
             $this->tag->setTitle('Hot Questions This Month');
             $lastMonths = new \DateTime();
             $lastMonths->modify('-6 month');
             $params = array($lastMonths->getTimestamp());
             $monthConditions = 'p.createdAt >= ?0';
             $itemBuilder->where($monthConditions);
             $totalBuilder->where($monthConditions);
             break;
         case 'questions':
             $this->tag->setTitle('Questions');
             $questionConditions = 'p.type = "questions"';
             $itemBuilder->where($questionConditions);
             $totalBuilder->where($questionConditions);
             break;
         case 'blog':
             $this->tag->setTitle('Blogs');
             $blogConditions = 'p.type = "blog"';
             $itemBuilder->where($blogConditions);
             $totalBuilder->where($blogConditions);
             break;
         case 'hackernews':
             $this->tag->setTitle('Hacker News');
             $tipConditions = 'p.type = "hackernews"';
             $itemBuilder->where($tipConditions);
             $totalBuilder->where($tipConditions);
             break;
         default:
             $this->tag->setTitle($this->config->application->tagline);
     }
     $type = Posts::POST_PAGE;
     $conditions = "p.deleted = 0 AND p.type != '{$type}'";
     $itemBuilder->andWhere($conditions);
     $totalBuilder->andWhere($conditions);
     //order like tabs sort
     if (!$tab) {
         $tab = 'hot';
     }
     $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(['tab' => $tab, 'type' => Posts::POST_ALL, 'posts' => $itemBuilder->getQuery()->execute($params), 'totalPages' => $totalPages, 'currentPage' => $page, 'tags' => Tags::find()]);
     return $this->view->pick('post');
 }