<?php
if($this->session->userdata('is_logged_in')===FALSE){
			redirect('login');
}
foreach ($template->result() as $template){
    $filename=$template->kdtemplate;
    $namafile=str_replace(" ","",$template->namatemplate);
}

$url = base_url("resources/template/penetapan/perdata_umum/".$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;
    }
    if (!in_multiarray($row->namaT, $tergugat)){
    	$tergugatItem = array();
    	$tergugatItem["nama"] = $row->namaT;
    	$tergugatItem["alamat"] = $row->alamatT;
    	$tergugatItem["statusT"] = $row->statusT;
    	$tergugatItem["urutanT"] = $row->urutanT;
    	$tergugat[] = $tergugatItem;
    }

	$namaPengadilan     =$this->templatehelper->standradnaming($row->namaPengadilan);
	$kota     =$this->templatehelper->namaKota($row->namaPengadilan);
	$nomorperkara   =$row->nomor_perkara;
	$jabatan   =$row->jabatan;
	$namaPejabat   = $this->templatehelper->standradnaming($row->nama_pejabat);
	$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);
}

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

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

$cPanitera = count($data_pp->result());
$i=1;
foreach ($data_pp->result() as $data_pp){
	$naskah ="\b ".$this->templatehelper->standradnaming($data_pp->nama_gelar)."\b0" ;
	$brnaskah = (($cPanitera > 1)?($i):("")).'\tab'.$naskah.' \\par '.PHP_EOL.(($i<$cPanitera)?("#namaPP#"):(""));
	$Template = str_replace("#namaPP#",''.$brnaskah.'',$Template);	
	$i++;
}

$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("#namaPejabat#",''.$namaPejabat.'',$Template);
$Template = str_replace("#jabatan#",''.$jabatan.'',$Template);
$Template = str_replace("#tglPenHakim#",''.$tglPenHakim.'',$Template);
$Template = str_replace("#tglPenPP#",''.$tglPenPP.'',$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;
?>