<?php
	define('thisscript','pagedetail');

	require_once 'globalad.php';	
	require_once '../class/PageCategory.php';
	require_once '../class/Page.php';
	$ctg = new PageCategory();
	$page = new Page();
	
	if (empty($userid)){
		redirecting("index.php");
	}
	
	if (empty($useraccess['manage_page'])){
		redirecting('index.php');
	}
	
	/* sanitize post, get, request */
	$_REQUEST['id'] = ((empty($_REQUEST['id']))?"":$_REQUEST['id']);
	$_POST['submits'] = ((empty($_POST['submits']))?"":$_POST['submits']);
	$_GET['getlist'] = ((empty($_GET['getlist']))?"":$_GET['getlist']);
	$_GET['keyword'] = ((empty($_GET['keyword']))?array():$_GET['keyword']);
	$_GET['field'] = ((empty($_GET['field']))?array():$_GET['field']);
	$_COOKIE['pglist'] = ((empty($_COOKIE['pglist']))?"":$_COOKIE['pglist']);
	/* end sanitize */
	
	/* build category list */
	$nav_query = $db->fetch_all("SELECT * FROM category_page WHERE parentid = -1 ORDER BY sort");
	$depth = 1;
	$exclude = array();
	array_push($exclude, 0);
	
	$curstatus = array();
	$curstatus['ref_title'] = '';
	$curstatus['title_1'] = '';
	$curstatus['content_1'] = '';
	$curstatus['title_2'] = '';
	$curstatus['content_2'] = '';
	$curstatus['status'] = '';
	$curstatus['type_of_page'] = '';
	
	$pagectgoption = '';
	
	if (!empty($_REQUEST['id'])){
		$page->setId($_REQUEST['id']);
		$curstatus = $page->getPageDetail();
		$cpid = empty($curstatus['categoryid'])?'-1':$curstatus['categoryid'];
		
		if (sizeof($nav_query) > 0){
			foreach ($nav_query as $nav_row){
				$goOn = 1;
				for ($x = 0; $x < count($exclude); $x++){
					if ($exclude[$x] == $nav_row['categoryid']){
						$goOn = 0;
						break;
					}
				}
				if ($goOn == 1){
					$pagectgoption .= "<option value=\"".$nav_row['categoryid']."\"".(($cpid == $nav_row['categoryid'])?' selected':'').">".$nav_row['categoryname_'.$langid]."</option>";
					array_push($exclude, $nav_row['categoryid']);
					$pagectgoption .= $ctg->buildChildAdmin($nav_row['categoryid'],$cpid);
				}
			}
		}
	}
	else{
		if (sizeof($nav_query) > 0){
			foreach ($nav_query as $nav_row){
				$goOn = 1;
				for ($x = 0; $x < count($exclude); $x++){
					if ($exclude[$x] == $nav_row['categoryid']){
						$goOn = 0;
						break;
					}
				}
				if ($goOn == 1){
					$pagectgoption .= "<option value=\"".$nav_row['categoryid']."\">".$nav_row['categoryname_'.$langid]."</option>";
					array_push($exclude, $nav_row['categoryid']);
					$pagectgoption .= $ctg->buildChildAdmin($nav_row['categoryid'],-1);
				}
			}
		}
	}
	
	if ($_POST['submits'] == 'Tambah'){
		if ($page->checkExist($_POST['ref_title'])){
			$errmsg = '- Identitas Halaman sudah ada dalam sistem. Silahkan masukkan identitas yang lain.';
			$curstatus['categoryid'] = $_POST['categoryid'];
			$curstatus['ref_title'] = $_POST['ref_title'];
			$curstatus['title_1'] = $_POST['title_1'];
			$curstatus['content_1'] = $_POST['content_1'];
			$curstatus['title_2'] = $_POST['title_2'];
			$curstatus['content_2'] = $_POST['content_2'];
		}
		else{
			$lastid = $page->savePage($_POST['categoryid'],$_POST['ref_title'],$_POST['title_1'],$_POST['content_1'],$_POST['title_2'],$_POST['content_2'],$_POST['type_of_page'],$_POST['status'],$userid);
			redirecting("pageslist.php");
		}
	}
	else if ($_POST['submits'] == 'Ubah'){
		if ($page->checkExist($_POST['ref_title'])){
			$errmsg = '- Identitas Halaman sudah ada dalam sistem. Silahkan masukkan identitas yang lain.';
			$curstatus['categoryid'] = $_POST['categoryid'];
			$curstatus['ref_title'] = $_POST['ref_title'];
			$curstatus['title_1'] = $_POST['title_1'];
			$curstatus['content_1'] = $_POST['content_1'];
			$curstatus['title_2'] = $_POST['title_2'];
			$curstatus['content_2'] = $_POST['content_2'];
		}
		else{
			$results = $page->updatePage($_POST['categoryid'],$_POST['ref_title'],$_POST['title_1'],$_POST['content_1'],$_POST['title_2'],$_POST['content_2'],$_POST['type_of_page'],$_POST['status'],$userid);
			redirecting("pageslist.php");
		}
	}
	
	require_once 'incl/global_template.php';
	
	$theme = gettemplate('pagedetail');
	eval("\$theme = \"$theme\";");
	echo $theme;
?>