<?php
class Links
{
	
	function DelRating($id)
	{
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_wertung WHERE Bereich='links' AND Objekt_Id='{$id}'");
		echo "<script>window.opener.location.reload(); window.close();</script>";
	}
	
	
	//=================================================================
	// Einstellungen
	//=================================================================
	function Settings()
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_links_einstellungen 
			SET 
				Kommentare = '" . $_POST['Kommentare'] . "',
				Wertung = '" . $_POST['Wertung'] . "',	
				LinkMelden = '1',	
				DefektMelden = '" . $_POST['DefektMelden'] . "',	
				Flaggen = '" . $_POST['Flaggen'] . "'
			");
		}
		
		$GLOBALS['tmpl']->assign('res', $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_links_einstellungen")); 
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Links_settings']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/settings.tpl'));
	}
	
	//=================================================================
	// Linkuebersicht
	//=================================================================
	function ListLinks()
	{
		$db_sort      = " ORDER BY Datum ASC";
		$nav_sort   = "&amp;sort=name_asc";
		$datesort = '';
		$def_search_n = '';
		$def_search = '';
		$namesort = '';
		$hitssort='';
		$usersort='';
		$categsort='';
		$activesort='';
		$scq='';
		$sc='';
		$pattern='';
		$pattern2='';
		$brokenq='';
		$brokenq_n='';
		$toplinksort='';
		
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			foreach($_POST['Aktiv'] as $lid => $li)
			{
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_links 
				SET 
					Kategorie = '" . $_POST['Kategorie'][$lid] . "',
					Aktiv = '" . $_POST['Aktiv'][$lid] . "',
					Hits = '" . $_POST['Hits'][$lid] . "',
					Sponsor = '" . $_POST['Sponsor'][$lid] . "' 
				WHERE Id = '$lid'");
				
				if(@$_POST['del'][$lid]==1)
				{
					$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_links WHERE Id='$lid'");
					$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kommentare WHERE Bereich='links' AND Objekt_Id='$lid'");
				}
			}
		}
		
		if(isset($_REQUEST['q']) && !empty($_REQUEST['q']) && $_REQUEST['q'] != 'empty' && strlen($_REQUEST['q'])>=3)
		{
			$pattern = preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9,_.:\/&=? ]*)/', '', $_REQUEST['q']);
			$pattern2 = koobispecialchars($pattern);
		}
		
		if(@$_REQUEST['categ']!='')
		{
			$scq = "AND (Kategorie='" . abs((int)$_REQUEST['categ']) . "') ";
			$sc = "&amp;categ=" . $_REQUEST['categ'];
		}
		
		if(@$_REQUEST['broken']==1)
		{
			$brokenq   = "AND (DefektGemeldet!='') ";
			$brokenq_n = "&amp;broken=1";
		}
		
		$_REQUEST['q']  = $pattern;
		$def_search_n   = "&amp;q=" . urlencode($pattern) . $sc;
		$def_search     = "{$scq} {$brokenq} AND (
		(Name_1 LIKE '%{$pattern}%' OR Beschreibung_1 LIKE '%{$pattern}%') OR 
		(Name_1 LIKE '%{$pattern2}%' OR Beschreibung_1 LIKE '%{$pattern2}%'))";
		
		
		switch(@$_REQUEST['sort'])
		{
			case '': default:
			case'date_desc':$db_sort='ORDER BY Datum DESC';$nav_sort='&amp;sort=date_desc';$datesort='date_asc';break;
			case'date_asc':$db_sort='ORDER BY Datum ASC';$nav_sort='&amp;sort=date_asc';$datesort='date_desc';break;
			
			case'name_asc':$db_sort = 'ORDER BY Name_1 ASC';$nav_sort = '&amp;sort=name_asc';$namesort = 'name_desc';break;
			case'name_desc':$db_sort='ORDER BY Name_1 DESC';$nav_sort='&amp;sort=name_desc';$namesort='name_asc';break;
			
			case'hits_desc':$db_sort='ORDER BY Hits DESC';$nav_sort='&amp;sort=hits_desc';$hitssort='hits_asc';break;
			case'hits_asc':$db_sort='ORDER BY Hits ASC';$nav_sort='&amp;sort=hits_asc';$hitssort='hits_desc';break;
			
			case'user_desc':$db_sort='ORDER BY Autor DESC';$nav_sort='&amp;sort=user_desc';$usersort='hits_asc';break;
			case'user_asc':$db_sort='ORDER BY Autor ASC';$nav_sort='&amp;sort=user_asc';$usersort='hits_desc';break;
			
			case'categ_desc':$db_sort='ORDER BY Kategorie DESC';$nav_sort='&amp;sort=categ_desc';$categsort='categ_asc';break;
			case'categ_asc':$db_sort='ORDER BY Kategorie ASC';$nav_sort='&amp;sort=categ_asc';$categsort='categ_desc';break;
			
			case'active_desc':$db_sort='ORDER BY Aktiv DESC';$nav_sort='&amp;sort=active_desc';$activesort='active_asc';break;
			case'active_asc':$db_sort='ORDER BY Aktiv ASC';$nav_sort='&amp;sort=active_asc';$activesort='active_desc';break;
			
			case'toplink_desc':$db_sort='ORDER BY Sponsor DESC';$nav_sort='&amp;sort=toplink_desc';$toplinksort='toplink_asc';break;
			case'toplink_asc':$db_sort='ORDER BY Sponsor ASC';$nav_sort='&amp;sort=toplink_asc';$toplinksort='toplink_desc';break;
			
		}
		
		
		$check = $GLOBALS['db']->RowQuery("SELECT 
			COUNT(Id) AS ResCount 
		FROM 
			" . PREFIX . "_links 
		WHERE 
			Sektion = '" . AREA . "' 
		{$def_search} {$db_sort}");
		
		$num = $check->ResCount;
		$limit = GetLimit(15);
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
			
		// Such-Abfrage
		$entries = array();
		$query_items = $GLOBALS['db']->Query("SELECT 
			*,
			Kategorie, 
			Name_1 AS Name,
			Beschreibung_1 AS Beschreibung 
		FROM 
			" . PREFIX . "_links 
		WHERE 
			Sektion = '" . AREA . "' 
		{$def_search} {$db_sort} LIMIT $a,$limit");
		
		
		while($row_items = $query_items->fetchrow())
		{
			// Wertung
			$row_items->User = getUsername($row_items->Autor);
			$row_items->Comments = getCommentCount('links', $row_items->Id);
			$row_items->CCount = getPageCommentCount($row_items->Id,'links');
			$row_items->Wertung = getRating($row_items->Id,'links');
			array_push($entries, $row_items);
		}
		
		// Navi
		if ($num > $limit) 
		{
			$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?do=links&sub=overview&categ=".@$_REQUEST['categ']."{$def_search_n}{$nav_sort}{$brokenq_n}&amp;pp={$limit}&amp;page={s}\">{t}</a> "));
		}
		
		$news_categ = array();
		$area = AREA;
		
		$GLOBALS['tmpl']->assign('Categs', $this->ListCategsSimple('', '', $news_categ, $area));
		$GLOBALS['tmpl']->assign('activesort', $activesort);
		$GLOBALS['tmpl']->assign('toplinksort', $toplinksort);
		$GLOBALS['tmpl']->assign('categsort', $categsort);
		$GLOBALS['tmpl']->assign('usersort', $usersort);
		$GLOBALS['tmpl']->assign('hitssort', $hitssort);
		$GLOBALS['tmpl']->assign('namesort', $namesort);
		$GLOBALS['tmpl']->assign('datesort', $datesort);
		$GLOBALS['tmpl']->assign('limit', $limit);
		$GLOBALS['tmpl']->assign('Entries', $entries);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Links']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/links.tpl'));
	}
	
	//=================================================================
	// Link kopieren
	//=================================================================
	function CopyLink($id)
	{
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_links WHERE Id='$id'");
		$q = "INSERT INTO " . PREFIX . "_links 
		(
			Id,	
			Kategorie, 	
			Datum, 	
			Bild, 	
			Name_1, 	
			Name_2, 	
			Name_3, 	
			Beschreibung_1, 	
			Beschreibung_2, 	
			Beschreibung_3, 	
			Url, 	
			Hits, 	
			Sektion, 	
			Autor, 	
			Aktiv,
			Sponsor
		) VALUES (
			'',
			'" . $res->Kategorie . "',
			'" . stime() . "',
			'" . $res->Bild . "',
			'" . addslashes($res->Name_1 . $GLOBALS['config_vars']['DbCopy']) . "',
			'" . addslashes($res->Name_2 . $GLOBALS['config_vars']['DbCopy']) ."',
			'" . addslashes($res->Name_3 . $GLOBALS['config_vars']['DbCopy']) ."',
			'" . addslashes($res->Beschreibung_1) . "',
			'" . addslashes($res->Beschreibung_2) . "',
			'" . addslashes($res->Beschreibung_3) . "',
			'" . $res->Url . "',
			'0',
			'" . $res->Sektion . "',
			'" . $_SESSION['benutzer_id'] . "',
			'" . $res->Aktiv . "',
			'" . $res->Sponsor . "'
		)";
		$GLOBALS['db']->Query($q);
		Logger('User ' . $_SESSION['user_name'] . ' copy Link (' . $res->Name_1 . ')', '3', $_SESSION['benutzer_id']);
		echo "<script>window.opener.location.reload(); window.close();</script>";
	}
	
	//=================================================================
	// Neuer Link
	//=================================================================
	function NewLink()
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$q = "INSERT INTO " . PREFIX . "_links 
		(
			Id,	
			Kategorie, 	
			Datum, 	
			Bild, 	
			Name_1, 	
			Name_2, 	
			Name_3, 	
			Beschreibung_1, 	
			Beschreibung_2, 	
			Beschreibung_3, 	
			Url, 	
			Hits, 	
			Sektion, 	
			Autor, 	
			Aktiv,
			Sprache
		) VALUES (
			'',
			'" . $_POST['Kategorie'] . "',
			'" . stime() . "',
			'" . $_POST['newImg_1'] . "',
			'" . $_POST['Name'] . "',
			'" . $_POST['Name'] . "',
			'" . $_POST['Name'] . "',
			'" . imgToPrefix($_POST['Beschreibung']) . "',
			'" . imgToPrefix($_POST['Beschreibung']) . "',
			'" . imgToPrefix($_POST['Beschreibung']) . "',
			'" . $_POST['Url'] . "',
			'0',
			'" . AREA. "',
			'" . $_SESSION['benutzer_id'] . "',
			'" . $_POST['Aktiv'] . "',
			'" . $_POST['Sprache'] . "'
		)";
		$GLOBALS['db']->Query($q);
		Logger('User ' . $_SESSION['user_name'] . ' add new Link (' . $_POST['Name'] . ')', '3', $_SESSION['benutzer_id']);
		echo "<script>window.opener.location.reload(); window.close();</script>";
		}
		
		$categs = array();
		$area = AREA;
		
		$GLOBALS['tmpl']->assign('Categs', $this->ListCategsSimple('', '', $categs, $area));
		$GLOBALS['tmpl']->assign('Beschreibung', fckInit(' ', 'Beschreibung', 350, 'Content'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Links_add']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/link_new.tpl'));
	}
	
	
	//=================================================================
	// Link bearbeiten
	//=================================================================
	function EditLink($id)
	{
		$LC = (isset($_REQUEST['langcode']) && $_REQUEST['langcode']!='') ? $_REQUEST['langcode'] : 1;
		$SetAll = '';
		
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$db_extra = '';
			
			if($LC==1)
			{
				$Bild = '';
				
				if(isset($_POST['newImg_1']) && !empty($_POST['newImg_1']))
				{
					$Bild = "Bild='" . $_POST['newImg_1'] . "',";
				}
				$Dl = (isset($_POST['DelDM']) && $_POST['DelDM']==1) ? "DefektGemeldet='',DEmail='',DName='',DDatum=''," : "";
				$Bild        = (isset($_POST['NoImg']) && $_POST['NoImg']==1  && empty($_POST['newImg_1'])) ? "Bild = ''," : $Bild;
				$db_extra = "{$Bild} Sprache='" . @$_POST['Sprache']. "', Url='" . $_POST['Url']. "', Kategorie='" . $_POST['Kategorie']. "', Aktiv='" . $_POST['Aktiv']. "', {$Dl} ";
			}
			
			if(isset($_POST['saveAllLang']) && $_POST['saveAllLang']==1)
			{
				$SetAll = "
					,Name_2 = '" . imgToPrefix($_POST['Name']) . "'
					,Beschreibung_2 = '" . imgToPrefix($_POST['Beschreibung']) . "'
					,Name_3 = '" . imgToPrefix($_POST['Name']) . "'
					,Beschreibung_3 = '" . imgToPrefix($_POST['Beschreibung']) . "'
					";
			}
			
			$db_in = "UPDATE " . PREFIX . "_links SET {$db_extra} Name_{$LC} = '" . $_POST['Name'] . "', Beschreibung_{$LC} = '" . imgToPrefix($_POST['Beschreibung']) . "' {$SetAll} WHERE Id='$id'";
			$GLOBALS['db']->Query($db_in);
			Logger('User ' . $_SESSION['user_name'] . ' edit Link (' . $_POST['Name']. ')', '3', $_SESSION['benutzer_id']);
			echo "<script>window.opener.location.reload();</script>";
		}
		
		$id = abs((int)$id);
		$res = $GLOBALS['db']->RowQuery("SELECT *, Beschreibung_{$LC} as Beschreibung, Name_{$LC} as Name FROM " . PREFIX . "_links WHERE Id='$id'");
		$categs = array();
		$area = AREA; 
		
		$GLOBALS['tmpl']->assign('res',$res); 
		$GLOBALS['tmpl']->assign('Categs', $this->ListCategsSimple('', '', $categs, $area));
		$GLOBALS['tmpl']->assign('Beschreibung', fckInit(imgPrefix($res->Beschreibung) . ' ', 'Beschreibung', 350, 'Content'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Links_edit']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/link_edit.tpl'));
	}
	
	//=================================================================
	// Kategorie loeschen
	//=================================================================
	function DelCateg($id)
	{
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_links_kategorie WHERE Id='$id'");
		Logger('User ' . $_SESSION['user_name'] . ' delete Link-Category (' . $id . ')', '3', $_SESSION['benutzer_id']);
		header('Location:index.php?do=links&sub=categs');
		exit;
	}
	
	//=================================================================
	// Kategorien listen
	//=================================================================
	function LinkCategs()
	{
		$categs = array();
		$area = AREA;
		
		$GLOBALS['tmpl']->assign('Categs', $this->ListCategsSimple('', '', $categs, $area));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Links_categs']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/link_categs.tpl'));
	}
	
	//=================================================================
	// Kategorie bearbeiten
	//=================================================================
	function EditCateg($id)
	{	
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$Name_1 = $_POST['Name_1'];
			$Name_2 = (isset($_POST['Name_2']) && $_POST['Name_2']!='') ? $_POST['Name_2'] : $_POST['Name_1'];
			$Name_3 = (isset($_POST['Name_3']) && $_POST['Name_3']!='') ? $_POST['Name_3'] : $_POST['Name_1'];
			
			$Beschreibung_1 = $_POST['Beschreibung_1'];
			$Beschreibung_2 = (isset($_POST['Beschreibung_2']) && $_POST['Beschreibung_2']!='') ? $_POST['Beschreibung_2'] : $_POST['Beschreibung_1'];
			$Beschreibung_3 = (isset($_POST['Beschreibung_3']) && $_POST['Beschreibung_3']!='') ? $_POST['Beschreibung_3'] : $_POST['Beschreibung_1'];
			
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_links_kategorie 
			SET 
				Name_1 = '$Name_1',
				Name_2 = '$Name_2',
				Name_3 = '$Name_3',
				Beschreibung_1 = '$Beschreibung_1',
				Beschreibung_2 = '$Beschreibung_2',
				Beschreibung_3 = '$Beschreibung_3' 
			WHERE Id='$id'");
			Logger('User ' . $_SESSION['user_name'] . ' edit Link-Category (' . $Name_1 . ')', '3', $_SESSION['benutzer_id']);
			echo "<script>window.opener.location.reload(); window.close();</script>";
		}
		
		$categs = array();
		$area = AREA;
		$res = $GLOBALS['db']->RowQuery("SELECT *  FROM " . PREFIX . "_links_kategorie WHERE Id='$id'");
		
		$GLOBALS['tmpl']->assign('res', $res); 
		$GLOBALS['tmpl']->assign('Categs', $this->ListCategsSimple('', '', $categs, $area));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Links_categs']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/link_categ.tpl'));
	}
	
	//=================================================================
	// Kategorie hinzufuegen
	//=================================================================
	function AddCateg()
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$Name_1 = $_POST['Name_1'];
			$Name_2 = (isset($_POST['Name_2']) && $_POST['Name_2']!='') ? $_POST['Name_2'] : $_POST['Name_1'];
			$Name_3 = (isset($_POST['Name_3']) && $_POST['Name_3']!='') ? $_POST['Name_3'] : $_POST['Name_1'];
			
			$Beschreibung_1 = $_POST['Beschreibung_1'];
			$Beschreibung_2 = (isset($_POST['Beschreibung_2']) && $_POST['Beschreibung_2']!='') ? $_POST['Beschreibung_2'] : $_POST['Beschreibung_1'];
			$Beschreibung_3 = (isset($_POST['Beschreibung_3']) && $_POST['Beschreibung_3']!='') ? $_POST['Beschreibung_3'] : $_POST['Beschreibung_1'];
			
			$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_links_kategorie 
			(
				Id,
				Parent_Id,
				Name_1,
				Name_2,
				Name_3,
				Beschreibung_1,
				Beschreibung_2,
				Beschreibung_3,
				Sektion
			) VALUES (
				'',
				'$_POST[categ]',
				'$Name_1',
				'$Name_2',
				'$Name_3',
				'$Beschreibung_1',
				'$Beschreibung_2',
				'$Beschreibung_3',
				'$_SESSION[a_area]'
			)");
			Logger('User ' . $_SESSION['user_name'] . ' add new Link-Category (' . $Name_1 . ')', '3', $_SESSION['benutzer_id']);
			echo "<script>window.opener.location.reload(); window.close();</script>";
		}
		
		$categs = array();
		$area = AREA;
		
		$GLOBALS['tmpl']->assign('Categs', $this->ListCategsSimple('', '', $categs, $area));
		$GLOBALS['tmpl']->assign('new', 1); 
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Links_categs']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/links/link_categ.tpl'));
	}
	
	//=================================================================
	// Kategorien fuer Dropdown holen
	//=================================================================
	function ListCategsSimple($id, $prefix, &$news_categ, &$area)
	{
		$query = $GLOBALS['db']->Query("SELECT Id, Parent_Id, Name_1 AS Name FROM " . PREFIX . "_links_kategorie WHERE Parent_Id = '$id' AND Sektion = '$area' ORDER BY Name ASC");
		if (!$query->numrows()) 
		{
			return;
		} 
		
		while ($item = $query->fetchrow())
		{
			$item->visible_title = $prefix . "  " . $item->Name;
			array_push($news_categ, $item);
			$this->ListCategsSimple($item->Id,$prefix . "- ",$news_categ,$area);
			
		} 
		return $news_categ;
	}
}
?>