<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// Delete Thread
	// ========================================================================================
	
		
	
	ReadFormArguments();
	Global $FormArguments;
			
	if (count($FormArguments) > 0) {
	
			
		ReadPermissions();
		Global $Permissions;
		
		
		$FormArguments['ThreadID'] = intval($FormArguments['ThreadID']);
		
		if ($FormArguments['ThreadID'] > 0) {
		
			if ($Permissions[202] == 'y') {
			
				$query = "select count(RecordNumber) as RecordsCount from `".$Settings['DBPrefix']."mb-Messages` where ThreadID=".$FormArguments['ThreadID']." limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
				
				$MessagesInThread = $row['RecordsCount'];
				
				
				
				// ===
				// read through all the messages being deleted in this thread and determine what to take away from each user's post count
				
				$UsersFound = 0;
				
				$query = "select * from `".$Settings['DBPrefix']."mb-Messages` where ThreadID=".$FormArguments['ThreadID']." limit ".$MessagesInThread;
				$result = mysql_query($query) or die (mysql_error());
				while ($row = mysql_fetch_array($result)) {
				
					$ThisUserFound = 'n';
					for ($i = 0; $i < $UsersFound; $i++) {
						if ($UsersFoundArray[$i] == $row['Poster']) {
							$ThisUserFound = 'y';
						}
					}
										
					if ($ThisUserFound == 'n') {
						$UsersFoundArray[] = $row['Poster'];
						$UsersFound++;
						$AdjustmentNumber[$row['Poster']] = 1;
					}
					else {
						$AdjustmentNumber[$row['Poster']]++;
					}
				
				}
				
				// ===
				// now actually take it away from their counts
				
				for ($i = 0; $i < $UsersFound; $i++) {
				
					$query55 = "update `".$Settings['DBPrefix']."core-Users` set ForumPosts=ForumPosts-".$AdjustmentNumber[$UsersFoundArray[$i]]." where RecordNumber='".$UsersFoundArray[$i]."' limit 1";
					$result55 = mysql_query($query55) or die (mysql_error());
				
				}			
								
				// ===
				
				
					
				$query = "delete from `".$Settings['DBPrefix']."mb-Messages` where ThreadID=".$FormArguments['ThreadID'];
				$result = mysql_query($query) or die (mysql_error());
				
				
				$query = "select * from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber=".$FormArguments['ThreadID']." limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
				
				$BoardID = $row['BoardID'];
				
				
				$query = "delete from `".$Settings['DBPrefix']."mb-Threads` where RecordNumber=".$FormArguments['ThreadID']." limit 1";
				$result = mysql_query($query) or die (mysql_error());
				
				
				$query55 = "update `".$Settings['DBPrefix']."mb-Boards` set Threads=Threads-1, Messages=Messages-".$MessagesInThread." where RecordNumber='".$BoardID."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
				
				
				
				$LastReply = 0;
				$LastUser = 0;
				
				$query = "select * from `".$Settings['DBPrefix']."mb-Threads` where BoardID=".$BoardID;
				$result = mysql_query($query) or die (mysql_error());
				while ($row = mysql_fetch_array($result)) {
				
					$query3 = "select * from `".$Settings['DBPrefix']."mb-Messages` where ThreadID=".$row['RecordNumber']." order by DateCode desc limit 1";
					$result3 = mysql_query($query3) or die (mysql_error());
					if (mysql_num_rows($result3) != 0) {
					
						$row3 = mysql_fetch_array($result3);
					
						if ($row3['DateCode'] > $LastReply) {
							$LastReply = $row3['DateCode'];
							$LastUser = $row3['Poster'];
						}
					}
				
				}
				
				
				$query55 = "update `".$Settings['DBPrefix']."mb-Boards` set LastReply='".$LastReply."', LastUser='".$LastUser."' where RecordNumber='".$BoardID."' limit 1";
				$result55 = mysql_query($query55) or die (mysql_error());
				
			
			
				
				header('Location: ' . $MainURL . '/forum/'.$BoardID.'.html');
			
			}		
			else {
			
				$PageContents = $MsgText[2][100];
			
			}	
		
		}
		else {
		
			$PageContents = $MsgText[2][58];
		
		}

	
	}
	else {
		$PageContents = $MsgText[0][98];
	}	
			
	
	
?>
