<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
class Navigation
{
	var $_sitemap_tpl = 'sitemap.tpl';
	var $_navi_tpl = 'navigation.tpl';
	
	function getDocument()
	{
		$document = split("/", $_SERVER['PHP_SELF']);
		$QueryString = eregi_replace('[^-A-Za-zÀ-ÖØ-öø-ÿ0-9;?&=]', '', $_SERVER['QUERY_STRING']);
		
		return $document[count($document)-1] . "?" . $QueryString;
	
	}
	
	//=================================================================
	// Sitemap-Aufruf
	//=================================================================
	function getSitemap($document='', $area_id='', $group_id = -1, $param='')
	{
		$GLOBALS['tmpl']->assign('document', $this->getDocument());
		$sitemap = '';
		$navi_cat = (is_array($document) && isset($document['id']) && !empty($document['id'])) ? $document['id'] : 1;
		$tpl = (is_array($document) && isset($document['tpl']) && !empty($document['tpl'])) ? $document['tpl'] : $this->_sitemap_tpl;
		
		$document = $this->getDocument();
		$area_id = $_SESSION['Area'];
		$group_id = $_SESSION['user_group'];
		
		$out = array();
		
		$Lc = $_SESSION['Langcode'];
		$navi_titles = $GLOBALS['db']->Query("SELECT Id, Name_{$Lc} as name FROM " . PREFIX . "_navi_cat WHERE Aktiv='1' AND Sektion = '$area_id' ORDER BY Position ASC");
		while($row_title = $navi_titles->fetchrow())
		{
			$document['id'] = $row_title->Id;
			$sitemap .= $this->getStaticNavigation($document['id'], $area_id, -1, $param, $this->_sitemap_tpl);
		}
		
		return  $sitemap;
	}


