<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

if(!ispageadmin($userID) OR substr(basename($_SERVER[REQUEST_URI]),0,15) != "admincenter.php") die('Access denied.');

if($_GET['delete']) {
  $faqID = $_GET['faqID'];
	
	safe_query(" DELETE FROM ".PREFIX."faq WHERE faqID='$faqID' ");
}

elseif($_GET['delcat']) {
	$faqcatID = $_GET['faqcatID'];
	
  safe_query("DELETE FROM ".PREFIX."faq WHERE faqcatID='$faqcatID'");
	safe_query("DELETE FROM ".PREFIX."faq_categories WHERE faqcatID='$faqcatID'");
	header("Location: admincenter.php?site=faq");
}

elseif($_POST['sortieren']) {
	$sortfaqcat = $_POST['sortfaqcat'];
	$sortfaq = $_POST['sortfaq'];
	
	if(is_array($sortfaqcat)) {
		foreach($sortfaqcat as $sortstring) {
		    $sorter=explode("-", $sortstring);
			safe_query("UPDATE ".PREFIX."faq_categories SET sort='$sorter[1]' WHERE faqcatID='$sorter[0]' ");
		}
	}
	if(is_array($sortfaq)) {
		foreach($sortfaq as $sortstring) {
		    $sorter=explode("-", $sortstring);
			safe_query("UPDATE ".PREFIX."faq SET sort='$sorter[1]' WHERE faqID='$sorter[0]' ");
		}
	}
	header("Location: admincenter.php?site=faq");
}

