<?php

class Journal extends CIF_Controller {

    public $layout = 'full';
    public $module = 'journal';
    public $model = 'Posts_model';

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model($this->model);
        $this->_primary_key = $this->{$this->model}->_primary_keys[0];
    }

    public function index() {

        $this->{$this->model}->custom_select = 'posts.*, blog_categories.title as category';
        $this->{$this->model}->joins = array(
            'blog_categories' => array('blog_categories.blog_category_id = posts.blog_category_id', 'inner')
        );
        $data['blog_categories'] = $this->db->select('blog_categories.*, (SELECT COUNT(*) FROM posts where blog_categories.blog_category_id = posts.blog_category_id) as count')->order_by('title')->get('blog_categories')->result();

        $data['items'] = $this->{$this->model}->get();
        
        $this->load->view($this->module, $data);
    }
}