	//=================================================================
	// Navigations-Aufruf
	//=================================================================
	function getStaticNavigation($document='', $area_id='', $group_id = -1, $param='', $t_force='')
	{
		$_SERVER['QUERY_STRING'] = preg_replace(array("/&page=([0-9]*)/","/&artpage=([0-9]*)/"), "", $_SERVER['QUERY_STRING']);

		$GLOBALS['tmpl']->assign('document', $this->getDocument());
		
		$navi_cat = (is_array($document) && isset($document['id']) && !empty($document['id']) || (!empty($t_force))) ? $document['id'] : 1;
		$tpl = (is_array($document) && isset($document['tpl']) && !empty($document['tpl'])) ? $document['tpl'] : $this->_navi_tpl;
		$tpl = (!empty($t_force)) ? $t_force : $this->_navi_tpl;
		$document = $this->getDocument();
		$area_id = $_SESSION['Area'];
		$group_id = $_SESSION['user_group'];
		
		$Lc = $_SESSION['Langcode'];
		$navi_title = $GLOBALS['db']->Query("SELECT 
			Name_{$Lc} AS name, 
			Name_1 AS defname
		FROM 
			" . PREFIX . "_navi_cat WHERE Aktiv='1' AND Id = '$navi_cat' AND Sektion = '$area_id'");
	
		$row_title = $navi_title->fetchrow();
		$row_title->name = ($row_title->name=='') ? $row_title->defname : $row_title->name;
		
		if(is_object($row_title))
		{
			if ($document != -1 && is_object($row_title))
			{
			  $no = '';
			  
				$r_navi = $GLOBALS['db']->Query("
					SELECT 
						a.Id as id,
						a.ParentId as parent_id,
						a.Dokument as document,
						a.Ziel as target,
						a.DokumentRub as document_rub 
					FROM 
						" . PREFIX . "_navi as a
					WHERE 
						a.Dokument = '$document' AND 
						a.NaviCat = '$navi_cat' AND 
						a.Sektion = $area_id 
						$no
					");
						
				$navi = $r_navi->fetchrow();
		
				if (isset($navi->parent_id) && $navi->parent_id != "0" && is_object($row_title))
				{
					$r_parent1 = $GLOBALS['db']->Query("
						SELECT 
							Id as id,
							ParentId as parent_id,
							Dokument as document,
							Ziel as target,
							DokumentRub as document_rub 
						FROM 
							" . PREFIX . "_navi 
						WHERE 
							id = '" . $navi->parent_id . "' AND 
							NaviCat = '$navi_cat' 
							$no"
						);

					$parent1 = $r_parent1->fetchrow();
				} 
				
				//=================================================================
				// wenn ParentId nicht 0 ist, dann existiert noch eine
				// ebene darueber
				//=================================================================
				if (isset($parent1->parent_id) && $parent1->parent_id != 0 && is_object($row_title)) 
				{
					
					$r_parent2 = $GLOBALS['db']->Query("
						SELECT
							Id as id,
							ParentId as parent_id,
							Dokument as document,
							Ziel as target,
							DokumentRub as document_rub
						FROM 
							" . PREFIX . "_navi 
						WHERE 
							Id = " . $parent1->parent_id . " AND 
							Sektion = $area_id  AND 
							NaviCat = '$navi_cat'  
							$no
						");
		
					$parent2 = $r_parent2->fetchrow();
				} 
			} 
		
			$r_navi = $GLOBALS['db']->Query("
				SELECT 
					Id as id, 
					Titel_{$Lc} as title, 
					Titel_1 as deftitle, 
					Dokument as document, 
					Sektion, 
					openonclick, 
					group_id,
					Position as posi,
					Ziel as target,
					DokumentRub as document_rub 
				FROM 
					" . PREFIX . "_navi 
				WHERE 
					ParentId = 0 AND  
					NaviCat = '$navi_cat'  AND 
					Sektion = $area_id 
					$no 
				ORDER BY
					Position ASC
				");
		
			$output = array(); 
			
			//=================================================================
			// erste ebene der navigation
			//=================================================================
			while ($navi = $r_navi->fetchrow())
			{
				$navi->title = ($navi->title=='') ? $navi->deftitle : $navi->title;
				$navi->group_array = ($navi->group_id != "") ? explode(",", $navi->group_id) : $navi->group_id; 
				
				//=================================================================
				// array der parameter in der url holen
				//=================================================================
				$navi_params = explode("&", trim(str_replace("index.php?", "", $navi->document)));
				$request_params = explode("&", $_SERVER['QUERY_STRING']); 
				
				//=================================================================
				// uebereinstimmungen der arrays berechnen
				//=================================================================
				$intersect = array_intersect($navi_params, $request_params);
		
				foreach ($intersect as $inter)
				{
					$pair = explode("=", $inter); 
					//=================================================================
					// wenn der parameter p in den uebereinstimmungen (page=agb)
					// dabei ist, markiere den navigationspunkt
					//=================================================================
					$defc = ($_REQUEST['p']=='content') ? 'p=' : 'page=';
					
					$static_nav = explode($defc, $navi->document);
					$static_req = explode($defc, $_SERVER['QUERY_STRING']);
					
					if(!empty($static_nav[1]) && is_array($static_nav) && $static_nav[1] != '')
					{
						$navi->active = false;
					} else {
						if ($pair[0] == "p" || $navi->document_rub == @$_REQUEST['p']) {
						$navi->active = true;
					} 
				  }
				} 
				
				//=================================================================
				// die navi nur dann anzeigen, wenn die gruppe die berechtigung hat
				//=================================================================
				if (@in_array($group_id, $navi->group_array) || $group_id == -1)
				{
					$search_parent = $navi->id;
					//=================================================================
					// der navigationspunkt wird expandiert angezeigt falls...
					//=================================================================
					$navi->document = (isset($navi->document)) ? $navi->document : "";
					$parent1->document = (isset($parent1->document)) ? $parent1->document : "";
					$parent2->document = (isset($parent2->document)) ? $parent2->document : "";
					if (!$navi->openonclick || 
						$navi->document == $document || 
						$parent1->document == $navi->document || 
						$parent2->document == $navi->document || 
						$document == -1 || 
						!empty($t_force))
					{
						
						//=================================================================
						// um den pfad spaeter verfolgen zu können
						// wird ein flag gesetzt
						//=================================================================
						if ($parent1->document == $navi->document || $parent2->document == $navi->document)
						{
							$navi->active = true;
						} 
						$q_n = "SELECT 
							DokumentRub as document_rub, 
							Id as id, 
							Titel_{$Lc} as title, 
							Dokument as document, 
							Sektion as area, 
							openonclick, 
							group_id, 
							Position,
							Ziel as target  
						FROM 
							" . PREFIX . "_navi 
						WHERE 
							ParentId  = '" . $navi->id . "' AND Titel_{$Lc} != '' AND Sektion = $area_id ORDER BY Position ASC";
					
						$r_sub_navi = $GLOBALS['db']->Query($q_n);
						$navi->sub_navi = array(); 
						
					
						//=================================================================
						// zweite ebene der navigation
						//=================================================================
						while ($sub_navi = $r_sub_navi->fetchrow())
						{
							$sub_navi->group_array = ($sub_navi->group_id != "") ? explode(",", $sub_navi->group_id) : $sub_navi->group_id;
							if (in_array($group_id, $sub_navi->group_array) || $group_id == -1) 
							{
								if (!empty($t_force) || !$sub_navi->openonclick || $sub_navi->document == $document || $parent1->document == $sub_navi->document || $parent2->document == $sub_navi->document || $document == -1) 
								{
									if ($parent1->document == $sub_navi->document || $parent2->document == $sub_navi->document)
									{
										$now_page = explode('index.php?', $_SERVER['REQUEST_URI']);
										$now = "index.php?" . $now_page[1];
										$sub_navi->active = true;
										
									} 
									
									$r_last_navi = $GLOBALS['db']->Query("SELECT 
										DokumentRub as document_rub, 
										Id as id, 
										Titel_{$Lc} as title, 
										Dokument as document, 
										Sektion as area, 
										openonclick, 
										group_id, 
										Position,
										Ziel 
									FROM 
										" . PREFIX . "_navi 
									WHERE 
										ParentId = " . $sub_navi->id . " AND Titel_{$Lc} != '' AND Sektion = $area_id ORDER BY Position ASC");
									$sub_navi->sub_navi = array(); 
									
									//=================================================================
									// letzte ebene der navigation
									//=================================================================
									while ($last_navi = $r_last_navi->fetchrow())
									{
										$last_navi->group_array = ($last_navi->group_id != "") ? explode(",", $last_navi->group_id) : $last_navi->group_id;
										$parent2->document = (!empty($parent2->document)) ? $parent2->document : "";
										$pos_last = (!empty($parent2->document)) ? stristr($last_navi->document,$parent2->document) : false;
										
										$now_page = explode('index.php?', $_SERVER['REQUEST_URI']);
										$now = "index.php?" . $now_page[1];
										
										if($pos_last!==false && $now==$last_navi->document) $last_navi->active = true;
										
										if (in_array($group_id, $last_navi->group_array) || $group_id == -1) 
										{
											array_push($sub_navi->sub_navi, $last_navi);
										} 
									} 
								} 
								array_push($navi->sub_navi, $sub_navi);
							} 
						} 
					} 
					array_push($output, $navi);
				} 
			} 
			
			$GLOBALS['tmpl']->assign('navi_title', $row_title->name);
			$GLOBALS['tmpl']->assign('SiteNavigation', $output);
			$naviout = $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/navi/' . $tpl);
			$GLOBALS['tmpl']->assign('navi', $naviout);
			return $naviout;
			
		}
	}
}
?>