<?php
class Navi
{
	function Speedbar()
	{
		if(isset($_POST['new']) && $_POST['new']==1 && isset($_POST['Name_1']) && !empty($_POST['Name_1']) && !empty($_POST['Dokument']))
		{
			$n1 = $_POST['Name_1'];
			$n2 = (isset($_POST['Name_2']) && !empty($_POST['Name_2'])) ? $_POST['Name_2'] : $n1;
			$n3 = (isset($_POST['Name_3']) && !empty($_POST['Name_3'])) ? $_POST['Name_3'] : $n1;
					
			$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_quicknavi 
			(
				Id,
				Sektion,
				Name_1,
				Name_2,
				Name_3,
				Dokument,
				Position,
				Ziel,
				Aktiv
			) VALUES (
				'',
				'" . AREA . "',
				'" . $n1 . "', 
				'" . $n2 . "', 
				'" . $n3 . "', 
				'" . $_POST['Dokument'] . "',
				'" . $_POST['Position'] . "',
				'_self',
				'1' 
			)");
			header('Location:index.php?do=navigation&sub=speedbar');
			exit;
		}
		
		if(isset($_POST['save']) && $_POST['save']==1 && isset($_POST['Name_1']))
		{
			foreach ($_POST['Name_1'] as $nid => $var)
			{
				if(!empty($_POST['Name_1'][$nid]))
				{
					$n1 = $_POST['Name_1'][$nid];
					$n2 = (isset($_POST['Name_2'][$nid]) && !empty($_POST['Name_2'][$nid])) ? $_POST['Name_2'][$nid] : $n1;
					$n3 = (isset($_POST['Name_3'][$nid]) && !empty($_POST['Name_3'][$nid])) ? $_POST['Name_3'][$nid] : $n1;
				
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_quicknavi SET 
						Name_1='" . $n1 . "', 
						Name_2='" . $n2 . "', 
						Name_3='" . $n3 . "', 
						Dokument='" . $_POST['Dokument'][$nid] . "',
						Position='" . @$_POST['Position'][$nid] . "',
						Ziel='" . $_POST['Ziel'][$nid] . "',
						Aktiv='" . $_POST['Aktiv'][$nid] . "' 
					WHERE Id='$nid'");
					
					if(isset($_POST['del'][$nid]) && $_POST['del'][$nid]==1)
					{
						$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_quicknavi WHERE Id='$nid'");
					}
				}
			}
			header('Location:index.php?do=navigation&sub=speedbar');
			exit;
		}
			
		$navis = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_quicknavi WHERE  Sektion = '" . $_SESSION['a_area'] . "' ORDER BY Position ASC");
		while($row = $sql->fetchrow())
		{
			array_push($navis, $row);
		}
		$GLOBALS['tmpl']->assign('navis', $navis);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Quicknavi']);
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/navigation/speedbar.tpl'));
	}
	
