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

class fileupload_c extends CI_Controller {
	function index(){
        if($this->session->userdata('is_logged_in')===FALSE){
        redirect('login'); }

		//Cara Penggunaan :
		/*<a href="#" onClick="popup_form('<?php echo base_url("fileupload/".$enc."/putsela/".$encputsela."/".$urutan);?>')">Edit E-Doc</a>*/
		/*<a href="#" onClick="popup_form('<?php echo base_url("fileupload/".$enc."/put");?>')">Edit E-Doc</a>*/
		//----------------------------------------------------
		$data['enc'] = $this->nativesession->get_flash_session('enc');
		$data['from'] = $this->nativesession->get_flash_session('fileupfrom');
		$data['enc1'] = $this->nativesession->get_flash_session('encputsela');		
		if ($data['from']=="putsela" || $data['from']=='bas' || $data['from']=='relaas'|| $data['from']=='persiapan_proses'){
			$data['urutan'] = $this->nativesession->get_flash_session('urutan');
		}
		elseif ($data['from']=="put" || $data['from']=="tuntut" || $data['from']=="datum" || $data['from']=="datumpdt" || $data['from']=="mediasi" || $data['from']=="persiapan" ||$data['from']=="perbaikangugatan" || $data['from']=='penikrar' || $data['from']=='aktacerai'|| $data['from']=='verzet'|| $data['from']=='keberatan'){
			$data['enc1']=$data['enc'];
			$data['urutan']="";
		}

		$this->load->model('fileupload/fileupload_m');
		if($data['from']=='relaas'){
			$data['namadoktersimpan']=$this->fileupload_m->get_nama_dok($this->encrypt->decode(base64_decode($data['enc'])),$data['from'],$this->encrypt->decode(base64_decode($data['enc1'])),$data['urutan']);		
		}else{
			$data['namadoktersimpan']=$this->fileupload_m->get_nama_dok($this->encrypt->decode(base64_decode($data['enc1'])),$data['from']);			
		}
		
		$this->load->vars($data);
		$this->load->view('fileupload/fileupload_v');
	}
	// fungsi untuk menerima kiriman file yang diupload
	function validatePost(){
			$this->load->model('fileupload/fileupload_m');

			$idperkara=$this->encrypt->decode(base64_decode($this->input->post('enc')));
			$from=$this->input->post('from');
			if ($from=="putsela"){
				$id1=$this->encrypt->decode(base64_decode($this->input->post('enc1')));
				$urutan=$this->input->post('urutan');
				$pengenal="putusan_sela_".$urutan;
				$columnname="amar_sela_dok";
			}
			elseif ($from=="bas"){
				$id1=$this->encrypt->decode(base64_decode($this->input->post('enc1')));
				$urutan=$this->input->post('urutan');
				$pengenal="bas_".$urutan;
				$columnname="edoc_bas";
			}
			elseif ($from=="relaas"){
				$idperkara=$this->encrypt->decode(base64_decode($this->input->post('enc')));
				$idjadwal=$this->encrypt->decode(base64_decode($this->input->post('enc1')));
				$pihak=$this->input->post('urutan');
				$pengenal="relaas_".$idperkara."_".$idjadwal."_".$pihak;
				$columnname="doc_relaas";
			}			
			elseif ($from=="put"){
				$id1=$idperkara;
				$pengenal="putusan_akhir";
				$columnname="amar_putusan_dok";
			}
			elseif ($from=="tuntut"){
				$id1=$idperkara;
				$pengenal="penuntutan";
				$columnname="isi_penuntutan_dok";
			}
			elseif ($from=="datum" || $from=="datumpdt"){
				$id1=$idperkara;
				if ($from=="datum"){
					$columnname="dakwaan_dok";
					$pengenal="dakwaan";
				}else{
					$columnname="petitum_dok";
					$pengenal="petitum";
				}
			}
			elseif ($from=="mediasi"){
				$id1=$this->encrypt->decode(base64_decode($this->input->post('enc1')));
				$pengenal="akta_perdamaian";
				$columnname="isi_akta_perdamaian_dok";
			}			
			elseif ($from=="persiapan"){
				$id1=$idperkara;
				$pengenal="perbaikan";
				$columnname="petitum_dok";
			}
			elseif ($from=="verzet"){
				$id1=$idperkara;
				$pengenal="verzet";
				$columnname="amar_putusan_verzet_dok";
			}
			elseif ($from=="perbaikangugatan"){
				$id1=$idperkara;
				$pengenal="perbaikan_gugatan";
				$columnname="petitum_dok";
			}
			elseif($from=="penikrar"){
				$id1=$idperkara;
				$pengenal="penetapan_ikrar";
				$columnname="ikrar_talak_dok";
			}
			elseif($from=="aktacerai"){
				$id1=$idperkara;
				$pengenal="aktacerai";
				$columnname="akta_cerai_dok";
			}
			elseif ($from=="keberatan"){
				$id1=$idperkara;
				$pengenal="keberatan";
				$columnname="amar_putusan_keberatan_dok";
			}
			elseif ($from=="persiapan_proses"){
				$id1=$this->encrypt->decode(base64_decode($this->input->post('enc1')));
				$urutan=$this->input->post('urutan');
				$pengenal="persiapan_proses_".$urutan;
				$columnname="edoc_persiapan_proses";
			}


			if(empty($idperkara) OR intval($idperkara)<1){
				echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Penyimpanan Dokumen Elektronik (E-Doc)Tidak Berhasil.'));
				return;
			}
			$datautama=$this->fileupload_m->get_detil_data_utama($idperkara,$from);
			foreach ($datautama as $datautama)
			{
				$no_perkara=$datautama->nomor_perkara;
				$tgldaftar=$datautama->tanggal_pendaftaran;
			}
			if($from=='relaas'){
				$namadokumen=$this->fileupload_m->get_nama_dok($idperkara,$from,$idjadwal,$pihak);
			}else{
				$namadokumen=$this->fileupload_m->get_nama_dok($id1,$from);	
			}
			

			$jumslash=substr_count($namadokumen,"/");
			if(!empty($namadokumen)){
				//check file and directory to delete
				$splittgldaftar=explode("-",$tgldaftar);
				$direktori="resources/file/doc/".$splittgldaftar[0]."/".$splittgldaftar[1];
				$parentdirektori="resources/file/doc/".$splittgldaftar[0];
				if($jumslash!=5){
					$filenya=$direktori."/".$namadokumen;
				}else{
					$filenya=$namadokumen;
				}
				//delete the file
				@unlink($filenya);
				@rmdir($direktori); 
				@rmdir($parentdirektori); 
			}

			//===== create file directory in not exist
			$splittgldaftar=explode("-",$tgldaftar);
			$direktori="resources/file/doc/".$splittgldaftar[0]."/".$splittgldaftar[1];
			@mkdir($direktori,0777,true);

			$splitnoperkara=explode("/",$no_perkara);

			$namafile=$_FILES['fileform']['name'];
			$extension=pathinfo($namafile);
			$extension=$extension['extension'];

			$fileName=$splitnoperkara[3]."_".$splitnoperkara[2]."_".$splitnoperkara[1]."_".$splitnoperkara[0]."_".$pengenal.".".$extension;
			$fileName=str_replace(' ','_',$fileName);
				
			$fileTmpName=$_FILES['fileform']['tmp_name'];
			$uploadpath=$direktori."/".$fileName;
			
		if (@move_uploaded_file($fileTmpName, $uploadpath)) {
				$datainsert=array(
					$columnname=>$uploadpath,
				);
			if($from=='relaas'){
				$this->fileupload_m->update_data($idperkara,$datainsert,$from,$idjadwal,$pihak);
			}else{
				$this->fileupload_m->update_data($id1,$datainsert,$from);	
			}
			

		    echo json_encode(array('st'=>1,'msg'=>'Penyimpanan Dokumen Elektronik (E-Doc) Berhasil.'));
			return;
		} else {
		    echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Penyimpanan Dokumen Elektronik (E-Doc)Tidak Berhasil.'));
			return;
		}
	}
	//============================================================
	function dummy(){
		    echo json_encode(array('st'=>1,'msg'=>'Penyimpanan Dokumen Elektronik (E-Doc) Berhasil.'));
			return;
	}

