<?php

$plugins->add_hook('datahandler_post_insert_post', 'bumpthread_newpost');
$plugins->add_hook('datahandler_post_insert_thread', 'bumpthread_newthread');
$plugins->add_hook('showthread_start', 'bumpthread_run');
$plugins->add_hook('forumdisplay_start', 'bumpthread_foruminject');

function bumpthread_info()
{
	return array(
		'name'			=> 'Bump Thread',
		'description'	=> 'Allows users to bump their own threads without posting.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0'
	);
}

function bumpthread_activate()
{
	global $db;
	$db->query('ALTER TABLE '.TABLE_PREFIX.'threads ADD (
		`lastpostbump` BIGINT(30) UNSIGNED NOT NULL DEFAULT 0
	)');
	$db->query('UPDATE '.TABLE_PREFIX.'threads SET lastpostbump=lastpost');
	
	
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('showthread', '#'.preg_quote('{$thread[\'subject\']}</strong>').'#', '{$thread[\'subject\']}</strong>{$bumpthread}');
	
	$new_template = ' (<a href="showthread.php?tid={$tid}&amp;action=bump">Bump This Thread</a>)';
	$db->insert_query(TABLE_PREFIX.'templates', array(
		'title' => 'showthread_bumpthread',
		'template' => $db->escape_string($new_template),
		'sid' => -1,
		'version' => 120
	));
	
	
	// add settings
	$db->insert_query(TABLE_PREFIX.'settinggroups', array(
		'name' => 'bumpthread',
		'title' => 'Bump Thread Options',
		'disporder' => 50,
	));
	$settings_gid = $db->insert_id();
	$db->insert_query(TABLE_PREFIX.'settings', array(
		'name' => 'bumpthread_interval',
		'optionscode' => 'text',
		'value' => 30,
		'title' => 'Time Between Bumps',
		'description' => 'The time (in minutes) a user must wait before they are allowed to (re) bump their thread.',
		'disporder' => 1,
		'gid' => $settings_gid
	));
	rebuildsettings();
}

function bumpthread_deactivate()
{
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('showthread', '#\{\$bumpthread\}#', '', 0);
	
	global $db;
	$db->delete_query(TABLE_PREFIX.'templates', 'title = "showthread_bumpthread"');
	
	$db->query('ALTER TABLE '.TABLE_PREFIX.'threads DROP `lastpostbump`');
	
	$gid = $db->fetch_field($db->simple_select(MY_TABLE_PREFIX.'settinggroups', 'gid', 'name="bumpthread"'), 'gid');
	if($gid)
	{
		$db->delete_query(MY_TABLE_PREFIX.'settings', 'gid='.$gid);
		$db->delete_query(MY_TABLE_PREFIX.'settinggroups', 'gid='.$gid);
	}
	rebuildsettings();
}

function bumpthread_newpost(&$ph)
{
	global $db;
	$db->update_query(TABLE_PREFIX.'threads', array('lastpostbump' => time()), 'tid='.$ph->data['tid']);
}

function bumpthread_newthread(&$ph)
{
	$ph->thread_insert_data['lastpostbump'] = $ph->data['dateline'];
}

function bumpthread_run()
{
	global $mybb, $thread, $db;
	if($mybb->input['action'] == 'bump')
	{
		if($mybb->usergroup['cancp'] != 'yes' && $mybb->usergroup['issupermod'] != 'yes' && $thread['uid'] != $mybb->user['uid'])
			error_no_permission();
		
		if($thread['lastpostbump'] + intval($mybb->settings['bumpthread_interval'])*60 > time())
			error('You cannot bump this thread within '.intval($mybb->settings['bumpthread_interval']).' minute(s) of its last bump.');
		
		$db->update_query(TABLE_PREFIX.'threads', array('lastpostbump' => time()), 'tid='.$thread['tid']);
		redirect('showthread.php?tid='.$thread['tid'], 'Thread Bumped');
	}
	else
	{
		// eval bump
		if($mybb->usergroup['cancp'] == 'yes' || $mybb->usergroup['issupermod'] == 'yes' || $thread['uid'] == $mybb->user['uid'])
		{
			global $bumpthread, $templates, $tid;
			eval('$bumpthread = "'.$templates->get('showthread_bumpthread').'";');
		}
	}
}

function bumpthread_foruminject()
{
	global $db;
	$db = new DummyDB($db);
}


// I love dummying classes! :)
class DummyDB extends databaseEngine
{
	function DummyDB(&$olddb)
	{
		$vars = get_object_vars($olddb);
		foreach($vars as $var => $val)
		{
			$this->$var = $val;
		}
	}
	
	function query($string, $hideerr=0)
	{
		$string = str_replace('t.lastpost', 't.lastpostbump', $string);
		return parent::query($string, $hideerr);
	}
}
?>