Phosphorum\Controllers\CategoriesController::viewAction PHP Метод

viewAction() публичный Метод

Shows latest posts by category
public viewAction ( integer $categoryId, string $slug, integer $offset )
$categoryId integer Category Id
$slug string Category Slug
$offset integer 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]);
    }