<?php
/***************************************************************************
                            topicmanager.php  -  description
                             -------------------
    begin                : Sat June 17 2000
    copyright            : (C) 2001 The phpBB Group
    email                : support@phpbb.com

    $Id: topicmanager.php,v 1.21 2001/06/16 06:05:50 thefinn Exp $

 ***************************************************************************/

/***************************************************************************
 *
 *   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.
 *
 ***************************************************************************/
include("header.php");
$accesserror = 0;
if ( $xoopsUser ) {
	if ( !$xoopsUser->isAdmin($xoopsModule->mid()) ) {
		if ( !is_moderator($forum, $xoopsUser->uid()) ) {
			$accesserror = 1;
		}
	}
} else {
	$accesserror = 1;
}
if ( $accesserror == 1 ) {
	redirect_header("viewtopic.php?topic_id=$topic_id&amp;post_id=$post_id&amp;order=$order&amp;viewmode=$viewmode&amp;pid=$pid&amp;forum=$forum",3,_MD_YANTMOTFTYCPTF);
	exit();
}


include(XOOPS_ROOT_PATH."/header.php");
OpenTable();
$pagetype = "bbcode_ref";
include('page_header.php');
if($HTTP_POST_VARS['submit']) {

   switch($mode) {
    case 'del':
      // Update the users's post count, this might be slow on big topics but it makes other parts of the
      // forum faster so we win out in the long run.
      $sql = "SELECT uid, post_id FROM ".$xoopsDB->prefix("bb_posts")." WHERE topic_id = $topic_id";
      if(!$r = $xoopsDB->query($sql))
			die(_MD_COULDNOTQUERY);
      while($row = $xoopsDB->fetchArray($r)) {
	 		if($row[uid] != 0) {
	    		$sql = "UPDATE ".$xoopsDB->prefix("users")." SET posts = posts - 1 WHERE uid = ".$row['uid']."";
	    		$xoopsDB->query($sql);
	 		}
      }

		// Get the post ID's we have to remove.
		$sql = "SELECT post_id FROM ".$xoopsDB->prefix("bb_posts")." WHERE topic_id = $topic_id";
		if(!$r = $xoopsDB->query($sql))
			die(_MD_COULDNOTQUERY);
      while($row = $xoopsDB->fetchArray($r))
      {
			$posts_to_remove[] = $row["post_id"];
		}

      $sql = "DELETE FROM ".$xoopsDB->prefix("bb_posts")." WHERE topic_id = $topic_id";
      if(!$result = $xoopsDB->query($sql))
			die(_MD_COULDNOTREMOVE);
      $sql = "DELETE FROM ".$xoopsDB->prefix("bb_topics")." WHERE topic_id = $topic_id";
      if(!$result = $xoopsDB->query($sql))
			die(_MD_COULDNOTQUERY);
		$sql = "DELETE FROM ".$xoopsDB->prefix("bb_posts_text")." WHERE ";
		for($x = 0; $x < count($posts_to_remove); $x++)
		{
			if($set)
			{
				$sql .= " OR ";
			}
			$sql .= "post_id = ".$posts_to_remove[$x];
			$set = TRUE;
		}

		if(!$xoopsDB->query($sql))
		{
			die(_MD_COULDNOTREMOVETXT);
		}
		sync($forum, 'forum');

      echo _MD_TTHBRFTD."<p><a href='viewforum.php?forum=$forum'>"._MD_RETURNTOTHEFORUM."</a></p><p><a href='index.php'>"._MD_RTTFI."</a></p>";
      break;
    case 'move':
      $sql = "UPDATE ".$xoopsDB->prefix("bb_topics")." SET forum_id = $newforum WHERE topic_id = $topic_id";
      if(!$r = $xoopsDB->query($sql))
		die(_MD_EPGBATA);
      $sql = "UPDATE ".$xoopsDB->prefix("bb_posts")." SET forum_id = $newforum WHERE topic_id = $topic_id";
      if(!$r = $xoopsDB->query($sql))
		die(_MD_EPGBATA);
		sync($newforum, 'forum');
		sync($forum, 'forum');

      echo _MD_TTHBM."<p><a href='viewtopic.php?topic_id=$topic_id&amp;forum=$newforum'>"._MD_VTUT."</a></p><p><a href='index.php'>"._MD_RTTFI."</a></p>";
      break;
    case 'lock':
      $sql = "UPDATE ".$xoopsDB->prefix("bb_topics")." SET topic_status = 1 WHERE topic_id = $topic_id";
      if(!$r = $xoopsDB->query($sql))
		die(_MD_EPGBATA);
      echo _MD_TTHBL."<p><a href='viewtopic.php?topic_id=$topic_id&amp;forum=$forum'>"._MD_VIEWTHETOPIC."</a></p><p><a href='index.php'>"._MD_RTTFI."</a></p>";
      break;
    case 'unlock':
      $sql = "UPDATE ".$xoopsDB->prefix("bb_topics")." SET topic_status = 0 WHERE topic_id = $topic_id";
      if(!$r = $xoopsDB->query($sql))
	die("Error - Could not unlock the selected topic. Please go back and try again.");
      echo _MD_TTHBU."<p><a href='viewtopic.php?topic_id=$topic_id&amp;forum=$forum'>"._MD_VIEWTHETOPIC."</a></p><p><a href='index.php'>"._MD_RTTFI."</a></p>";
      break;
    case 'sticky':
      $sql = "UPDATE ".$xoopsDB->prefix("bb_topics")." SET topic_sticky = 1 WHERE topic_id = $topic_id";
      if(!$r = $xoopsDB->query($sql))
		die("Error - Could not sticky the selected topic. Please go back and try again.");
      echo _MD_TTHBS."<p><a href='viewtopic.php?topic_id=$topic_id&amp;forum=$forum'>"._MD_VIEWTHETOPIC."</a></p><p><a href='index.php'>"._MD_RTTFI."</a></p>";
      break;
    case 'unsticky':
      $sql = "UPDATE ".$xoopsDB->prefix("bb_topics")." SET topic_sticky = 0 WHERE topic_id = $topic_id";
      if(!$r = $xoopsDB->query($sql))
		die("Error - Could not unsticky the selected topic. Please go back and try again.");
      echo _MD_TTHBUS."<p><a href='viewtopic.php?topic_id=$topic_id&amp;forum=$forum'>"._MD_VIEWTHETOPIC."</a></p><p><a href='index.php'>"._MD_RTTFI."</a></p>";
      break;
	}
}
else {  // No submit
?>
<form action='<?php echo $PHP_SELF?>' method='post'>
<table border='0' cellpadding='1' cellspacing='0' align='center' valign='top' width='95%'><tr><td class='bg2'>
<table border='0' cellpadding='1' cellspacing='1' width='100%'>
<tr class='bg3' align='left'>
<?php
	switch($mode) {
		case 'del':
?>
	<td colspan='2'><?php echo _MD_OYPTDBATBOTFTTY;?></td>
<?php
		break;
		case 'move':
?>
	<td colspan='2'><?php echo _MD_OYPTMBATBOTFTTY;?></td>
<?php
		break;
		case 'lock':
?>
	<td colspan='2'><?php echo _MD_OYPTLBATBOTFTTY;?></td>
<?php
		break;
		case 'unlock':
?>
	<td colspan='2'><?php echo _MD_OYPTUBATBOTFTTY;?></td>
<?php
		break;
		case 'sticky':
?>
	<td colspan='2'><?php echo _MD_OYPTSBATBOTFTTY;?></td>
<?php
		break;
		case 'unsticky':
?>
	<td colspan='2'><?php echo _MD_OYPTTBATBOTFTTY;?></td>
<?php
		break;
	}
?>
</tr>
<?php

	if($mode == 'move') {
?>
<tr>
	<td class='bg3'><?php echo _MD_MOVETOPICTO;?></td>
	<td class='bg1'><select name='newforum' size='0'>
<?php
	$sql = "SELECT forum_id, forum_name FROM ".$xoopsDB->prefix("bb_forums")." WHERE forum_id != $forum ORDER BY forum_id";
	if($result = $xoopsDB->query($sql)) {
		if($myrow = $xoopsDB->fetchArray($result)) {
			do {
				echo "<option value='".$myrow['forum_id']."'>".$myrow['forum_name']."</option>\n";
			} while($myrow = $xoopsDB->fetchArray($result));
		}
		else {
			echo "<option value='-1'>"._MD_NOFORUMINDB."</option>\n";
		}
	}
	else {
		echo "<option value='-1'>"._MD_DATABASEERROR."</option>\n";
	}
?>
	</select></td>
</tr>
<?php
	}
?>
<tr class='bg3'>
	<td colspan='2' align='center'>
<?php
	switch($mode) {
		case 'del':
?>
		<input type='hidden' name='mode' value='del' />
		<input type='hidden' name='topic_id' value='<?php echo $topic_id?>' />
		<input type='hidden' name='forum' value='<?php echo $forum?>' />
		<input type='submit' name='submit' value='<?php echo _MD_DELTOPIC;?>' />
<?php
		break;
		case 'move':
?>
		<input type='hidden' name='mode' value='move' />
		<input type='hidden' name='topic_id' value='<?php echo $topic_id?>' />
		<input type='hidden' name='forum' value='<?php echo $forum?>' />
		<input type='submit' name='submit' value='<?php echo _MD_MOVETOPIC;?>' />
<?php
		break;
		case 'lock':
?>
		<input type='hidden' name='mode' value='lock' />
		<input type='hidden' name='topic_id' value='<?php echo $topic_id?>' />
		<input type='hidden' name='forum' value='<?php echo $forum?>' />
		<input type='submit' name='submit' value='<?php echo _MD_LOCKTOPIC;?>' />
<?php
		break;
		case 'unlock':
?>
		<input type='hidden' name='mode' value='unlock' />
		<input type='hidden' name='topic_id' value='<?php echo $topic_id?>' />
		<input type='hidden' name='forum' value='<?php echo $forum?>' />
		<input type='submit' name='submit' value='<?php echo _MD_UNLOCKTOPIC;?>' />
<?php
		break;
		case 'sticky':
?>
		<input type='hidden' name='mode' value='sticky' />
		<input type='hidden' name='topic_id' value='<?php echo $topic_id?>' />
		<input type='hidden' name='forum' value='<?php echo $forum?>' />
		<input type='submit' name='submit' value='<?php echo _MD_STICKYTOPIC;?>' />
<?php
		break;
		case 'unsticky':
?>
		<input type='hidden' name='mode' value='unsticky' />
		<input type='hidden' name='topic_id' value='<?php echo $topic_id?>' />
		<input type='hidden' name='forum' value='<?php echo $forum?>' />
		<input type='submit' name='submit' value='<?php echo _MD_UNSTICKYTOPIC;?>' />
<?php
		break;
	}
?>
</td></tr>
</form>
</table></td></tr></table>
<?php
}
include('page_tail.php');
CloseTable();
include(XOOPS_ROOT_PATH."/footer.php");
?>