<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Post_site extends MX_Controller
{

    public function posts()
    {
        $this->config->load('post/config');

        $this->load->model('post/post_tmodel');
        $this->load->model('post/post_category_tmodel');

        $category = $this->post_category_tmodel->fetch_current();
        $post_predicates = [];
        if (!empty($category)) {
            $post_predicates['pc.id_post_category'] = $category['id_post_category'];
        }

        $content = [
            'items'      => $this->post_tmodel->set_predicates($post_predicates)->fetch_page($this->uri->get_next_segment('page'), $this->config->item('post_per_page')),
            'pagination' => $this->load->library('post/post_pagination', [
                'total_rows' => $this->post_tmodel->set_predicates($post_predicates)->count(),
                'per_page'   => $this->config->item('post_per_page'),
            ])->create_links(),
        ];

        return $this->parser->parse('post/posts', $content, TRUE);
    }

    public function current($column)
    {
        $this->load->model('post/post_tmodel');
        $post = $this->post_tmodel->fetch_current();

        return !empty($post[$column]) ? $post[$column] : '';
    }
}
