<?php
class Glossar
{
	//=================================================================
	// Begriff bearbeiten
	//=================================================================
	function editGlossar($id)
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$GLOBALS['db']->Query("UPDATE " . PREFIX . "_glossar SET Wort='".$_POST['Wort']."',Beschreibung='".imgToPrefix($_POST['Beschreibung'])."' WHERE Id='$id'");
			Logger('User ' . $_SESSION['user_name'] . ' update Glossar (' . $_POST['Wort'] . ')', '3', $_SESSION['benutzer_id']);
			echo "<script>window.opener.location.reload(); window.close();</script>";
		}
		
		$res = $GLOBALS['db']->RowQuery("SELECT * FROM " . PREFIX . "_glossar WHERE Id='$id'");
		
		$GLOBALS['tmpl']->assign('res', $res);
		$GLOBALS['tmpl']->assign('Beschreibung', fckInit(imgPrefix($res->Beschreibung) . ' ', 'Beschreibung', 400, 'Settings'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Glossar']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/glossar/edit.tpl'));
	}
	
	//=================================================================
	// Neuer Begriff
	//=================================================================
	function addGlossar()
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_glossar (Id,Wort,Beschreibung,Aktiv) VALUES ('', '".$_POST['Wort']."','".imgToPrefix($_POST['Beschreibung'])."','1')");
			Logger('User ' . $_SESSION['user_name'] . ' add new Glossar-Word (' . $_POST['Wort'] . ')', '3', $_SESSION['benutzer_id']);
			echo "<script>window.opener.location.reload(); window.close();</script>";
		}
		
		$GLOBALS['tmpl']->assign('Beschreibung', fckInit(' ', 'Beschreibung', 400, 'Settings'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Glossar']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/glossar/edit.tpl'));
	}
	
	//=================================================================
	// Uebersicht
	//=================================================================
	function showGlossar()
	{
		// Speichern
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			foreach($_POST['Aktiv'] as $lid => $li)
			{
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_glossar  
				SET 
					Wort = '" . $_POST['Wort'][$lid] . "',
					Aktiv = '" . $_POST['Aktiv'][$lid] . "' 
				WHERE Id = '$lid'");
				
				if(@$_POST['del'][$lid]==1)
				{
					$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_glossar WHERE Id='$lid'");
				}
			}
		}
		
		$db_sort      = " ORDER BY Name ASC";
		$nav_sort   = "&amp;sort=name_asc";
		$datesort = '';
		$def_search_n = '';
		$def_search = '';
		$namesort = '';
		$hitssort='';
		$pattern='';
		$pattern2='';
		
		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);
		}
		
		switch(@$_REQUEST['sort'])
		{
			case '': default:
			case'name_asc':$db_sort = 'ORDER BY Wort ASC';$nav_sort = '&amp;sort=name_asc';$namesort = 'name_desc';break;
			case'name_desc':$db_sort='ORDER BY Wort 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'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;
		}
		
		$_REQUEST['q']  = $pattern;
		$def_search_n   = "&amp;q=" . urlencode($pattern);
		$def_search     = " AND ((Wort LIKE '%{$pattern}%') OR (Wort LIKE '%{$pattern2}%'))";
		
		$check = $GLOBALS['db']->RowQuery("SELECT COUNT(Id) AS ResCount FROM " . PREFIX . "_glossar WHERE Id!='0' {$def_search}");
		
		$num = $check->ResCount;
		$limit = GetLimit(25);
		$seiten = ceil($num / $limit);
		$a = prepage() * $limit - $limit;
		
		$items = array();
		$query = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_glossar WHERE Id!='0' {$def_search} {$db_sort} LIMIT $a,$limit");
		while($row = $query->fetchrow())
		{
			array_push($items,$row);
		}
		
		// Navi
		if ($num > $limit) 
		{
			$GLOBALS['tmpl']->assign('Navi', pagenav($seiten, '', " <a class=\"page_navigation\" href=\"index.php?do=glossar{$def_search_n }&amp;pp={$limit}&amp;page={s}\">{t}</a> "));
		}
		
		$GLOBALS['tmpl']->assign('activesort', @$activesort);
		$GLOBALS['tmpl']->assign('namesort', $namesort);
		$GLOBALS['tmpl']->assign('hitssort', $hitssort);
		$GLOBALS['tmpl']->assign('limit', $limit);
		$GLOBALS['tmpl']->assign('items', $items);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Glossar']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/glossar/overview.tpl'));
	}
	
}
?>