/** * Shows latest posts by category * * @param int $categoryId Category Id * @param string $slug Category Slug * @param int $offset Posts offset */ public function viewAction($categoryId, $slug, $offset = 0) { if (!($category = Categories::findFirstById($categoryId))) { $this->flashSession->notice("The category doesn't exist"); $this->logger->error("The category doesn't exist"); $this->response->redirect(); return; } $this->tag->setTitle("Discussions in category {$category->name}"); $readposts = []; if ($userId = $this->session->get('identity')) { $ur = TopicTracking::findFirst(['user_id= ?0', 'bind' => [$userId]]); $readposts = $ur ? explode(',', $ur->topic_id) : []; } /** * @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder * @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder */ list($itemBuilder, $totalBuilder) = $this->prepareQueries(); $totalBuilder->where('p.categories_id = ?0 AND p.deleted = 0'); $posts = $itemBuilder->where('p.categories_id = ?0 AND p.deleted = 0')->orderBy('p.created_at DESC')->offset((int) $offset)->getQuery()->execute([$categoryId]); if (!count($posts)) { $this->flashSession->notice('There are no posts in category: ' . $category->name); $this->response->redirect(); return; } $totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute([$categoryId]); $this->view->setVars(['readposts' => $readposts, 'posts' => $posts, 'totalPosts' => $totalPosts, 'currentOrder' => null, 'offset' => (int) $offset, 'paginatorUri' => "category/{$category->id}/{$category->slug}", 'logged' => $userId]); }
/** * Shows latest posts using an order clause * * @param string $order * @param int $offset */ public function indexAction($order = null, $offset = 0) { /** * @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder * @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder */ list($itemBuilder, $totalBuilder) = $this->prepareQueries($order == "answers"); /** * Create the conditions according to the parameter order */ $userId = $this->session->get('identity'); $readposts = []; if ($userId != '') { $ur = TopicTracking::findFirst("user_id='" . $userId . "'"); if ($ur !== false) { $readposts = explode(",", $ur->topic_id); } } $params = null; switch ($order) { case 'hot': $this->tag->setTitle('Hot Discussions'); $itemBuilder->orderBy('p.modified_at DESC'); break; case 'my': $this->tag->setTitle('My Discussions'); if ($userId) { $params = [$userId]; $myConditions = 'p.users_id = ?0'; $itemBuilder->where($myConditions); $totalBuilder->where($myConditions); } break; case 'unanswered': $this->tag->setTitle('Unanswered Discussions'); $unansweredConditions = 'p.number_replies = 0 AND p.accepted_answer <> "Y"'; $itemBuilder->where($unansweredConditions); $totalBuilder->where($unansweredConditions); break; case 'answers': $this->tag->setTitle('My Answers'); if ($userId) { $params = [$userId]; $answersConditions = 'r.users_id = ?0'; $itemBuilder->where($answersConditions); $totalBuilder->where($answersConditions); } break; default: $this->tag->setTitle('Discussions'); } $notDeleteConditions = 'p.deleted = 0'; $itemBuilder->andWhere($notDeleteConditions); $totalBuilder->andWhere($notDeleteConditions); if ($offset > 0) { $itemBuilder->offset((int) $offset); } $order = $order ?: 'new'; $this->view->setVars(['logged' => $userId, 'readposts' => $readposts, 'posts' => $itemBuilder->getQuery()->execute($params), 'totalPosts' => $totalBuilder->getQuery()->setUniqueRow(true)->execute($params), 'currentOrder' => $order, 'offset' => $offset, 'paginatorUri' => "discussions/{$order}", 'canonical' => '']); }
/** * Shows latest posts by category */ public function categoryAction($categoryId, $slug, $offset = 0) { $this->tag->setTitle('Discussions'); $userId = $this->session->get('identity'); if ($userId != '') { $ur = TopicTracking::findFirst("user_id='" . $userId . "'"); $this->view->readposts = explode(",", $ur->topic_id); } $category = Categories::findFirstById($categoryId); if (!$category) { $this->flashSession->notice('The category doesn\'t exist'); return $this->response->redirect(); } /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder */ /** @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder */ list($itemBuilder, $totalBuilder) = $this->prepareQueries(); $totalBuilder->where('p.categories_id = ?0 AND p.deleted = 0'); $posts = $itemBuilder->where('p.categories_id = ?0 AND p.deleted = 0')->orderBy('p.created_at DESC')->offset((int) $offset)->getQuery()->execute(array($categoryId)); if (!count($posts)) { $this->flashSession->notice('There are no posts in category: ' . $category->name); return $this->response->redirect(); } $totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute(array($categoryId)); $this->view->posts = $posts; $this->view->totalPosts = $totalPosts; $this->view->currentOrder = null; $this->view->offset = (int) $offset; $this->view->paginatorUri = 'category/' . $category->id . '/' . $category->slug; $this->view->logged = $this->session->get('identity'); }