<?php 
    function cek_session_admin(){
    	$ci = & get_instance();
    	$session = $ci->session->userdata('level');
    	if ($session != 'admin'){
    		redirect(base_url());
    	}
    }
	
	function format_rupiah($angka){
	  $rupiah=number_format($angka,0,',','.');
	  return $rupiah;
	}
	function glinext($tabel, $field_kunci, $pad) {
	$CI 	=& get_instance();
	$nama	= $CI->db->query("SELECT max($field_kunci) AS last FROM $tabel ")->row();
	$data	= (intval($nama->last)) + 1;
	$last	= str_pad($data, $pad, '0', STR_PAD_LEFT);
	return $last;
	}
	
	function gli_stambuk($tabel, $field_kunci, $pad,$field_where, $field_cari) {
	$CI 	=& get_instance();
	$nama	= $CI->db->query("SELECT max($field_kunci) AS last FROM $tabel WHERE ".$field_where." = '".$field_cari."'")->row();
	
	$data	= substr($nama->last,12,3) + 1;
	$last	= str_pad($data, $pad, '0', STR_PAD_LEFT);
	return $last;
	}
	function  convertKeTglDatepicker($tgl){
    $tanggal = substr($tgl,8,2);
    $bulan   = substr($tgl,5,2);
    $tahun   = substr($tgl,0,4);
    return  $tanggal.'-'.$bulan.'-'.$tahun;	
	}
	function get_valuezz($tabel, $kunci, $id) {
		$ci =& get_instance();
		$q_ambil	= $ci->db->query("SELECT * FROM ".$tabel." WHERE ".$kunci." = '".$id."'")->row_array();
		return $q_ambil;
	}
    function cetak($str){
        return strip_tags(htmlentities($str, ENT_QUOTES, 'UTF-8'));
    }
	function get_value($id){
        $ci = & get_instance();
        $title = $ci->db->query("SELECT gbr_pegawai FROM pegawai WHERE id_pegawai = '".$id."' ORDER BY id_pegawai")->row_array();
        return $title['gbr_pegawai'];
    }
    function title(){
        $ci = & get_instance();
        $title = $ci->db->query("SELECT nama_website FROM identitas ORDER BY id_identitas DESC LIMIT 1")->row_array();
        return $title['nama_website'];
    }
	function mod(){
	$data = (object)array(
	  'server'	=> 'http://update.pinisisoft.com/',
	  'app_name'=> 'panjar',
	  'app_vers'=> '3.3'
	);
	return $data;}
    function description(){
        $ci = & get_instance();
        $title = $ci->db->query("SELECT meta_deskripsi FROM identitas ORDER BY id_identitas DESC LIMIT 1")->row_array();
        return $title['meta_deskripsi'];
    }

    function keywords(){
        $ci = & get_instance();
        $title = $ci->db->query("SELECT meta_keyword FROM identitas ORDER BY id_identitas DESC LIMIT 1")->row_array();
        return $title['meta_keyword'];
    }

    function tgl_indo($tgl){
            $tanggal = substr($tgl,8,2);
            $bulan = getBulan(substr($tgl,5,2));
            $tahun = substr($tgl,0,4);
            return $tanggal.' '.$bulan.' '.$tahun;       
    } 

    function tgl_simpan($tgl){
            $tanggal = substr($tgl,0,2);
            $bulan = substr($tgl,3,2);
            $tahun = substr($tgl,6,4);
            return $tahun.'-'.$bulan.'-'.$tanggal;       
    }

    function tgl_view($tgl){
            $tanggal = substr($tgl,8,2);
            $bulan = substr($tgl,5,2);
            $tahun = substr($tgl,0,4);
            return $tanggal.'-'.$bulan.'-'.$tahun;       
    }

    function tgl_grafik($tgl){
            $tanggal = substr($tgl,8,2);
            $bulan = getBulan(substr($tgl,5,2));
            $tahun = substr($tgl,0,4);
            return $tanggal.'_'.$bulan;       
    }    

    function seo_title($s) {
        $c = array (' ');
        $d = array ('-','/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+','–');
        $s = str_replace($d, '', $s); // Hilangkan karakter yang telah disebutkan di array $d
        $s = strtolower(str_replace($c, '-', $s)); // Ganti spasi dengan tanda - dan ubah hurufnya menjadi kecil semua
        return $s;
    }

    function hari_ini($w){
        $seminggu = array("Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu");
        $hari_ini = $seminggu[$w];
        return $hari_ini;
    }
	function number_to_words($number)
	{
		$before_comma = trim(to_word($number));
		$after_comma = trim(comma($number));
		return ucwords($results = $before_comma);
	}

	function to_word($number)
	{
		$words = "";
		$arr_number = array(
		"",
		"satu",
		"dua",
		"tiga",
		"empat",
		"lima",
		"enam",
		"tujuh",
		"delapan",
		"sembilan",
		"sepuluh",
		"sebelas");

		if($number<12)
		{
			$words = " ";
		}
		else if($number<20)
		{
			$words = to_word($number-10)." belas";
		}
		else if($number<100)
		{
			$words = to_word($number/10)." puluh ".to_word($number%10);
		}
		else if($number<200)
		{
			$words = "seratus ".to_word($number-100);
		}
		else if($number<1000)
		{
			$words = to_word($number/100)." ratus ".to_word($number%100);
		}
		else if($number<2000)
		{
			$words = "seribu ".to_word($number-1000);
		}
		else if($number<1000000)
		{
			$words = to_word($number/1000)." ribu ".to_word($number%1000);
		}
		else if($number<1000000000)
		{
			$words = to_word($number/1000000)." juta ".to_word($number%1000000);
		}
		else
		{
			$words = "undefined";
		}
		return $words;
	}

	function comma($number)
	{
		$after_comma = stristr($number,',');
		$arr_number = array(
		"nol",
		"satu",
		"dua",
		"tiga",
		"empat",
		"lima",
		"enam",
		"tujuh",
		"delapan",
		"sembilan");

		$results = "";
		$length = strlen($after_comma);
		$i = 1;
		while($i<$length)
		{
			$get = substr($after_comma,$i,1);
			$results .= " ".$arr_number[$get];
			$i++;
		}
		return $results;
	}
	function terbilang($numb){  
		$huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");  
		if($numb < 12)  
			return " " . $huruf[$numb];
		elseif ($numb < 20)  
			return terbilang($numb - 10) . " belas";  
		elseif ($numb < 100)  
			return terbilang($numb / 10) . " puluh" . terbilang($numb % 10);  
		elseif ($numb < 200)  
			return " seratus" . terbilang($numb - 100);  
		elseif ($numb < 1000) 
			return terbilang($numb / 100) . " ratus" . terbilang($numb % 100);  
		elseif ($numb < 2000)  
			return " seribu" . terbilang($numb - 1000);   
		elseif ($numb < 1000000)  
			return terbilang($numb / 1000) . " ribu" . terbilang($numb % 1000);   
		elseif ($numb < 1000000000)  
			return terbilang($numb / 1000000) . " juta" . terbilang($numb % 1000000);   
		elseif ($numb >= 1000000000)  
			return false;  
	}
    function getBulan($bln){
                switch ($bln){
                    case 1: 
                        return "Januari";
                        break;
                    case 2:
                        return "Februari";
                        break;
                    case 3:
                        return "Maret";
                        break;
                    case 4:
                        return "April";
                        break;
                    case 5:
                        return "Mei";
                        break;
                    case 6:
                        return "Juni";
                        break;
                    case 7:
                        return "Juli";
                        break;
                    case 8:
                        return "Agustus";
                        break;
                    case 9:
                        return "September";
                        break;
                    case 10:
                        return "Oktober";
                        break;
                    case 11:
                        return "November";
                        break;
                    case 12:
                        return "Desember";
                        break;
                }
            } 

