<?php
/*****************************************************************************
 *   Close Thread (/inc/plugins/myplaza/closetopic.php)
 *     - MyPlaza for MyBB 1.2
 *    By ZiNgA BuRgA, 2007-2008
 * 
 * Allows users to close a thread.
 *****************************************************************************/

if(!defined("IN_MYBB"))
	die("This file cannot be accessed directly.");

function closetopic_lang()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->thread_id = 'URL of thread, or tid to close:';
			
			$lang->must_supply_value = 'Invalid thread entered.';
			$lang->already_closed = 'The thread is already closed!';
			
			$lang->thread_closed = 'Successfully closed thread #%1$s';
			$lang->thread_closed_log = 'Closed thread <a href="%1$s">%2$s</a>';
	}
}

function closetopic_lang_admin()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->closetopic_name = 'Close Thread';
			$lang->closetopic_description = 'Allows users to close a thread.';
			
			$lang->item_closetopic_close_name = 'Close Thread';
			$lang->item_closetopic_close_desc = 'Close a thread!';
	}
}

function closetopic_info()
{
	return array(
		"website"		=> "http://myplaza.zingaburga.com/",
		"author"		=> "ZiNgA BuRgA",
		"authorsite"	=> "http://zingaburga.com/",
		"version"		=> "1.0",
		"compatibility"	=> array(0.5)
	);
}

function closetopic_activate()
{
	// stop people trying to activate this module as a MyBB plugin
	if(!defined("IN_MYPLAZA_ADMIN"))
		cperror('This is not a normal MyBB plugin!  Please upload this file to your /inc/plugins/myplaza directory.');
	
	global $lang;
	// add our items
	myplaza_add_item(array(
		"idname" => 'closetopic_close',
		"cost" => 1000,
		"buylimitamount" => 2,
		"buylimittime" => 259200, // 3 days
		"htmlextra" => '{$lang->thread_id} <input type="text" name="thread" value="" size="30" />'
	));
}

function closetopic_deactivate()
{
	myplaza_remove_module_items();
}

function closetopic_run($item)
{
	global $mybb, $lang, $db, $buyRtnMsg, $buyLogMsg;
	
	if(!($t = $mybb->input['thread']))
	{
		$buyRtnMsg = $lang->must_supply_value;
		return false;
	}
	if(!($tid = intval($t)))
	{
		// try parsing the URL
		if(strpos($t, 'tid='))
		{
			$t = substr($t, strpos($t, 'tid=')+4);
			$tid = intval($t);
		}
	}
	
	if($tid)
	{
		// check if thread exists
		$thread = $db->fetch_array($db->simple_select(MY_TABLE_PREFIX.'threads', 'tid,closed,subject', 'tid = '.$tid));
	}
	if(!isset($thread) || !$thread)
	{
		$buyRtnMsg = $lang->must_supply_value;
		return false;
	}
	
	if($thread['closed'] == 'yes')
	{
		$buyRtnMsg = $lang->already_closed;
		return false;
	}
	
	// all seems okay
	$db->update_query(MY_TABLE_PREFIX.'threads', array('closed' => 'yes'), 'tid = '.$tid);
	
	$buyRtnMsg = sprintf($lang->thread_closed, $tid);
	$buyLogMsg = sprintf($lang->thread_closed_log, $mybb->settings['bburl'].'/showthread.php?tid='.$tid, $thread['subject']);
	
	return true;
}

?>