<?php
ob_start();
require_once("init.php");

if(!permission("adminpanel")){ exit(); }

$tmpl = new Koobi("templates/" . ADMINTHEME . "/faq/");
$tmpl->assign('langadmin', $langadmin);
$tmpl->assign('theme', ADMINTHEME);
$tmpl->assign('area', $area);
$tmpl->register_function("permission", "permission");
$tmpl->assign('pref', $pref);



// ===========================================================
// <<-- FAQ- Reihenfolge speichern + Aktiv oder inaktiv setzen -->>
// ===========================================================
if ($_REQUEST['action'] == "save_posi") {
	
	foreach ($_POST['posi'] as $id => $posi) {
		if( ($posi != '') && (is_numeric($posi)) ) {
			$query = "UPDATE " . PREFIX . "_faq SET posi=$posi WHERE id = $id";
			$result = new dbc($query);
		}
		
		if(permission('editfaq')) { 
			foreach ($_POST['active'] as $id => $active) {
				$sql = new dbc("SELECT uid FROM ".PREFIX."_faq WHERE uid='".UID."' AND id=$id ");
				$num = $sql->numrows();
				$activate = 1;
				
				if( !permission('alles') ) {
					if( (!permission('editfaq')) && ($num<1) ){
						$activate = 0;
					}
				}
				
				if($activate == 1) {
					$query = "UPDATE " . PREFIX . "_faq SET active=$active WHERE id = $id";
					$result = new dbc($query);
				}
			}
		}
	}
	header("Location:faq.php?action=overview&area=".$_REQUEST['area']."");
}
// ===========================================================
// <<-- FAQ- Übersicht -->>
// ===========================================================
if($_REQUEST['action']=="overview"){
	if( (!permission('editfaq')) && (!permission('addfaq')) )  { echo error($langadmin['noperms']); exit(); }
	
	$faq = array();
	getFaq(0, "", $faq, $area, $dummy, $dummy, $dummy, 1, '','');
	$tmpl->assign('faq', $faq);
	$tmpl->display("a_faq_overview.tpl");
}

if($_REQUEST['action']=="edit"){
	$sql = new dbc("SELECT uid FROM ".PREFIX."_faq WHERE uid='".UID."' AND id=$_REQUEST[id]");
	$num = $sql->numrows();
	
	// Rechte prüfen
	if( !permission('alles') ) {
		if( (!permission('editfaq')) && ($num<1) ){
			echo error($langadmin['noperms']); 
			exit();
		}
	}
	
	
	// Faq holen
	$sql = new dbc("SELECT inline_screens,faq,answer FROM ".PREFIX."_faq WHERE id=$_REQUEST[id]");
	$row = $sql->fetchrow();
	
	if($row->inline_screens){
		$tmpl->assign('inlineshots', base64_encode(serialize($row->inline_screens)));
	}
	
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->answer;
	include("forms/forminit.php");
	
	
	$tmpl->assign('faq', htmlspecialchars($row->faq));
	$tmpl->display("a_faq_editnew.tpl");
}

// Bearbeitete FAQ speichern
if($_REQUEST['action']=="save"){
	$sql = new dbc("SELECT uid FROM ".PREFIX."_faq WHERE uid='".UID."' AND id=$_REQUEST[id]");
	$num = $sql->numrows();
	
	
	// Rechte prüfen
	if( !permission('alles') ) {
		if( (!permission('editfaq')) && ($num<1) ){
			echo error($langadmin['noperms']); 
			exit();
		}
	}
	
	$answer = $_REQUEST['text2'];
	if($_REQUEST['autobr2']==1) $answer = nl2br($answer);
	
	$ishots = addslashes(base64_decode($_REQUEST['inline_screens']));
	if(strlen($ishots)<15) $ishots = "";
	
	$sql = new dbc("
	UPDATE ".PREFIX."_faq 
	SET
	faq = '".$_REQUEST['faq']."',
	answer = '$answer',
	inline_screens = '$ishots'
	WHERE 
	id=$_REQUEST[id]");
	
	header("location:faq.php?action=overview&area=".$_REQUEST['area']."");
}

// ==============================================
// <<-- FAQ hinzufügen -->>
// ==============================================
if($_REQUEST['action']=="new"){
	if( !permission('addfaq') ) { echo error($langadmin['noperms']); exit();  }
	
	// <<-- EDITOR INITIALISIEREN -->>
	$fck_value = $row->answer;
	include("forms/forminit.php");
	
	if($_REQUEST['save']==1){
		
		$answer = $_REQUEST['text2'];
		if($_REQUEST['autobr2']==1) $answer = nl2br($answer);
		
		$ishots = addslashes(base64_decode($_REQUEST['inline_screens']));
		if(strlen($ishots)<15) $ishots = "";
		
		$sql = new dbc("
		INSERT INTO ".PREFIX."_faq 
		(id,parent_id,posi,faq,answer,ctime,uid,active,area,inline_screens)
		VALUES 
		('','$_REQUEST[cid]','1','$_REQUEST[faq]','$answer','".time()."','".UID."','$_REQUEST[active]','$_REQUEST[area]','".$ishots."')
		");
		
		header("location:faq.php?action=overview&area=".$_REQUEST['area']."");
	}
	
	$faq = array();
	getFaq(0, '', $faq, $area, '', '', '', 1, '','');
	$tmpl->assign('faq', $faq);
	$tmpl->display("a_faq_editnew.tpl");
}


// ==============================================
// <<-- FAQ löschen -->>
// ==============================================
if($_REQUEST['action']=="delete"){
	$sql = new dbc("SELECT uid FROM ".PREFIX."_faq WHERE uid='".UID."' AND id=$_REQUEST[id]");
	$num = $sql->numrows();
	
	
	// Rechte prüfen
	if( !permission('alles') ) {
		if( (!permission('editfaq')) && ($num<1) ){
			echo error($langadmin['noperms']); 
			exit();
		}
	}
	
	faqdel($_REQUEST['id']);
	
	// Weiterleiten...
	header("location:faq.php?action=overview&area=".$_REQUEST['area']."");
}
?>