<?php
class Muser extends CI_Model{

	//Tambah data 
	function simpan()  
	{
		date_default_timezone_set('Asia/Jakarta');$tahun = date("Y");
		$time = date("Y-m-d h:i:s");
		$nama=$this->input->post('nama');
		$email=$this->input->post('email');
		$user_id=$nama.$email;
		$pengacak = 'ABCDEFGHIJKLMNOPQRSTU1234567890';//Pengacak User ID
		$user_id = "";
		$panjang = 8; 
		for($i=0;$i<$panjang;$i++) {
			$user_id .= $pengacak[rand(0, 30)];
		}
		
		$simpan_data=array(
		'namadepan'   		=>$this->input->post('namadepan'),
		'nama'   			=>$this->input->post('nama'),
		'email'   			=>$this->input->post('email'),
		'pass'   			=>md5($this->input->post('pass')),
		'telp'   			=>$this->input->post('telp'),
		'level'   			=>1,
		'tgldaftar'   		=>$time,
		'kodeact'			=>$user_id
	   );
		$this->db->insert('user', $simpan_data);
			$telp=$this->input->post('telp');
			
			$sms=' KODE : '.$user_id.' = adalah kode verifikasi pendaftaran anda di Aplikasi Laporan Pengaduan LPMP DIY ';  
			$nohp=$telp;
			$modem='Modem-1';//Nama Modem di Sms Center
			$tipe='normal';
			
			$this->kirimSMS($nohp, $sms,$modem,$tipe);//Ke Model kirim SMS
			
			$nama=$this->input->post('nama');
			$email=$this->input->post('email');
			$pass=$this->input->post('pass');
			
			$subject= 'Pendaftaran anda telah Sukses !! ';
			$nm_tujuan= 'Yth. '.$nama.'';
			$isi='Username : '.$email.'    Password : '.$pass.'   Kode Verifikasi : '.$user_id.' ';
			$email_tujuan=$email;
			//$link_file='tes.txt';
			
			$this->kirimEMAIL($subject,$nm_tujuan,$isi,$email_tujuan);//Ke Model kirim Email
	}
	
	function kirimSMS($nohp, $sms, $modem, $tipe)
		{
			// ----------------------------------------	
			$url = "";
			
			// ----------------------------------------
			
			$url .= "/smsapi.php";
			$curlHandle = curl_init();
			curl_setopt($curlHandle, CURLOPT_URL, $url);
			curl_setopt($curlHandle, CURLOPT_POSTFIELDS, "nohp=".$nohp."&pesan=".urlencode($sms)."&modem=".$modem."&type=".$tipe);
			curl_setopt($curlHandle, CURLOPT_HEADER, 0);
			curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
			curl_setopt($curlHandle, CURLOPT_POST, 1);
			curl_exec($curlHandle);
			curl_close($curlHandle);
		}
	
	//Konfigurasi  untuk gmail 
	
	function kirimEMAIL($subject,$nm_tujuan,$isi,$email_tujuan)
	{
		require_once(APPPATH.'libraries/PHPMailerAutoload.php');//letak library di CI
		$mail = new PHPMailer();
		$mail->IsSMTP(); // we are going to use SMTP
		$mail->SMTPAuth   = true; // enabled SMTP authentication
		$mail->SMTPSecure = "ssl";  // prefix for secure protocol to connect to the server
		$mail->Host       = "ssl://smtp.gmail.com";      // setting GMail as our SMTP server
		$mail->Port       = 465;                   // SMTP port to connect to GMail
		$mail->Username   = "notifikasi.iapi.diy@gmail.com";  // user email address 
		$mail->Password   = "iapidiy2020";            // password in GMail
		$mail->SetFrom('notifikasi.iapi.diy@gmail.com', ''.$subject.'');  //Who is sending the email
		$mail->AddReplyTo("notifikasi.iapi.diy@gmail.com","NO REPLAY !!");  //email address that receives the response
		$mail->Subject    = $subject;
		$mail->Body      = "".$nm_tujuan."
		".$isi." ";
		$mail->AltBody    = "";
		$destino = "".$email_tujuan.""; //diisi dengan alamat tujuan diambil dari parameter
		$mail->AddAddress($destino, "");
			
			//$path=$_SERVER["DOCUMENT_ROOT"];
			//$file=$path."/pengaduan/file/".$link_file."";//alamat letak file
			
			//$mail->AddAttachment($file);      // some attached files
			//$mail->AddAttachment(""); // as many as you want
			$mail->Send(); 
	}	
	
	
	function status_user ()
	 {
		$kodeact=$this->input->post('kodeact');
		
		$data = array(
		'sts'=>1
		);
		$this->db->where('kodeact',$kodeact);
        $this->db->update('user', $data);
	 
	}	
	
