<?php
	define('thisscript','pagecategorydetail');
	
	require_once 'globalad.php';
	require_once '../class/PageCategory.php';
	$trdest = new PageCategory();
	
	if (empty($useraccess['manage_category_page'])){
		redirecting('index.php');
	}
	
	/* sanitize post, get, request */
	$_GET['add'] = ((empty($_GET['add']))?"":$_GET['add']);
	$_POST['check'] = ((empty($_POST['check']))?"":$_POST['check']);
	/* end sanitize */
	
	$cpid = '';
	$cname = '';
	$rsctg = array();
	$id = checkFormValue('id');
	
	$cid = checkFormValue('id');
	
	$cname1 = '';
	$cname2 = '';
		
	if (isFormKeyExists('submits') && $_POST['submits'] == 'Tambah'){
		$ctgname1 = trim($_POST['ctgname_1']);
		$ctgname2 = trim($_POST['ctgname_2']);
		$ctgpid = trim($_POST['ctgpid']);
		$ctgshows = $_POST['pg_shows'];
		if (!isset($ctgpid)){
			$ctgpid = -1;
		}
		if (!isset($ctgshows)){
			$ctgshows = 1;
		}
		
		$lastorder = $trdest->getLastOrder($ctgpid);
		$lastid = $trdest->savePageCategory($ctgname1,$ctgname2,$ctgpid,$ctgshows,$lastorder,$userid);
			
		redirecting('pagecategory.php');
	}
	else if (isFormKeyExists('submits') && $_POST['submits'] == 'Ubah'){
		if (!empty($cid)){
			$ctgname1 = trim($_POST['ctgname_1']);
			$ctgname2 = trim($_POST['ctgname_2']);
			$ctgpid = trim($_POST['ctgpid']);
			$ctgshows = $_POST['pg_shows'];
			$ctgpcur = $_POST['ctgpcur'];

			if (!isset($ctgshows)){
				$ctgshows = 1;
			}
			$addchange = '';

			if ($ctgpid != $ctgpcur){
				$lastorder = getlastid('category_page','sort','parentid=\''.$db->clean($ctgpid).'\'');
				$addchange = ',sort=\''.$db->clean($lastorder).'\' , parentid=\''.$db->clean($ctgpid).'\'';

				$dbctg = $db->fetch_one("SELECT * FROM category_page WHERE categoryid='".$db->clean($cid)."'");
				$pop = $dbctg['parentid'];

				$db->query("UPDATE category_page SET parentid='".$db->clean($pop)."' WHERE parentid='".$db->clean($cid)."'");
			}
			$db->query("UPDATE category_page SET categoryname_1='".$db->clean($ctgname1)."', categoryname_2='".$db->clean($ctgname2)."', status='".$db->clean($ctgshows)."'".$addchange." WHERE categoryid='".$db->clean($cid)."'");

			if ($ctgpid != $ctgpcur){
				reorder('category_page','sort','categoryid','parentid=\''.$db->clean($ctgpcur).'\'');
			}
		}
		redirecting('pagecategory.php');
	}
	
	/* build category list */
	$nav_query = $db->fetch_all("SELECT * FROM category_page WHERE parentid = -1 ORDER BY sort");
	$tree = "";
	$depth = 1;
	$exclude = array();
	array_push($exclude, 0);
	$cshows = 1;
	$menuimage = '';
	if (!empty($cid)){
		$rsctg = $db->fetch_one("SELECT * FROM category_page WHERE categoryid='".$db->clean($cid)."'");
		if (sizeof($rsctg) > 0){
			$cid = $rsctg['categoryid'];
			$cname1 = htmlspecialchars($rsctg['categoryname_1']);
			$cname2 = htmlspecialchars($rsctg['categoryname_2']);
			$csort = $rsctg['sort'];
			$cpid = $rsctg['parentid'];
			$cshows = $rsctg['status'];
		}

		$alldisid = $trdest->buildAllChildID($cid);
		$alldisid = $cid.$alldisid;

		$alldid = explode(",",$alldisid);

		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){
					if (!in_array($nav_row['categoryid'],$alldid)){
						$tree .= "<option value=\"".$nav_row['categoryid']."\"".(($cpid==$nav_row['categoryid'])?" selected":"").">".$nav_row['categoryname_'.$langid]."</option>";
					}
					else{
						$tree .= "<option value=\"".$nav_row['categoryid']."\" disabled>".$nav_row['categoryname_'.$langid]."</option>";
					}
					array_push($exclude, $nav_row['categoryid']);
					$tree .= $trdest->buildChildAdmin($nav_row['categoryid'],$cpid,'',true,$alldid);
				}
			}
		}
	}
	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){
					$tree .= "<option value=\"".$nav_row['categoryid']."\"".(($cpid==$nav_row['categoryid'])?" selected":"").">".$nav_row['categoryname_'.$langid]."</option>";
					array_push($exclude, $nav_row['categoryid']);
					$tree .= $trdest->buildChildAdmin($nav_row['categoryid'],$cpid);
				}
			}
		}
	}
	
	$treef = '<select id="ctgpid" name="ctgpid" class="form-control"><option value="-1"'.(($cpid==-1)?" selected":"").'>No Parent / Main Category</option>'.$tree.'</select>';
	
	require_once 'incl/global_template.php';
	
	$theme = gettemplate('pagecategorydetail');
	eval("\$theme = \"$theme\";");
	echo $theme;
?>