<?php
// Ensure we have everything in order
VBSHOP::verify_purchases($post);

foreach ($post['dbtech_vbshop_purchase'] as $purchaseid => $purchase)
{
	if ($purchase['feature'] != 'item' OR $purchase['userid'] != $post['userid'])
	{
		// Skip the feature
		continue;
	}
	
	if (!$purchase['active'])
	{
		// Skip the feature
		continue;
	}
	
	if (!$item = VBSHOP::$cache[$purchase['feature']][$purchase['featureid']])
	{
		// Skip this
		continue;
	}

	if (!$item['active'])
	{
		// Skip this
		continue;
	}
	
	// Init item
	$itemobj = VBSHOP::init_item(array_merge($item, $purchase));
	
	if (method_exists($itemobj, 'fetch_mpost'))
	{
		// Override permissions caching
		$itemobj->fetch_mpost($post);
	}
}

if (!VBSHOP::$postbitcache[$post['userid']])
{
	$vbshop_postbit = array();

	($hook = vBulletinHook::fetch_hook('dbtech_vbshop_postbit')) ? eval($hook) : false;
	
	foreach ((array)VBSHOP::$cache['currency'] as $currencyid => $currency)
	{
		if (!$currency['active'])
		{
			// Inactive currency
			continue;
		}
		
		// Set the points
		$currency['points'] = vb_number_format(round(VBSHOP::$cache['currencies'][$post['userid']][$currencyid], intval($currency['rounding'])));
		$currency['bankpoints'] = vb_number_format(round(VBSHOP::$cache['bank'][$post['userid']][$currencyid], intval($currency['rounding'])));
		
		// Create the main stats template
		$templater = vB_Template::create('dbtech_vbshop_postbit_stats');
			$templater->register('currency', $currency);
			$templater->register('post', $post);
		$vbshop_postbit['point'] .= $templater->render();
	}
	
	if (!is_array($post['dbtech_vbshop_purchase']))
	{
		// Just to be 100% sure
		$post['dbtech_vbshop_purchase'] = array();
	}
	krsort($post['dbtech_vbshop_purchase'], SORT_NUMERIC);
	
	$i = 0;
	$feature = 'item';
	$purchases = '';
	foreach ((array)$post['dbtech_vbshop_purchase'] as $purchaseid => $purchase)
	{
		if ($purchase['feature'] != $feature)
		{
			// We only want achievements atm
			continue;
		}
		
		if (!$purchase['active'])
		{
			// We only want achievements atm
			continue;
		}
		
		if ($purchase['hidden'])
		{
			// We only want achievements atm
			continue;
		}
		
		if ($purchase['userid'] != $post['userid'])
		{
			// We're not displaying stuff not intended for us
			continue;
		}			
		
		// This is the info we need
		$purchase = array_merge($purchase, (array)VBSHOP::$cache[$purchase['feature']][$purchase['featureid']]);
		
		if (!$purchase['active'])
		{
			// We only want achievements atm
			continue;
		}		

		if ($purchase['nodisplay'])
		{
			// We only want achievements atm
			continue;
		}		

		// Init item
		$itemobj = VBSHOP::init_item($purchase);
		
		if (method_exists($itemobj, 'postbit_display_complete'))
		{
			// Override permissions caching
			$itemobj->postbit_display_complete($post);
		}

		if ($i >= $this->registry->options['dbtech_vbshop_numgifts'])
		{
			// That's too many!
			continue;
		}

		// Ensure this is set
		$purchase['icon'] = ($purchase['icon'] ? $purchase['icon'] : 'unknown.gif');

		if ($this->registry->options['dbtech_vbshop_enablegiftgiver'] AND $purchase['buyer'] != $purchase['userid'])
		{
			$purchase['date'] = vbdate($this->registry->options['dateformat'], $purchase['dateline'], true);
			$purchase['time'] = vbdate($this->registry->options['timeformat'], $purchase['dateline']);

			// Override this
			$purchase['title'] .= "\n" . construct_phrase(
				$vbphrase['dbtech_vbshop_gift_received_x_from_y'],
				$purchase['date'] . (!$show['detailedtime'] ? ', ' . $purchase['time'] : ''),
				VBSHOP::$cache['userlookup'][$purchase['buyer']],
				($purchase['message'] ? "\n" . $vbphrase['message'] . ': ' . $purchase['message'] : '')
			);
		}

		// Create the achievement bit
		$templater = vB_Template::create('dbtech_vbshop_postbit_purchasebit');
			$templater->register('purchase', $purchase);
			$templater->register('post', $post);
		$purchases .= $templater->render();
		
		$i++;
	}
	
	if ($purchases)
	{
		$templater = vB_Template::create('dbtech_vbshop_postbit_purchase');
			$templater->register('phrase', $vbphrase["dbtech_vbshop_{$feature}s"]);
			$templater->register('purchases', $purchases);
			$templater->register('post', $post);
		$vbshop_postbit[$feature] = $templater->render();
		
		if (intval($this->registry->versionnumber) == 3)
		{
			$vbshop_postbit[$feature] = '<div>' . $vbshop_postbit[$feature] . '</div>';
		}
		else
		{
			$vbshop_postbit[$feature] = '<dd>' . $vbshop_postbit[$feature] . '</dd>';
		}
	}
	
	// Cache this
	VBSHOP::$postbitcache[$post['userid']] = $vbshop_postbit;
}

