<?php
/*****************************************************************************
 *   Investment Module (/inc/plugins/myplaza/invest.php)
 *     - MyPlaza for MyBB 1.2
 *    By ZiNgA BuRgA, 2007-2008
 * 
 * Let your users invest into new registrations, posts or threads.
 *****************************************************************************/

if(!defined("IN_MYBB"))
	die("This file cannot be accessed directly.");

function invest_lang()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->pay_per_registration = '%1$s paid per new registration';
			$lang->pay_per_thread = '%1$s paid per new thread';
			$lang->pay_per_post = '%1$s paid per new post';
			
			$lang->effect_length = 'Effect length (days)';
	}
}

function invest_lang_admin()
{
	global $mybb, $lang;
	switch($mybb->settings['bblanguage'])
	{
		default:
			$lang->invest_name = 'Invest in Board';
			$lang->invest_description = 'Let your users invest into new registrations, posts or threads.';
			
			$lang->item_invest_registration_name = 'Invest in New Registrations';
			$lang->item_invest_registration_desc = 'Pays you some money every time a new user registers on the forums.  This effect only lasts for a certain period of time.';
			
			$lang->item_invest_thread_name = 'Invest in New Threads';
			$lang->item_invest_thread_desc = 'Pays you some money every time a new thread is made on the forums.  This effect only lasts for a certain period of time.';

			$lang->item_invest_post_name = 'Invest in New Posts';
			$lang->item_invest_post_desc = 'Pays you some money every time a new post is made on the forums.  This effect only lasts for a certain period of time.';
			
			$lang->setting_invest_registrations_time = 'Expiry time of Invest in Registrations item';
			$lang->setting_invest_registrations_time_desc = 'How long (in days) the user keeps this item.';
			
			$lang->setting_invest_registrations_pay = 'Payment per Registration';
			$lang->setting_invest_registrations_pay_desc = 'Amount of money paid to a user with the Invest in Registrations item, every time a new user is registered on the board.';
			
			$lang->setting_invest_threads_time = 'Expiry time of Invest in Threads item';
			$lang->setting_invest_threads_time_desc = 'How long (in days) the user keeps this item.';
			
			$lang->setting_invest_threads_pay = 'Payment per Thread';
			$lang->setting_invest_threads_pay_desc = 'Amount of money paid to a user with the Invest in Threads item, every time a new thread is made on the board.';
			
			$lang->setting_invest_posts_time = 'Expiry time of Invest in Posts item';
			$lang->setting_invest_posts_time_desc = 'How long (in days) the user keeps this item.';
			
			$lang->setting_invest_posts_pay = 'Payment per Post';
			$lang->setting_invest_posts_pay_desc = 'Amount of money paid to a user with the Invest in Posts item, every time a new post is made on the board.';
			
			
			$lang->invest_options = 'Investment Options';
	}
}

function invest_info()
{
	return array(
		"website"		=> "http://myplaza.zingaburga.com",
		"author"		=> "ZiNgA BuRgA",
		"authorsite"	=> "http://zingaburga.com/",
		"version"		=> "1.0",
		"compatibility"	=> array(0.5)
	);
}

function invest_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.');
	
	db_create_table('plaza_invest_registrations', array(
		'uid' => array('type' => 'int', 'size' => 10, 'unsigned' => true, 'not_null' => true),
		'expiretime' => array('type' => 'bigint', 'size' => 30, 'unsigned' => true, 'not_null' => true)
	), array(
		array('type' => 'primary', 'fields' => 'uid,expiretime')
	));
	db_create_table('plaza_invest_threads', array(
		'uid' => array('type' => 'int', 'size' => 10, 'unsigned' => true, 'not_null' => true),
		'expiretime' => array('type' => 'bigint', 'size' => 30, 'unsigned' => true, 'not_null' => true)
	), array(
		array('type' => 'primary', 'fields' => 'uid,expiretime')
	));
	db_create_table('plaza_invest_posts', array(
		'uid' => array('type' => 'int', 'size' => 10, 'unsigned' => true, 'not_null' => true),
		'expiretime' => array('type' => 'bigint', 'size' => 30, 'unsigned' => true, 'not_null' => true)
	), array(
		array('type' => 'primary', 'fields' => 'uid,expiretime')
	));
	
	
	// add our items
	myplaza_add_item(array(
		"idname" => 'invest_registration',
		"cost" => 750,
		"buylimitamount" => 1,
		"buylimittime" => 1209600, // 14 days
		"htmlextra" => '{$lang->pay_per_registration}<br />{$lang->effect_length}: {$vars[\'reg_time\']}'
	));
	myplaza_add_item(array(
		"idname" => 'invest_thread',
		"cost" => 750,
		"buylimitamount" => 1,
		"buylimittime" => 1209600, // 14 days
		"htmlextra" => '{$lang->pay_per_thread}<br />{$lang->effect_length}: {$vars[\'thread_time\']}'
	));
	myplaza_add_item(array(
		"idname" => 'invest_post',
		"cost" => 750,
		"buylimitamount" => 1,
		"buylimittime" => 1209600, // 14 days
		"htmlextra" => '{$lang->pay_per_post}<br />{$lang->effect_length}: {$vars[\'post_time\']}'
	));
	
	// add settings
	add_settings(array(
		array(
			"name"			=> 'invest_registrations_time',
			"optionscode"	=> 'text',
			"value"			=> '14',
		),
		array(
			"name"			=> 'invest_registrations_pay',
			"optionscode"	=> 'text',
			"value"			=> '10.00',
		),
		array(
			"name"			=> 'invest_threads_time',
			"optionscode"	=> 'text',
			"value"			=> '14',
		),
		array(
			"name"			=> 'invest_threads_pay',
			"optionscode"	=> 'text',
			"value"			=> '8.00',
		),
		array(
			"name"			=> 'invest_posts_time',
			"optionscode"	=> 'text',
			"value"			=> '14',
		),
		array(
			"name"			=> 'invest_posts_pay',
			"optionscode"	=> 'text',
			"value"			=> '0.50',
		),
	));
	
	
	if(MYBB_VERCODE <= 1206)
		$mybb127prefix = '';
	else
		$mybb127prefix = 'class_moderation_';

	plugins_add_passive_hook('datahandler_user_insert', 'invest_reg_new');
	plugins_add_passive_hook('admin_users_do_delete', 'invest_reg_del', 10, true);
	plugins_add_passive_hook('datahandler_post_insert_post', 'invest_post_new');
	plugins_add_passive_hook($mybb127prefix.'delete_post', 'invest_post_del');
	plugins_add_passive_hook('datahandler_post_insert_thread', 'invest_thread_new');
	plugins_add_passive_hook($mybb127prefix.'delete_thread', 'invest_thread_del');
}

