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]);
 }
 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]);
 }