<?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;

$tergugat = array();
$penggugat = array();
foreach ($dataPenetapan->result() as $row) {
    if (!in_multiarray($row->namaP, $penggugat)){
    	$penggugatItem = array();
    	$penggugatItem["nama"] = $row->namaP;
    	$penggugatItem["alamat"] = $row->alamatP;
    	$penggugatItem["statusP"] = $row->statusP;
    	$penggugatItem["urutanP"] = $row->urutanP;
    	$penggugat[] = $penggugatItem;
    }

	$namaPengadilan=$this->templatehelper->standradnaming($row->namaPengadilan);
	$kota=$this->templatehelper->namaKota($row->namaPengadilan);
	$nomorperkara=$row->nomor_perkara;
	$tanggalSurat=$this->templatehelper->convertKeTglIndo($row->tanggal_surat);
	$tglPenHakim=$this->templatehelper->convertKeTglIndo($row->penetapan_majelis_hakim);
	$namaHakimKetua=$this->templatehelper->standradnaming($row->namaHakimKetua);
	$penetapanHariSidang=$this->templatehelper->convertKeTglIndo($row->penetapanHariSidang);
	$sidangPertama=$this->templatehelper->convertKeTglIndo($row->sidang_pertama);
	$hariSidang=$this->templatehelper->getHari($sidangPertama);
	$jamSidangx= $row->jam_sidang;
	$hours=explode(":",$jamSidangx);
	$hh=$hours['0'];
	$mm=$hours['1'];
	$jamSidang=$hh.":".$mm;
	$Template=str_replace("#kota#",''.$kota.'',$Template);
}

$cPenggugat = count($penggugat);
$i = 1;
foreach ($penggugat as $item) {
	$naskah ="\b ".$this->templatehelper->standradnaming($item["nama"])."\b0  Bertempat tingal di ".$item["alamat"];
	$brnaskah = (($cPenggugat > 1)?($i):("")).'.'.$naskah."; ".PHP_EOL.(($i<$cPenggugat)?("#pemohon#"):(""));
	$Template = str_replace("#pemohon#",''.$brnaskah.'',$Template);	
	$i++;
}

$Template = str_replace("#namaPengadilan#",''.$namaPengadilan.'',$Template);
$Template = str_replace("#nomorperkara#",''.$nomorperkara.'',$Template);
$Template = str_replace("#tglPermohonan#",''.$tanggalSurat.'',$Template);
$Template = str_replace("#tglPenHakim#",''.$tglPenHakim.'',$Template);
$Template = str_replace("#namaHakimKetua#",''.$namaHakimKetua.'',$Template);
$Template = str_replace("#penHariSidang#",''.$penetapanHariSidang.'',$Template);
$Template = str_replace("#sidangPertama#",''.$sidangPertama.'',$Template);
$Template = str_replace("#hariSidang#",''.$hariSidang.'',$Template);
$Template = str_replace("#jamSidang#",''.$jamSidang.'',$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;
?>