function invest_deactivate()
{
	global $db;
	db_drop_tables(array('plaza_invest_registrations', 'plaza_invest_threads', 'plaza_invest_posts'));
	myplaza_remove_module_items();
	$db->delete_query(MY_TABLE_PREFIX.'settings', "name IN ('invest_registrations_time', 'invest_registrations_pay', 'invest_threads_time', 'invest_threads_pay', 'invest_posts_time', 'invest_posts_pay')");
	
	plugins_remove_passive_hooks();
}

function invest_run($item)
{
	global $mybb, $lang, $db, $buyRtnMsg, $buyLogMsg;
	
	$db->insert_query(MY_TABLE_PREFIX.'plaza_'.$item['idname'].'s', array(
		'uid' => $mybb->user['uid'],
		'expiretime' => REQUEST_TIME + intval($mybb->settings[$item['idname'].'_time'])
	));
	
	return true;
}

/*
function invest_get_uids($where)
{
	global $mybb, $db, $rand;
	$timecutoff = REQUEST_TIME - intval($mybb->settings['invest_'.$where.'_time']) * 86400;
	$query = db_select('plaza_invest_'.$where, 'uid, COUNT(*) AS num', 'expiretime > '.$timecutoff, array('group_by' => 'uid'));
	$uitems = array();
	while($u = $db->fetch_array($query))
		$uitems[$u['uid']] = $u['num'];
	
	if($rand <= 1)
		$db->delete_query(MY_TABLE_PREFIX.'plaza_invest_'.$where, 'expiretime <= '.$timecutoff);
	
	return $uitems;
}
*/

function invest_run_pay($where, $rate = 1)
{
	@ignore_user_abort(true);
	global $mybb, $db, $rand;
	$timecutoff = REQUEST_TIME - intval($mybb->settings['invest_'.$where.'s_time']) * 86400;
	$query = db_select('plaza_invest_'.$where.'s', 'uid, COUNT(*) AS num', 'expiretime > '.$timecutoff, array('group_by' => 'uid'));
	$updates = array();
	
	$tmpstr = '{$values[\''.MYPLAZA_MONEY_COLUMN.'\']} + (';
	$factor = floatval($mybb->settings['invest_'.$where.'s_pay']);
	
	// now generate update expressions array and update
	while($u = $db->fetch_array($query))
		$updates[$u['uid']] = array(
			MYPLAZA_MONEY_COLUMN => $tmpstr.($u['num']*$factor*$rate).')'
		);
	
	db_update_rows('users', $updates, 'uid', true);
	
	if($rand <= 1)
		$db->delete_query(MY_TABLE_PREFIX.'plaza_invest_'.$where.'s', 'expiretime <= '.$timecutoff);
	
	@ignore_user_abort(false);
}

function invest_reg_new()		{ invest_run_pay('registration'); }
function invest_post_new()		{ invest_run_pay('post'); }
function invest_thread_new()	{ invest_run_pay('thread'); }
function invest_reg_del()		{ invest_run_pay('registration', -1); }
function invest_post_del()		{ invest_run_pay('post', -1); }
function invest_thread_del($tid) {
	global $postthread_cache;
	// ouch!  two large update queries :S
	invest_run_pay('thread', -1);
	invest_run_pay('post', count($postthread_cache[$tid]));
}

function invest_admin($process)
{
	if($process)
	{
		myplaza_process_settings(array('invest_registrations_time', 'invest_registrations_pay', 'invest_threads_time', 'invest_threads_pay', 'invest_posts_time', 'invest_posts_pay'));
	}
	else
	{
		global $lang;
		myplaza_langload('invest');
		
		cpheader_myplaza_module();
		starttable();
		tableheader($lang->invest_options);
		myplaza_generate_settings_code(array('invest_registrations_time', 'invest_registrations_pay', 'invest_threads_time', 'invest_threads_pay', 'invest_posts_time', 'invest_posts_pay'));
		endtable();
		
		cpfooter_myplaza_module();
	}
}

function invest_htmlextra()
{
	global $lang, $mybb, $vars;
	$lang->pay_per_registration = sprintf($lang->pay_per_registration, my_format_money(floatval($mybb->settings['invest_registrations_pay'])));
	$lang->pay_per_thread = sprintf($lang->pay_per_thread, my_format_money(floatval($mybb->settings['invest_threads_pay'])));
	$lang->pay_per_post = sprintf($lang->pay_per_post, my_format_money(floatval($mybb->settings['invest_posts_pay'])));
	
	$vars['reg_time'] = intval($mybb->settings['invest_registrations_time']);
	$vars['thread_time'] = intval($mybb->settings['invest_threads_time']);
	$vars['post_time'] = intval($mybb->settings['invest_posts_time']);
}
?>