<?php

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

/**
 * Description of Post_category_tmodel
 *
 * @author CIEXT skeleton
 */
class Post_category_tmodel extends \Ciext\Model\AbstractTableModel
{

    protected $table = 'post_category';
    protected $pkey = 'id_post_category';
    protected $select = 't.id_post_category, pct.title, rtp.alias';
    protected $id_lan = 1;

    public function __construct()
    {
        $this->load->library('db_lang_lib');
        $this->id_lan = $this->db_lang_lib->get_current_lang_id();
    }

    public function fetch_current()
    {
        $id_page = $this->core_model->get_current_page_id();

        $res = $this->prepare()
            ->select($this->select, $this->select_escape)
            ->where('t.id_resource_type_page', (int)$id_page)
            ->get($this->table . ' t')
            ->row_array();

        $this->notify(__FUNCTION__, ['category' => $res]);

        return $res;
    }

    protected function prepare()
    {
        return $this->db
            ->join('post_category_translate pct', '(pct.id_post_category = t.id_post_category AND pct.id_lan = ' . $this->id_lan . ')', 'left')
            ->join('resource_type_page rtp', 'rtp.id_resource_type_page = t.id_resource_type_page', 'left')
            ->join('resource_type_page_data rtpd', 'rtpd.id_resource_type_page = t.id_resource_type_page', 'left')
            ->where('rtp.active', 1);
    }
}
