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

class Fileupload_putusan_c extends CI_Controller {
	function index(){
        if($this->session->userdata('is_logged_in')===FALSE){
        redirect('login'); }
        $data['from'] = $this->nativesession->get_flash_session('fileupfrom');
		$data['enc'] = $this->nativesession->get_flash_session('enc');
		$this->load->model('fileupload/fileupload_m');
		$data['namadoktersimpan']=$this->fileupload_m->get_nama_dok($this->encrypt->decode(base64_decode($data['enc'])),$data['from']);		
		$dok_anonimisasi=$this->fileupload_m->get_dok_anonimisasi($this->encrypt->decode(base64_decode($data['enc'])));
		if(!empty($dok_anonimisasi)){
			$data['nama_dok_anonimisasi'] = $dok_anonimisasi[0]->filename;
		}else{
			$data['nama_dok_anonimisasi'] = '';
		}

		$this->load->vars($data);
		$this->load->view('fileupload/fileupload_putusan_v');
	}
	// fungsi untuk menerima kiriman file yang diupload
	function validatePost(){
			$idperkara=$this->encrypt->decode(base64_decode($this->input->post('enc')));
			$id=$idperkara;
			$this->load->model('fileupload/fileupload_m');
			$this->load->model('api/convert_insert_m');
			$ref_id = $this->convert_insert_m->get_ref_dirput_putusan($idperkara,99);
			$ref_id1 = $this->convert_insert_m->get_ref_dirput_putusan($idperkara,100);
			$dokumen_ref_id = $ref_id[0]['dokumen_ref_id'];
			$dokumen_ref_id1 = $ref_id1[0]['dokumen_ref_id'];

			//$pengenal="putusan_akhir";
			$columnname="amar_putusan_dok";

			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;
			}
			
			$namadokumen=$this->fileupload_m->get_nama_dok($idperkara,"put");
			//print_r($namadokumen);exit();
			$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;
				}
				if(isset($namafile)){
					//delete the file
					@unlink($filenya);
					@rmdir($direktori); 
					@rmdir($parentdirektori); 	
				}
				
			}

			// $dok_anonimisasi=$this->fileupload_m->get_dok_anonimisasi($this->encrypt->decode(base64_decode($data['enc'])));
			// if(!empty($dok_anonimisasi)){
			// 	$nama_dok_anonimisasi = $dok_anonimisasi[0]->path_filename;
			// }else{
			// 	$nama_dok_anonimisasi = '';
			// }

			// if(!empty($nama_dok_anonimisasi)){
			// 	//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."/".$nama_dok_anonimisasi;
			// 	}else{
			// 		$filenya=$nama_dok_anonimisasi;
			// 	}
			// 	if(isset($namafile1)){
			// 		//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]."_putusan_akhir.".$extension;
			$fileName=str_replace(' ','_',$fileName);
			$fileTmpName=$_FILES['fileform']['tmp_name'];
			$uploadpath=$direktori."/".$fileName;

			$namafile1=$_FILES['fileform1']['name'];
			$extension1=pathinfo($namafile1);
			$extension1=$extension1['extension'];
			$fileName1=$splitnoperkara[3]."_".$splitnoperkara[2]."_".$splitnoperkara[1]."_".$splitnoperkara[0]."_putusan_akhir_Anonymous.".$extension1;
			$fileName1=str_replace(' ','_',$fileName1);
			$fileTmpName1=$_FILES['fileform1']['tmp_name'];
			$uploadpath1=$direktori."/".$fileName1;
			//echo $fileName1;exit();
			$this->load->helper('convert_helper');
			$xxx=false;
			if(isset($namafile)){
				if(move_uploaded_file($fileTmpName, $uploadpath)){
					$datainsert=array(
						$columnname=>$uploadpath,
					);
					$this->fileupload_m->update_data($idperkara,$datainsert,"put");
					
					_convert_insert_put($idperkara,$dokumen_ref_id);
					$xxx = true;
				}
			}
			if(isset($namafile1)){
				if(move_uploaded_file($fileTmpName1, $uploadpath1)){
					$this->load->model('api/dirput_model');
					$this->dirput_model->convert_insert_Annmous($idperkara,$uploadpath1,$dokumen_ref_id1);
			    	if(_convert_insert_put_Annmous($idperkara,$dokumen_ref_id1)){
			    		echo json_encode(array('st'=>1,'msg'=>'Penyimpanan Dokumen Elektronik (E-Doc) Putusan Akhir Berhasil.'));	
			    	}else{
			    		if(!$xxx){
			    			echo json_encode(array('st'=>0,'msg'=>'gagal convert/file 1 berhasil upload.'));
			    		}else{
			    			echo json_encode(array('st'=>0,'msg'=>'gagal convert'));
			    		}
			    	};
				}else{
					if(!$xxx) 
						echo json_encode(array('st'=>0,'msg'=>'dua2nya gagal.'));	
					else
						echo json_encode(array('st'=>1,'msg'=>'yang atas berhasil'));		
				}
			}else{
				if(!$xxx) 
					echo json_encode(array('st'=>0,'msg'=>'Penyimpanan Dokumen Elektronik (E-Doc) Putusan Akhir Berhasil.'));	
				else
					echo json_encode(array('st'=>1,'msg'=>'yang atas berhasil'));	
			};
			/*
			if(isset($namafile1)){
				if(move_uploaded_file($fileTmpName1, $uploadpath1)){
					$this->load->model('api/dirput_model');
					$this->dirput_model->convert_insert_Annmous($idperkara,$fileName1,$uploadpath1,'6');
			    	echo json_encode(array('st'=>1,'msg'=>'Penyimpanan Dokumen Elektronik (E-Doc) Putusan Akhir Anonymous 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"){
				$idkirim = $this->encrypt->decode(base64_decode($this->uri->segment(5)));
		}
		elseif ($from=="put" || $from=="tuntut" || $from=="datum" || $from=="datumpdt" || $from=="mediasi" || $from=="persiapan" || $from=="penikrar" || $from=="aktacerai"){
				$idkirim = $idperkara;
		}

		$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($this->fileupload_m->hapus_data($idkirim,$from)){
			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_putusan_c.php */
/* Location: ./application/controllers/Fileupload_putusan_c.php */