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


if(!function_exists('_convert_pdf')){
	function _convert_pdf($fileasal,$filetarget) {
		$return = '';
			
			$instance = explode(" ",php_uname());
			if($instance[0] == 'Windows')
			{	
				$word = new COM("Word.Application") or die ("Could not initialise Object.");
				$word->Visible = 0;
				$word->DisplayAlerts = 0;
				$word->Documents->Open($fileasal); 
				$word->ActiveDocument->SaveAs('baru.doc');
				$word->ActiveDocument->ExportAsFixedFormat($filetarget, 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
				$word->Quit(false);
				unset($word);
				$return = json_encode(array('st'=>0,'msg'=>'Convert Sukses'));
			}else
			{
				//echo ('python '.FCPATH.'resources/pdf-master/converter.py '.$fileasal.' '.$filetarget);
				//exec('python '.FCPATH.'resources/pdf-master/converter.py '.$fileasal.' '.$filetarget);
				//exec('lowriter --convert-to pdf:writer_pdf_Export -env:UserInstallation=file:///tmp/.SIPP-pdf-${USER} --headless '.$fileasal.' --outdir '.dirname($filetarget));
				exec('soffice --convert-to pdf:writer_pdf_Export -env:UserInstallation=file:///tmp/.SIPP-pdf-${USER} --headless '.$fileasal.' --outdir '.dirname($filetarget));
				$return = json_encode(array('st'=>0,'msg'=>'Convert Sukses'));
			}	
		return $return;
		
	}
}

if(!function_exists('_convert_pdf_windows')){
	function _convert_pdf_windows($fileasal,$filetarget){
		$CI = &get_instance();
		
		$fileasal= str_replace('/','\\',$fileasal);
		$filetarget= str_replace('/','\\',$filetarget);
		 
		$CI->load->helper('file');
		$tipe=get_mime_by_extension($fileasal);
		if(!$tipe == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'){
			$filetarget = $filetarget.'.temp.docx';
			_convert_pdf($fileasal,$filetarget);
			return $filetarget;
		}	
		_convert_pdf($fileasal,$filetarget);	
		    return $filetarget;
	}
}

if(!function_exists('_convert_insert_put')){
	function _convert_insert_put($perkara_id,$dokumen_ref_id,$status_publish){
		$CI = &get_instance();
		$CI->load->model('api/convert_insert_m');
		$amar_putusan_dok = $CI->convert_insert_m->getfile($perkara_id);
		$fileinsert = $amar_putusan_dok[0]['amar_putusan_dok'];
		$instance = explode(" ",php_uname());
		if($instance[0] == 'Windows'){
			$namafile = str_replace('/','\\',FCPATH.$amar_putusan_dok[0]['amar_putusan_dok']);
	    }else{
			$namafile = FCPATH.$amar_putusan_dok[0]['amar_putusan_dok'];
		} 
		if(file_exists($namafile)){
			$namafile = pathinfo($namafile);
			
			$fileasal = $namafile['dirname'].'/'.$namafile['filename'].'.'.$namafile['extension'];
			$perkara_id = $perkara_id;
			$dokumen_ref_id = $dokumen_ref_id;

			$filetarget = $namafile['dirname'].'/'.$namafile['filename'].'.pdf';
			$filetargetinsert = str_replace(FCPATH,'',$filetarget);
			$instance = explode(" ",php_uname());
				if($instance[0] == 'Windows'){
					_convert_pdf_windows($fileasal,$filetarget);								
				} else {	
					_convert_pdf($fileasal,$filetarget);
				}
				$CI->load->model('api/dirput_model');
				$CI->dirput_model->convert_insert($perkara_id,$filetargetinsert,$dokumen_ref_id,$status_publish);
				$CI->dirput_model->insert_antrian_upload($perkara_id);		
		}
		else {
			echo $namafile."<br>";
		}
	}
}

if(!function_exists('_convert_insert_put_Annmous')){
	function _convert_insert_put_Annmous($perkara_id,$dokumen_ref_id_Annmous){
		$CI = &get_instance();
		$CI->load->model('api/convert_insert_m');
		//$path = $CI->convert_insert_m->getfile_Annmous($perkara_id,$dokumen_ref_id_Annmous);
		//echo $path;exit();
		$path = $CI->convert_insert_m->getfile_Annmous($perkara_id,$dokumen_ref_id_Annmous);
		$fileinsert = $path[0]['path_filename'];
		
		$instance = explode(" ",php_uname());
		if($instance[0] == 'Windows'){
			$namafile = str_replace('/','\\',FCPATH.$path[0]['path_filename']);
	    }else{
			$namafile = FCPATH.$path[0]['path_filename'];
		} 
		if(file_exists($namafile)){
			$namafile = pathinfo($namafile);
			
			$fileasal = $namafile['dirname'].'/'.$namafile['filename'].'.'.$namafile['extension'];
			$perkara_id = $perkara_id;
			$dokumen_ref_id = $dokumen_ref_id_Annmous;
			$filetarget = $namafile['dirname'].'/'.$namafile['filename'].'.pdf';
			$filetargetinsert = str_replace(FCPATH,'',$filetarget);
			$instance = explode(" ",php_uname());
				if($instance[0] == 'Windows'){
					_convert_pdf_windows($fileasal,$filetarget);								
				} else {	
					_convert_pdf($fileasal,$filetarget);
				}
				$CI->load->model('api/dirput_model');
				$CI->dirput_model->convert_update_Annmous($perkara_id,$filetargetinsert,$dokumen_ref_id);
				//$CI->dirput_model->insert_antrian_upload($perkara_id);		
				return true;
		}
		else {
			return false;

		}
	}
}