<?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/putusan/perdata_umum/".$filename.".rtf");
$Template = file_get_contents($url);
/*PDT_8_15_214_1*/
$Template = file_get_contents($url);
//--------------MULAI--------------------------------------------//
function cetakPihak(&$strbuf, $tmp_label, $arrDef, $hasilFetch, $cetakLabel = true){
    $tblcnt = 1;
    $jmlphk = count($hasilFetch);
    foreach($hasilFetch as $baris){
        $idxKolom = 1;
        foreach($arrDef as $kolomTabel => $label){
            $counter = (($jmlphk > 1)?($tblcnt.'.'):(''));
            $strbuf = str_replace($tmp_label,
                    (($idxKolom == 1)?($counter):('')).
                        ' \tab '.(($cetakLabel)?$label:('')).(($cetakLabel)?(' \tab : \tab '):('')).$baris[$kolomTabel].'; \\par'.
                        PHP_EOL.
                        //$tmp_label,
                        (($idxKolom < count($arrDef)) ? 
                            ($tmp_label) 
                            :  
                            (($tblcnt<$jmlphk)?($tmp_label):('')) 
                        ),
                    $strbuf);
            $idxKolom++;
        }
        $tblcnt++;
    }
}
//-------------Nomor Perkara----------------------//
$data_perkara = $getDataPerkara;
$nomorperkara = '';
foreach ($data_perkara->result() as $row) {
        $nomorperkara = $row->nomor_perkara;
        $nomorsurat = $row->nomor_surat;
    }
if(empty($nomorperkara)){ $nomorperkara = '........'; }
if(empty($nomorsurat)){ $nomorsurat = '........'; }
$Template = str_replace("#nomorperkara#",''.$nomorperkara.'',$Template);
$Template = str_replace("#nomorsurat#",''.$nomorsurat.'',$Template);
//---------------Data Pengadilan--------------------//
$datapengadilan = $getDataPN;
$namapn = '';
foreach ($datapengadilan->result() as $row) {
    $namapn = $this->templatehelper->standradnaming($row->NamaPN);
    $namakota =str_replace("Pengadilan Negeri ",'',$namapn);
}
if(empty($namapn)){ $namapn = '........'; }
$Template = str_replace('#namapn#',''.$namapn.'',$Template);
$Template = str_replace('#namakota#',''.$namakota.'',$Template);
//---------------Data Hakim & PP--------------------//
$datahakim = $getHakim;
$datapanitera = $getPanitera;
foreach ($datahakim->result() as $row) {
    $namahakim = $this->templatehelper->standradnaming($row->hakim_nama);
}
foreach ($datapanitera->result() as $row) {
    $namapp = $this->templatehelper->standradnaming($row->panitera_nama);
}
if(empty($namahakim)){ $namahakim = '........'; }
if(empty($namapp)){ $namapp = '........'; }
$Template = str_replace('#namahakim#',''.$namahakim.'',$Template);
$Template = str_replace('#namapp#',''.$namapp.'',$Template);
//-------------Data Tanggal Putusan & Isi Penetapan-------------------//
$tanggalPutusan=$getTglPts;
foreach ($tanggalPutusan->result() as $row) {
    $tglputus=$this->templatehelper->convertKeTglIndo($row->tanggal_putusan);
    $isiamar=$row->amar_putusan;
}
$isipenetapan = $this->templatehelper->cleanHtmlTagSpecial($isiamar);
$Template = str_replace('#tglputus#',''.$tglputus.'',$Template);
$Template = str_replace('#isipenetapan#',''.$isipenetapan.'',$Template);
//-------------Data Pihak Penggugat-------------------------//
$datapenggugat = $cetakPihakP;
$tblcnt = 1;
$datapenggugat->num_rows>0;
$p = array();
$gkolom = array(
    'nama' => 'Nama Lengkap',
    'alamat' => 'Tempat Tinggal',
    'tempat_lahir' => 'Tempat Tanggal Lahir',
    'jenis_kelamin' => 'Jenis Kelamin',    
    'pekerjaan' => 'Pekerjaan',
    'email' => 'Email/Handphone'
);
foreach ($datapenggugat->result() as $row){    
    $inpass = array();
    if(empty($row->nama)){
        $row->nama = '......'; 
    }
    $namapihak = $this->templatehelper->standradnaming($row->nama);
    $inpass['nama'] = $namapihak;
    if(empty($row->alamat)){
        $row['alamat'] = '......'; 
    }
    $alamat = $row->alamat;
    $inpass['alamat'] = $alamat;
    if(empty($row->tempat_lahir)){
        $row->tempat_lahir = '......'; 
    }
    $tempatlahir = $row->tempat_lahir;
    if(empty($row->tanggal_lahir)){
        $tgllahir = '......'; 
    }else{
        $tgllahir = $this->templatehelper->convertKeTglIndo($row->tanggal_lahir);
    } 
    $inpass['tempat_lahir'] = $tempatlahir.' / '.$tgllahir;
    if($row->jenis_kelamin=='L'){
        $row->jenis_kelamin = 'Laki - Laki';
    }elseif($row->jenis_kelamin=='P'){
        $row->jenis_kelamin = 'Perempuan';
    }
    $sex = $row->jenis_kelamin;
    $inpass['jenis_kelamin'] = $sex;           
    if(empty($row->pekerjaan)){
        $row->pekerjaan = '......'; 
    }
    $pekerjaan = $row->pekerjaan;     
    $inpass['pekerjaan'] = $pekerjaan;
    if(empty($row->telepon)){
        $row->telepon = '.......';
    }
    if(empty($row->email)){
        $row->email = '.......';
    }
    $hp = $row->telepon;
    $mail = $row->email;
    $inpass['email'] = $mail.' / '.$hp;
        
    $tblcnt++;
    $p[] = $inpass;
}
    cetakPihak($Template, '#penggugatkwaparties#', $gkolom, $p);    
