<?php
	define('thisscript','pagenewsdetail');

	require_once 'globalad.php';	
	require_once '../class/news.php';
	$pagenews = new News();
	
	if (empty($userid)){
		redirecting("index.php");
	}
	
	if (empty($useraccess['manage_news'])){
		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 pagenews 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'])){
		$pagenews->setId($_REQUEST['id']);
		$curstatus = $pagenews->getNewsDetail();
		
		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['newsid']){
						$goOn = 0;
						break;
					}
				}
				if ($goOn == 1){
					array_push($exclude, $nav_row['newsid']);
				}
			}
		}
	}
	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['newsid']){
						$goOn = 0;
						break;
					}
				}
				if ($goOn == 1){
					array_push($exclude, $nav_row['newsid']);
					
				}
			}
		}
	}

	if ($_POST['submits'] == 'Tambah'){
		if ($pagenews->checkExist($_POST['ref_title'])){
			$errmsg = '- Identitas Halaman sudah ada dalam sistem. Silahkan masukkan identitas yang lain.';
			$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 = $pagenews->savePage($_POST['ref_title'],$_POST['title_1'],$_POST['content_1'],$_POST['title_2'],$_POST['content_2'],$_POST['type_of_page'],$_POST['status'],$userid);
			redirecting("newslist.php");
		}
	}
	else if ($_POST['submits'] == 'Ubah'){
		if ($pagenews->checkExist($_POST['ref_title'])){
			$errmsg = '- Identitas Halaman sudah ada dalam sistem. Silahkan masukkan identitas yang lain.';
			$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 = $pagenews->updatePage($_POST['ref_title'],$_POST['title_1'],$_POST['content_1'],$_POST['title_2'],$_POST['content_2'],$_POST['type_of_page'],$_POST['status'],$userid);
			redirecting("newslist.php");
		}
	}
	
	require_once 'incl/global_template.php';
	
	$theme = gettemplate('pagenewsdetail');
	eval("\$theme = \"$theme\";");
	echo $theme;
?>