<?php
if ($threadedmode != 0)
{
	// $cache_postids
	$post_ids = 'post.postid IN (' . $cache_postids . ')';
}
else
{
	if (intval($vbulletin->versionnumber) == 3)
	{
		// $cache_postids
		$post_ids = 'post.postid IN (0' . $ids . ')';
	}
	else
	{
		// $cache_postids
		$post_ids = 'post.postid IN (0' . implode(',', $ids) . ')';
	}
}

$currencydata = array(
	'fields' => array(),
	'tables' => array(),
	'lookup' => array(),
);

foreach ((array)VBSHOP::$cache['currency'] as $currencyid => $currency)
{
	if (!$currency['active'])
	{
		// Inactive currency
		continue;
	}
	
	// Setup the fields and tables
	$currencydata['fields'][] = 'pointstable_' . $currencyid . '.' . $currency['pointscolumn'] . ' AS points_' . $currencyid;
	$currencydata['tables'][] = 'LEFT JOIN ' . TABLE_PREFIX . $currency['pointstable'] . ' AS pointstable_' . $currencyid . ' ON(pointstable_' . $currencyid . '.userid = post.userid)';
	$currencydata['lookup']['points_' . $currencyid] = $currencyid;
}

$purchases = array();
if (count($currencydata['fields']))
{
	// Fetch rewards
	$results = VBSHOP::$db->fetchAll('
		SELECT
			' . implode(',', $currencydata['fields']) . ',
			post.userid,
			user.dbtech_vbshop_purchase
		FROM $post AS post
		LEFT JOIN  $user AS user ON(user.userid = post.userid)
		' . implode(' ', $currencydata['tables']) . '
		WHERE :postIds
	', array(
		':postIds' => $post_ids
	));
	foreach ($results as $results_r)
	{
		foreach ($currencydata['lookup'] as $key => $val)
		{
			VBSHOP::$cache['currencies'][$results_r['userid']][$val] = $results_r[$key];
		}

		// Do purchases
		$purchases[$results_r['userid']] = $results_r['dbtech_vbshop_purchase'];
	}

	if ($vbulletin->options['dbtech_vbshop_bank_postbit'])
	{
		// Fetch rewards
		$results = VBSHOP::$db->fetchAll('
			SELECT
				bank.currencyid,
				bank.points,
				post.userid
			FROM $dbtech_vbshop_bank AS bank
			LEFT JOIN $post AS post ON(post.userid = bank.userid)
			WHERE :postIds
				AND bank.currencyid :currencyIds
		', array(
			':postIds' 		=> $post_ids,
			':currencyIds' 	=> VBSHOP::$db->queryList($currencydata['lookup'])
		));
		foreach ($results as $results_r)
		{
			VBSHOP::$cache['bank'][$results_r['userid']][$results_r['currencyid']] = $results_r['points'];
		}
	}
}
else if ($vbulletin->options['dbtech_vbshop_enablegiftgiver'])
{
	// Fetch rewards
	$results = VBSHOP::$db->fetchAll('
		SELECT
			post.userid,
			user.dbtech_vbshop_purchase
		FROM $post AS post
		LEFT JOIN  $user AS user ON(user.userid = post.userid)
		WHERE :postIds
	', array(
		':postIds' => $post_ids
	));
	foreach ($results as $results_r)
	{
		// Do purchases
		$purchases[$results_r['userid']] = $results_r['dbtech_vbshop_purchase'];
	}
}

if ($vbulletin->options['dbtech_vbshop_enablegiftgiver'])
{
	$userids = array();
	foreach ($purchases as $userid => $purchase)
	{
		$purchase = @unserialize($purchase);
		$purchase = (is_array($purchase) ? $purchase : array());

		foreach ($purchase as $item)
		{
			if ($item['buyer'] == $userid)
			{
				// Not a gift
				continue;
			}

			$userids[] = $item['buyer'];
		}
	}

	// Get rid of dupes
	$userids = array_unique($userids, SORT_NUMERIC);

	// Store cached usernames
	VBSHOP::$cache['userlookup'] = VBSHOP::$db->fetchAllSingleKeyed('
		SELECT userid, username
		FROM $user
		WHERE userid :userIds
	', 'userid', 'username', array(
		':userIds' => VBSHOP::$db->queryList($userids)
	));		
}
?>