<?php
// $segment = $this->uri->segment_array();
// $idperkara = $segment[3];

// if(!is_numeric($idperkara)){
// 	show_404();
// }

// foreach ($template->result() as $template)
// {
//     $filename=$template->kdtemplate;
//     $namafile=str_replace(" ","",$template->namatemplate);
// }

// $url = base_url("resources/template/".$filename.".rtf");
foreach ($template->result() as $template)
{
    $filename=$template->kdtemplate;
    $namafile=str_replace(" ","",$template->namatemplate);
}

$url = base_url("resources/template/putusan/pidana_pra/".$filename.".rtf");
//$url = base_url("resources/template/putusan/pidana_pra/PID.PRA_15_212_1.rtf");
// $namafile = "Penetepan Sidang Pertama";

$Template = file_get_contents($url);
//=============================================roby======================================================

function in_multiarray($sval, $arr){
	$ret = false;
	foreach ($arr as $value) {
		if (is_array($value)){
			$ret = $ret | in_multiarray($sval, $value);
		}else{
			if ($value == $sval){
				return true;
			}
		}
	}
	return $ret;
}

$dataPenetapan = $data_template;
$itunganTerdakwa = count($dataPenetapan);
$termohon = array();
$pemohon = array();
foreach ($dataPenetapan->result() as $row) {
    if (!in_multiarray($row->namaP, $pemohon)){
    	$pemohonItem = array();
    	$pemohonItem["nama"] = $row->namaP;
    	$pemohonItem["alamat"] = $row->alamatP;
    	$pemohonItem["statusP"] = $row->statusP;
    	$pemohonItem["urutanP"] = $row->urutanP;
    	$pemohon[] = $pemohonItem;
    }
    if (!in_multiarray($row->namaT, $termohon)){
    	$termohonItem = array();
    	$termohonItem["nama"] = $row->namaT;
    	$termohonItem["alamat"] = $row->alamatT;
    	$termohonItem["statusT"] = $row->statusT;
    	$termohonItem["urutanT"] = $row->urutanT;
    	$termohon[] = $termohonItem;
    }


	$namaPengadilan =$this->templatehelper->standradnaming($row->namaPengadilan);
	$kota     =$this->templatehelper->namaKota($row->namaPengadilan);
	$nomorperkara =$row->nomor_perkara;
	$tanggalPendaftaran   =$this->templatehelper->convertKeTglIndo($row->tanggal_pendaftaran);
	$tanggalSurat   =$this->templatehelper->convertKeTglIndo($row->tanggal_surat);
	$nomorSurat   =$row->nomor_surat;
	$tglPenHakim   =$this->templatehelper->convertKeTglIndo($row->penetapan_majelis_hakim);
	$tglPenPP   =$this->templatehelper->convertKeTglIndo($row->penetapan_panitera_pengganti);
	$namaHakimKetua   =$this->templatehelper->standradnaming($row->namaHakimKetua);
	$penetapanHariSidang   =$this->templatehelper->convertKeTglIndo($row->penetapanHariSidang);
	$sidangPertama   =$this->templatehelper->convertKeTglIndo($row->sidang_pertama);
	$jamSidangx   = $row->jam_sidang;
	$hariCetak = $this->templatehelper->getHari($sidangPertama);
	$hours = explode(":",$jamSidangx);
	$hh = $hours['0'];
	$mm = $hours['1'];
	$jamSidang = $hh.":".$mm;
	$Template = str_replace("#kota#",''.$kota.'',$Template);
	$namaPP =$this->templatehelper->standradnaming($row->nama_pp);
	//$tglpputus =$this->templatehelper->convertKeTglIndo($row->pputus);
	$haripputus =$this->templatehelper->getHari($row->pputus);
	$tglpputus =$this->templatehelper->convertKeTglIndo($row->pputus);
}

$cpemohon = count($pemohon);
$i = 1;
foreach ($pemohon as $item) {
	$naskah ="\b ".$this->templatehelper->standradnaming($item["nama"])." , \b0 ".$item["statusP"]."\b  pemohon ".$this->templatehelper->converToAngkaRomawi($item["urutanP"])."\b0 ;";
	//$naskah = $item["nama"];
	$brnaskah = (($cpemohon > 1)?($i):("")).' \tab '.$naskah.' \\par '.PHP_EOL.(($i<$cpemohon)?("#pemohonParties#"):(""));
	$Template = str_replace("#pemohonParties#",''.$brnaskah.'',$Template);	
	#print_r($brnaskah);
	$i++;
}

$ctermohon = count($termohon);
$i = 1;
foreach ($termohon as $item) {
	$naskah ="\b ".$this->templatehelper->standradnaming($item["nama"])." , \b0 ".$item["statusT"]."\b  termohon ".$this->templatehelper->converToAngkaRomawi($item["urutanT"])."\b0 ;";
	//$naskah = $item["nama"];
	$brnaskah = (($ctermohon > 1)?($i):("")).' \tab '.$naskah.' \\par '.PHP_EOL.(($i<$ctermohon)?("#termohonParties#"):(""));
	$Template = str_replace("#termohonParties#",''.$brnaskah.'',$Template);	
	#print_r($brnaskah);
	$i++;
}

if($itunganTerdakwa==1){
    $jenisTerdakwa = "Terdakwa";
} else {
    $jenisTerdakwa = "Para Terdakwa";
}

$Template = str_replace("#namaPengadilan#",''.$namaPengadilan.'',$Template);
$Template = str_replace("#nomorperkara#",''.$nomorperkara.'',$Template);
$Template = str_replace("#tanggalPendaftaran#",''.$tanggalPendaftaran.'',$Template);
$Template = str_replace("#tanggalSurat#",''.$tanggalSurat.'',$Template);
$Template = str_replace("#nomorSurat#",''.$nomorSurat.'',$Template);
$Template = str_replace("#tglPenHakim#",''.$tglPenHakim.'',$Template);
$Template = str_replace("#tglPenPP#",''.$tglPenPP.'',$Template);
$Template = str_replace("#namaHakimKetua#",''.$namaHakimKetua.'',$Template);
$Template = str_replace("#penetapanHariSidang#",''.$penetapanHariSidang.'',$Template);
$Template = str_replace("#sidangPertama#",''.$sidangPertama.'',$Template);
$Template = str_replace("#hariCetak#",''.$hariCetak.'',$Template);
$Template = str_replace("#jamSidang#",''.$jamSidang.'',$Template);
$Template = str_replace("#namaPP#",''.$namaPP.'',$Template);
$Template = str_replace("#jenisTerdakwa#",''.$jenisTerdakwa.'',$Template);
$Template = str_replace("#tglputus#",''.$tglpputus.'',$Template);
$Template = str_replace("#haripputus#",''.$haripputus.'',$Template);
 

header("Content-type: application/rtf");
header("Content-disposition: inline; filename=".$namafile."-".$nomorperkara.".rtf");
header("Content-length: ".strlen($Template));
echo $Template;


?>