	//=======================================================
	// Navigationen listen
	//=======================================================
	function ListNavi()
	{
		if(perm('navigation_edit'))
		{
			// Neu anlegen
			if(isset($_POST['new']) && $_POST['new']==1)
			{
				$t1 = $_POST['Name_1'];
				$t2 = (!empty($_POST['Name_2'])) ? $_POST['Name_2'] : $t1;
				$t3 = (!empty($_POST['Name_3'])) ? $_POST['Name_3'] : $t1;
				
				$GLOBALS['db']->Query("INSERT INTO ".PREFIX."_navi_cat 
				(
					Id,
					Name_1,
					Name_2,
					Name_3,
					Sektion,
					Position,
					Aktiv
				) VALUES (
					'',
					'$t1',
					'$t2',
					'$t3',
					'" . $_SESSION['a_area'] . "',
					'" . $_POST['Position'] . "',
					'" . $_POST['Aktiv'] . "'
				)");
				header('Location:index.php?do=navigation&sub=edit&id=' . $GLOBALS['db']->InsertId());
				exit;
			}
			
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				foreach($_POST['nid'] as $nid => $var)
				{
					if(!empty($_POST['Name_1'][$nid]))
					{
						$t1 = $_POST['Name_1'][$nid];
						$t2 = (!empty($_POST['Name_2'][$nid])) ? $_POST['Name_2'][$nid] : $t1;
						$t3 = (!empty($_POST['Name_3'][$nid])) ? $_POST['Name_3'][$nid] : $t1;
						
						$GLOBALS['db']->Query("UPDATE ".PREFIX."_navi_cat 
						SET
							Name_1 = '$t1',
							Name_2 = '$t2',
							Name_3 = '$t3',
							Position = '" . $_POST['Position'][$nid] . "',
							Aktiv = '" . $_POST['Aktiv'][$nid] . "'
						WHERE
							Id = '$nid'");
					}	
				}
				header('Location:index.php?do=navigation');
				exit;
			}
			
			
			$navis = array();
			$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_navi_cat WHERE  Sektion = '" . $_SESSION['a_area'] . "' ORDER BY Id ASC");
			while($row = $sql->fetchrow())
			{
				array_push($navis, $row);
			}
			$GLOBALS['tmpl']->assign('navis', $navis);
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Navigation_list']);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/navigation/navi_categs.tpl'));
		}
	}
	
	//=======================================================
	// Navigation loeschen
	//=======================================================
	function DelNavi($id)
	{
		if(perm('navigation_edit'))
		{
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_navi WHERE NaviCat = '$id'");
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_navi_cat WHERE Id = '$id'");
			header('Location:index.php?do=navigation');
			exit;
		}
	}
	
	//=======================================================
	// Navigation bearbeiten
	//=======================================================
	function EditNavi($id)
	{
		if(perm('navigation_edit'))
		{
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				foreach ($_POST['Titel_1'] as $nid => $var)
				{
					if(!empty($_POST['Titel_1'][$nid]) && !empty($_POST['Dokument'][$nid]))
					{
						$update = $GLOBALS['db']->Query("UPDATE " . PREFIX . "_navi SET 
							Titel_1='" . $_POST['Titel_1'][$nid] . "', 
							Dokument='" . $_POST['Dokument'][$nid] . "',
							Position='" . @$_POST['Position'][$nid] . "',
							Ziel='" . $_POST['Ziel'][$nid] . "'
						WHERE Id='$nid'");
					}
				}
			}
			
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Navigation_docsedit']);
			$GLOBALS['tmpl']->assign('items', $this->getNavigation($id));
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/navigation/navi_show.tpl'));
		}
	}
	
	//=======================================================
	// Neuer Navigations-Punkt
	//=======================================================
	function NewNaviDoc($navi)
	{
		if(perm('navigation_edit'))
		{	
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$t1 = $_POST['Titel_1'];
				$t2 = (!empty($_POST['Titel_2'])) ? $_POST['Titel_2'] : $t1;
				$t3 = (!empty($_POST['Titel_3'])) ? $_POST['Titel_3'] : $t1;
				
				$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_navi 
				(
					Id, 	
					NaviCat, 	
					ParentId, 	
					Titel_1, 	
					Titel_2, 	
					Titel_3, 	
					Dokument, 	
					DokumentRub, 	
					Sektion, 	
					openonclick, 	
					group_id, 	
					Position, 	
					Ziel
				) VALUES (
					'',
					'$navi',
					'"  . $_POST['ParentId'] . "',
					'"  . $t1 . "',
					'"  . $t2 . "',
					'"  . $t3 . "',
					'"  . $_POST['Dokument'] . "',
					'',
					'"  . $_SESSION['a_area'] . "',
					'"  . $_POST['openonclick'] . "',
					'"  . implode(',',$_POST['group_id']) . "',
					'"  . $_POST['Position'] . "',
					'"  . $_POST['Ziel'] . "'
				)");
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Navigation_newdoc']);
			$GLOBALS['tmpl']->assign('items', $this->getNavigation($navi));
			$GLOBALS['tmpl']->assign('groups', $this->Groups());
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/navigation/edit_navidoc.tpl'));
		}
	}
	
	//=======================================================
	// Navigations-Punkt loeschen
	//=======================================================
	function DeleteNaviDoc($id,$navi)
	{
		if(perm('navigation_edit'))
		{
			$r_sub_navi = $GLOBALS['db']->Query("SELECT id FROM " . PREFIX . "_navi WHERE ParentId = '$id' ");
			
			while ($sub_navi = $r_sub_navi->fetchrow())
			{
				$r_del_sub_sub_navi = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_navi WHERE ParentId = '" . $sub_navi->id . "'");
			}
			
			$r_del_sub_navi = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_navi WHERE ParentId = '$id'");
			$r_del_navi = $GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_navi WHERE Id = '$id'");
			
		}
		header('Location:index.php?do=navigation&sub=edit&id=' . $navi);
		exit;
	}
	
	//=======================================================
	// Navigations-Punkt bearbeiten
	//=======================================================
	function EditNaviDoc($id)
	{
		if(perm('navigation_edit'))
		{
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_navi 
				SET 
					Titel_1 = '" . $_POST['Titel_1'] . "',
					Titel_2 = '" . @$_POST['Titel_2'] . "',
					Titel_3 = '" . @$_POST['Titel_3'] . "',
					Dokument = '" . $_POST['Dokument'] . "',
					openonclick = '" . $_POST['openonclick'] . "',
					group_id = '" . implode(',',$_POST['group_id']) . "',
					Position = '" . $_POST['Position'] . "',
					Ziel = '" . $_POST['Ziel'] . "'
				WHERE Id='$id'");
				
				if(isset($_POST['perms_to_section']) && $_POST['perms_to_section']==1)
				{
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_navi SET group_id = '" . implode(',',$_POST['group_id']) . "' WHERE Id!=0 AND NaviCat='" . $_POST['categ'] . "'");
				}
			}
			
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM ".PREFIX."_navi WHERE Id='$id'");
			$res->Gruppen = explode(',', $res->group_id);
			
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Navigation_docedit']);
			$GLOBALS['tmpl']->assign('res', $res);
			$GLOBALS['tmpl']->assign('groups', $this->Groups());
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/navigation/edit_navidoc.tpl'));
		}
	}
	
	//=======================================================
	// Navigations-Eintraege
	//=======================================================
	function getNavigation($id)
	{
		// 1. Ebene
		$items = array();
		$sql = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_navi WHERE NaviCat='$id' AND ParentId='0' ORDER BY Position ASC");
		while($row = $sql->fetchrow())
		{
			// 2. Ebene
			$items_2 = array();
			$sql_2 = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_navi WHERE NaviCat='$id' AND ParentId='$row->Id' ORDER BY Position ASC");
			while($row_2 = $sql_2->fetchrow())
			{
				// 3. Ebene
				$items_3 = array();
				$sql_3 = $GLOBALS['db']->Query("SELECT * FROM ".PREFIX."_navi WHERE NaviCat='$id' AND ParentId='$row_2->Id' ORDER BY Position ASC");
				while($row_3 = $sql_3->fetchrow())
				{
					array_push($items_3, $row_3);
				}
				$row_2->sub2 = $items_3;
				array_push($items_2, $row_2);
			}
			
			$row->sub1 = $items_2;
			array_push($items, $row);
		}
		return $items;
	}
	
	//=======================================================
	// Gruppen listen
	//=======================================================
	function Groups()
	{
		$result = $GLOBALS['db']->Query("SELECT Id AS ugroup,Name AS groupname FROM  " . PREFIX . "_benutzer_gruppen");
		$groups = array();
		while ($group = $result->fetchrow()) $groups[] = $group;
		return $groups;
	}
	
	//=======================================================
	// Nix
	//=======================================================
	function NaviSuggest()
	{
		$res = array("index.php" => "Startseite");
		foreach ($res as $key=>$value) 
		{
			echo stripslashes($key) . "\n"; // $key;
		}
	}
	
}
?>