<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if(isset($_POST['ikdata'])){
	if (!isset($_SESSION)) session_start();
	if(!isset($_SESSION['profile'])) exit;
	header("Content-type: application/json");
	$db=pDatabase::getInstance();
	
	$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 getGambar($data){
	$db=pDatabase::getInstance();
	$cari=$data[3];
	
	$cr="%$cari%";
	$jenis=$data[4];
	$total=0;
	$datarr=array();
	$datapush=array();
	
	$qtot="SELECT COUNT(*) as total FROM tb_dokumen
		WHERE idjenis =? and nama_awal LIKE ?";
	
	$datarr=array($jenis,$cr);
	
	$wh="";
	$datamerg=array();
	if($_SESSION['profile']['level']<25){
		$wh="and t2.idsub=? ";
		array_push($datamerg,$_SESSION['profile']['idlazis']);
	}
		
	if($jenis==0){
		$qtot="SELECT COUNT(*) as total FROM tb_dokumen as t1
			inner join tb_username as t2 on t1.iduser=t2.id
			WHERE t1.nama_awal LIKE ? ".$wh;

		$datarr=array_merge(array($cr),$datamerg);
	}
	
	$total=gethalaman($qtot,$datarr,$data[2]);
	
	$query="SELECT *,$total as halaman FROM tb_dokumen as t1
			inner join tb_username as t2 on t1.iduser=t2.id
		WHERE t1.idjenis =? and t1.nama_awal LIKE ? ".$wh."ORDER BY t1.id desc LIMIT ?,?";
	
	$datapush=array_merge(array($jenis,$cr),$datamerg,array($data[1],$data[2]));
	
	if($jenis==0){
		$query="SELECT *,$total as halaman FROM tb_dokumen as t1
			inner join tb_username as t2 on t1.iduser=t2.id
		WHERE t1.nama_awal LIKE ? ".$wh."ORDER BY t1.id desc LIMIT ?,?";
		
		$datapush=array_merge(array($cr),$datamerg,array($data[1],$data[2]));
	}
	
	$rs=$db->jsonData($query,$datapush);
	
	return $rs;
}

?>