<?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("judul","keterangan");
	$alias=array("Judul Link","keterangan");
	$kolom=array("judul","keterangan");
	$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 judul LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacari01(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	$qt="SELECT COUNT(*) as total FROM tb_linkterkait
		WHERE id>0 and jenis=2 ".$wh;
		
	$total=gethalaman($qt,$datapush,$data[2]);
	array_push($datapush,$data[1],$data[2]);
	
	$query="SELECT *,
			$total as halaman FROM tb_linkterkait		
		WHERE id>0 and jenis=2 ".$wh."
		ORDER BY urutan ASC LIMIT ?,?";
	$rs=$db->jsonData($query,$datapush);
	return $rs;
}

function inputbanner($data){
	$db=pDatabase::getInstance();
	$urutan=1;
	$query="select urutan from tb_linkterkait order by urutan desc";
	$rs=$db->loadAssoc($query);
	if(isset($rs['urutan'])){
		$urutan=$rs['urutan']+1;
	}
	
	$query="insert into tb_linkterkait (judul,idgambar,keterangan,tautan,urutan,jenis) values (?,?,?,?,?,2)";
	$rsb=$db->query($query,array($data['judul'],$data['idgambar'],$data['keterangan'],$data['tautan'],$urutan));
	return $rsb;
}

function editbanner($data){
	$db=pDatabase::getInstance();
	$query="update tb_linkterkait set judul=?,idgambar=?,keterangan=?,tautan=? where id=? and jenis=2 ";
	$rs=$db->query($query,array($data['judul'],$data['idgambar'],$data['keterangan'],$data['tautan'],$data['id']));
	return $rs;
}

function hapusData($data){
	$db=pDatabase::getInstance();
	
	$query="select idgambar from tb_linkterkait where id=? and jenis=2 ";
	$rsg=$db->loadAssoc($query,array($data['id']));
	
	$query="DELETE FROM tb_linkterkait WHERE id=?";
	$rs=$db->query($query,array($data['id']));
	if($rs==1 && isset($rsg['idgambar'])){
		$db->hapusdokumen($rsg['idgambar']);
	}
	return $rs;
}
function hapusGambar($data){
	$db=pDatabase::getInstance();
	return $db->hapusdokumen($data[1]);
}
function ubahurutan($data){
	$db=pDatabase::getInstance();
	$query="update tb_linkterkait set urutan=? where id=? and jenis=2 ";
	$rs=$db->query($query,array($data['urutan'],$data['id']));
	return $rs;
}

function setStatus($data){
	$db=pDatabase::getInstance();
	$query="UPDATE tb_linkterkait SET status=? WHERE id=? and jenis=2 ";
	$rs=$db->query($query,array($data['status'],$data['id']));
	return $rs;
}
?>