function cek_terakhir($datetime, $full = false) {
		$today = time();    
                 $createdday= strtotime($datetime); 
                 $datediff = abs($today - $createdday);  
                 $difftext="";  
                 $years = floor($datediff / (365*60*60*24));  
                 $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
                 $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
                 $hours= floor($datediff/3600);  
                 $minutes= floor($datediff/60);  
                 $seconds= floor($datediff);  
                 //year checker  
                 if($difftext=="")  
                 {  
                   if($years>1)  
                    $difftext=$years." Tahun";  
                   elseif($years==1)  
                    $difftext=$years." Tahun";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($months>1)  
                    $difftext=$months." Bulan";  
                    elseif($months==1)  
                    $difftext=$months." Bulan";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($days>1)  
                    $difftext=$days." Hari";  
                    elseif($days==1)  
                    $difftext=$days." Hari";  
                 }  
                 //hour checker  
                 if($difftext=="")  
                 {  
                    if($hours>1)  
                    $difftext=$hours." Jam";  
                    elseif($hours==1)  
                    $difftext=$hours." Jam";  
                 }  
                 //minutes checker  
                 if($difftext=="")  
                 {  
                    if($minutes>1)  
                    $difftext=$minutes." Menit";  
                    elseif($minutes==1)  
                    $difftext=$minutes." Menit";  
                 }  
                 //seconds checker  
                 if($difftext=="")  
                 {  
                    if($seconds>1)  
                    $difftext=$seconds." Detik";  
                    elseif($seconds==1)  
                    $difftext=$seconds." Detik";  
                 }  
                 return $difftext;  
	}
	function check_remote_file_exists($url){

		$ch = curl_init($url);
		
		curl_setopt($ch, CURLOPT_NOBODY, true);
		curl_exec($ch);
		$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		// $retcode >= 400 -> not found, $retcode = 200, found.
		curl_close($ch);
     return $retcode;
}