	function validateDocDelete()
	{
        if($this->nativesession->is_flash_session('security')===FALSE){
        redirect('main'); }

        if($this->session->userdata('is_logged_in')===FALSE){
        redirect('login'); }

		if(empty($_POST)){
			show_404();
		}

		if($this->session->userdata('is_logged_in')===FALSE){
			echo json_encode(array('st'=>0,'msg'=>'Anda Belum Login, Silahkan Login Terlebih Dahulu.'));
			return;
		}

		$this->load->model('fileupload/fileupload_m');

		$idperkara = $this->encrypt->decode(base64_decode($this->uri->segment(3)));
        if(!is_numeric($idperkara) OR !intval($idperkara)>0){
            show_404();
            exit();
        }

		$from=$this->uri->segment(4);
		if ($from=="putsela"||$from=="bas"||$from=="persiapan_proses"){
				$idkirim = $this->encrypt->decode(base64_decode($this->uri->segment(5)));
		}
		elseif ($from=="put" || $from=="tuntut" || $from=="datum" || $from=="datumpdt" || $from=="mediasi" || $from=="persiapan" || $from=="perbaikangugatan" || $from=="penikrar" || $from=="aktacerai" || $from=="verzet" || $from=="keberatan"){
				$idkirim = $idperkara;
		}

		if($from=='relaas'){
			$idperkara=$this->encrypt->decode(base64_decode($this->input->post('enc')));
			$idjadwal=$this->encrypt->decode(base64_decode($this->input->post('enc1')));
			$pihak=$this->input->post('urutan');
			$namadokumen=$this->fileupload_m->get_nama_dok($idperkara,$from,$idjadwal,$pihak);
		}else{
			$namadokumen=$this->fileupload_m->get_nama_dok($idkirim,$from);	
		}
		

		//check file and directory to delete
		$arrdirectory=explode('/',$namadokumen);
		array_pop($arrdirectory);
		$direktori=implode("/",$arrdirectory);
		$arrparentdirectory=explode('/',$direktori);
		array_pop($arrparentdirectory);
		$parentdirektori=implode("/",$arrparentdirectory);
		
		//delete the file
		@unlink($namadokumen);
		//delete the directories if empty. if at least one file inside, this script is passed automaticaly
		@rmdir($direktori); 
		@rmdir($parentdirektori); 

		if($from=='relaas'){
			$result=$this->fileupload_m->hapus_data($idperkara,$from,$idjadwal,$pihak);
		}else{
			$result=$this->fileupload_m->hapus_data($idkirim,$from);
		}
		if($result){
			echo json_encode(array('st'=>1,'msg'=>'Penghapusan Dokumen Elektronik (E-Doc) Berhasil'));
			return;
		}else{
			echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Penghapusan Dokumen Elektronik (E-Doc)Tidak Berhasil'));
			return;
		}
	}
}
/* End of file fileupload_c.php */
/* Location: ./application/controllers/fileupload_c.php */