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

class Adminaccessaction_model extends CI_Model {
    protected $_table='admin_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);
    }

    // Thêm nhiều quyền truy cập
    public function addBatch($dataInsertBatch){
        $this->db->insert_batch($this->_table, $dataInsertBatch);
    }

    // Xóa quyền truy cập
    public function delete($admin_id, $access_action_id){
        $this->db->where('admin_id', $admin_id);
        $this->db->where('access_action_id', $access_action_id);
        $this->db->delete($this->_table);
    }

    // Lấy danh sách ID quyền truy cập action của user
    public function getByAdminId($admin_id, $group_id){
        // Lấy mảng controller_id từ group_id
        $this->load->model('mgroupaccesscontroller');
        $listControllerByGroupId = $this->mgroupaccesscontroller->getByGroupId($group_id);
        //
        $this->db->select('aa.*');
        $this->db->from($this->_table.' adaa');
        $this->db->join('access_action aa', 'aa.id = adaa.access_action_id');
        $this->db->where('adaa.admin_id', $admin_id);
        if(count($listControllerByGroupId) > 0){
            $this->db->where_in('aa.controller_id', $listControllerByGroupId);
        }
        $query = $this->db->get();
        $result = $query->result_array();
        $list = array();
        foreach($result as $value){
            $list[] = $value['id'];
        }
        return $list;
    }

    // Lấy danh sách ID quyền truy cập action của user
    public function getActionAssignedByAdminId($admin_id, $is_active=true){
        $this->db->select('aa.*');
        $this->db->from($this->_table.' adaa');
        $this->db->join('access_action aa', 'aa.id = adaa.access_action_id');
        $this->db->where('adaa.admin_id', $admin_id);
        if($is_active == true){
            $this->db->where('aa.is_active', 1);
        }
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getListPermissionForMenu(){
        $this->load->model('AdminModel');
        $admin_id = $this->session->userdata('id');

        $this->db->select('aa.name');
        $this->db->from($this->_table.' adaa');
        $this->db->join('access_action aa', 'aa.id = adaa.access_action_id');
        $this->db->where('adaa.admin_id', $admin_id);
        $this->db->where('aa.is_active', 1);
        $query = $this->db->get();
        $result = $query->result_array();
        $list = array();
        foreach($result as $value){
            $list[] = $value['name'];
        }
        return $list;
    }

    public function getCountActionInControllerAssigned(){
        $this->load->model('AdminModel');

        $admin_id = $this->session->userdata('id');
        $adminInfo = $this->madmin->getById($admin_id);
        $group_id = $adminInfo[0]['group_id'];

        $this->db->select('ac.name AS controller_name, COUNT(*) AS count_action');
        $this->db->from($this->_table.' adaa');
        $this->db->join('access_action aa', 'aa.id = adaa.access_action_id');
        $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->join('group g', 'g.id = gac.group_id');

        $this->db->where('g.is_active', 1);
        $this->db->where('ac.is_active', 1);
        $this->db->where('aa.is_active', 1);
        $this->db->where('adaa.admin_id', $admin_id);
        $this->db->where('gac.group_id', $group_id);

        $this->db->group_by(array('gac.access_controller_id'));

        $query = $this->db->get();
        $result = $query->result_array();
        $list = array();
        foreach($result as $value){
            $list[] = $value['controller_name'];
        }
        return $list;
    }


}

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