<?php
class News
{
	function DelRating($id)
	{
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_wertung WHERE Bereich='news' AND Objekt_Id='{$id}'");
		echo "<script>window.opener.location.reload(); window.close();</script>";
	}
	
	//=================================================================
	// Kategorie loeschen
	//=================================================================
	function delCateg($id)
	{
		if(perm('news_category'))
		{
			$this->categDel($id);
		}
		header('location:index.php?do=news&sub=categories');
		exit;
	}

	//=================================================================
	// Kategorie bearbeiten
	//=================================================================
	function editCateg($id)
	{
		if(perm('news_category'))
		{
			// 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'];
				
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_news_kategorie 
				SET 
					Name_1 = '$Name_1',
					Name_2 = '$Name_2',
					Name_3 = '$Name_3' 
				WHERE Id='$id'");
				
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_news_kategorie  WHERE Id='$id'");
			$GLOBALS['tmpl']->assign('res', $res);
			$GLOBALS['tmpl']->assign('newscategs', $this->Categs());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Global_CategEdit']); 
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/news_categ.tpl'));
		} else {
			no_perm();
		}
	}
	
	//=================================================================
	// Kategorie neu
	//=================================================================
	function newCateg()
	{
		if(perm('news_category'))
		{
			// 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'];
				
				$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_news_kategorie 
				(
					Id,
					Parent_Id,
					Name_1,
					Name_2,
					Name_3,
					Sektion
				) VALUES (
					'',
					'$_POST[categ]',
					'$Name_1',
					'$Name_2',
					'$Name_3',
					'$_SESSION[a_area]'
				)");
				
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$GLOBALS['tmpl']->assign('new', 1);
			$GLOBALS['tmpl']->assign('newscategs', $this->Categs());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Global_NewCateg']); 
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/news_categ.tpl'));
		} else {
			no_perm();
		}
	}
	
	


