<?PHP
class BlogView extends View
{
    //лимит кол-ва постов на странице
    public $posts_per_page = 3;

    public function fetch()
    {
        // Если указан урл поста, выводим страницу поста, иначе ленту блога
        return $this->routes->second ? $this->post() : $this->blog();
    }

    private function post()
    {
        $url = $this->request->filter($this->routes->second, 'string');

        $post = $this->blog->get_post($url);
        if(!$post || !$post->visible)
            return false;

        $schema[] = $this->schema->create_webpage($post);

        //увеличим число просмотров
        if(!$this->sessions->last_post_read || $this->sessions->last_post_read <> $post->id)
        {
            $this->sessions->last_post_read = $post->id;
            $this->db->query("UPDATE blog SET views = views + 1 WHERE id =?", $post->id);
        }

        $this->design->assign('post', $post);
        $this->design->assign('meta_title', $post->meta_title);
        $this->design->assign('meta_description', $post->meta_description);
        $this->design->assign('schema', $schema);

        return $this->design->fetch('post.tpl');
    }

    // Отображение списка постов
    private function blog()
    {
        $filter = array();
        // Выбираем только видимые посты
        $filter['visible'] = 1;

        // Поиск по словам
        if($keyword = $this->request->get('keyword', 'string'))
        {
            $filter['keyword'] = $keyword;
            $this->design->assign('keyword', $keyword);
        }

        // Поиск по тегам
        if($tag = $this->request->get('tag', 'string'))
        {
            $filter['tag'] = $tag;
            $this->design->assign('tag', $tag);
        }

        // Фильтр по категориям
        if($category = $this->request->get('category', 'string'))
        {
            $filter['category'] = $category;
            $this->design->assign('category', $category);
        }

        // Вычисляем количество постов
        $posts_count = $this->blog->count_posts($filter);

        // Кол-во страниц в постраничном выводе
        $pages_num = ceil($posts_count / $this->posts_per_page);
        $this->design->assign('total_pages_num', $pages_num);

        // Текущая страница в постраничном выводе, eсли не задана, то равна 1
        $current_page = max(1, $this->request->get('page', 'integer'));
        $this->design->assign('current_page_num', $current_page);

        $filter['page'] = $current_page;
        $filter['limit'] = $this->posts_per_page;

        $posts = $this->blog->get_posts($filter);

        $page = $this->pages->get_page($this->routes->first);
        //теги
        $tags = array();
        $this->db->query("SELECT tags FROM blog WHERE tags != ''");
        if($tmp = $this->db->results('tags'))
        foreach($tmp AS $tags_string)
        {
            foreach(explode(",", $tags_string) AS $tag_value)
                $tags[$tag_value] = $tag_value;
        }

        $this->design->assign('page', $page);
        $this->design->assign('meta_title', $page->meta_title);
        $this->design->assign('meta_description', $page->meta_description);

        $this->design->assign('tags', $tags);
        $this->design->assign('posts', $posts);
        $this->design->assign('categories', $this->blog->get_categories());

        $body = $this->design->fetch('blog.tpl');
        return $body;
    }
}
