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

if (! function_exists('change_key')) {
 function change_key($array, $old_key, $new_key) {

	if( ! array_key_exists( $old_key, $array ) )
		return $array;

	$keys = array_keys( $array );
	$keys[ array_search( $old_key, $keys ) ] = $new_key;

	return array_combine( $keys, $array );
 }
}

if (! function_exists('columnify')) {
function columnify($leftCol, $rightCol, $leftWidth, $rightWidth, $space = 1)
	{
		$leftWrapped = wordwrap($leftCol, $leftWidth, "\n", true);
		$rightWrapped = wordwrap($rightCol, $rightWidth, "\n", true);
	
		$leftLines = explode("\n", $leftWrapped);
		$rightLines = explode("\n", $rightWrapped);
		$allLines = array();
		for ($i = 0; $i < max(count($leftLines), count($rightLines)); $i ++) {
			$leftPart = str_pad(isset($leftLines[$i]) ? $leftLines[$i] : "", $leftWidth, " ");
			$rightPart = str_pad(isset($rightLines[$i]) ? $rightLines[$i] : "", $rightWidth, " ");
			$allLines[] = $leftPart . str_repeat(" ", $space) . $rightPart;
		}
		return implode("\n",$allLines) . "\n";
	}
}

// if (! function_exists('columnify')) {
// 	function columnify($leftCol, $middle1Col, $middle2Col, $middle3Col, $rightCol, $leftWidth, $rightWidth,$middle1Width, $middle2Width, $middle3Width, $space = 2)
// 		{
// 			$leftWrapped = wordwrap($leftCol, $leftWidth, "\n", true);
// 			$rightWrapped = wordwrap($rightCol, $rightWidth, "\n", true);
	
// 			$middle1Wrapped = wordwrap($middle1Col, $middle1Width, "\n", true);
// 			$middle2Wrapped = wordwrap($middle2Col, $middle2Width, "\n", true);
// 			$middle3Wrapped = wordwrap($middle3Col, $middle3Width, "\n", true);
	
		
// 			$leftLines = explode("\n", $leftWrapped);
// 			$rightLines = explode("\n", $rightWrapped);
	
// 			$middle1Lines = explode("\n", $$middle1Wrapped);
// 			$middle2Lines = explode("\n", $$middle2Wrapped);
// 			$middle3Lines = explode("\n", $$middle3Wrapped);
// 			$allLines = array();
// 			for ($i = 0; $i < max(count($leftLines), count($rightLines) , count($middle1Lines), count($middle2Lines) ,count($middle3Lines) ); $i ++) {
	
	
	
// 				$leftPart = str_pad(isset($leftLines[$i]) ? $leftLines[$i] : "", $leftWidth, " ");
// 				$middle1Part = str_pad(isset($middle1Lines[$i]) ? $middle1Lines[$i] : "", $middle1Width, " ");
// 				$middle2Part = str_pad(isset($middle2Lines[$i]) ? $middle2Lines[$i] : "", $middle2Width, " ");
// 				$middle3Part = str_pad(isset($middle3Lines[$i]) ? $middle3Lines[$i] : "", $middle3Width, " ");
// 				$rightPart = str_pad(isset($rightLines[$i]) ? $rightLines[$i] : "", $rightWidth, " ");
// 				$allLines[] = $leftPart . str_repeat(" ", $space) .
// 				$middle1Part . $middle2Part . $middle3Part.$rightPart;
// 			}
// 			return implode("\n",$allLines) . "\n";
// 		}
// 	}

if (! function_exists('last_insert_id')) {
    function last_insert_id($tablename)
    {

        $ci = get_instance();
		$ci->db->select('ID');
		$ci->db->order_by('ID','DESC');
		$ci->db->limit(1);
		$query=$ci->db->get($tablename);
		
		$result=$query->row();
		if(!empty($result)){
			$lastid=$result->ID;
		}else{
			$lastid=1;
		}
       	return $lastid;
        
    }
}

if (! function_exists('last_insert_coloum_id')) {
    function last_insert_coloum_id($tablename , $coloumname)
    {

        $ci = get_instance();
		$ci->db->select($coloumname.' AS ID');
		$ci->db->order_by($coloumname,'DESC');
		$ci->db->limit(1);
		$query=$ci->db->get($tablename);
		
		$result=$query->row();
		if(!empty($result)){
			$lastid=$result->ID;
		}else{
			$lastid=1;
		}
       	return $lastid;
        
    }
}

if (! function_exists('response')) {
    function response($data,$message,$code,$object=true)
    {
		if(empty($data)){
			$data = new StdClass();
			if(!$object){
				$data = array();
			}
		}
		http_response_code($code);
		$response = array(
			'header' => array(
				'code' => $code,
				'message' => $message,
			),
			'body' => $data
		);
		header('Content-Type: application/json');
		echo json_encode($response);
    }
}