	//=================================================================
	// News hinzufuegen
	//=================================================================
	function addNews()
	{
		if(perm('news_new'))
		{
			// Speichern
			if(isset($_POST['save']) && $_POST['save']==1)
			{
				$end = '0';
				
				$start = @explode('.',$_POST['ZeitStart']);
				$start = @mktime(0,0,1,$start[1],$start[0],$start[2]);
				
				if(isset($_POST['ZeitEnde']) && $_POST['ZeitEnde']>1)
				{
					$end = @explode('.',$_POST['ZeitEnde']);
					$end = @mktime(23,59,49,$end[1],$end[0],$end[2]);
				}
				
				@$_POST['Bild_1'] = @textToPrefix($_POST['Bild_1']);
				@$_POST['Bild_2'] = @textToPrefix($_POST['Bild_2']);
				
				$query = "INSERT INTO " . PREFIX . "_news 
				(
					Id,
					Kategorie,
					Autor,
					Sektion,
					Zeit,
					ZeitStart,
					ZeitEnde,
					Titel1,
					Titel2,
					Titel3,
					Intro1,
					Intro2,
					Intro3,
					News1,
					News2,
					News3,
					Bild1,
					Bild2,
					Bild3,
					BildAusrichtung,
					Textbilder1,
					AlleSektionen,
					Aktiv,
					Suche, 	
					Bewertung, 	
					Kommentare, 	
					Tags, 	
					Galerien,
					Topnews,
					Topnews_Bild_1,
					Topnews_Bild_2,
					Topnews_Bild_3 
				) VALUES (
					'',
					'" . $_POST['categ'] . "',
					'" . $_SESSION['benutzer_id'] . "',
					'" . $_SESSION['a_area'] . "',
					'" . stime() . "',
					'" . $start . "',
					'" . $end . "',
					'" . @$_POST['Titel'] . "',
					'" . @$_POST['Titel'] . "',
					'" . @$_POST['Titel'] . "',
					'" . @$_POST['Intro'] . "',
					'" . @$_POST['Intro'] . "',
					'" . @$_POST['Intro'] . "',
					'" . @$_POST['News'] . "',
					'" . @$_POST['News'] . "',
					'" . @$_POST['News'] . "',
					'" . @$_POST['Bild_1'] . "',
					'" . @$_POST['Bild_1'] . "',
					'" . @$_POST['Bild_1'] . "',
					'" . @$_POST['BildAusrichtung'] . "',
					'" . addslashes(base64_decode($_POST['inline_screens'])) . "',
					'" . $_POST['AlleSektionen'] . "',
					'" . $_POST['Aktiv'] . "',
					'" . $_POST['Suche'] . "', 	
					'" . $_POST['Bewertung'] . "',	
					'" . $_POST['Kommentare'] . "',	
					'" . $_POST['Tags'] . "',	
					'" . @implode(',',@$_POST['Gallery']) . "',
					'" . @$_POST['Topnews'] . "',
					'" . @$_POST['Bild_2'] . "',
					'" . @$_POST['Bild_2'] . "',
					'" . @$_POST['Bild_2'] . "'
				)";
				$GLOBALS['db']->Query($query);
				echo "<script>window.opener.location.reload(); window.close();</script>";
			}
			
			$GLOBALS['tmpl']->assign('ContentLinks',getContentLinks(AREA));
			$GLOBALS['tmpl']->assign('cforms',getInlineForms());
			$GLOBALS['tmpl']->assign('Gallery', $this->galCategs($_SESSION['a_area'])); 
			$GLOBALS['tmpl']->assign('Intro', fckInit(' ', 'Intro', 150, 'Basic','false'));
			$GLOBALS['tmpl']->assign('News', fckInit(' ', 'News', 350, 'Content'));
			$GLOBALS['tmpl']->assign('newscategs', $this->Categs());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['News_new']); 
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/news_new.tpl'));
			
		} else {
			no_perm();
		}
	}
	
	
	//=================================================================
	// News bearbeiten
	//=================================================================
	function editNews($id)
	{
		if(perm('news_edit'))
		{
			$check =  $GLOBALS['db']->RowQuery("SELECT Autor FROM " . PREFIX . "_news  WHERE Id='$id'");
			if($check->Autor != $_SESSION['benutzer_id'] && (!perm('news_edit_all')))
			{
				$this->nopermDoc();
			} else {
				$LC = (isset($_REQUEST['langcode']) && !empty($_REQUEST['langcode'])) ? abs((int)$_REQUEST['langcode']) : 1;
				$isdb = '';
				$start = '';
				$end = '';
				$categ = '';
				$Bild = '';
				$TopnewsBild = '';
				$Gallery = '';
				$BildAusr = '';
				$Sonstiges = '';
				$SetAll = '';
				
				
				// Speichern
				if(isset($_POST['save']) && $_POST['save']==1)
				{	
					if(perm('gallery_inlineshots') && isset($_POST['inline_screens']))
					{
						$isdb = ",Textbilder{$LC} = '" . addslashes(base64_decode($_POST['inline_screens'])) . "' ";
					}
					
					$Bild        = (isset($_POST['NoImg']) && $_POST['NoImg']==1) ? ",Bild{$LC} = ''" : ",Bild{$LC} = '" . @textToPrefix($_POST['Bild_1']) . "'";
					$TopnewsBild = (isset($_POST['NoTopnewsImg']) && $_POST['NoTopnewsImg']==1) ? ",Topnews_Bild_{$LC} = ''" : ",Topnews_Bild_{$LC} = '" . @textToPrefix($_POST['Bild_2']) . "'";
					
					// Nur speichern, wenn 1. Sprache gewaehlt. Sonst nur Titel, Intro und Text
					if(isset($_POST['langcode']) && $_POST['langcode']==1)
					{
						$categ = ",Kategorie='" . $_POST['categ'] . "'";
						$Gallery = ",Galerien='" . @implode(',',@$_POST['Gallery']) . "'"; 
						
						// Veroeffentlichung
						if(!empty($_POST['ZeitStart']))
						{
							$start = @explode('.',$_POST['ZeitStart']);
							$start = @mktime(0,0,1,$start[1],$start[0],$start[2]);
							$start = ",ZeitStart='{$start}'";
						}
						
						if(isset($_POST['saveAllLang']) && $_POST['saveAllLang']==1)
						{
							$SetAll = "
								,Intro2 = '" . imgToPrefix($_POST['Intro']) . "'
								,News2 = '" . imgToPrefix($_POST['News']) . "'
								,Intro3 = '" . imgToPrefix($_POST['Intro']) . "'
								,News3 = '" . imgToPrefix($_POST['News']) . "'
								";
						}
						
						$Sonstiges = "
							{$SetAll}
							,Tags = '" . @trim($_POST['Tags']) . "'
							,BildAusrichtung = '" . $_POST['BildAusrichtung'] . "'
							,Suche = '" . $_POST['Suche'] . "'
							,Bewertung = '" . $_POST['Bewertung'] . "'
							,Kommentare = '" . $_POST['Kommentare'] . "'
							,Aktiv = '" . $_POST['Aktiv'] . "'
							,AlleSektionen = '" . $_POST['AlleSektionen'] . "'
						";
						// Ablauf
						if(!empty($_POST['ZeitEnde']))
						{
							$end = @explode('.',$_POST['ZeitEnde']);
							$end = @mktime(23,59,49,$end[1],$end[0],$end[2]);
							$end = ",ZeitEnde='{$end}'";
						} else {
							$end = ",ZeitEnde='0'";
						}
					}
					
					
					
					$GLOBALS['db']->RowQuery("UPDATE " . PREFIX . "_news 
					SET
						Topnews  = '" . $_POST['Topnews'] . "',
						Titel{$LC} = '" . $_POST['Titel'] . "',
						Topnews = '" . $_POST['Topnews'] . "',
						Intro{$LC} = '" . imgToPrefix($_POST['Intro']) . "',
						News{$LC} = '" . imgToPrefix($_POST['News']) . "'
						{$categ}
						{$end}
						{$Bild}
						{$TopnewsBild}
						{$Sonstiges}
						{$start}
						{$Gallery}
						{$isdb} 
					WHERE Id='$id'");
					
				}
				
				$LC = (isset($_REQUEST['langcode']) && !empty($_REQUEST['langcode'])) ? abs((int)$_REQUEST['langcode']) : 1;
				$news =  $GLOBALS['db']->RowQuery("SELECT 
					*, 
					Bild{$LC} as Bild,
					Textbilder{$LC} as Textbilder,
					Titel{$LC} as Titel,
					Topnews_Bild_{$LC} as Topnews_Bild,
					Intro{$LC} as Intro,
					News{$LC} as News 
				FROM " . PREFIX . "_news  WHERE Id='$id'");
				
				$news->Galerien = explode(',', $news->Galerien);
				$news->Bild = imgPrefix($news->Bild);
				$news->Topnews_Bild = imgPrefix($news->Topnews_Bild);
				
				$GLOBALS['tmpl']->assign('cforms',getInlineForms());
				$GLOBALS['tmpl']->assign('Gallery', $this->galCategs($_SESSION['a_area'])); 
				$GLOBALS['tmpl']->assign('Intro', fckInit(imgPrefix($news->Intro) . ' ', 'Intro', 150, 'Basic','false'));
				$GLOBALS['tmpl']->assign('News', fckInit(imgPrefix($news->News) . ' ', 'News', 350, 'Content'));
				$GLOBALS['tmpl']->assign('ContentLinks',getContentLinks(AREA));
				$GLOBALS['tmpl']->assign('InlineShots', unserialize($news->Textbilder)); 
				$GLOBALS['tmpl']->assign('news', $news); 
				$GLOBALS['tmpl']->assign('field_inline', "Textbilder{$LC}"); 
				$GLOBALS['tmpl']->assign('newscategs', $this->Categs());
				$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['News_edit']); 
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/news_edit.tpl'));
			}
		} else {
			no_perm();
		}
	}
	
	//=================================================================
	// Bild-Kategorien holen
	//=================================================================
	function galCategs($area)
	{
		$categs = array();
		$query = $GLOBALS['db']->Query("SELECT Id,Name_1 AS CategName FROM " . PREFIX . "_galerie_kategorien WHERE Sektion = '" . $area . "' ORDER BY Name_1 ASC ");
		while($row = $query->fetchrow())
		{
			$gals = array();
			$qg = $GLOBALS['db']->Query("SELECT Id AS GalId,Name_1 AS GalName FROM " . PREFIX . "_galerie WHERE Kategorie = '$row->Id' ORDER BY Name_1 ASC ");
			while($rowg = $qg->fetchrow())
			{
				array_push($gals, $rowg);
			}
			
			$row->Gals = $gals;
			array_push($categs, $row);
		}
		
		return $categs;
	}
	
	//=================================================================
	// News loeschen
	//=================================================================
	function deleteNews($id)
	{
		$check =  $GLOBALS['db']->RowQuery("SELECT Autor FROM " . PREFIX . "_news  WHERE Id='$id'");
		if($check->Autor != $_SESSION['benutzer_id'] && (perm('news_delete_all')))
		{
			$GLOBALS['db']->RowQuery("DELETE FROM " . PREFIX . "_news  WHERE Id='$id'");
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kommentare WHERE Bereich='news' AND Objekt_Id='$row->Id'");	
		} else {
			if(perm('news_delete'))
			{
				$GLOBALS['db']->RowQuery("DELETE FROM " . PREFIX . "_news  WHERE Id='$id'");
				$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kommentare WHERE Bereich='news' AND Objekt_Id='$row->Id'");
			}	
		}
		
		echo '<script>window.opener.location.reload(); window.close();</script>';
	}
	
	
	//=================================================================
	// Alle News listen
	//=================================================================
	function showNews()
	{
		$def_order    = 'ORDER BY Id DESC';
		$def_order_n  = '';
		$def_search   = '';
		$def_status   = '';
		$def_status_n = '';
		$def_categ    = '';
		$def_categ_n  = '';
		$def_search_n = '';
		$def_datetill = '';
		$def_datetill_n = '';
		$def_hits     = '';
		$def_hits_n   = '';
		$news         = array();
		$area         = $_SESSION['a_area'];
		$limit        = GetLimit();
		
		// Schnellspeichern
		if(isset($_POST['quicksave']) && $_POST['quicksave']==1)
		{
			foreach($_POST['nid'] as $cat => $nid)
			{
				 $GLOBALS['db']->Query("UPDATE " . PREFIX . "_news SET Topnews = '" . $_POST['Topnews'][$nid] . "', Kategorie='" . $_POST['Kategorie'][$nid] . "' WHERE Id='$nid'");
			}
		}
		
		
		// Multiaktionen
		if(isset($_POST['multi']) && $_POST['multi']!='' && isset($_SESSION['NewsSearch']) && perm('news_multioptions'))
		{
			switch($_POST['multiaction'])
			{
				
				case 'movecateg':
					 $GLOBALS['db']->Query("UPDATE " . PREFIX . "_news SET Kategorie='" . $_POST['newcateg'] . "' " . $_SESSION['NewsSearch'] . " AND Sektion='{$area}'");
				break;
				
				case 'open':
					 $GLOBALS['db']->Query("UPDATE " . PREFIX . "_news SET Aktiv='1' " . $_SESSION['NewsSearch'] . " AND Sektion='{$area}'");
				break;
				
				case 'close':
					$GLOBALS['db']->Query("UPDATE " . PREFIX . "_news SET Aktiv='0' " . $_SESSION['NewsSearch'] . " AND Sektion='{$area}'");
				break;
				
				case 'delete':
					$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_news " . $_SESSION['NewsSearch'] . " AND Sektion='{$area}'");
				break;
			}
			
			$GLOBALS['tmpl']->assign('multi_done',1);
			
		} else {
			unset($_SESSION['NewsSearch']);
		}
		
		// Kategorie
		if(isset($_REQUEST['categ']) && $_REQUEST['categ']!='0')
		{
			$def_categ = "AND Kategorie = '" . abs((int)$_REQUEST['categ']) . "'";
			$def_categ_n = "&amp;categ=" . abs((int)$_REQUEST['categ']);
		}
		
		// Hits
		if(isset($_REQUEST['hits_from']) && isset($_REQUEST['hits_to']) && $_REQUEST['hits_from']<$_REQUEST['hits_to'])
		{
			$def_hits = "AND (Hits BETWEEN '" . abs((int)$_REQUEST['hits_from']) . "' AND '" . abs((int)$_REQUEST['hits_to']) . "')";
			$def_hits_n = "&amp;hits_from=" . abs((int)$_REQUEST['hits_from']) . "&amp;hits_to=" . abs((int)$_REQUEST['hits_to']);
		}
		
		// Status
		if(isset($_REQUEST['aktiv']) && $_REQUEST['aktiv']!='')
		{
			$def_status = "AND Aktiv = '" . abs((int)$_REQUEST['aktiv']) . "'";
			$def_status_n = "&amp;aktiv=" . abs((int)$_REQUEST['aktiv']);
		}
		
		// Datum erstellt bis
		if(!empty($_REQUEST['date_till']) && !empty($_REQUEST['date_till']))
		{
			$lafrom = @explode('.',$_REQUEST['date_till']);
			$lafrom = @mktime(23,59,59,$lafrom[1],$lafrom[0],$lafrom[2]);
			
			if($lafrom)
			{
				$def_datetill = " AND (Zeit <= $lafrom) ";
				$def_datetill_n = "&amp;date_till=" . $_REQUEST['date_till'];
			}
		}
		
		
		// Suche wurde gestartet...
		if(isset($_REQUEST['q']) && !empty($_REQUEST['q']) && strlen($_REQUEST['q'])>=1)
		{
			$q = trim(preg_replace('/([^-A-Za-zÀ-ÖØ-öø-ÿ0-9.@ ]*)/', '', $_REQUEST['q']));
			$_REQUEST['q'] = $q;
			
			$def_search = " 
			AND (
				Titel1 LIKE '%$q%' OR 
				Titel2 LIKE '%$q%' OR 
				Titel3 LIKE '%$q%' 
			)";
			$def_search_n = "&amp;q={$q}";
		}
		
		// Sortierungen
		if(isset($_REQUEST['sort']))
		{
			$curr_page = ((isset($_REQUEST['page'])) ? '&page=' . abs((int)($_REQUEST['page'])) : '&amp;page=1');
			
			switch($_REQUEST['sort'])
			{
				case 'name_asc' : 
					$def_order    = ' ORDER BY Titel1 ASC';
					$def_order_n  = '&sort=name_asc' . $curr_page;
					$def_order_ns = '&sort=name_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('name_s',$def_order_ns);
				break;
				
				case 'name_desc': 
					$def_order    = ' ORDER BY Titel1 DESC'; 
					$def_order_n  = '&sort=name_desc' . $curr_page;
					$def_order_ns = '&sort=name_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('name_s',$def_order_ns);
				break;
				
				case 'username_asc' : 
					$def_order    = ' ORDER BY Autor ASC';
					$def_order_n  = '&sort=username_asc' . $curr_page;
					$def_order_ns = '&sort=username_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('username_s',$def_order_ns);
				break;
				
				case 'username_desc' : 
					$def_order    = ' ORDER BY Autor DESC';
					$def_order_n  = '&sort=username_desc' . $curr_page;
					$def_order_ns = '&sort=username_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('username_s',$def_order_ns);
				break;
				
				
				case 'date_asc' : 
					$def_order    = ' ORDER BY Zeit ASC';
					$def_order_n  = '&sort=date_asc' . $curr_page;
					$def_order_ns = '&sort=date_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('date_s',$def_order_ns);
				break;
				
				case 'date_desc' : 
					$def_order    = ' ORDER BY Zeit DESC';
					$def_order_n  = '&sort=date_desc' . $curr_page;
					$def_order_ns = '&sort=date_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('date_s',$def_order_ns);
				break;
				
				
				case 'start_asc' : 
					$def_order    = ' ORDER BY ZeitStart ASC';
					$def_order_n  = '&sort=start_asc' . $curr_page;
					$def_order_ns = '&sort=start_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('start_s',$def_order_ns);
				break;
				
				case 'start_desc' : 
					$def_order    = ' ORDER BY ZeitStart DESC';
					$def_order_n  = '&sort=start_desc' . $curr_page;
					$def_order_ns = '&sort=start_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('start_s',$def_order_ns);
				break;
				
				case 'end_asc' : 
					$def_order    = ' ORDER BY ZeitEnde ASC';
					$def_order_n  = '&sort=end_asc' . $curr_page;
					$def_order_ns = '&sort=end_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('end_s',$def_order_ns);
				break;
				
				case 'end_desc' : 
					$def_order    = ' ORDER BY ZeitEnde DESC';
					$def_order_n  = '&sort=end_desc' . $curr_page;
					$def_order_ns = '&sort=end_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('end_s',$def_order_ns);
				break;
				
				case 'hits_asc' : 
					$def_order    = ' ORDER BY Hits ASC';
					$def_order_n  = '&sort=hits_asc' . $curr_page;
					$def_order_ns = '&sort=hits_desc' . $curr_page;
					$GLOBALS['tmpl']->assign('hits_s',$def_order_ns);
				break;
				
				case 'hits_desc' : 
					$def_order    = ' ORDER BY Hits DESC';
					$def_order_n  = '&sort=hits_desc' . $curr_page;
					$def_order_ns = '&sort=hits_asc' . $curr_page;
					$GLOBALS['tmpl']->assign('hits_s',$def_order_ns);
				break;
				
			}
		}
		
		$sql = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_news WHERE Sektion='{$area}' {$def_search} {$def_categ} {$def_status} {$def_hits} {$def_datetill}");
		
		$num = $sql->numrows();
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		$q = "SELECT * FROM " . PREFIX . "_news WHERE Sektion='{$area}' {$def_search} {$def_status} {$def_categ} {$def_hits} {$def_datetill} {$def_order} LIMIT $a,$limit";
		$sql = $GLOBALS['db']->Query($q);
		while($row=$sql->fetchrow())
		{
			$row->Comments = getCommentCount('news', $row->Id);
			$row->User = getUsername($row->Autor);
			$row->Wertung = getRating($row->Id,'news');
			array_push($news,$row);
		}
		
	
		$ordstr = "index.php?do=news&amp;sub=overview{$def_search_n}{$def_status_n}{$def_categ_n}&amp;pp={$limit}{$def_hits_n}{$def_datetill_n}";
		$nastr = "{$def_order_n}{$def_search_n}{$def_status_n}{$def_categ_n}&amp;pp={$limit}{$def_hits_n}{$def_datetill_n}";
		
		
		if(isset($_POST['startsearch']) && $_POST['startsearch']==1) 
		{
			$_SESSION['NewsSearch'] = "WHERE Id!='0' {$def_search} {$def_status} {$def_categ} {$def_hits} {$def_datetill}";
		}
	
		
		
		$GLOBALS['tmpl']->assign('ordstr',$ordstr);
		$GLOBALS['tmpl']->assign('news', $news); 
		$GLOBALS['tmpl']->assign('newscategs', $this->Categs());
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['News_show']); 
		$GLOBALS['tmpl']->assign('limit',$limit);
		if($num>$limit)$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, '', "<a class=\"page_navigation\" href=\"index.php?do=news&amp;sub=overview{$nastr}&pp={$limit}&page={s}\">{t}</a> "));
		
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/news.tpl'));
	}
	
	function Openclose($openclose,$id)
	{
		if(perm('news_openclose'))
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_news SET Aktiv='{$openclose}' WHERE Id='{$id}'");
		}
		
		echo '<script>window.opener.location.reload(); window.close();</script>';
	}
	
	
	//=================================================================
	// Kategorien
	//=================================================================
	function displayCategs()
	{
		if(perm('news_category'))
		{
			$GLOBALS['tmpl']->assign('newscategs', $this->Categs());
			$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['News_Categs']); 
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/news/categs.tpl'));
		} else {
			no_perm();
		}
	}
	
	
	function Categs($prefix='')
	{
		$area   = $_SESSION['a_area'];
		$categs = array();
		return $this->getNewsCategs(0, $prefix,$categs, $area);
	}
	
	
	function getNewsCategs($id, $prefix, &$news_categ, &$area)
	{
		$query = $GLOBALS['db']->Query("SELECT *, Name_1 AS Name FROM " . PREFIX . "_news_kategorie WHERE Parent_Id = '$id' AND Sektion = '$area' ORDER BY POSI ASC");
		if (!$query->numrows()) 
		{
			return;
		} 
		
		while ($item = $query->fetchrow())
		{
			$item->visible_title = $prefix . " " . $item->Name;
			array_push($news_categ, $item);
			$this->getNewsCategs($item->Id, $prefix . " - ", $news_categ, $area);
		} 
		return $news_categ;
	}
	
	
	//=================================================================
	// Kategorie Loesch-Funktion
	//=================================================================
	function categDel($id)
	{
		$query = $GLOBALS['db']->Query("SELECT Id, Parent_Id FROM " . PREFIX . "_news_kategorie  WHERE Parent_Id=$id");
		while ($item = $query->fetchrow()) 
		{
			// News-kommentare loschen
			$sql = $GLOBALS['db']->Query("SELECT Id FROM " . PREFIX . "_news WHERE Kategorie='$id'");	
			while($row = $sql->fetchrow())
			{
				$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kommentare WHERE Bereich='news' AND Objekt_Id='$row->Id'");	
			}
			// News loschen
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_news WHERE Kategorie='$id'");		
			// <<--Rekursion -->>
			$this->categDel($item->Id);
		}
		
		// News loeschen
		
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_news_kategorie WHERE Id='$id'");			
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_news WHERE Kategorie='$id'");
	}
		
	
	//=================================================================
	//=================================================================
	function nopermDoc()
	{
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/error/no_perm_document.tpl'));
	}
}
?>