//-------------Data Pihak Tergugat-------------------------//
$datatergugat = $cetakPihakT;
$tblcnt = 1;
$datatergugat->num_rows>0;
$p = array();
$gkolom = array(
    'nama' => 'Nama Lengkap',
    'alamat' => 'Tempat Tinggal',
    'tempat_lahir' => 'Tempat Tanggal Lahir',
    'jenis_kelamin' => 'Jenis Kelamin',    
    'pekerjaan' => 'Pekerjaan',
    'email' => 'Email/Handphone'
);
foreach ($datatergugat->result() as $row){    
    $inpass = array();
    if(empty($row->nama)){
        $row->nama = '......'; 
    }
    $namapihak = $this->templatehelper->standradnaming($row->nama);
    $inpass['nama'] = $namapihak;
    if(empty($row->alamat)){
        $row['alamat'] = '......'; 
    }
    $alamat = $row->alamat;
    $inpass['alamat'] = $alamat;
    if(empty($row->tempat_lahir)){
        $row->tempat_lahir = '......'; 
    }
    $tempatlahir = $row->tempat_lahir;
    if(empty($row->tanggal_lahir)){
        $tgllahir = '......'; 
    }else{
        $tgllahir = $this->templatehelper->convertKeTglIndo($row->tanggal_lahir);
    } 
    $inpass['tempat_lahir'] = $tempatlahir.' / '.$tgllahir;
    if($row->jenis_kelamin=='L'){
        $row->jenis_kelamin = 'Laki - Laki';
    }elseif($row->jenis_kelamin=='P'){
        $row->jenis_kelamin = 'Perempuan';
    }
    $sex = $row->jenis_kelamin;
    $inpass['jenis_kelamin'] = $sex;           
    if(empty($row->pekerjaan)){
        $row->pekerjaan = '......'; 
    }
    $pekerjaan = $row->pekerjaan;     
    $inpass['pekerjaan'] = $pekerjaan;
    if(empty($row->telepon)){
        $row->telepon = '.......';
    }
    if(empty($row->email)){
        $row->email = '.......';
    }
    $hp = $row->telepon;
    $mail = $row->email;
    $inpass['email'] = $mail.' / '.$hp;
        
    $tblcnt++;
    $p[] = $inpass;
}
    cetakPihak($Template, '#tergugatkwaparties#', $gkolom, $p);

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\"");
header("Content-length: ".strlen($Template));
echo $Template;

?>