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

$url = base_url("resources/template/penetapan/pidana_pra/".$filename.".rtf");

$Template = file_get_contents($url);

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"] = $this->templatehelper->standradnaming($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"] = $this->templatehelper->standradnaming($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);
	$jamSidang = substr($jamSidangx, 0, 5);
	$namaPP =$this->templatehelper->standradnaming($row->nama_pp);
	$haripputus =$this->templatehelper->getHari($row->pputus);
	$tglpputus =$this->templatehelper->convertKeTglIndo($row->pputus);
	$jabatan   =$row->jabatan;
	$namaPejabat   =$this->templatehelper->standradnaming($row->nama);
}

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

$ctermohon = count($termohon);
$i = 1;
foreach ($termohon as $item) {
	$naskah ="\b ".$this->templatehelper->standradnaming($item["nama"])." , \b0 ".$this->templatehelper->standradnaming($item["statusT"])."\b  Termohon ".$this->templatehelper->converToAngkaRomawi($item["urutanT"])."\b0 ;";
	$brnaskah = (($ctermohon > 1)?($i):("")).' \tab '.$naskah.' \\par '.PHP_EOL.(($i<$ctermohon)?("#termohonParties#"):(""));
	$Template = str_replace("#termohonParties#",''.$brnaskah.'',$Template);	
	$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("#penHariSidang#",''.$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);
$Template = str_replace("#namaPejabat#",''.$namaPejabat.'',$Template);
$Template = str_replace("#jabatan#",''.$jabatan.'',$Template);
$Template = str_replace("#kota#",''.$kota.'',$Template);
 
header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control:  must-revalidate, post-check=0, pre-check=0");
header("Content-disposition: attachment; filename=\"".$namafile."-".$nomorperkara.".rtf\"");
echo $Template;
?>