<?php
	/*****************************************************************************\
	* phpGroupWare - Forums                                                       *
	* http://www.phpgroupware.org                                                 *
	* Written by Jani Hirvinen <jpkh@shadownet.com>                               *
	* -------------------------------------------                                 *
	*  This program is free software; you	can redistribute it and/or modify it   *
	*  under the terms of	the GNU	General	Public License as published by the  *
	*  Free Software Foundation; either version 2	of the License,	or (at your *
	*  option) any later version.                                                 *
	\*****************************************************************************/

	/* $Id: preference_forum.php 21086 2010-03-25 22:56:09Z Caeies $ */

	$GLOBALS['phpgw_info']['flags'] = array(
						'currentapp'		=> 'forum',
						'enable_nextmatchs_class' => True
						);
	if($action) {
		$phpgw_info["flags"]["noheader"] = True;
		$phpgw_info["flags"]["nonavbar"] = True;
	}
	include_once('../header.inc.php');

	$actiontype = 'addforum';
	$buttontext = lang('Add Forum');
	$for_id = intval($_REQUEST['for_id']);
	$cat_id = intval($_REQUEST['cat_id']);

	if($_REQUEST['act'] == "edit")
	{
		$GLOBALS['phpgw']->db->query('SELECT * FROM f_forums WHERE id = ' . intval($for_id), __LINE__, __FILE__ );
		if( !$GLOBALS['phpgw']->db->next_record() )
		{
			print "Error in reading database<br>\n";
			$GLOBALS['phpgw']->common->phpgw_exit();
		}
		else
		{
			$forname = $GLOBALS['phpgw']->db->f("name");
			$fordescr = $GLOBALS['phpgw']->db->f("descr");
			$cat_id = $GLOBALS['phpgw']->db->f("cat_id");
			if ($cat_id > 0)
			{
				$GLOBALS['phpgw']->db->query("SELECT * FROM f_categories WHERE id = $cat_id", __LINE__, __FILE__);
				if( !$GLOBALS['phpgw']->db->next_record() )
				{
					print "Error in readindg database<br>\n";
					$GLOBALS['phpgw']->common->phpgw_exit();
				}

				$catname = $GLOBALS['phpgw']->db->f('name');
			}
			else
			{
				$catname = lang('No Category');
			}
			$extrahidden = "<input type=\"hidden\" name=\"for_id\" value=\"$for_id\">";
			$buttontext = lang("Update Forum");
			$actiontype = "updforum";
		}
	}


	if($action)
	{
		if ( $_REQUEST['action'] == "addforum")
		{
			$db_resp = $GLOBALS['phpgw']->db->query("INSERT INTO f_forums(name,descr,cat_id) VALUES ('" 
					. $GLOBALS['phpgw']->db->db_addslashes($_REQUEST['forname']) . "','" 
					. $GLOBALS['phpgw']->db->db_addslashes($_REQUEST['$fordescr']) . "',"
					. intval($_REQUEST['goestocat']), __LINE__, __FILE__);
			if ( !$db_resp )
			{
				echo "Error in adding forum to database<br>\n";
				$GLOBALS['phpgw']->common->phpgw_exit();
			}
			else
			{
				$GLOBALS['phpgw']->redirect_link('/forum/preference_index.php');
				$GLOBALS['phpgw']->common->phpgw_exit();
			}
		}
		elseif ( $_REQUEST['action'] == "updforum" && $for_id)
		{
			$db_resp = $GLOBALS['phpgw']->db->query('UPDATE f_forums'
					. " SET name = '" . $GLOBALS['phpgw']->db->db_addslashes($_REQUEST['forname']) . "',"
						. " descr = '" . $GLOBALS['phpgw']->db->db_addslashes($_REQUEST['fordescr']) . "',"
						. ' cat_id = ' . intval($_REQUEST['goestocat'])
					. ' WHERE id=$for_id', __LINE__, __FILE__);
			if ( !$db_resp )
			{
				print "Error in updating forum on database<br>\n";
				$GLOBALS['phpgw']->common->phpgw_exit();
			}
			else
			{
				$GLOBALS['phpgw']->redirect_link('/forum/preference_index.php');
				$GLOBALS['phpgw']->common->phpgw_exit();
			}
		}
	}



	?>

	<p>
	<table border="0" width=100%>
	<tr>
	<?php echo "<td bgcolor=\"" . $phpgw_info["theme"]["th_bg"] . "\" align=\"left\"><b>" . lang("Forums") . " " . lang("Admin") . "</b></td>" . "</tr>"; ?>

	<tr>
	<td>
	<font size=-1>
	<?php
	echo "<a href=\"" . $GLOBALS['phpgw']->link("/forum/preference_category.php") . "\">" . lang("New Category") ."</a>";
	echo " | ";
	echo "<a href=\"" . $GLOBALS['phpgw']->link("/forum/preference_forum.php") . "\">" . lang("New Forum") ."</a>";   
	echo " | ";
	echo "<a href=\"" . $GLOBALS['phpgw']->link("/forum/preference_index.php") . "\">" . lang("Return to Admin") ."</a>";  
	echo " | ";
	echo "<a href=\"" . $GLOBALS['phpgw']->link("/forum") . "\">" . lang("Return to Forums") ."</a>";

	?>
	</font>
	<br><br>
	<center>
	<table border="0" width=80% bgcolor="<?php echo $phpgw_info["theme"]["table_bg"]?>">
	<tr>
	<td colspan=2 bgcolor="<?php echo $phpgw_info["theme"]["th_bg"]?>">
	<center><?php echo $buttontext ?></center>
	</td>
	</tr>
	<tr>
	<form method="POST" action="<?php echo $GLOBALS['phpgw']->link("/forum/preference_forum.php"); ?>">
	<?php echo $extrahidden ?> 
	<input type="hidden" name="action" value="<?php echo $actiontype?>">

	<td><?php echo lang("Belongs to Category") ?>:</td>
	<td>
	<select name="goestocat">
	<?php
	$q = $GLOBALS['phpgw']->db->query("select * from f_categories");

	while($GLOBALS['phpgw']->db->next_record($q)) {
		$cat_id = $GLOBALS['phpgw']->db->f("id");
		$cat_name = $GLOBALS['phpgw']->db->f("name");
		if ($catname==$cat_name) { echo "<option value=\"$cat_id\" selected>$cat_name</option>\n"; }
		else { echo "<option value=\"$cat_id\">$cat_name</option>\n"; }
	}
	?>
	    <option value=-1><?php echo lang("No Category") ?></option>
	    </select>
	   </td>
	   <tr>
	    <td><?php echo lang("Forum Name") ?>:</td>
	    <td><input type="text" name="forname" size=40 maxlength=49 value="<?php echo $forname ?>"></td>
	   </tr>  
	   <tr>
	    <td><?php echo lang("Forum Description") ?>:</td>
	    <td><textarea rows="3" cols="40" name="fordescr" virtual-wrap maxlength=240><?php echo $fordescr ?></textarea></td>
	   </tr>
	   <tr>
	    <td colspan=2 align=right>

	     <input type="submit" value="<?php echo $buttontext?>">
	    </td>
	   </tr>

	  </table>
	  </center>
	  <br>
	 </td>
	</tr>

	   </tr>
	  </table>
	  </center>
	  <br>




	 </td>
	</tr>
	</table>


<?php
  $GLOBALS['phpgw']->common->phpgw_footer();
?>
