File: /home/cafsindia/uds.cafsinfotech.in/application/models/Module.php
<?php
class Module extends CI_Model{
function __construct(){
parent::__construct();
}
private $enckey = 'vDIa5JdknBqfrKOu8d7UpddnBMCH1vza'; //32 characters
public function get_module_name($module_id){
$query = $this->db->get_where('modules', array('module_id' => $module_id), 1);
if($query->num_rows() == 1){
$row = $query->row();
return $this->lang->line($row->name_lang_key);
}
return $this->lang->line('error_unknown');
}
public function update_password($employees_id,$confirm_password){
$this->db->select('password');
$this->db->from('cw_employees');
$this->db->where('prime_employees_id', $employees_id);
$query = $this->db->get();
$old_password = $query->result()[0]->password;
if($old_password === md5($confirm_password)){
echo json_encode(array('success' => false, 'message' => "This password matches your previous one. Please enter a new password!"));
exit(0);
}else{
$confirm_password = $this->cryptoEncrypt(md5($confirm_password));
$prime_update_query = 'UPDATE cw_employees SET password ="'. $confirm_password .'",first_time_login = "2" WHERE prime_employees_id = "'. $employees_id .'"';
$prime_update_result = $this->db->query("$prime_update_query");
}
return $prime_update_result;
}
public function get_allowed_modules($logged_id){
if((int)$this->session->userdata('logged_role') === 12){
$this->db->from('modules');
$this->db->join('permissions', 'permissions.permission_id = modules.module_id');
$this->db->join('grants_customer', 'permissions.permission_id = grants_customer.permission_id');
$this->db->where('prime_customer_id', $logged_id);
$this->db->order_by('sort', 'asc');
return $this->db->get();
}else{
$this->db->from('modules');
$this->db->join('permissions', 'permissions.permission_id = modules.module_id');
$this->db->join('grants', 'permissions.permission_id = grants.permission_id');
$this->db->where('prime_employees_id', $logged_id);
$this->db->order_by('sort', 'asc');
return $this->db->get();
}
}
public function get_header_menu($logged_id){
if((int)$this->session->userdata('logged_role') === 12){
$this->db->select('main_menu.menu_name,modules.module_id,module_name,sub_menu_name');
$this->db->from('modules');
$this->db->join('permissions', 'permissions.permission_id = modules.module_id');
$this->db->join('grants_customer', 'permissions.permission_id = grants_customer.permission_id');
$this->db->join('main_menu', 'main_menu.prime_menu_id = modules.menu_id');
$this->db->join('sub_menu', 'sub_menu.prime_sub_menu_id = modules.sub_menu_id','left');
$this->db->where('prime_customer_id', $logged_id);
$this->db->where('modules.trans_status',1);
$this->db->where('modules.show_module',1);
$this->db->where('main_menu.trans_status',1);
$this->db->where('main_menu.menu_status',1);
$this->db->order_by('menu_sort', 'asc');
$this->db->order_by('sub_menu_sort', 'asc');
$this->db->order_by('sort', 'asc');
$query = $this->db->get();
return $query->result();
}else{
$this->db->select('main_menu.menu_name,modules.module_id,module_name,sub_menu_name');
$this->db->from('modules');
$this->db->join('permissions', 'permissions.permission_id = modules.module_id');
$this->db->join('grants', 'permissions.permission_id = grants.permission_id');
$this->db->join('main_menu', 'main_menu.prime_menu_id = modules.menu_id');
$this->db->join('sub_menu', 'cw_sub_menu.prime_sub_menu_id = modules.sub_menu_id','left');
$this->db->where('prime_employees_id', $logged_id);
$this->db->where('modules.trans_status',1);
$this->db->where('modules.show_module',1);
$this->db->where('main_menu.trans_status',1);
$this->db->where('main_menu.menu_status',1);
$this->db->order_by('menu_sort', 'asc');
$this->db->order_by('sub_menu_sort', 'asc');
$this->db->order_by('sort', 'asc');
$query = $this->db->get();
return $query->result();
}
}
//GET REPORT DETAILS FOR ROLE BASED HEADER -- 13MARCH2019
public function get_report_menu($logged_user){
$logged_id = $logged_user->prime_employees_id;
$logged_role = $logged_user->user_right;
$this->db->select('prime_report_setting_id,report_name,menu_name');
$this->db->from('report_setting');
$this->db->join('main_menu', 'main_menu.prime_menu_id = report_setting.report_menu');
$this->db->where('report_setting.trans_status',1);
$this->db->where('main_menu.menu_status',1);
$this->db->where("report_for LIKE '%".$logged_role."%'");
//$this->db->where("report_for IN (".$logged_role.")",NULL, false);
//$this->db->where("FIND_IN_SET('".$logged_role."',report_for)!=",0);
$this->db->order_by('menu_sort', 'asc');
$this->db->order_by('report_name', 'asc');
$this->db->order_by('prime_report_setting_id', 'asc');
$query = $this->db->get();
//echo $this->db->last_query();
$report_result = $query->result_array();
$report_result = array_reduce($report_result, function($result, $arr){
$result[$arr['menu_name']][] = $arr;
return $result;
}, array());
return $report_result;
}
//GET REPORT DETAILS FOR ROLE BASED HEADER -- 13MARCH2019
public function get_template_menu($logged_user){
$logged_id = $logged_user->prime_employees_id;
$logged_role = $logged_user->role;
$this->db->select('prime_bank_template_setting_id,template_name');
$this->db->from('bank_template_setting');
$this->db->where('bank_template_setting.trans_status',1);
$this->db->where("template_for LIKE '%".$logged_role."%'");
//$this->db->where("template_for IN (".$logged_role.")",NULL, false);
//$this->db->where("FIND_IN_SET('".$logged_role."',template_for)!=",0);
$this->db->order_by('prime_bank_template_setting_id', 'asc');
$query = $this->db->get();
return $query->result();
}
/* USED IN BOTH EMPLOYEE AND CUSTOMER MODULE - START*/
public function get_all_modules($control_name){
if(strtoupper($control_name) === "EMPLOYEES"){
$this->db->from('modules');
$this->db->join('cw_main_menu', 'cw_main_menu.prime_menu_id = modules.menu_id');
$this->db->join('sub_menu', 'cw_sub_menu.prime_sub_menu_id = modules.sub_menu_id','left');
$this->db->order_by('abs(menu_sort)', 'asc');
$this->db->where('modules.show_module',1);
$this->db->where('cw_main_menu.menu_status',1);
$query = $this->db->get();
return $query->result();
}else{
$query = $this->db->query("SELECT * FROM cw_modules JOIN `cw_main_menu` ON `cw_main_menu`.`prime_menu_id` = cw_modules.menu_id left join cw_sub_menu on cw_sub_menu.prime_sub_menu_id = cw_modules.sub_menu_id where FIND_IN_SET('2',rights_to) and cw_main_menu.menu_status = 1 and show_module = 1 ORDER BY abs(menu_sort) ASC");
return $query->result();
}
}
public function has_grant($control_name,$permission_id, $logged_id){
if($permission_id == null){
return TRUE;
}
if(strtoupper($control_name) === "EMPLOYEES"){
$query = $this->db->get_where('grants', array('prime_employees_id' => $logged_id, 'permission_id' => $permission_id), 1);
}else
if(strtoupper($control_name) === "EMPLOYEE_PERMISSION"){
$query = $this->db->get_where('employee_permission', array('role' => $logged_id, 'permission_id' => $permission_id), 1);
}else{
$query = $this->db->get_where('grants_customer', array('prime_customer_id' => $logged_id, 'permission_id' => $permission_id), 1);
}
return((int)$query->num_rows() === 1);
}
public function has_access($control_name,$permission_id, $logged_id){
$this->db->select('access_add,access_update,access_delete,access_search,access_export,access_import,grants_menu_id,grants_sub_menu_id');
if(strtoupper($control_name) === "EMPLOYEES"){
$this->db->from('grants');
$this->db->where('prime_employees_id', $logged_id);
}else
if(strtoupper($control_name) === "EMPLOYEE_PERMISSION"){
$this->db->from('employee_permission');
$this->db->where('role', $logged_id);
}else{
$this->db->from('grants_customer');
$this->db->where('prime_customer_id', $logged_id);
}
$this->db->where('permission_id', $permission_id);
return $this->db->get()->result_array();
}
public function update_grants($control_name,$logged_id,$grants_data,$access_data){
if($grants_data){
if(strtoupper($control_name) === "EMPLOYEES"){
$success = $this->db->delete('grants', array('prime_employees_id' => $logged_id));
}else{
$success = $this->db->delete('grants_customer', array('prime_customer_id' => $logged_id));
}
if($success){
foreach($grants_data as $permission_id){
$add = 0;
if (in_array("$permission_id::add", $access_data)){
$add = 1;
}
$update = 0;
if (in_array("$permission_id::update", $access_data)){
$update = 1;
}
$delete = 0;
if (in_array("$permission_id::delete", $access_data)){
$delete = 1;
}
$search = 0;
if (in_array("$permission_id::search", $access_data)){
$search = 1;
}
$export = 0;
if (in_array("$permission_id::export", $access_data)){
$export = 1;
}
$import = 0;
if (in_array("$permission_id::import", $access_data)){
$import = 1;
}
$this->db->select('menu_id,sub_menu_id');
$this->db->from('modules');
$this->db->where('module_id', $permission_id);
$menu_data = $this->db->get()->row();
$menu_id = $menu_data->menu_id;
$sub_menu_id = $menu_data->sub_menu_id;
if(strtoupper($control_name) === "EMPLOYEES"){
$insert_values .= "(\"$permission_id\",\"$logged_id\",\"$menu_id\",\"$sub_menu_id\",\"$add\",\"$update\",\"$delete\",\"$search\",\"$export\",\"$import\"),";
}else{
$insert_values .= "(\"$permission_id\",\"$logged_id\",\"$add\",\"$update\",\"$delete\",\"$search\",\"$export\",\"$import\"),";
}
}
if(isset($insert_values)){
$insert_values = rtrim($insert_values,",");
if(strtoupper($control_name) === "EMPLOYEES"){
$insert_query = "INSERT INTO cw_grants (`permission_id`, `prime_employees_id`, `grants_menu_id`, `grants_sub_menu_id`, `access_add`, `access_update`, `access_delete`, `access_search`, `access_export`, `access_import`) VALUES $insert_values";
$this->db->query("$insert_query");
}else{
$insert_query = "INSERT INTO cw_grants_customer (`permission_id`, `prime_customer_id`, `access_add`, `access_update`, `access_delete`, `access_search`, `access_export`, `access_import`) VALUES $insert_values";
$this->db->query("$insert_query");
}
}
}
}
}
/* USED IN BOTH EMPLOYEE AND CUSTOMER MODULE - END*/
//get notification details about fileds
public function get_notification(){
$remainder_query = $this->db->query("select * from cw_payroll_remainder where cw_payroll_remainder.trans_status =1 order by cw_payroll_remainder.prime_payroll_remainder_id asc");
return $remainder_query->result();
}
//notification list and details MRJ --updates
public function get_notification_count(){
$remainder_query = $this->db->query("select * from cw_payroll_remainder where cw_payroll_remainder.trans_status =1 order by cw_payroll_remainder.prime_payroll_remainder_id asc");
$remainter_rslt = $remainder_query->result();
$remainder_name = array();
foreach($remainter_rslt as $remainder){
$remainder_column = $remainder->remainder_field;
$days_before = $remainder->number_of_days;
$remainder_head = $remainder->remainder_heading;
$start_date = date('m-d');
$end_date = date("m-d", strtotime("+$days_before day"));
$employees_data_qry = 'select '.$remainder_column.',employee_code,emp_name from cw_employees where trans_status = 1 and role !=1 and DATE_FORMAT('.$remainder_column.', "%m-%d") BETWEEN "'.$start_date.'" and "'.$end_date.'"';
$employees_data_info = $this->db->query("CALL sp_a_run ('SELECT','$employees_data_qry')");
$employees_result = $employees_data_info->result();
$employees_data_info->next_result();
$employees_count = $employees_data_info->num_rows();
$remainder_name[$remainder_column] = array('remainder_column' => $remainder_column,'days_before' => $days_before,'remainder_head' => $remainder_head,'remainder_count'=>$employees_count);
}
return $remainder_name;
}
//GETTING COMPANY INFORMATION
public function get_company_info(){
$company_info_query = $this->db->query("select * from cw_company_information where cw_company_information.trans_status = 1");
$company_info_rslt = $company_info_query->result();
return $company_info_rslt;
}
public function productkey_save($product_info){
if($product_info){
return $this->db->insert('product_info', $product_info);
}
}
//FOLLOWING ALL FUNCTIONS FOR SMS OTP
public function send_sms($mobile_number,$user_name,$password,$resend,$otp_for){
if($otp_for === 'login'){
$sms_type = 'password_reset_otp';
}else{
$sms_type = 'onboard_pass';
}
$sms_content_qry ='select * from cw_sms_content where sms_for="'.$sms_type.'" and trans_status=1';
$content_info = $this->db->query("CALL sp_a_run ('SELECT','$sms_content_qry')");
$content_rslt = $content_info->result();
$content_info->next_result();
$company_info = $this->get_company_info();
$sms_for = $content_rslt[0]->sms_for;
$sms_content = $content_rslt[0]->sms_content;
$template_id = $content_rslt[0]->template_id;
if($sms_content !== ""){
$sms_data = array(
'to_mobile' => $mobile_number,
'sms_txt' => $sms_content,
'sms_for' => $sms_for,
'trans_created_by' => $user_name,
'trans_created_date' => date("Y-m-d h:i:s"),
);
}
$this->save_sms($sms_data);
$sms_rslt =$this->trigger_sms($mobile_number,$sms_content,$template_id,$company_info,$user_name,$password,$sms_for,$sms_type);
return $sms_rslt;
}
//SAVE SMS DETAILS FOR SMS LOG TABLE
public function save_sms($sms_data){
return $this->db->insert('sms_log', $sms_data);
}
//FUNCTION FOR SMS SEND TO MOBILE NUMBER
public function trigger_sms($mobile_number,$sms_content,$template_id,$company_info,$user_name,$password,$sms_for,$sms_type){
$sms_url = $company_info[0]->sms_url;
$sms_id = $company_info[0]->sms_sender_id;
$sms_pwd = $company_info[0]->sms_password;
$company_name = $company_info[0]->company_short_name;
if($sms_type === "password_reset_otp"){
$login_otp = rand(100000, 999999);
$content = str_replace("@otp@",$login_otp,$sms_content);
}else
if($sms_type === "onboard_pass"){
$site_url = site_url();
$search = array('@user_name@', '@password@' ,'@url@' ,'@company_name@');
$replace = array($user_name, $password,$site_url,$company_name);
$content = str_replace($search,$replace,$sms_content);
}else{
return array('success'=>false,'message'=>"Message Not sent...Content not exist...!!");
}
if($content){
$url = $sms_url.'method=SendMessage&send_to='.$mobile_number.'&msg='.urlencode($content).'"&msg_type=TEXT&userid='.$template_id.'&auth_scheme=plain&password='.$sms_pwd.'&v=1.1&format=text';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
if ($output === false) {
echo "Failed to send SMS. cURL error: " . curl_error($ch);
} else {
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode === 200) {
return array('success'=>true,'message'=>"SMS Sent your Mobile Number",'otp_code'=>$login_otp);
} else {
return array('success'=>false,'message'=>"SMS Not Sent");
}
}
curl_close($ch);
}
public function cryptoEncrypt($data){
try {
// For Password Encryption
$hash1 = hash('sha512', $data);
$hash2 = hash('sha1', $hash1);
$Hash3 = hash('haval160,4', $hash2);
$Hash4 = hash('haval160,5', $Hash3);
// Generate the HMAC hash
$finalhash = hash_hmac('sha256', $Hash4, $this->enckey);
return $finalhash;
} catch (Exception $e) {
// Log the error or handle it as needed
error_log("Encryption Error: " . $e->getMessage()); // Log the error for debugging
return false;
}
}
}
?>