VBSHOP::$postbitcache2 = VBSHOP::$postbitcache["$post[userid]"];
foreach (VBSHOP::$postbitcache2 as $feature => &$template)
{
	// Ensure this is updated with the proper postid
	//$template = str_replace('[postid]', $post['postid'], $template);
	
	if ($this->registry->options["dbtech_vbshop_{$feature}s_postbit"] & 1)
	{
		// We're hooking into this location
		//$template_hook['postbit_userinfo_left'] .= '<dl class="userinfo_extra">' . $template . '</dl>';
	}
	
	if ($this->registry->options["dbtech_vbshop_{$feature}s_postbit"] & 2)
	{
		// We're hooking into this location
		$template_hook['postbit_userinfo_right_after_posts'] .= $template;
	}
	
	if ($this->registry->options["dbtech_vbshop_{$feature}s_postbit"] & 4)
	{
		// We're hooking into this location
		//$template_hook['postbit_userinfo_right'] .= '<div class="imlinks"><dl class="userinfo_extra">' . $template . '</dl></div>';
	}
	
	if ($this->registry->options["dbtech_vbshop_{$feature}s_postbit"] & 8)
	{
		// We're hooking into this location
		//$template_hook['postbit_signature_start'] .= '<div class="userinfo"><dl class="userinfo_extra">' . $template . '</dl></div>';
	}
	
	if ($this->registry->options["dbtech_vbshop_{$feature}s_postbit"] & 16)
	{
		// We're hooking into this location
		//$template_hook['postbit_signature_end'] .= '<div class="userinfo"><dl class="userinfo_extra">' . $template . '</dl></div>';
	}
}

if (isset(VBSHOP::$postbitcache3[$post['userid']]['icons']) AND is_array(VBSHOP::$postbitcache3[$post['userid']]['icons']))
{
	foreach (VBSHOP::$postbitcache3[$post['userid']]['icons'] as $hook_location => $code)
	{
		$template_hook[$hook_location] .= $code;
	}
}

$vbshop_postbit = VBSHOP::$postbitcache2;
if (intval($this->registry->versionnumber) > 3)
{
	vB_Template::preRegister('postbit', array('vbshop_postbit' => VBSHOP::$postbitcache2));
	vB_Template::preRegister('postbit_legacy', array('vbshop_postbit' => VBSHOP::$postbitcache2));
}

if (class_exists('POSTBITTABS'))
{
	if (!POSTBITTABS::$created['dbtech_vbshop'])
	{
		// DragonByte Tech: Postbit Tabs - registerView()
		POSTBITTABS::registerView('dbtech_vbshop_points', 	'DragonByte Tech: vBShop - Points Display', (intval($this->registry->versionnumber) == 3 ? '{$vbshop_postbit[point]}' : '{vb:raw vbshop_postbit.point}'));
		POSTBITTABS::registerView('dbtech_vbshop_gifts', 	'DragonByte Tech: vBShop - Items', 			(intval($this->registry->versionnumber) == 3 ? '{$vbshop_postbit[item]}' : '{vb:raw vbshop_postbit.item}'));
		
		// Set created
		POSTBITTABS::$created['dbtech_vbshop'] = true;
	}
}
?>