Files
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

165 lines
4.8 KiB
PHP

<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Acl_model extends CI_Model {
private $read_replica;
public function __construct() {
parent::__construct();
$this->read_replica = $this->load->database('savvy_replica', TRUE);
}
public function insert_acl($data) {
$data = [
'class_name' => $data['controller'],
'method_name' => $data['method']
];
$this->read_replica->select('id');
$this->read_replica->from('bko_acl');
$this->read_replica->where('class_name', $data['class_name']);
$this->read_replica->where('method_name', $data['method_name']);
$query = $this->read_replica->get();
$id = isset($query->result_array()[0]['id']) ? $query->result_array()[0]['id'] : 0;
if (!$id) {
$insert_query = $this->db->insert_string('bko_acl', $data);
$this->db->query($insert_query);
$id = $this->db->insert_id();
}
return $id;
}
public function insert_acl_permission_level($data) {
$data = [
'bko_acl_id' => $data['bko_acl_id'],
'plevel' => $data['plevel'],
];
$insert_query = $this->db->insert_string('bko_acl_permission_level', $data);
$insert_query .= " ON CONFLICT (bko_acl_id, plevel) DO NOTHING";
$this->db->query($insert_query);
}
public function insert_acl_whitelist($data) {
$data = [
'bko_acl_id' => $data['bko_acl_id']
];
$insert_query = $this->db->insert_string('bko_acl_whitelist', $data);
$insert_query .= " ON CONFLICT (bko_acl_id) DO NOTHING";
$this->db->query($insert_query);
}
public function update_acl_permission_level($data) {
$this->db->set('plevel', $data['plevel']);
$this->db->where('bko_acl_id', $data['bko_acl_id']);
$this->db->update('bko_acl_permission_level');
return $this->db->affected_rows();
}
// Fetch records
public function getData($rowno,$rowperpage, $filters = []) {
$this->read_replica->select( 'acl.id AS id,
acl.method_name AS method_name,
acl.class_name AS class_name,
pl.plevel as plevel,
pl.name AS name');
$this->read_replica->from('bko_acl acl');
$this->read_replica->join('bko_acl_permission_level', 'acl.id = bko_acl_permission_level.bko_acl_id ');
$this->read_replica->join('bko_permission_level pl', 'bko_acl_permission_level.plevel = pl.plevel ');
foreach($filters as $key => $value) {
if ($key === 'card_class_filter') {
$this->read_replica->where('lower(acl.class_name)', strtolower($value));
} else if ($key === 'method_filter') {
$this->read_replica->like('acl.method_name', strtolower($value));
} else {
$this->read_replica->where('pl.plevel', $value);
}
}
$this->read_replica->order_by('id');
$this->read_replica->limit($rowperpage, $rowno);
$query = $this->read_replica->get();
return $query->result_array();
}
// Select total records
public function getrecordCount($filters = []) {
$this->read_replica->select('count(*) as allcount');
$this->read_replica->from('bko_acl acl');
$this->read_replica->join('bko_acl_permission_level', 'acl.id = bko_acl_permission_level.bko_acl_id ');
$this->read_replica->join('bko_permission_level pl', 'bko_acl_permission_level.plevel = pl.plevel ');
foreach($filters as $key => $value) {
if ($key === 'card_class_filter') {
$this->read_replica->where('lower(acl.class_name)', strtolower($value));
} else if ($key === 'method_filter') {
$this->read_replica->like('acl.method_name', strtolower($value));
} else {
$this->read_replica->where('pl.plevel', $value);
}
}
$query = $this->read_replica->get();
$result = $query->result_array();
return $result[0]['allcount'];
}
public function getRecordControllerMethodPlevel($data) {
$this->read_replica->select('acl.id AS id');
$this->read_replica->from('bko_acl acl');
$this->read_replica->join('bko_acl_permission_level acl_p_l', 'acl.id = acl_p_l.bko_acl_id');
$this->read_replica->where('acl.class_name', $data['controller']);
$this->read_replica->where('acl.method_name', $data['method']);
$this->read_replica->where('acl_p_l.plevel', $data['plevel']);
$query = $this->read_replica->get();
return $query->num_rows();
}
public function getRecordByPermissionLevel($data) {
$query = $this->read_replica->get_where('bko_permission_level', $data);
return $query->num_rows();
}
public function gerRecordAclById($data) {
$query = $this->read_replica->get_where('bko_acl', $data);
return $query->num_rows();
}
public function deleteAclById($id) {
$this->db->where('id', $id);
$this->db->delete('bko_acl');
return $this->db->affected_rows();
}
}