<?php
class Faq
{
	//=================================================================
	// Anzeigen
	//=================================================================
	function Show()
	{
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			foreach($_POST['Aktiv'] as $fid => $faq)
			{
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_faq SET Aktiv='".$_POST['Aktiv'][$fid]."', Position='".$_POST['Position'][$fid]."' WHERE Id = '$fid'");
			}
			header('Location:index.php?do=faq');
			exit;
		}
		
		if(isset($_POST['new']) && $_POST['new']==1 && !empty($_POST['Name_1']))
		{
			$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 . "_faq
			(
				Id,
				Parent_Id,
				Datum,
				Benutzer,
				Aktiv,
				Sektion,
				Name_1,
				Name_2,
				Name_3,
				Position
			) VALUES (
				'',
				'0',
				'".stime()."',
				'".$_SESSION['benutzer_id']."',
				'1',
				'".AREA."',
				'$n1',
				'$n2',
				'$n3',
				'".abs((int)$_POST['Position'])."'
			)");
				
			Logger('User ' . $_SESSION['user_name'] . ' add new F.A.Q.-Category ('.$n1.')', '3', $_SESSION['benutzer_id']);
			header('Location:index.php?do=faq');
			exit;
		}
		
		$faq = array();
		$dummy = '';
		$area = AREA;
		$this->getFaq(0, "", $faq, $area, $dummy, $dummy, $dummy, 1, '','');
		$GLOBALS['tmpl']->assign('faq', $faq);
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Faq']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/faq/a_faq_overview.tpl'));
	}
	
	//=================================================================
	// Bearbeiten
	//=================================================================
	function editFaq($id)
	{	
		$LC = (isset($_REQUEST['langcode']) && $_REQUEST['langcode']!='') ? $_REQUEST['langcode'] : 1;
		
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			if(@$_POST['notext']==1)
			{
				$n1 = $_POST['Name'];
				$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("UPDATE " . PREFIX . "_faq 
				SET 
					Name_1 = '$n1',
					Name_2 = '$n2',
					Name_3 = '$n3'
				WHERE Id = '$id'");
				
			} else {
				$GLOBALS['db']->Query("UPDATE " . PREFIX . "_faq 
				SET 
					Textbilder_{$LC} = '" . @addslashes(base64_decode($_POST['inline_screens'])) . "',
					Name_{$LC} = '".$_POST['Name']."', 
					Antwort_{$LC} = '".imgToPrefix($_POST['Beschreibung'])."' 
				WHERE Id = '$id'");
			}
			Logger('User ' . $_SESSION['user_name'] . ' edit F.A.Q. ('.$n1.')', '3', $_SESSION['benutzer_id']);
		}
		
		
		$res = $GLOBALS['db']->RowQuery("SELECT *,Textbilder_{$LC} as Textbilder,Name_{$LC} AS Name, Antwort_{$LC} AS Beschreibung FROM " . PREFIX . "_faq  WHERE Id = '$id'");
		$GLOBALS['tmpl']->assign('res',$res); 
		$GLOBALS['tmpl']->assign('InlineShots', unserialize($res->Textbilder)); 
		$GLOBALS['tmpl']->assign('field_inline', "Textbilder_{$LC}"); 
		$GLOBALS['tmpl']->assign('Beschreibung', fckInit(imgPrefix($res->Beschreibung) . ' ', 'Beschreibung', 350, 'Settings'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Faq']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/faq/faq_edit.tpl'));
	}
	
	//=================================================================
	// Neu erstellen
	//=================================================================
	function newFaq($id='')
	{	
		if(isset($_POST['save']) && $_POST['save']==1)
		{
			$q = "INSERT INTO " . PREFIX . "_faq 
			(
				Id,
				Parent_Id,
				Name_1,
				Name_2,
				Name_3,
				Antwort_1,
				Antwort_2,
				Antwort_3,
				Textbilder_1,
				Datum,
				Benutzer,
				Aktiv,
				Sektion
			) VALUES (
				'',
				'" . $_POST['Parent_Id'] . "',
				'" . $_POST['Name'] . "',
				'" . $_POST['Name'] . "',
				'" . $_POST['Name'] . "',
				'" . imgToPrefix($_POST['Beschreibung']). "',
				'" . imgToPrefix($_POST['Beschreibung']). "', 
				'" . imgToPrefix($_POST['Beschreibung']). "', 
				'" . addslashes(base64_decode($_POST['inline_screens'])) . "',
				'" . stime() . "',
				'" . $_SESSION['benutzer_id'] . "',
				'1',
				'" . AREA . "'
			)";
			Logger('User ' . $_SESSION['user_name'] . ' add new F.A.Q. ('.$_POST['Name'].')', '3', $_SESSION['benutzer_id']);
			$GLOBALS['db']->Query($q);
			echo '<script>window.opener.location.reload(); window.close();</script>';
		}
		
		$faq = array();
		$dummy = '';
		$area = AREA;
		$this->getFaq(0, "", $faq, $area, $dummy, $dummy, $dummy, 1, '','');
		$GLOBALS['tmpl']->assign('faqCategs', $faq);
		$GLOBALS['tmpl']->assign('Beschreibung', fckInit(' ', 'Beschreibung', 350, 'Settings'));
		$GLOBALS['tmpl']->assign('title', $GLOBALS['config_vars']['Faq_new']); 
		$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/faq/faq_new.tpl'));
	}
	
	//=================================================================
	// Loeschen
	//=================================================================
	function deleteFaqRecursive($id)
	{
		$this->deleteFaq($id);
		Logger('User ' . $_SESSION['user_name'] . ' delete F.A.Q. ('.$id.')', '3', $_SESSION['benutzer_id']);
		echo '<script>window.opener.location.reload(); window.close();</script>';
	}
	
	function deleteFaq($id)
	{
		$query = $GLOBALS['db']->Query(" SELECT * FROM " . PREFIX . "_faq  WHERE Parent_Id='$id'");
		
		while ($item = $query->fetchrow())
		{
			$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_faq WHERE Id='$item->Id'");			
			$this->deleteFaq($item->Id);
		}
		
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_faq WHERE Id='$id'");
	}
	
	//=================================================================
	//=================================================================
	function getFaq($id, $prefix, &$faq, &$area, $active, $orderby, $ascdesc, $i, $vor = '', $f, $curr_faq = 0)
	{
		$LC = $_SESSION['Langcode'];
	
		switch (@$_REQUEST['orderby']) 
		{
			case "date" : $orderby = "Datum";
				break;
			case "author" : $orderby = "Benutzer";
				break;
			case "title" : $orderby = "Faq";
				break;
				case '':
			default : $orderby = "Position";
				break;
		} 
	
		switch (@$_REQUEST['ascdesc'])
		{
			case "asc": $ascdesc = "ASC";break;
			case "desc": $ascdesc = "DESC";break;
			case '':default : $ascdesc = "ASC";break;
		} 
	
		
		$query = $GLOBALS['db']->Query("SELECT *,Name_1 AS DefFaq, Name_1 AS Faq, Antwort_1 AS Antwort FROM " . PREFIX . "_faq WHERE Parent_Id = $id AND Sektion = '" . AREA . "'  ORDER BY $orderby $ascdesc");
	
		$i++;
		$f++;
		$a = 0;
	
		$faq_erg = array();
	
		$r_faq = $GLOBALS['db']->Query("SELECT *,Name_1 AS DefFaq, Name_1 AS Faq, Antwort_1 AS Antwort FROM " . PREFIX . "_faq WHERE Id = '$id' AND Sektion = '" . AREA . "'");
		$curr_faq = $r_faq->fetchrow();
		
		if(is_object($curr_faq))
		{
			$curr_faq->visible_title = ($curr_faq->Faq=='') ? $curr_faq->DefFaq : $curr_faq->Faq;
			$curr_faq->main = $curr_faq->Parent_Id;
		}
		if (!$query->numrows()) return;
	
		while ($item = $query->fetchrow()) 
		{
			$a++;
			$firstid = $item->Position ;
			
			$item->Faq = ($item->Faq=='') ? $item->DefFaq : $item->Faq;
		
			if ($item->Parent_Id == 0) {
				$item->visible_title = ($vor) . ($a) . $prefix . ". " . $item->Faq;
			} else {
				$item->visible_title = ($vor) . ($a) . '.' . $prefix . " " . $item->Faq;
			} 
	
			$item->Seotitle  = $item->Faq;
			$item->created = $item->Datum;
			$item->User = getusername($item->Benutzer);
			$item->hits = $item->Hits;
			$item->posi = $item->Position;
			$item->fid = $item->Id;
			$item->ulid = $f-1;
			$item->text = $item->Antwort;
			$item->text = high($item->Antwort);
			$item->faqonce = $item->Faq;
			@$item->main = $item->main;
	
			array_push($faq, $item);
	
			$this->getFaq($item->Id, $prefix . "  " , $faq, $area, $active, $orderby, $ascdesc, $i, $vor . $a . ".", $f);
		} 
		return;
	}
}
?>