function decode_komdanas_json($local){
	define( 'DS', DIRECTORY_SEPARATOR);
	$remote 		="http://komdanas.mahkamahagung.go.id/jsons/".str_replace("jsons","",str_replace("..".DS,"",$local));
	$local_checksum = explode(".json",$local);
	$local_checksum = $local_checksum[0]."checksum.json";
	
	$remote_checksum ="http://komdanas.mahkamahagung.go.id/jsons/".str_replace("jsons".DS,"",str_replace("..".DS,"",$local_checksum));  
	$copy=0; 
	 
	if(file_exists($local_checksum))
	{
		$content				=file_get_contents($local_checksum);
		$local_cheksum_value	=json_decode($content,true);  
	}
	else
	{
		$copy=1;
	} 
	if(check_remote_file_exists($remote_checksum)==200)
	{
		$content				=file_get_contents($remote_checksum);
		$remote_cheksum_value	=json_decode($content,true);  
	}
	else
	{
		$copy=0;
	}
	
	if( 
	($remote_cheksum_value[0]['checksum']!="")and (!is_null($remote_cheksum_value[0]['checksum'])) 
	and 
	($local_cheksum_value[0]['checksum']!="") and (!is_null($local_cheksum_value[0]['checksum'])) 
	and 
	($local_cheksum_value[0]['checksum']!=$remote_cheksum_value[0]['checksum'])
	)
	{
		$copy=1;
	}
	elseif(!is_array($local_cheksum_value))
	{
		$copy=1;
	}
	 
	if($copy==1)
	{
		if ( copy($remote_checksum, $local_checksum) ) {}
		
		if ( copy($remote, $local) ) {
			  $content=file_get_contents($local);
			  if(($content === FALSE)and (!is_array($content)))
			  {
				  copy($remote_checksum, $local_checksum);
				  copy($remote, $local);
				  $content=file_get_contents($local);
				  return json_decode($content,true); 
			  }
			  else
			  {
				 return json_decode($content,true);  
			  }
		}
		else
		{
			$content=file_get_contents($remote);
			  if(($content === FALSE)and (!is_array($content)))
			  {
				  copy($remote_checksum, $local_checksum);
				  copy($remote, $local);
				  $content=file_get_contents($local);
				  return json_decode($content,true); 
			  }
			  else
			  {
				 return json_decode($content,true);  
			  }
			
		}
		
	}
	else
	{
		$content=file_get_contents($local);
			  if(($content === FALSE)and (!is_array($content)))
			  {
				  copy($remote_checksum, $local_checksum);
				  copy($remote, $local);
				  $content=file_get_contents($local); 
				  return json_decode($content,true); 
			  }
			  else
			  {  
				 return json_decode($content,true);  
			  }
	}
}
function decode_json($local){
	 
		$content=file_get_contents($local); 
		return json_decode($content,true);   
}