<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Accessaction_model extends CI_Model {
    protected $_table='access_action';

    public function __construct(){
        parent::__construct();
        date_default_timezone_set("Asia/Ho_Chi_Minh");
    }

    public function add($dataInsert){
        $this->db->insert($this->_table, $dataInsert);
        $insert_id = $this->db->insert_id();
        return $insert_id;
    }

    public function update($id, $dataUpdate){
        $this->db->where('id', $id);
        $this->db->update($this->_table, $dataUpdate);
    }

    public function delete($id){
        $this->db->where('id', $id);
        $this->db->delete($this->_table);
    }

    public function getById($id){
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('id', $id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getList($is_active=true){
        $this->db->select('*');
        $this->db->from($this->_table);
        if($is_active == true){
            $this->db->where('is_active', 1);
        }
        $this->db->order_by($this->_table.'.name', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getListByGroupId($group_id, $is_active=true){
        $this->db->select('aa.*');
        $this->db->from($this->_table.' aa');
        $this->db->join('access_controller ac', 'ac.id = aa.controller_id');
        $this->db->join('group_access_controller gac', 'gac.access_controller_id = ac.id');
        $this->db->where('gac.group_id', $group_id);
        if($is_active == true){
            $this->db->where('aa.is_active', 1);
        }
        $this->db->order_by('aa.name', 'asc');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Hàm kiểm tra trùng lặp tên action dựa vào controller_id
     * Tên action có thể trùng nhau nhưng nằm ở các controller khác nhau
     * @param $controller_id
     */
    public function getActionByControllerId($action_name, $controller_id){
        $this->db->select('*');
        $this->db->from($this->_table);
        $this->db->where('LOWER(name)', strtolower($action_name));
        $this->db->where('controller_id', $controller_id);
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getListPermissionForMenu($firstItem=''){
        $this->db->select('aa.name');
        $this->db->from($this->_table.' aa');
        $this->db->join('access_controller ac', 'ac.id = aa.controller_id');
        $this->db->where('aa.is_active', 1);
        $this->db->where('ac.is_active', 1);
        $query = $this->db->get();
        $result = $query->result_array();
        $list = array();
        if($firstItem != ''){
            $list[''] = $firstItem;
        }
        foreach($result as $value){
            $list[] = $value['name'];
        }
        return $list;
        //return $result;
    }

    public function countAll($name=null){
        $this->db->select('COUNT(*) AS totalResults');
        $this->db->from($this->_table);
        if($this->session->userdata('controller_id')){
            $this->db->where($this->_table.'.controller_id', $this->session->userdata('controller_id'));
        }
        if($name != null){
            $this->db->like($this->_table.'.name', $name, 'both');
        }

        $query = $this->db->get();
        $result = $query->result_array();
        if($result){
            return $result[0]['totalResults'];
        }else{
            return 0;
        }
    }

    public function getPagination($record, $start, $name){
        $this->db->select($this->_table.'.*, access_controller.name as controllerName');
        $this->db->from($this->_table);
        $this->db->join('access_controller', 'access_controller.id = access_action.controller_id');
        if($this->session->userdata('controller_id')){
            $this->db->where($this->_table.'.controller_id', $this->session->userdata('controller_id'));
        }
        if($name != null){
            $this->db->like($this->_table.'.name', $name, 'both');
        }
        $this->db->order_by($this->_table.'.controller_id', 'asc');
        $this->db->order_by($this->_table.'.name', 'asc');
        $this->db->limit($record, $start);
        $query = $this->db->get();
        return $query->result_array();
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */