<?php
function pr($val){
	echo (is_array($val))?'<pre>':'';
	print_r($val);
	echo (is_array($val))?'</pre>':'';
	exit;
}

function pre($val){
	echo (is_array($val))?'<pre>':'';
	print_r($val);
	echo (is_array($val))?'</pre>':'';
	echo '<br>';
}

function set_upload_service($path){
    $config = array();
    $config['upload_path'] = $path;
    $config['allowed_types'] = '*';
    $config['overwrite'] = FALSE;
    return $config;
}

function encode_param($param = ''){
		if(empty($param)){
			return;
		}
		$encode = base64_encode('{*}'.$param.'{*}');
		$encode = base64_encode('a%a'.$encode.'a%a');
		$encode = base64_encode('b'.$encode.'b');
		$encode = base64_encode('Ta7K'.$encode.'eyRq');
		return urlencode($encode);
	}

	function decode_param($param = ''){
		if(empty($param)){
			return;
		}
		$decode = urldecode(trim($param));
		$decode = trim(base64_decode(urldecode($decode)),'Ta7K');
		$decode = trim($decode,'eyRq');
		$decode = trim(base64_decode(urldecode($decode)),'b');
		$decode = trim(base64_decode(urldecode($decode)),'a%a');
		$decode = trim(base64_decode(urldecode($decode)),'{*}');
		return $decode;
	}


function send_custom_email($from, $mail_head, $to, $subject, $email_message)
{

// $headers = "Organization: The 303\r\n";
// $headers .= "MIME-Version: 1.0\r\n";
// $headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
// $headers .= "Reply-To: The3O3 <'.$from.'>\r\n";
// $headers .= 'From: The3O3 <'.$from.'>' . "\r\n"; 
// $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
// $headers  .= "X-Priority: 1\r\n"; 
// if (mail($to, $subject, $email_message, $headers)) {
//    return TRUE; 
// }else{
//    return FALSE;
// }
	 $Settings = getSettings();
  $ci =& get_instance();
    $ci->load->database();

    $ci->load->library('email');


   // $config['protocol'] = "smtp";
   //  $config['smtp_host'] = $Settings['smtp_host'];
   //  $config['smtp_port'] = "587";
   //  $config['smtp_user'] = $Settings['smtp_username'];
   //  $config['smtp_pass'] = $Settings['smtp_password'];
   //  $config['charset'] = "utf-8";
   //  $config['mailtype'] = "html";
   //  $config['newline'] = "\r\n";
     $config['protocol'] = "smtp";
    $config['smtp_host'] = "smtp.sendgrid.net";
    $config['smtp_port'] = "587";
    $config['smtp_user'] = 'adarsh@techware.in';
    $config['smtp_pass'] = 'Golden_123';
    $config['charset'] = "utf-8";
    $config['mailtype'] = "html";
    $config['newline'] = "\r\n";


    $ci->email->initialize($config);
    $ci->email->from($from, $mail_head);
    $ci->email->to($to);
    $ci->email->reply_to('no-replay@gmail.com');
    $ci->email->subject($subject);
    $ci->email->message($email_message);
    $send = $ci->email->send();
    if($send == TRUE)
    {
        return TRUE;
    } else{
        return FALSE;
    }


 	}

 	function get_profile_image(){
	$CI = & get_instance();
	$rs = $CI->db->select('profile_picture')->get('admin');
	if($rs->num_rows()>0){
		$res = $rs->row();
		return $res->profile_picture;
	} else {
		return base_url('assets/images/admin.jpg');
	}
}


	function getSettings(){
		$CI = & get_instance();
		$settings = $CI->db->get('settings');
		return (!empty($settings))?$settings->row_array():'';
	}
   function getNotifTemplate(){
		$CI = & get_instance();
		$templates = $CI->db->get('notification_templates');
		return (!empty($templates))?$templates->row_array():'';
	}

	function getCurrency(){
		$CI = & get_instance();
		$CI->db->where('status !=', '2');
		$currency = $CI->db->get('country');
		return (!empty($currency))?$currency->result_array():'';
	}

function move_files($path) {
  $CI = & get_instance();
    $CI->load->library('ftp');
    $ftp_config['hostname'] = '35.202.195.236'; 
    $ftp_config['username'] = 'nuvento';
    $ftp_config['password'] = 'Golden_123';
    $ftp_config['debug'] = TRUE;
    $CI->ftp->connect($ftp_config);
    $destination = '/access.nuvento.com/portal/assets'.$path;
    $CI->ftp->upload('.'.$path, ".".$destination);
    @unlink($path);
    $CI->ftp->close();
 

}
function getgarage_code(){
	 $CI = & get_instance();
		 $CI->load->helper('string');


$date =  date("Y-m-d");
$d = date_parse_from_format("Y-m-d", $date);

$month = $d['month']; // 25
$day = $d['day']; // 09
$year = $d['year'];

		$G_code='G'.$day.$month.$year.random_string('numeric', 2);

		return ($G_code);
	}

	function getexpress_code(){
	 $CI = & get_instance();
		 $CI->load->helper('string');


$date =  date("Y-m-d");
$d = date_parse_from_format("Y-m-d", $date);

$month = $d['month']; // 25
$day = $d['day']; // 09
$year = $d['year'];

		$G_code='EXP'.$day.$month.$year.random_string('numeric', 2);

		return ($G_code);
	}
	function getpos_code(){
	 $CI = & get_instance();
		 $CI->load->helper('string');


$date =  date("Y-m-d");
$d = date_parse_from_format("Y-m-d", $date);

$month = $d['month']; // 25
$day = $d['day']; // 09
$year = $d['year'];

		$G_code='POS'.$day.$month.$year.random_string('numeric', 2);

		return ($G_code);
	}
	function getroadassist_code(){
	 $CI = & get_instance();
		 $CI->load->helper('string');


$date =  date("Y-m-d");
$d = date_parse_from_format("Y-m-d", $date);

$month = $d['month']; // 25
$day = $d['day']; // 09
$year = $d['year'];

		$G_code='POS'.$day.$month.$year.random_string('numeric', 2);

		return ($G_code);
	}

	function getpospart_code(){
	 $CI = & get_instance();
		 $CI->load->helper('string');


$date =  date("Y-m-d");
$d = date_parse_from_format("Y-m-d", $date);

$month = $d['month']; // 25
$day = $d['day']; // 09
$year = $d['year'];

		$G_code='PP'.$day.$month.$year.random_string('numeric', 2);

		return ($G_code);
	}
function getImage_url(){
		
		$image_url = 'access.nuvento.com/portal/';
		return ($image_url);
	}
function imageResize($imgName, $sizes, $path){
	$CI = & get_instance();
    $CI->load->library('image_lib');
    $config['image_library'] = 'gd2';
    $config['source_image'] = $path.$imgName;
    $config['new_image'] = $path.$sizes['width'].'x'.$sizes['height']."_".$imgName;
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $sizes['width'];
    $config['height'] = $sizes['height'];
    $CI->image_lib->clear();
    $CI->image_lib->initialize($config);
    $CI->image_lib->resize();
    if ( ! $CI->image_lib->resize()){ 
        echo $CI->image_lib->display_errors();
    }
    return $sizes['width'].'x'.$sizes['height']."_".$imgName; 
}

	
	function roleManagement(){

		$ci=& get_instance();
	    $ci->load->database();
		 $Data = '';

	 		$RolemngData = $ci->db->query("SELECT PGC.id,R.id as role_id,R.title as role_name,P.id as privilege_id,P.title AS privileges_name,GROUP_CONCAT(DISTINCT(M.title)) AS method_name,GROUP_CONCAT(DISTINCT(M.id)) AS method_id FROM privilege_group_connection AS PGC 
	 									   INNER JOIN privileges AS P ON (P.id=PGC.privilege_id)
	 									   INNER JOIN method AS M ON (M.id=PGC.method_id)
	 									    INNER JOIN role AS R ON (R.id=PGC.role_id)
	 									   WHERE PGC.status!='1' AND role_id='1


	 									   ' GROUP BY PGC.privilege_id")->result();

		if(isset($RolemngData)){
			$privileges = array();
			foreach ($RolemngData as $rs) {
				$privileges[$rs->privileges_name] = explode(',', $rs->method_name);		
			}
		    return $privileges;
	 	}
	}
?>