<?php
if (class_exists('VBSHOP') AND $user['userid'])
{
	$fetch = array();
	if (!isset($user['dbtech_vbshop_purchase']))
	{
		if (isset(VBSHOP::$cache['purchase'][$user['userid']]))
		{
			// Set the purchase log cache
			$user['dbtech_vbshop_purchase'] = VBSHOP::$cache['purchase'][$user['userid']];
		}
		else
		{
			$fetch[] = 'dbtech_vbshop_purchase';
		}
	}
	
	if ($vbulletin->options['dbtech_vbshop_forceformatting'] AND !empty($fetch))
	{
		// Fetch purchase log
		$purchaselog = $vbulletin->db->query_first_slave("SELECT " . implode(',', $fetch) . " FROM " . TABLE_PREFIX . "user WHERE userid = " . intval($user['userid']));
		foreach ($fetch as $key)
		{
			if (!$purchaselog[$key])
			{
				// Prevent constant re-fetch
				$purchaselog[$key] = serialize(array());
			}
			
			$user[$key] = VBSHOP::$cache[str_replace('dbtech_vbshop_', '', $key)][$user['userid']] = $purchaselog[$key];
		}
	}	
	
	if (isset($user['dbtech_vbshop_purchase']))
	{
		// Verify purchases
		VBSHOP::verify_purchases($user);
		
		$reload = false;
		foreach ($user['dbtech_vbshop_purchase'] as $purchaseid => $purchase)
		{
			if ($purchase['feature'] != 'item' OR $purchase['userid'] != $user['userid'])
			{
				// Skip the feature
				continue;
			}
		
			if (!$purchase['active'])
			{
				// Skip the feature
				continue;
			}
			
			if (!is_array(VBSHOP::$cache[$purchase['feature']][$purchase['featureid']]))
			{
				// Skip this
				continue;
			}
			
			// Init item
			$itemobj = VBSHOP::init_item(array_merge(VBSHOP::$cache[$purchase['feature']][$purchase['featureid']], $purchase));
			
			if (method_exists($itemobj, 'fetch_musername'))
			{
				// Override musername
				$itemobj->fetch_musername($user, $displaygroupid);
			}
		}
	}
}