<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if(isset($_POST['ikdata'])){
if (!isset($_SESSION)) session_start();
$db=pDatabase::getInstance();
$datafungsi=[
	"getTabel"=>"getTabel",
	"datacari01"=>"datacari01",
	"inputmasterpelajaran"=>"inputmasterpelajaran",
	"editmasterpelajaran"=>"editmasterpelajaran",
	"hapusData"=>"hapusData",
	"setStatus"=>"setStatus",
];

if(!$db->ceckmodule($cl)) exit;
	$data=$_POST['ikdata'];
	$ex=false;
	if(isset($datafungsi[$data[0]])) $ex=$datafungsi[$data[0]]($data,$cl);
	echo $ex;
}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("kode","nama","sekolah");
	$alias=array("Kode","Nama","Sekolah");
	$kolom=array("t1.kode","t1.nama","t3.nama");
	$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 t1.nama LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacari01(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	if($_SESSION['profile']['modesekolah']==1){
		$wh.="and t1.idsekolah=? ";
		array_push($datapush,$_SESSION['profile']['idsekolah']);
	}
	
	$qt="SELECT COUNT(*) as total FROM tb_pelajaran	as t1
		inner join tb_jenis_pelajaran as t2 on t1.idjenis=t2.id
		inner join tb_jenis_sekolah as t3 on t1.idsekolah=t3.id
		WHERE t1.id>0 ".$wh;
		
	$total=gethalaman($qt,$datapush,$data[2]);
	array_push($datapush,$data[1],$data[2]);
	
	$query="SELECT t1.`id`,
			t1.`idsekolah`,
			t1.`idjenis`,
			t1.`kode`,
			t1.`nama`,
			t1.`status`,
			t2.nama as namajenis,
			t3.nama as namasekolah,
			$total as halaman FROM tb_pelajaran as t1
		inner join tb_jenis_pelajaran as t2 on t1.idjenis=t2.id
		inner join tb_jenis_sekolah as t3 on t1.idsekolah=t3.id
		WHERE t1.id>0 ".$wh."
		ORDER BY t1.idsekolah asc,t1.kode ASC LIMIT ?,?";
	$rs=$db->jsonData($query,$datapush);
	return $rs;
}
function inputmasterpelajaran($data){
	$db=pDatabase::getInstance();
	
	$idsekolah=$data['idsekolah'];
	if($_SESSION['profile']['modesekolah']==1){
		$idsekolah=$_SESSION['profile']['idsekolah'];
	}
	$datainput=array("idsekolah"=>$idsekolah,
		"idjenis"=>$data['idjenis'],
		"kode"=>$data['kode'],
		"nama"=>$data['nama']);
		
	$rs=$db->insert("tb_pelajaran",$datainput);
	return $rs;
	
}
function editmasterpelajaran($data){
	$db=pDatabase::getInstance();
	
	$idsekolah=$data['idsekolah'];
	if($_SESSION['profile']['modesekolah']==1){
		$idsekolah=$_SESSION['profile']['idsekolah'];
	}
	
	$wh="";
	$datapush=array($idsekolah,$data['idjenis'],$data['kode'],$data['nama'],$data['id']);
	if($_SESSION['profile']['modesekolah']==1){
		$wh.="and idsekolah=? ";
		array_push($datapush,$_SESSION['profile']['idsekolah']);
	}
	
	$datainput=array("idsekolah"=>$idsekolah,
		"idjenis"=>$data['idjenis'],
		"kode"=>$data['kode'],
		"nama"=>$data['nama']);
		
	$rs=$db->update("tb_pelajaran",$datainput,"id",$data['id']);
	return $rs;
	
	
}
function hapusData($data){
	$db=pDatabase::getInstance();
	$wh="";
	$datapush=array($data['id']);
	if($_SESSION['profile']['modesekolah']==1){
		$wh.="and idsekolah=? ";
		array_push($datapush,$_SESSION['profile']['idsekolah']);
	}
	
	$query="DELETE FROM tb_pelajaran WHERE id=? ".$wh;
	$rs=$db->query($query,$datapush);
	return $rs;
}

function setStatus($data){
	$db=pDatabase::getInstance();
	
	$wh="";
	$datapush=array($data['status'],$data['id']);
	if($_SESSION['profile']['modesekolah']==1){
		$wh.="and idsekolah=? ";
		array_push($datapush,$_SESSION['profile']['idsekolah']);
	}
	
	$query="update tb_pelajaran set status=? where id=? ".$wh;
	$rs=$db->query($query,$datapush);
	return $rs;
}
?>