<?php
class App_model extends CI_Model{

  function get_identitas(){
		return $this->db->query("SELECT * FROM identitas");
  }
  function get_all_produk() {
        $this->datatables->select('nomor_perkara,tanggal_surat,pihak1_text,pihak2_text,petitum,tanggal_pendaftaran');
        $this->datatables->from('perkara');
        $this->datatables->join('jenis_perkara', 'id=jenis_perkara_id');
        $this->datatables->add_column('view', '<a href="javascript:void(0);" class="edit_record btn btn-info btn-xs" data-kode="$1" data-nama="$2" data-harga="$3" data-kategori="$4">Edit</a>  <a href="javascript:void(0);" class="hapus_record btn btn-danger btn-xs" data-kode="$1">Hapus</a>','nomor_perkara,tanggal_pendaftaran,pihak1_text,pihak2_text,petitum');
        return $this->datatables->generate();
  }
  //select table by komar
	public function edit($table, $data){
        return $this->db->get_where($table, $data);
    }
    public function view_where($table,$data){
        $this->db->where($data);
        return $this->db->get($table);
    }
	public function getSelectedData($table,$data)
	{
		return $this->db->get_where($table, $data);
	}
	//update table by komar
	public function updateData($table, $data, $where){
        return $this->db->update($table, $data, $where); 
    }
	//Hapus table by komar
	function deleteData($table,$data)
	{
		$this->db->delete($table,$data);
	}
	//Tambah data table by komar
	function insertData($table,$data)
	{
		$this->db->insert($table,$data);
	}
	//query manual Mahasiswa
	function list_mahasiswa(){
        return $this->db->query("SELECT A.mahasiswa_id,A.nim,A.stambuk,A.nama_lengkap,A.alamat,A.semester,A.ruangan_id,A.semester_aktif,A.tanggal_lahir,A.status,
									B.keterangan AS angkatan,
									C.keterangan AS agama,
									D.nama_ruangan,
									E.nama_lengkap AS nama_dosen
								FROM tb_mahasiswa A
								LEFT JOIN tb_tahun_akademik B ON A.tahun_akademik_id=B.tahun_akademik_id
								LEFT JOIN tb_agama C ON A.agama_id=C.agama_id
								LEFT JOIN tb_ruangan D ON A.ruangan_id=D.ruangan_id
								LEFT JOIN tb_dosen E ON A.dosen_wali_id=E.dosen_id
								ORDER BY A.stambuk DESC,A.ruangan_id ASC");
    }
	public function hari_ini($hari){
		date_default_timezone_set('Asia/Jakarta'); // PHP 6 mengharuskan penyebutan timezone.
		$seminggu = array("Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu");
		//$hari = date("w");
		$hari_ini = $seminggu[$hari];
		return $hari_ini;
	}
	
	
	//Konversi tanggal
	public function tgl_sql($date){
		$exp = explode('-',$date);
		if(count($exp) == 3) {
			$date = $exp[2].'-'.$exp[1].'-'.$exp[0];
		}
		return $date;
	}
	public function tgl_str($date){
		$exp = explode('-',$date);
		if(count($exp) == 3) {
			$date = $exp[2].'-'.$exp[1].'-'.$exp[0];
		}
		return $date;
	}

	
	/* Tanggal dan Jam */
	public function Jam_Now(){
		date_default_timezone_set("Asia/Jakarta");
   		$jam = date("H:i:s"); 
   		
		return $jam;
		//echo "$jam WIB";
	}
	
	public function Hari_Bulan_Indo(){
		date_default_timezone_set('Asia/Jakarta'); // PHP 6 mengharuskan penyebutan timezone.
		$seminggu = array("Minggu","Senin","Selasa","Rabu","Kamis","Jum\'at","Sabtu");
		$hari = date("w");
		$hari_ini = $seminggu[$hari];
		
		return $hari_ini;
	}
	
	public function tgl_indo($tanggal){
			date_default_timezone_set('Asia/Jakarta'); // PHP 6 mengharuskan penyebutan timezone.
			$tgl = $tanggal; //date("Y m d");
			$tanggal = substr($tgl,8,2);
			$bulan = $this->app_model->getBulan(substr($tgl,5,2));
			$tahun = substr($tgl,0,4);
			return $tanggal.' '.$bulan.' '.$tahun;		 
	}
	
	public function tgl_now_indo(){
			date_default_timezone_set('Asia/Jakarta'); // PHP 6 mengharuskan penyebutan timezone.
			$tgl = date("Y m d");
			$tanggal = substr($tgl,8,2);
			$bulan = $this->app_model->getBulan(substr($tgl,5,2));
			$tahun = substr($tgl,0,4);
			return $tanggal.' '.$bulan.' '.$tahun;		 
	}	

	public 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;
		}
	} 
	
	/*fungsi terbilang*/
	public function bilang($x) {
		$x = abs($x);
		$angka = array("", "satu", "dua", "tiga", "empat", "lima",
		"enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
		$result = "";
		if ($x <12) {
			$result = " ". $angka[$x];
		} else if ($x <20) {
			$result = $this->app_model->bilang($x - 10). " belas";
		} else if ($x <100) {
			$result = $this->app_model->bilang($x/10)." puluh". $this->app_model->bilang($x % 10);
		} else if ($x <200) {
			$result = " seratus" . $this->app_model->bilang($x - 100);
		} else if ($x <1000) {
			$result = $this->app_model->bilang($x/100) . " ratus" . $this->app_model->bilang($x % 100);
		} else if ($x <2000) {
			$result = " seribu" . $this->app_model->bilang($x - 1000);
		} else if ($x <1000000) {
			$result = $this->app_model->bilang($x/1000) . " ribu" . $this->app_model->bilang($x % 1000);
		} else if ($x <1000000000) {
			$result = $this->app_model->bilang($x/1000000) . " juta" . $this->app_model->bilang($x % 1000000);
		} else if ($x <1000000000000) {
			$result = $this->app_model->bilang($x/1000000000) . " milyar" . $this->app_model->bilang(fmod($x,1000000000));
		} else if ($x <1000000000000000) {
			$result = $this->app_model->bilang($x/1000000000000) . " trilyun" . $this->app_model->bilang(fmod($x,1000000000000));
		}      
			return $result;
	}
	public function terbilang($x, $style=4) {
		if($x<0) {
			$hasil = "minus ". trim($this->app_model->bilang($x));
		} else {
			$hasil = trim($this->app_model->bilang($x));
		}      
		switch ($style) {
			case 1:
				$hasil = strtoupper($hasil);
				break;
			case 2:
				$hasil = strtolower($hasil);
				break;
			case 3:
				$hasil = ucwords($hasil);
				break;
			default:
				$hasil = ucfirst($hasil);
				break;
		}      
		return $hasil;
	}
	function pengunjung(){
		$ip = $_SERVER['REMOTE_ADDR'];
		$tanggal = date("Ymd");
        $waktu   = time();
		$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
		$os_user = $this->agent->platform();
		$browser_user = $this->agent->browser();
		$text = "SELECT * FROM statistik WHERE ip='$ip' AND tanggal='$tanggal'";
		$stat = $this->model_utama->manualQuery($text);
		$row = $stat->num_rows();
		if($row>0){
			$this->model_utama->manualQuery("UPDATE statistik SET hits=hits+1, online='$waktu' WHERE ip='$ip' AND tanggal='$tanggal'");
		}else{
			$this->model_utama->manualQuery("INSERT INTO statistik(ip, tanggal, hits, online, hostname,os, browser) VALUES('$ip','$tanggal','1','$waktu','$hostname','$os_user','$browser_user')");
		}
		//$hasil = $this->app_model->manualQuery("SELECT sum(hits) as total FROM statistik"); 
		$hasil = $this->db->query("SELECT COUNT(hits) as total FROM statistik");
		foreach($hasil->result() as $dt){
			$totalpengunjung=$dt->total;
		}
		
		$hasil = $totalpengunjung;
		
		return $hasil;
	}
	function pengunjung_hari_ini(){
		$tanggal = date("Ymd");
		$data       = $this->db->query("SELECT * FROM statistik WHERE tanggal='$tanggal' GROUP BY ip");
		$hasil = $data->num_rows();
		return $hasil;
	}
	function online(){
		$bataswaktu       = time() - 300;
        $data = $this->db->query("SELECT * FROM statistik WHERE online > '$bataswaktu'");
		$hasil = $data->num_rows();
		return $hasil;
	}
	
	function ip_address(){
		$ip = $_SERVER['REMOTE_ADDR'];
		return $ip;
	}
	
	///proses update
	public function proses_update($server,$app_name,$new_vers){
		$url 		= "$server"."$app_name/$new_vers/update.zip";
		$filename 	= substr($url, strrpos($url, '/') + 1);
		file_put_contents($filename, file_get_contents($url)) or die(pesan());
		$this->load->library('extractor');
		$this->extractor->extract($filename, FCPATH.'/');
		unlink($filename);
		if(file_exists(FCPATH.'/query.sql')){
		$this->sqlquery_update(file_get_contents('query.sql'));
		unlink('query.sql');}
		return 'DIPERBARUI';
	}
	
	function sqlquery_update($sql=''){		
		$sqls = explode(';;', $sql);
		array_pop($sqls);
		foreach($sqls as $statement){
			$statment = $statement . ";";
			$this->db2->query($statement);	
		}		
	}
		
	public function get_info_update($url_update,$app,$log=NULL,$token=NULL){
		$ch = curl_init($url_update);
		curl_setopt( $ch, CURLOPT_POST, 1);
		curl_setopt( $ch, CURLOPT_POSTFIELDS,"app=$app&log=$log&token=$token");
		curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt( $ch, CURLOPT_HEADER, 0);
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
		return curl_exec($ch);
	}

	public function get_http_response_code($url) {
		$ch = curl_init();
		curl_setopt ( $ch, CURLOPT_URL, $url );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
		$http_respond = curl_exec($ch);
		$http_respond = trim( strip_tags( $http_respond ) );
		$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
		if ( ( $http_code == "200" ) || ( $http_code == "302" ) ) {
			return true;}
		else {
			return false;}
		curl_close( $ch );
		}
	public function GetKonfigurasi_pengguna($key=''){
		$query = "select nama_website from identitas";
		return $this->db->query("$query")->row();
	}
}
