<?php

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

/**
 * Description of TABLE_tmodel
 *
 * @author CIEXT skeleton
 */
class TABLE_tmodel extends CI_Model
{

    protected $table = 'TABLE';
    protected $pkey = 'id_TABLE';

    /**
     * Fetch one record
     * @param integer $id
     * @return array
     */
    public function fetch($id = 0)
    {
        $res = $this->where()
                ->select('t.*')
                ->where('t.' . $this->pkey, (int) $id)
                ->get($this->table . ' t')
                ->row_array();

        return $res;
    }

    /**
     * Fetch all records
     * @return array
     */
    public function fetch_all()
    {
        $res = $this->where()
                ->select('t.*')
                ->order_by('t.view_order')
                ->get($this->table . ' t')
                ->result_array();

        return $res;
    }

    /**
     * Fetch records of page
     * @param integer $page
     * @param integer $limit
     * @return array
     */
    public function fetch_page($page = 1, $limit = 1)
    {
        $offset = ($page <= 1) ? 0 : $page - 1;
        $this->db->limit($limit)->offset($offset * $limit);

        return $this->fetch_all();
    }

    /**
     * Records count
     * @return integer
     */
    public function count()
    {
        $res = $this->where()->count_all_results($this->table . ' t');

        return $res;
    }

    protected function where()
    {
        return $this->db->where('t.active', TRUE);
    }

}