elseif($_POST['save']) {
	$faqcat = $_POST['faqcat'];
	$question = $_POST['question'];
	$answer = $_POST['answer'];
	
  if($faqcat=="") $faqcat=0;
	safe_query("INSERT INTO ".PREFIX."faq ( faqcatID, question, answer, sort )
              values( '$faqcat', '$question', '$answer', '1' )");
	header("Location: admincenter.php?site=faq");
}

elseif($_POST['savecat']) {
	$faqcatname = $_POST['faqcatname'];
	$description = $_POST['description'];

	safe_query("INSERT INTO ".PREFIX."faq_categories ( faqcatname, description, sort )
	            values( '$faqcatname', '$description', '1' )");
	header("Location: admincenter.php?site=faq");
}

elseif($_POST['saveedit']) {
	$faqcat = $_POST['faqcat'];
	$question = $_POST['question'];
	$answer = $_POST['answer'];
	$faqID = $_POST['faqID'];

	safe_query("UPDATE ".PREFIX."faq SET faqcatID='$faqcat', question='$question', answer='$answer' WHERE faqID='$faqID' ");
	header("Location: admincenter.php?site=faq");
}

elseif($_POST['saveeditcat']) {
	$faqcatname = $_POST['faqcatname'];
	$description = $_POST['description'];
	$faqcatID = $_POST['faqcatID'];
	
	safe_query("UPDATE ".PREFIX."faq_categories SET faqcatname='$faqcatname', description='$description' WHERE faqcatID='$faqcatID' ");
	header("Location: admincenter.php?site=faq");
}

echo'<h2>FAQ</h2>';
if($_GET['action']=="add") {   // neue FAQ ertstellen
    $ergebnis=safe_query("SELECT * FROM ".PREFIX."faq_categories ORDER BY sort");
    $faqcats='<select name="faqcat">';
	while($ds=mysql_fetch_array($ergebnis)) {
	    $faqcats.='<option value="'.$ds[faqcatID].'">'.$ds[faqcatname].'</option>';
	}
	$faqcats.='</select>';
		
	echo'<form method="post" action="admincenter.php?site=faq">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category:</td>
		   <td>'.$faqcats.'</td>
		 </tr>
		 <tr>
		   <td>FAQ:</td>
		   <td><input type="text" name="question" size="91" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Answer:</td>
		   <td><textarea cols="90" rows="10" name="answer" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></textarea></td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="save" value="add FAQ"></td>
		 </tr>
		 </table>
		 </form>';
}

elseif($_GET['action']=="edit") { // FAQ bearbeiten

	$faqID = $_GET['faqID'];

    $ergebnis=safe_query("SELECT * FROM ".PREFIX."faq WHERE faqID='$faqID'");
	$ds=mysql_fetch_array($ergebnis);
	
	$faqcategory=safe_query("SELECT * FROM ".PREFIX."faq_categories ORDER BY sort");
    $faqcats='<select name="faqcat">';
	while($dc=mysql_fetch_array($faqcategory)) {
	    $faqcats.='<option value="'.$dc[faqcatID].'">'.$dc[faqcatname].'</option>';
	}
	$faqcats.='</select>';
		
	echo'<form method="post" action="admincenter.php?site=faq">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category:</td>
		   <td>'.$faqcats.'</td>
		 </tr>
		 <tr>
		   <td>FAQ:</td>
		   <td><input type="text" name="question" value="'.$ds[question].'" size="91" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Answer:</td>
		   <td><textarea cols="90" rows="10" name="answer" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'">'.$ds[answer].'</textarea></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="faqID" value="'.$faqID.'"></td>
		   <td><input type="submit" name="saveedit" value="update FAQ"></td>
		 </tr>
		 </table>
		 </form>';
}

elseif($_GET['action']=="addcat") { // Kategorie erstellen
    echo'<form method="post" action="admincenter.php?site=faq" enctype="multipart/form-data">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category Name:</td>
		   <td><input type="text" name="faqcatname" size="61" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Description:</td>
		   <td><textarea cols="60" rows="6" name="description" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></textarea></td>
		 </tr>
		 <tr>
		   <td>&nbsp;</td>
		   <td><input type="submit" name="savecat" value="add Category"></td>
		 </tr>
		 </table>
		 </form>';
}

elseif($_GET['action']=="editcat") {  // Kategorie bearbeiten

	$faqcatID = $_GET['faqcatID'];

    $ergebnis=safe_query("SELECT * FROM ".PREFIX."faq_categories WHERE faqcatID='$faqcatID'");
	$ds=mysql_fetch_array($ergebnis);
	
	echo'<form method="post" action="admincenter.php?site=faq">
	     <table cellpadding="4" cellspacing="0">
		 <tr>
		   <td>Category Name:</td>
		   <td><input type="text" name="faqcatname" value="'.$ds[faqcatname].'" size="61" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'"></td>
		 </tr>
		 <tr>
		   <td>Description:</td>
		   <td><textarea cols="60" rows="6" name="description" class="form_off" onFocus="this.className=\'form_on\'" onBlur="this.className=\'form_off\'">'.$ds[description].'</textarea></td>
		 </tr>
		 <tr>
		   <td><input type="hidden" name="faqcatID" value="'.$faqcatID.'"></td>
		   <td><input type="submit" name="saveeditcat" value="update"></td>
		 </tr>
		 </table>
		 </form>';
}
else {
	echo'<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=faq&action=add\');return document.MM_returnValue" value="new FAQ">
	     <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=faq&action=addcat\');return document.MM_returnValue" value="new Category">';	

	echo'<form method="post" action="admincenter.php?site=faq">
		     <table width="100%" cellpadding="4" cellspacing="1" bgcolor="#999999">
		     <tr bgcolor="#CCCCCC">
			   <td class="title">FAQ:</td>
			   <td class="title">Actions:</td>
			   <td class="title">Sort:</td>
			 </tr>';
			 
	$ergebnis=safe_query("SELECT * FROM ".PREFIX."faq_categories ORDER BY sort");
	$anz=safe_query("SELECT count(faqcatID) FROM ".PREFIX."faq_categories");
	$anz=mysql_result($anz, 0);
	
	while($ds=mysql_fetch_array($ergebnis)) {
		echo'<tr><td colspan="3" bgcolor="#FFFFFF"></td></tr>
		     <tr bgcolor="#CCCCCC">
		      <td width="470"><b>&#8226; '.$ds[faqcatname].'</b><br>'.$ds[description].'</td>
			    <td><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=faq&action=editcat&faqcatID='.$ds[faqcatID].'\');return document.MM_returnValue" value="edit"> <input type="button" class="button" onClick="MM_confirm(\'really delete this category and the category faqs?\', \'admincenter.php?site=faq&delcat=true&faqcatID='.$ds[faqcatID].'\')" value="delete"></td>
			   <td>
			     <select name="sortfaqcat[]">';
		for($n=1; $n<=$anz; $n++) {
		    if($ds[sort] == $n) echo'<option value="'.$ds[faqcatID].'-'.$n.'" selected>'.$n.'</option>';
			else echo'<option value="'.$ds[faqcatID].'-'.$n.'">'.$n.'</option>';
	       }		
	    echo'</select></td>
		     </tr>
			 <tr><td colspan="3" bgcolor="#FFFFFF"></td></tr>';		 
		
	    $faq=safe_query("SELECT * FROM ".PREFIX."faq WHERE faqcatID='$ds[faqcatID]' ORDER BY sort");
		$anzfaq=safe_query("SELECT count(faqID) FROM ".PREFIX."faq WHERE faqcatID='$ds[faqcatID]'");
		$anzfaq=mysql_result($anzfaq, 0);

		while($db=mysql_fetch_array($faq)) {
			echo'<tr>
		          <td bgcolor="#FFFFFF" width="470"><b>- '.$db[question].'</b><br>'.$db[answer].'</td>
		          <td bgcolor="#FFFFFF">
				  <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=faq&action=edit&faqID='.$db[faqID].'\');return document.MM_returnValue" value="edit"> 
				  <input type="button" class="button" onClick="MM_confirm(\'really delete this FAQ?\', \'admincenter.php?site=faq&delete=true&faqID='.$db[faqID].'\')" value="delete">
				  </td>
		          <td bgcolor="#FFFFFF">
		            <select name="sortfaq[]">';
	        for($j=1; $j<=$anzfaq; $j++) {
	            if($db[sort] == $j) echo'<option value="'.$db[faqID].'-'.$j.'" selected>'.$j.'</option>';
		        else echo'<option value="'.$db[faqID].'-'.$j.'">'.$j.'</option>';
	           }		
	           echo'   </select>
	             </td>
	           </tr>';
		}
	}	 
	
	$faq=safe_query("SELECT * FROM ".PREFIX."faq WHERE faqcatID='0' ORDER BY sort");
	$anzfaq=safe_query("SELECT count(faqID) FROM ".PREFIX."faq WHERE faqcatID='0'");
	$anzfaq=mysql_result($anzfaq, 0);
	
	while($db=mysql_fetch_array($faq)) {
		echo'<tr bgcolor="dcdcdc">
	          <td bgcolor="#FFFFFF"><b>'.$db[question].'</b></td>
	          <td bgcolor="#FFFFFF">
			  <input type="button" class="button" onClick="MM_goToURL(\'parent\',\'admincenter.php?site=faq&action=edit&faqID='.$db[faqID].'\');return document.MM_returnValue" value="edit"> 
			  <input type="button" class="button" onClick="MM_confirm(\'really delete this FAQ?\', \'admincenter.php?site=faq&delete=true&faqID='.$db[faqID].'\')" value="delete">
	          </td>
	          <td bgcolor="#FFFFFF">
	            <select name="sort[]">';
		for($n=1; $n<=$anzfaq; $n++) {
	    	if($ds[sort] == $n) echo'<option value="'.$db[faqID].'-'.$n.'" selected>'.$n.'</option>';
	        else echo'<option value="'.$db[faqID].'-'.$n.'">'.$n.'</option>';
		}
		echo'</select></td></tr>';
	}
	
	echo'<tr bgcolor="#CCCCCC">
		       <td>&nbsp;</td>
			   <td colspan="3" align="right"><input type="submit" name="sortieren" value="sort"></td>
			 </tr>
			 </table></form>';
}
?>