<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if(isset($_POST['ikdata'])){
if (!isset($_SESSION)) session_start();
$db=pDatabase::getInstance();
if(!$db->ceckmodule($cl)) exit;

header("Content-type: application/json");
	$data=$_POST['ikdata'];
	$fungsi = strip_tags(stripslashes($data[0]));
	$ex=$fungsi($data);
	if($ex){
		echo $ex;
	}else{
		echo false;
	}
}else{
	echo false;
}
function gethalaman($qr,$dt,$jumlah){
	$db=pDatabase::getInstance();
	
	$rs=$db->loadAssoc($qr,$dt);
	
	$total=1;
	if(isset($rs['total'])){
		$total=ceil($rs['total']/$jumlah);
	}
	if($total==0){
		return 1;
	}else{
		return $total;
	}
}

function datacari01($data=array())
{
	$mode=0;
	$kunci=array("nama","id");
	$alias=array("Nama Plugin","ID");
	$kolom=array("nama","id");
	$jenis=array();
	
	$hasil=array("kunci"=>$kunci,"kolom"=>$kolom,"alias"=>$alias,"jenis"=>$jenis);
	if(sizeof($data)>0){
		$mode=$data[1];
	}
	if($mode==0){
		return $hasil;
	}else{
		return json_encode($hasil);
	}
}

function getTabel($data){
	$db=pDatabase::getInstance();
	$cari=$data[3];
	$arrcr=explode("&", $cari);
	
	$datapush=array();
	$wh="";
	if(sizeof($arrcr)>0){
		$arrkunci0=explode("=", $arrcr[0]);
		if(sizeof($arrcr)==1 && sizeof($arrkunci0)==1){
			$cr="%$cari%";
			$wh.="AND nama LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacari01(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	$qt="SELECT COUNT(*) as total FROM tb_plugin_cetak 
		WHERE id>0 ".$wh;
		
	$total=gethalaman($qt,$datapush,$data[2]);
	array_push($datapush,$data[1],$data[2]);
	
	$query="SELECT *,
			$total as halaman FROM tb_plugin_cetak
		WHERE id>0 ".$wh."
		ORDER BY id ASC LIMIT ?,?";
	$rs=$db->jsonData($query,$datapush);
	
	return $rs;
}
function getdatatemplate($data){
	$db=pDatabase::getInstance();
	$query="select t1.`id`,
			t1.`idplugin`,
			t1.`idtemplate`,
			t2.nama
		from tb_plugin_cetak_template as t1
		inner join tb_master_template_cetak as t2 on t1.idtemplate=t2.id
		where t1.idplugin=?";
	$rs=$db->jsonData($query,array($data['idplugin']));
	return $rs;
}

function datacaritemplate($data=array())
{
	$mode=0;
	$kunci=array("nama");
	$alias=array("Nama Layanan");
	$kolom=array("t1.kategori");
	$jenis=array();
	
	$hasil=array("kunci"=>$kunci,"kolom"=>$kolom,"alias"=>$alias,"jenis"=>$jenis);
	if(sizeof($data)>0){
		$mode=$data[1];
	}
	if($mode==0){
		return $hasil;
	}else{
		return json_encode($hasil);
	}
}

function getdatatemplateplugin($data){
	$db=pDatabase::getInstance();
	$cari=$data['cari'];
	$arrcr=explode("&", $cari);
	
	$datapush=array();
	$wh="";
	if(sizeof($arrcr)>0){
		$arrkunci0=explode("=", $arrcr[0]);
		if(sizeof($arrcr)==1 && sizeof($arrkunci0)==1){
			$cr="%$cari%";
			$wh.="AND t1.nama LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacaritemplate(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	$wh.="and t1.id not in (select idtemplate as id from tb_plugin_cetak_template where idplugin=? and idtemplate is not null)";
	array_push($datapush,$data['idplugin']);
	
	$posisihalaman=($data['halaman']-1)*$data['jumlah'];
	array_push($datapush,$posisihalaman,$data['jumlah']);
	
	$query="SELECT 
				SQL_CALC_FOUND_ROWS t1.id,
					t1.`id`,
					t1.`kode`,
					t1.`nama`,
					t1.`template`,
					t1.`status`
			FROM tb_master_template_cetak as t1
		WHERE t1.id>0 and t1.status=1 ".$wh."
		ORDER BY t1.id asc LIMIT ?,?";
	$rs=$db->loadAll($query,$datapush);
	
	$qrow="SELECT FOUND_ROWS() as totalbaris;";
	$rsrow=$db->loadAssoc($qrow);
	$jumlahhalaman=1;
	if(isset($rsrow['totalbaris'])){
		$jumlahhalaman=ceil($rsrow['totalbaris']/$data['jumlah']);
	}
	
	return json_encode(array("data"=>$rs,"totalhalaman"=>$jumlahhalaman));
}

function simpantemplatecetak($data){
	$db=pDatabase::getInstance();
	
	$db->begin();
	$status=true;
	$datatmpl=$data['datatemplate'];
	if(sizeof($datatmpl)>0){
		foreach ($datatmpl as $obj) {
			$datainput=array(
				"idplugin"=>$data['idplugin'],
				"idtemplate"=>$obj
			);
			
			$rs=$db->insert("tb_plugin_cetak_template",$datainput);
			if($rs!=1) $status=false;
		}
	}
	
	if($status){
		$db->commit();
		return true;
	}else{
		$db->rollback();
		return false;
	}
}

function hapustemplate($data){
	$db=pDatabase::getInstance();
	
	$query="DELETE FROM tb_plugin_cetak_template WHERE id=?";
	$rs=$db->query($query,array($data['id']));
	return $rs;
}
function setStatus($data){
	$db=pDatabase::getInstance();
	$query="update tb_plugin_cetak set status=? WHERE id=?";
	$rs=$db->query($query,array($data['status'],$data['id']));
	return $rs;
}
?>