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

class Groupaccesscontroller_model extends CI_Model {
    protected $_table='group_access_controller';

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

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

    public function addBatch($dataInsertBatch){
        $this->db->insert_batch($this->_table, $dataInsertBatch);
    }

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

    public function getByGroupId($group_id){
        $this->db->select('access_controller_id');
        $this->db->from($this->_table);
        $this->db->where('group_id', $group_id);
        $query = $this->db->get();
        $result = $query->result_array();
        $list = array();
        foreach($result as $value){
            $list[] = $value['access_controller_id'];
        }
        return $list;
    }

    /**
     * Lấy list controller được phân quyền cho Nhóm
     * @return array
     */
    public function getControllerNameAssigned(){
        $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');
        $this->db->from($this->_table.' gac');
        $this->db->join('access_controller ac', 'ac.id = gac.access_controller_id');
        $this->db->join('group g', 'g.id = gac.group_id');
        $this->db->where('gac.group_id', $group_id);
        $this->db->where('g.name !=', 'dcv_super_admin');
        $this->db->where('g.is_active', 1);
        $this->db->where('ac.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.' gac');
        $this->db->join('group g', 'g.id = gac.group_id');
        $this->db->join('access_controller ac', 'ac.id = gac.access_controller_id');
        $this->db->join('access_action aa', 'aa.id = adaa.access_action_id');
        $this->db->join('group_access_controller gac', 'gac.access_controller_id = ac.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();
        return $query->result_array();
    }


}

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