<?php
ob_start();
require_once("init.php");

define ("GET_ALL_NAVIS", -1);

if(!permission("adminpanel")) {
	exit();
}

$tmpl = new Koobi("templates/" . ADMINTHEME . "/navi/");
$tmpl->assign("theme", ADMINTHEME);
$tmpl->assign("langadmin", $langadmin);
$tmpl->assign("area", $area);
$tmpl->register_function("permission", "permission");


if($_REQUEST["action"] == "saveposi"){
	if($_POST['posi']>=1){
		foreach($_POST['posi'] as $id => $posi) {
			if( $posi != "")  {
				$query = new dbc("UPDATE ".PREFIX."_navi SET posi='$posi' WHERE id = '$id'");
				
			}
		}
	}
	
	
		foreach($_POST['target'] as $id => $target) {
				$query = new dbc("UPDATE ".PREFIX."_navi SET target='$target' WHERE id = '$id'");
	}
	
	header("Location: navigation.php?action=view&area=$_REQUEST[area]");
}

// ====================================
//   <<-- ÜBERSICHT -->>
// ====================================
if ($_REQUEST["action"] == "view") {
	$r_area = new dbc("SELECT area_id, area_name FROM " . PREFIX . "_areas WHERE area_id = $area");
	
	$tmpl->assign("area", $r_area->fetchrow());
	$tmpl->assign("navis", getStaticNavigation(GET_ALL_NAVIS, $area));
	$tmpl->display("a_navigation.tpl");
	
	// ====================================
	//   <<-- BEARBEITEN -->>
	// ====================================
	} elseif ($_REQUEST["action"] == "edit") {
	if(!permission("naviedit")) { echo error($langadmin['noperms']); exit(); }
	
	if ($_REQUEST["del"] == 1) {
		$id = $_REQUEST["id"];
		
		$r_sub_navi = new dbc("SELECT id FROM " . PREFIX . "_navi WHERE parent_id = $id");
		
		while ($sub_navi = $r_sub_navi->fetchrow()) {
			$r_del_sub_sub_navi = new dbc("DELETE FROM " . PREFIX . "_navi WHERE parent_id = " . $sub_navi->id);
		}
		
		$r_del_sub_navi = new dbc("DELETE FROM " . PREFIX . "_navi WHERE parent_id = $id");
		$r_del_navi = new dbc("DELETE FROM " . PREFIX . "_navi WHERE id = $id");
		
		header("Location: navigation.php?action=view&area=$_REQUEST[area]");
		
	} else {		
		$r_navi = new dbc("
		SELECT 
		n.id, 
		n.target,
		n.parent_id, 
		n.title, 
		n.document, 
		n.area, 
		n.openonclick, 
		n.group_id,
		n.posi
		FROM
		" . PREFIX . "_navi AS n
		WHERE
		n.id = " . $_REQUEST["id"]
		);
		
		$r_groups = new dbc("
		SELECT
		ugroup,
		groupname
		FROM
		" . PREFIX . "_usergroup
		");
		
		$groups = array();
		
		while ($group = $r_groups->fetchrow()) {
			array_push($groups, $group);
		}
		
		$navi = $r_navi->fetchrow();
		$navi->group_id = explode(",", $navi->group_id);
		
		$tmpl->assign("groups", $groups);
		$tmpl->assign("navis", getStaticNavigation(GET_ALL_NAVIS, $_REQUEST["area"]));
		$tmpl->assign("navi", $navi);
		
		$tmpl->display("a_navigation_edit.tpl");
	}
	
	
	// ====================================
	//   <<-- AENDERUNG UEBERNEHMEN -->>
	// ====================================
	} elseif ($_REQUEST["action"] == "apply") {
	$errorMessages = array();
	
	if ($_REQUEST["title"] == "") {
		array_push($errorMessages, $langadmin["errortitle"]);
	}
	
	if ($_REQUEST["document"] == "") {
		array_push($errorMessages, $langadmin["errordocument"]);
	}
	
	if (count($errorMessages)) {
		if ($_REQUEST["id"] != "") {
			$r_navi = new dbc("
			SELECT 
			n.id, 
			n.parent_id, 
			n.title, 
			n.document, 
			n.area, 
			n.openonclick, 
			n.group_id
			FROM
			" . PREFIX . "_navi AS n
			WHERE
			n.id = " . $_REQUEST["id"]
			);
			
			$tmpl->assign("navi", $r_navi->fetchrow());
		}
		
		$r_areas = new dbc("
		SELECT
		area_id,
		area_name
		FROM
		" . PREFIX . "_areas
		");
		
		$areas = array();
		
		while ($area = $r_areas->fetchrow()) {
			array_push($areas, $area);
		}
		
		$groups = array();
		
		$r_groups = new dbc("
		SELECT
		ugroup,
		groupname
		FROM
		" . PREFIX . "_usergroup
		");
		
		while ($group = $r_groups->fetchrow()) {
			array_push($groups, $group);
		}
		
		$tmpl->assign("groups", $groups);
		$tmpl->assign("areas", $areas);
		$tmpl->assign("navis", getStaticNavigation(GET_ALL_NAVIS,$area));
		$tmpl->assign("errorMessages", $errorMessages);
		
		$tmpl->display("a_navigation_edit.tpl");
	} else {
		if ($_REQUEST["id"] != "") {
			
			if($_REQUEST["group"]=="") {
				$g_id = "1";
			} else {
				$g_id = implode(",", $_REQUEST["group"]);
			}
			
			$query = new dbc("
			UPDATE
			" . PREFIX . "_navi
			SET
			title = '" . $_REQUEST["title"] . "',
			document = '" . $_REQUEST["document"] . "',
			openonclick = " . $_REQUEST["openonclick"] . ",
			group_id = '" . $g_id . "',
			posi = " . $_REQUEST["position"] . "
			WHERE
			id = " . $_REQUEST["id"]
			);
		} else {
			
			
			if(!permission("navinew")) { echo error($langadmin['noperms']); exit(); }
			$group = (is_array($_REQUEST["group"])) ? implode(",", $_REQUEST["group"]) : $_REQUEST["group"];
			if(!$group) $group = "1";
			
			$query = new dbc("
			INSERT INTO
			" . PREFIX . "_navi
			(id, parent_id, title, document, area, openonclick, group_id, posi)
			VALUES (
			'',
			'" . $_REQUEST["parent"] . "',
			'" . $_REQUEST["title"] . "',
			'" . $_REQUEST["document"] . "',
			'" . $_REQUEST["area"] . "',
			'" . $_REQUEST["openonclick"] . "',
			'$group',
			'" . $_REQUEST["position"] . "')
			");
			echo 
			
			"
			INSERT INTO
			" . PREFIX . "_navi
			(id, parent_id, title, document, area, openonclick, group_id, posi)
			VALUES (
			'',
			'" . $_REQUEST["parent"] . "',
			'" . $_REQUEST["title"] . "',
			'" . $_REQUEST["document"] . "',
			'" . $_REQUEST["area"] . "',
			'" . $_REQUEST["openonclick"] . "',
			'$group',
			'" . $_REQUEST["position"] . "')
			";
		}
		header("Location: navigation.php?action=view&area=$_REQUEST[area]");
		
	}
	
	// ====================================
	// <<-- NEUE NAVI ANLEGEN -->>
	// ====================================
	} elseif ($_REQUEST["action"] == "new") {
	$r_groups = new dbc("
	SELECT
	ugroup,
	groupname
	FROM
	" . PREFIX . "_usergroup
	");
	
	$groups = array();
	
	while ($group = $r_groups->fetchrow()) {
		array_push($groups, $group);
	}
	
	$tmpl->assign("groups", $groups);
	$tmpl->assign("navis", getStaticNavigation(GET_ALL_NAVIS, $_REQUEST["area_id"]));
	
	$tmpl->display("a_navigation_edit.tpl");
}
?>