	function t_profil()
    { 	
		//Ambil Session id_user pegawai
		$id_user=$this->session->userdata('id_user');
		
		$this->db->select('*');
		//$this->db->join('tbl_jabatan','tbl_jabatan.id_jabatan=tbl_peg.id_jabatan');
		//$this->db->join('tbl_level','tbl_level.id_level=tbl_peg.id_level');
		//$this->db->join('tbl_unit','tbl_unit.id_unit=tbl_peg.id_unit');
		//$this->db->join('tbl_pagol','tbl_pagol.id_pagol=tbl_peg.id_pagol');
		$this->db->where('user.id_user', $id_user);
		$query = $this->db->get('user');
        return $query->result();
	}
	
	//============================================================================
	
	
	
	
	
	
	
	
	
	
	 //Query untuk Ubah data
	function ambil($id) 
	{   
		$this->db->select('*');
		//$this->db->join('m_tbl_golru','m_tbl_golru.id_gol = tbl_peserta.pagol','left');
		//$this->db->join('m_tbl_kab','m_tbl_kab.id_kab = tbl_peserta.kab','left');
		//$this->db->join('m_tbl_kec','m_tbl_kec.id_kec = tbl_peserta.kec','left');
		//$this->db->join('m_tbl_kec','m_tbl_kec.id_kec = tbl_peserta.kec','left');
		$this->db->where('tbl_peserta.id', $id);
		$query = $this->db->get('tbl_peserta');
		return $query->row();
	}
	
	
	//Query Simpan edit tanpa foto
	function ed_simpan($id) //Simpan saat edit tanpa file upload
	{	
		$file = $_POST['pwd'];
		if (!empty($file)) { //Jika password diubah
		$simpan_data=array(	
				'email'   			=>$this->input->post('email'),
				'nm_peserta'   		=>$this->input->post('nm_peserta'),
				'nuptk'   			=>$this->input->post('nuptk'),
				'nip'   			=>$this->input->post('nip'),
				'npwp'   			=>$this->input->post('npwp'),
				'nik'   			=>$this->input->post('nik'),
				'tmp_lahir'   		=>$this->input->post('tmp_lahir'),
				'tgl_lahir'   		=>$this->input->post('tgl_lahir'),
				'jekel'   			=>$this->input->post('jekel'),
				'agama'   			=>$this->input->post('agama'),
				'sts_kawin'   		=>$this->input->post('sts_kawin'),
				'alamat_rmh'   		=>$this->input->post('alamat_rmh'),
				'kec'   			=>$this->input->post('kec'),
				'kab'   			=>$this->input->post('kab'),
				'telp'   			=>$this->input->post('telp'),
				'kd_pos'   			=>$this->input->post('kd_pos'),
				'pendidikan'   		=>$this->input->post('pendidikan'),
				'jur'   			=>$this->input->post('jur'),
				'th_lulus'   		=>$this->input->post('th_lulus'),
				'lemb_pend'   		=>$this->input->post('lemb_pend'),
				'sts_peg'   		=>$this->input->post('sts_peg'),
				'pagol'   			=>$this->input->post('pagol'),
				'jabatan'   		=>$this->input->post('jabatan'),
				'unit_kerja'   		=>$this->input->post('unit_kerja'),
				'alamat_unit'   	=>$this->input->post('alamat_unit'),
				'kec_unit'   		=>$this->input->post('kec_unit'),
				'kab_unit'   		=>$this->input->post('kab_unit'),
				'telp_unit'   		=>$this->input->post('telp_unit'),
				'kd_pos_unit'   	=>$this->input->post('kd_pos_unit'),
				'ms_kerja_th'   	=>$this->input->post('ms_kerja_th'),
				'ms_kerja_bln'   	=>$this->input->post('ms_kerja_bln'),
				'ms_kerja_sk_th'   	=>$this->input->post('ms_kerja_sk_th'),
				'ms_kerja_sk_bln'   =>$this->input->post('ms_kerja_sk_bln'),
				'bid_studi_ajar'   	=>$this->input->post('bid_studi_ajar'),
				'jml_jam'   		=>$this->input->post('jml_jam'),
				'pwd'   			=>md5($this->input->post('pwd')),
				'user_id'   		=>$this->input->post('user_id'),
				'level'   			=>2
		);
		$this->db->where('tbl_peserta.id', $id);
		$this->db->update('tbl_peserta', $simpan_data);
		}else{
		$simpan_data=array(	//Jika password tidak diubah		
				'email'   			=>$this->input->post('email'),
				'nm_peserta'   		=>$this->input->post('nm_peserta'),
				'nuptk'   			=>$this->input->post('nuptk'),
				'nip'   			=>$this->input->post('nip'),
				'npwp'   			=>$this->input->post('npwp'),
				'nik'   			=>$this->input->post('nik'),
				'tmp_lahir'   		=>$this->input->post('tmp_lahir'),
				'tgl_lahir'   		=>$this->input->post('tgl_lahir'),
				'jekel'   			=>$this->input->post('jekel'),
				'agama'   			=>$this->input->post('agama'),
				'sts_kawin'   		=>$this->input->post('sts_kawin'),
				'alamat_rmh'   		=>$this->input->post('alamat_rmh'),
				'kec'   			=>$this->input->post('kec'),
				'kab'   			=>$this->input->post('kab'),
				'telp'   			=>$this->input->post('telp'),
				'kd_pos'   			=>$this->input->post('kd_pos'),
				'pendidikan'   		=>$this->input->post('pendidikan'),
				'jur'   			=>$this->input->post('jur'),
				'th_lulus'   		=>$this->input->post('th_lulus'),
				'lemb_pend'   		=>$this->input->post('lemb_pend'),
				'sts_peg'   		=>$this->input->post('sts_peg'),
				'pagol'   			=>$this->input->post('pagol'),
				'jabatan'   		=>$this->input->post('jabatan'),
				'unit_kerja'   		=>$this->input->post('unit_kerja'),
				'alamat_unit'   	=>$this->input->post('alamat_unit'),
				'kec_unit'   		=>$this->input->post('kec_unit'),
				'kab_unit'   		=>$this->input->post('kab_unit'),
				'telp_unit'   		=>$this->input->post('telp_unit'),
				'kd_pos_unit'   	=>$this->input->post('kd_pos_unit'),
				'ms_kerja_th'   	=>$this->input->post('ms_kerja_th'),
				'ms_kerja_bln'   	=>$this->input->post('ms_kerja_bln'),
				'ms_kerja_sk_th'   	=>$this->input->post('ms_kerja_sk_th'),
				'ms_kerja_sk_bln'   =>$this->input->post('ms_kerja_sk_bln'),
				'bid_studi_ajar'   	=>$this->input->post('bid_studi_ajar'),
				'jml_jam'   		=>$this->input->post('jml_jam'),
				//'pwd'   			=>md5($this->input->post('pwd')),
				'user_id'   		=>$this->input->post('user_id'),
				'level'   			=>2
		);
		$this->db->where('tbl_peserta.id', $id);
		$this->db->update('tbl_peserta', $simpan_data);
		}
	}
	//Query Simpan edit dengan foto
	function ed_simpan_ada($id,$userfile) //Simpan saat edit dengan file upload
	{
		$file = $_POST['pwd'];
		if (!empty($file)) {//Jika password diubah
		$simpan_data=array(			
				'email'   			=>$this->input->post('email'),
				'nm_peserta'   		=>$this->input->post('nm_peserta'),
				'nuptk'   			=>$this->input->post('nuptk'),
				'nip'   			=>$this->input->post('nip'),
				'npwp'   			=>$this->input->post('npwp'),
				'nik'   			=>$this->input->post('nik'),
				'tmp_lahir'   		=>$this->input->post('tmp_lahir'),
				'tgl_lahir'   		=>$this->input->post('tgl_lahir'),
				'jekel'   			=>$this->input->post('jekel'),
				'agama'   			=>$this->input->post('agama'),
				'sts_kawin'   		=>$this->input->post('sts_kawin'),
				'alamat_rmh'   		=>$this->input->post('alamat_rmh'),
				'kec'   			=>$this->input->post('kec'),
				'kab'   			=>$this->input->post('kab'),
				'telp'   			=>$this->input->post('telp'),
				'kd_pos'   			=>$this->input->post('kd_pos'),
				'pendidikan'   		=>$this->input->post('pendidikan'),
				'jur'   			=>$this->input->post('jur'),
				'th_lulus'   		=>$this->input->post('th_lulus'),
				'lemb_pend'   		=>$this->input->post('lemb_pend'),
				'sts_peg'   		=>$this->input->post('sts_peg'),
				'pagol'   			=>$this->input->post('pagol'),
				'jabatan'   		=>$this->input->post('jabatan'),
				'unit_kerja'   		=>$this->input->post('unit_kerja'),
				'alamat_unit'   	=>$this->input->post('alamat_unit'),
				'kec_unit'   		=>$this->input->post('kec_unit'),
				'kab_unit'   		=>$this->input->post('kab_unit'),
				'telp_unit'   		=>$this->input->post('telp_unit'),
				'kd_pos_unit'   	=>$this->input->post('kd_pos_unit'),
				'ms_kerja_th'   	=>$this->input->post('ms_kerja_th'),
				'ms_kerja_bln'   	=>$this->input->post('ms_kerja_bln'),
				'ms_kerja_sk_th'   	=>$this->input->post('ms_kerja_sk_th'),
				'ms_kerja_sk_bln'   =>$this->input->post('ms_kerja_sk_bln'),
				'bid_studi_ajar'   	=>$this->input->post('bid_studi_ajar'),
				'jml_jam'   		=>$this->input->post('jml_jam'),
				'pwd'   			=>md5($this->input->post('pwd')),
				'user_id'   		=>$this->input->post('user_id'),
				'level'   			=>2,
				'foto'   			=>$userfile
		);
		$this->db->where('tbl_peserta.id', $id);
		$this->db->update('tbl_peserta', $simpan_data);
		}else{
		$simpan_data=array(	//Jika password tidak diubah		
				'email'   			=>$this->input->post('email'),
				'nm_peserta'   		=>$this->input->post('nm_peserta'),
				'nuptk'   			=>$this->input->post('nuptk'),
				'nip'   			=>$this->input->post('nip'),
				'npwp'   			=>$this->input->post('npwp'),
				'nik'   			=>$this->input->post('nik'),
				'tmp_lahir'   		=>$this->input->post('tmp_lahir'),
				'tgl_lahir'   		=>$this->input->post('tgl_lahir'),
				'jekel'   			=>$this->input->post('jekel'),
				'agama'   			=>$this->input->post('agama'),
				'sts_kawin'   		=>$this->input->post('sts_kawin'),
				'alamat_rmh'   		=>$this->input->post('alamat_rmh'),
				'kec'   			=>$this->input->post('kec'),
				'kab'   			=>$this->input->post('kab'),
				'telp'   			=>$this->input->post('telp'),
				'kd_pos'   			=>$this->input->post('kd_pos'),
				'pendidikan'   		=>$this->input->post('pendidikan'),
				'jur'   			=>$this->input->post('jur'),
				'th_lulus'   		=>$this->input->post('th_lulus'),
				'lemb_pend'   		=>$this->input->post('lemb_pend'),
				'sts_peg'   		=>$this->input->post('sts_peg'),
				'pagol'   			=>$this->input->post('pagol'),
				'jabatan'   		=>$this->input->post('jabatan'),
				'unit_kerja'   		=>$this->input->post('unit_kerja'),
				'alamat_unit'   	=>$this->input->post('alamat_unit'),
				'kec_unit'   		=>$this->input->post('kec_unit'),
				'kab_unit'   		=>$this->input->post('kab_unit'),
				'telp_unit'   		=>$this->input->post('telp_unit'),
				'kd_pos_unit'   	=>$this->input->post('kd_pos_unit'),
				'ms_kerja_th'   	=>$this->input->post('ms_kerja_th'),
				'ms_kerja_bln'   	=>$this->input->post('ms_kerja_bln'),
				'ms_kerja_sk_th'   	=>$this->input->post('ms_kerja_sk_th'),
				'ms_kerja_sk_bln'   =>$this->input->post('ms_kerja_sk_bln'),
				'bid_studi_ajar'   	=>$this->input->post('bid_studi_ajar'),
				'jml_jam'   		=>$this->input->post('jml_jam'),
				//'pwd'   			=>md5($this->input->post('pwd')),
				'user_id'   		=>$this->input->post('user_id'),
				'level'   			=>2,
				'foto'   			=>$userfile
		);
		$this->db->where('tbl_peserta.id', $id);
		$this->db->update('tbl_peserta', $simpan_data);
		}
	}

			
}