if (! function_exists('auth_token')) {
    function auth_token($token)
    {
		$ci = get_instance();
		$ci->db->where('REMEMBER_TOKEN', $token);
		$query=$ci->db->get('USERS');
		if($query->num_rows()==1){
			return true;
		}
		else{
			return false;
		}
    }
}

if (! function_exists('array_check_key')) {
    function array_check_key($array,$my_index)
    {
		$value = '';
		return $value = array_key_exists($my_index, $array) ? $array[$my_index] : '';
    }
}

if (! function_exists('print_r_pre')) {
    function print_r_pre($var, $heading="")
    {
        if ($heading!="") {
            echo "<br><b>$heading:</b>";
        }
        echo "<pre>";
        print_r($var);
        echo "</pre>";
    }
}

if (! function_exists('last_insert_id1')) {
    function last_insert_id1($tablename,$id,$conn)
    {
		$conn->select($id);
		$conn->order_by($id,'DESC');
		$conn->limit(1);
		$query=$conn->get($tablename);
		
		$result=$query->row();
		if(!empty($result)){
			$lastid=$result->$id;
		}else{
			$lastid=1;
		}
       	return $lastid;
        
    }
}

//////////////////////////////////////////////////////////////////////
    //PARA: Date Should In YYYY-MM-DD Format
    //RESULT FORMAT:
    // '%y Year %m Month %d Day %h Hours %i Minute %s Seconds'      =>  1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
    // '%y Year %m Month %d Day'                                    =>  1 Year 3 Month 14 Days
    // '%m Month %d Day'                                            =>  3 Month 14 Day
    // '%d Day %h Hours'                                            =>  14 Day 11 Hours
    // '%d Day'                                                     =>  14 Days
    // '%h Hours %i Minute %s Seconds'                              =>  11 Hours 49 Minute 36 Seconds
    // '%i Minute %s Seconds'                                       =>  49 Minute 36 Seconds
    // '%h Hours                                                    =>  11 Hours
    // '%a Days                                                     =>  468 Days
	//////////////////////////////////////////////////////////////////////
	if (! function_exists('datetimeDifference')) {
		function datetimeDifference($date_1, $date_2, $differenceFormat = '%a')
		{
			$datetime1 = date_create($date_1);
			$datetime2 = date_create($date_2);

			$interval = date_diff($datetime1, $datetime2);

			return $interval->format($differenceFormat);
		}
	}
	
	if (! function_exists('numberTowords')) {
	function numberTowords($num)
	{ 
		$ones = array( 
		1 => "one", 
		2 => "two", 
		3 => "three", 
		4 => "four", 
		5 => "five", 
		6 => "six", 
		7 => "seven", 
		8 => "eight", 
		9 => "nine", 
		10 => "ten", 
		11 => "eleven", 
		12 => "twelve", 
		13 => "thirteen", 
		14 => "fourteen", 
		15 => "fifteen", 
		16 => "sixteen", 
		17 => "seventeen", 
		18 => "eighteen", 
		19 => "nineteen" 
		); 
		$tens = array( 
		1 => "ten",
		2 => "twenty", 
		3 => "thirty", 
		4 => "forty", 
		5 => "fifty", 
		6 => "sixty", 
		7 => "seventy", 
		8 => "eighty", 
		9 => "ninety" 
		); 
		$hundreds = array( 
		"hundred", 
		"thousand", 
		"million", 
		"billion", 
		"trillion", 
		"quadrillion" 
		); //limit t quadrillion 
		$num = number_format($num,2,".",","); 
		$num_arr = explode(".",$num); 
		$wholenum = $num_arr[0]; 
		$decnum = $num_arr[1]; 
		$whole_arr = array_reverse(explode(",",$wholenum)); 
		krsort($whole_arr); 
		$rettxt = ""; 
		foreach($whole_arr as $key => $i){ 
		if($i < 20){ 
		$rettxt .= $ones[$i]; 
		}elseif($i < 100){ 
		$rettxt .= $tens[substr($i,0,1)]; 
		$rettxt .= " ".$ones[substr($i,1,1)]; 
		}else{ 
		$rettxt .= $ones[substr($i,0,1)]." ".$hundreds[0]; 
		$rettxt .= " ".$tens[substr($i,1,1)]; 
		$rettxt .= " ".$ones[substr($i,2,1)]; 
		} 
		if($key > 0){ 
		$rettxt .= " ".$hundreds[$key]." "; 
		} 
		} 
		if($decnum > 0){ 
		$rettxt .= " and "; 
		if($decnum < 20){ 
		$rettxt .= $ones[$decnum]; 
		}elseif($decnum < 100){ 
		$rettxt .= $tens[substr($decnum,0,1)]; 
		$rettxt .= " ".$ones[substr($decnum,1,1)]; 
		} 
		} 
		return $rettxt; 
	}
	}
?>