<?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['form'] = $this->nativesession->get_flash_session('fileupfrom');
		$data['enc'] = $this->nativesession->get_flash_session('enc');
		$this->load->model('fileupload/fileupload_m');
		if($data['form']=='putusan'){
			$data['title'] = 'PUTUSAN AKHIR';
			$columnname="amar_putusan_dok";
			$dok_putusan = $this->fileupload_m->get_dok_putusan($this->encrypt->decode(base64_decode($data['enc'])),$columnname);
			$publikasi = $this->fileupload_m->get_put_publikasi($this->encrypt->decode(base64_decode($data['enc'])));
			//print_r($publikasi);exit();
			if (!empty($publikasi[0]->published)) {
			$data['publikasikan'] = $publikasi[0]->published;
		    } else {
            $data['publikasikan'] = 0;
		    }
			$link_dirput = $publikasi[0]->link_dirput;
			if(!empty($link_dirput)){
				$data['link_dirput']=$link_dirput;
			}else{
				$data['link_dirput'] ='';
			}
			if(!empty($dok_putusan)){
				$data['namadoktersimpan']= $dok_putusan[0]->amar_putusan_dok;
			}else{
				$data['namadoktersimpan']= 'File E-Doc Belum Di Upload';
			}
		}elseif ($data['form']=='anonimisasi') {
			$data['title'] = 'PUTUSAN ANONIMISASI';
			$columnname="amar_putusan_anonimisasi_dok";
			//$dok_putusan = $this->fileupload_m->get_dok_anonimisasi($this->encrypt->decode(base64_decode($data['enc'])));
			$dok_putusan = $this->fileupload_m->get_dok_putusan($this->encrypt->decode(base64_decode($data['enc'])),$columnname);
			if(!empty($dok_putusan)){
				//$data['namadoktersimpan'] = $dok_anonimisasi[0]->filename;
				$data['namadoktersimpan']= $dok_putusan[0]->amar_putusan_anonimisasi_dok;
			}else{
				$data['namadoktersimpan'] = '';
			}
		}

        //echo "<pre>";
        //print_r($data);exit;

		$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')));
			$form = $this->input->post('form');
			$status_publish = $this->input->post('status_publish');
			$id=$idperkara;
			$this->load->model('fileupload/fileupload_m');
			$this->load->model('api/convert_insert_m');

			if($form=='putusan'){
				$ref_id = $this->convert_insert_m->get_ref_dirput_putusan($idperkara,99);
				$dokumen_ref_id = $ref_id[0]['dokumen_ref_id']; 
			}else{
				$ref_id = $this->convert_insert_m->get_ref_dirput_putusan($idperkara,100);
				$dokumen_ref_id = $ref_id[0]['dokumen_ref_id'];
			}
			
			if($form=='putusan'){
				$columnname="amar_putusan_dok";
			}else{
				$columnname="amar_putusan_anonimisasi_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,"put");
			foreach ($datautama as $datautama)
			{
				$no_perkara=$datautama->nomor_perkara;
				$tgldaftar=$datautama->tanggal_pendaftaran;
			}
			
			$namadokumen=$this->fileupload_m->get_nama_dok($idperkara,"put");
			$tglputus=$this->fileupload_m->get_tgl_putusan($idperkara);
			$jumslash=substr_count($namadokumen,"/");
			if(!empty($namadokumen)){
				$splittgldaftar=explode("-",$tglputus);
				$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); 	
				}
				
			}


			//===== create file directory in not exist
			$splittgldaftar=explode("-",$tglputus);
			if($form=='putusan'){
				$folder='doc';
			}else{
				$folder='anonimisasi';
			}
			$direktori="resources/file/".$folder."/".$splittgldaftar[0]."/".$splittgldaftar[1];
			//$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'];
			if($form=='putusan'){
				$fileName=$splitnoperkara[3]."_".$splitnoperkara[2]."_".$splitnoperkara[1]."_".$splitnoperkara[0]."_putusan_akhir.".$extension;
			}else{
				$fileName=$splitnoperkara[3]."_".$splitnoperkara[2]."_".$splitnoperkara[1]."_".$splitnoperkara[0]."_putusan_anonimisasi.".$extension;
			}
			$fileName=str_replace(' ','_',$fileName);
			$fileTmpName=$_FILES['fileform']['tmp_name'];
			$uploadpath=$direktori."/".$fileName;
			$this->load->helper('convert_helper');

			if(isset($namafile)){
				if($form=='putusan'){
					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,$status_publish);
						echo json_encode(array('st'=>1,'msg'=>'Penyimpanan Dokumen Elektronik (E-Doc) Berhasil.'));
						return;
					}
				}else{
					if(move_uploaded_file($fileTmpName, $uploadpath)){
					$datainsert=array(
						$columnname=>$uploadpath,
					);
					$this->fileupload_m->update_data($idperkara,$datainsert,"put");
					$this->load->model('api/dirput_model');
					$this->dirput_model->convert_insert_Annmous($idperkara,$uploadpath,$dokumen_ref_id);
					_convert_insert_put_Annmous($idperkara,$dokumen_ref_id);
					echo json_encode(array('st'=>1,'msg'=>'Penyimpanan Dokumen Elektronik (E-Doc) Putusan Akhir Berhasil.'));
					return;
			    	// if(_convert_insert_put_Annmous($idperkara,$dokumen_ref_id)){
			    	// 	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{
				echo json_encode(array('st'=>0,'msg'=>'Error:<br /> Penyimpanan Dokumen Elektronik (E-Doc)Tidak Berhasil.'));
				return;
			}
			/*
			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'));	
			};*/
	}
	//============================================================
	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 */