<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
require(root."/".adminFolder."/_lib/auth.php");
require(root."/".adminFolder."/_lib/bootstraps.php");
$pageSubOf = "settings";
$pageName = "website";
$pageTitle = "Website";
$thisUrl = "website";
$ajax_page = "ajax_website.php";
$pageUrl = "/".adminFolder."/$thisUrl";
$a = cekpost(cleanQuery($_GET["a"]), "");
$err = "";
if ($a=="section_main") {
	if (!authorizedModule("website_main", $ses_kduser)) {
		unAuthorizedAccess("website_main");
	} else {
		$fil_seksi = trim(cekpost(cleanQuery($_REQUEST["fil_seksi"]), ""));
		$fil_isi = trim(cekpost(cleanQuery($_REQUEST["fil_isi"]), ""));
		$swhere = '';
		if ($fil_seksi!="") { $swhere .= " AND a.seksi LIKE '%$fil_seksi%' "; }
		if ($fil_isi!="") { $swhere .= " AND a.isi LIKE '%$fil_isi%' "; }
		
		$rs=array(); $q="SELECT a.* FROM s_website a WHERE 1=1 $swhere";
		callRS($rs, $q);
		if (count($rs)!=0) {
			$ld = 20;
			$cp = cekpost(cleanQuery($_GET["cp"]), "1");
			$tp = intval(count($rs) / $ld) + (count($rs)%$ld==0?0:1);
			$sr = ($cp-1) * $ld;
			$pagination = '';
			if ($tp!=1) {
				$pagination .= ('<ul class="pagination pagination-sm">');
					$disabled = '';
					$purl = '#';
					$onclick = '';
					if ($cp<=1) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', 1);return false;"').'>&lt;&lt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="'.$purl.'" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp-1).');return false;"').'>&lt;</a></li>');
					for ($c=1;$c<=$tp;$c++) {
						$active = '';
						$purl = '#';
						$disabled = '';
						$onclick = '';
						if ($c==$cp) {
							$active = 'active';
						} else {
							$purl = '#';
							$onclick = ' onclick="filterReload(\''.$pageName.'\', '.$c.');return false;" ';
						}
						$pagination .= ('<li class="'.$active.' '.$disabled.'"><a href="'.$purl.'" '.$onclick.'>'.$c.'</a></li>');
					}
					$disabled = '';
					$onclick = '';
					if ($cp>=$tp) {
						$disabled = 'disabled';
						$onclick = ' onclick="return false;" ';
					}
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.($cp+1).');return false;"').'>&gt;</a></li>');
					$pagination .= ('<li class="'.$disabled.'"><a href="#" '.($onclick!=""?$onclick:'onclick="filterReload(\''.$pageName.'\', '.$tp.');return false;"').'>&gt;&gt;</a></li>');
				$pagination .= ('</ul>');
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<table class="table table-striped table-bordered">');
				cout('<thead>');
					cout('<tr>');
						cout('<th class="text-center">#</th>');
						if (authorizedModule("website_element_section", $ses_kduser)) {
							cout('<th class="text-center">Section</th>');
						}
						if (authorizedModule("website_element_description", $ses_kduser)) {
							cout('<th class="text-center">Content</th>');
						}
						cout('<th class="col-xs-2"></th>');
					cout('</tr>');
				cout('</thead>');
				cout('<tbody>');
				$cnt = 0;
				#printR($_SESSION);
				for ($i=$sr;$i<count($rs) && $cnt<$ld;$i++) {
					$cnt++;
					$kd = $rs[$i]["kd"];
					$isi = str_replace("\n", "<br/>", $rs[$i]["isi"]);
					cout('<tr>');
						cout('<td class="text-center">'.($i+1).'</td>');
						if (authorizedModule("website_element_section", $ses_kduser)) {
							cout('<td class="text-center"><a href="'.$pageUrl.'/view/'.$rs[$i]["kd"].'/">'.$rs[$i]["seksi"].'</a></td>');
						}
						if (authorizedModule("website_element_description", $ses_kduser)) {
							cout('<td class="text-center">'.$isi.'</td>');
						}
						cout('<td class="text-center">');
							if (authorizedModule("website_edit", $ses_kduser)) {
								cout('<a href="'.$pageUrl.'/edit/'.$kd.'/"><span class="glyphicon glyphicon-pencil"></span><span class="hidden-xs hidden-sm"> Edit</span></a>');
								cout(BSTextSeparator());
							}
							if ($_SESSION["user"]["kduser"]==0) {
								if (authorizedModule("website_delete", $ses_kduser)) {
									cout('<a href="'.$pageUrl.'/delete/'.$kd.'/"><span class="glyphicon glyphicon-trash"></span><span class="hidden-xs hidden-sm"> Delete</span></a>');
								}
							}
						cout('</td>');
					cout('</tr>');
				}
				cout('</tbody>');
			cout('</table>');
			
			if ($tp!=1) {
				cout($pagination);
			}
			cout('<div class="alert alert-info" role="alert"><strong>Total whole data</strong> <span class="badge">'.count($rs).'</span></div>');
			$act_ket = "S Website: Main List";
			if ($swhere!="") { $act_ket .= ": ".cleanQuery($swhere).""; }
			updateUserActivities($act_ket);
		} else {
			cout('<div class="alert alert-info" role="alert">');
				cout('<strong>No data available.</strong> Please add new data for display.');
			cout('</div>');
		}
	}
}
if ($a=="section_view") {
	if (!authorizedModule("website_view", $ses_kduser)) {
		unAuthorizedAccess("website_view");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT a.* FROM s_website a WHERE kd='$id'";
		callRS($rs, $q);
		$modby = (trim($rs[0]["modby"])!=""?'By: <strong>'.$rs[0]["modby"].'</strong>':'By: &lt;<em>no data</em>&gt;').(trim($rs[0]["modtime"])?' ('.date('d M Y, H:i:s A', $rs[0]["modtime"]).')':' &lt;<em>no data</em>&gt;');
		cout('<dl class="dl-horizontal dldata">');
			if (authorizedModule("website_element_section", $ses_kduser)) {
				cout('<dt>Section</dt>');
				cout('<dd>'.$rs[0]["seksi"].'</dd>');
			}
			if (authorizedModule("website_element_description", $ses_kduser)) {
				$isi = str_replace("\n", "<br/>", $rs[0]["isi"]);
				cout('<dt>Description</dt>');
				cout('<dd>'.$isi.'</dd>');
			}
			if (authorizedModule("website_element_modification", $ses_kduser)) {
				cout('<dt>Modified</dt>');
				cout('<dd>'.$modby.'</dd>');
			}
			
		cout('</dl>');
		
		$act_ket = "Rules: View: [kd: ".$rs[0]["kd"]."][kode: ".$rs[0]["kode"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_delete") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	if ($kirim=="1") {
		$delby = $_SESSION["user"]["inisial"];
		$deltime = time();
		$kd = cekpost(cleanQuery($_POST["kd"]), "0");
		$rs=array(); $q="SELECT * FROM s_website WHERE kd='$kd'";
		callRS($rs, $q);
		
		$q = "DELETE FROM s_website WHERE kd='$kd'";
		execRS($q);
		$act_ket = "S Website: Delete: Deleted [kd: $kd][seksi: ".$rs[0]["seksi"]."][isi: ".$rs[0]["isi"]."]";
		updateUserActivities($act_ket);
		cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/";</script>');
		exit;
	}
}
if ($a=="section_delete") {
	if (!authorizedModule("website_delete", $ses_kduser)) {
		unAuthorizedAccess("website_delete");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM s_website WHERE kd='$id'";
		callRS($rs, $q);
		
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_delete" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kd" id="kd" value="'.$rs[0]["kd"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("website_element_section", $ses_kduser)) {
					cout('<dt>Section</dt>');
					cout('<dd>'.$rs[0]["seksi"].'</dd>');
				}
				if (authorizedModule("website_element_description", $ses_kduser)) {
					$isi = str_replace("\n", "<br/>", $rs[0]["isi"]);
					cout('<dt>Description</dt>');
					cout('<dd>'.$isi.'</dd>');
				}
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Delete</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "S Website: Deleting... [kd: ".$rs[0]["kd"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_edit") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$kd = cekpost(cleanQuery($_REQUEST["kd"]), "0");
		$seksi = cekpost(cleanQuery($_REQUEST["seksi"]), "");
		$isi = cekpost(cleanQuery($_REQUEST["isi"]), "");
		if ($seksi=="") { $err.= BSErrorMessage('Section must be filled'); }
		#if ($isi=="") { $err.= BSErrorMessage('Description must be filled'); }
		if ($err=="") {
			$modby = $_SESSION["user"]["inisial"];
			$modtime = time();
			$q = "UPDATE s_website SET seksi='$seksi', isi='$isi', modby='$modby', modtime='$modtime' 
				WHERE kd='$kd'";
			execRS($q);
			$act_ket = "S Website: Edit: Updated [kd: $kd][seksi: $seksi][isi: $isi]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kd.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}

if ($a=="section_edit") {
	if (!authorizedModule("website_edit", $ses_kduser)) {
		unAuthorizedAccess("website_edit");
	} else {
		$id = cekpost(cleanQuery($_GET["id"]), "0");
		$rs=array(); $q="SELECT * FROM s_website WHERE kd='$id'";
		callRS($rs, $q);
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_edit" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<input type="hidden" name="kd" id="kd" value="'.$rs[0]["kd"].'" />');
			cout('<dl class="dl-horizontal dldata">');
				if (authorizedModule("website_element_section", $ses_kduser)) {
					cout('<dt><label for="seksi">Section</label></dt>');
					cout('<dd><input class="form-control" type="text" name="seksi" id="seksi" value="'.$rs[0]["seksi"].'" /></dd>');
				}
				if (authorizedModule("website_element_description", $ses_kduser)) {
					cout('<dt><label for="isi">Description</label></dt>');
					cout('<dd><textarea name="isi" id="isi" rows="10" cols="80" class="form-control">'.$rs[0]["isi"].'</textarea></dd>');
				}
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Update</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "Rules: Editing... [kd: ".$rs[0]["kd"]."][nama: ".$rs[0]["nama"]."]";
		updateUserActivities($act_ket);
		exit;
	}
}
if ($a=="do_add") {
	$kirim = cekpost(cleanQuery($_REQUEST["kirim"]), "");
	$err = "";
	if ($kirim=="1") {
		$seksi = cekpost(cleanQuery($_REQUEST["seksi"]), "");
		$isi = cekpost(cleanQuery($_REQUEST["isi"]), "");
		if ($seksi=="") { $err.= BSErrorMessage('Section must be filled'); }
		#if ($isi=="") { $err.= BSErrorMessage('Description must be filled'); }
		if ($err=="") {
			$modby = $_SESSION["user"]["inisial"];
			$modtime = time();
			$kd = getID("s_website", "kd");
			$q = "INSERT INTO s_website (kd, seksi, isi, modby, modtime) 
				VALUES ('$kd', '$seksi', '$isi', '$creby', '$cretime')";
			execRS($q);
			$act_ket = "S Website: Add [kd: $kd][seksi: $seksi][isi: $isi]";
			updateUserActivities($act_ket);
			cout('<script type="text/javascript">parent.window.location="'.$pageUrl.'/view/'.$kd.'/";</script>');
		} else {
			cout('<script type="text/javascript">parent.showError(\''.$err.'\');</script>');
		}
		exit;
	}
	exit;
}
if ($a=="section_add") {
	if (!authorizedModule("website_add", $ses_kduser)) {
		unAuthorizedAccess("website_add");
	} else {
		cout('<form method="post" target="quick_iframe" action="/'.adminFolder.'/_ajax/'.$ajax_page.'?a=do_add" role="form">');
			cout('<input type="hidden" name="kirim" id="kirim" value="1" />');
			cout('<dl class="dl-horizontal dldata">');
				cout('<dt><label for="seksi">Section</label></dt>');
				cout('<dd><input class="form-control" type="text" name="seksi" id="seksi" /></dd>');
				
				cout('<dt><label for="isi">Description</label></dt>');
				cout('<dd><textarea name="isi" id="isi" rows="10" cols="80" class="form-control"></textarea></dd>');
				
				cout('<dt></dt>');
				cout('<dd class="err"></dd>');
				
				cout('<dt></dt>');
				cout('<dd><button class="btn btn-primary" type="submit">Add</button></dd>');
			cout('</dl>');
		cout('</form>');
		$viewFrame = 0;
		if ($viewFrame==1) {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="100" style="overflow:;border:1px red solid;" visibility="" scrolling="yes" frameborder="1"></iframe>');
		} else {
			cout('<iframe name="quick_iframe" id="quick_iframe" src="/'.adminFolder.'/_ajax/index.php" width="300" height="11" style="overflow:hidden;" visibility="hidden" scrolling="no" frameborder="0"></iframe>');
		}
		$act_ket = "S Website: Adding...";
		updateUserActivities($act_ket);
		exit;
	}
}
?>