<?php
do
{
	if (!$vbulletin->userinfo['userid'])
	{
		// No guests
		break;
	}

	$itemIds = array();
	foreach (VBSHOP::$cache['item'] as $itemid => $item)
	{
		if (!$item['active'])
		{
			// Skip this
			continue;
		}
		
		if (!VBSHOP::checkPermissions($vbulletin->userinfo, $item['permissions']))
		{
			// This item isn't to be displayed
			continue;
		}

		if (!$category = VBSHOP::$cache['category'][$item['categoryid']])
		{
			// This category isn't to be displayed
			continue;
		}
		
		if (!$category['active'])
		{
			// This category isn't to be displayed
			continue;
		}
		
		if (!VBSHOP::checkPermissions($vbulletin->userinfo, $category['permissions']))
		{
			// This category isn't to be displayed
			continue;
		}

		if ($item['threadcreation'])
		{
			// We can buy this on the thread page
			$itemIds[$itemid] = $itemid;
		}
	}

	if (!count($itemIds))
	{
		// No items
		break;
	}

	$purchases = VBSHOP::$db->fetchAllSingleKeyed('
		SELECT featureid
		FROM $dbtech_vbshop_purchase
		WHERE feature = ?
			AND userid = ?
	', 'featureid', 'featureid', array(
		'item',
		$vbulletin->userinfo['userid'],
	));

	$itemsByType = array();
	$items = VBSHOP::$db->fetchAll('
		SELECT item.itemid, item.categoryid, shopinventory.*
		FROM $dbtech_vbshop_shopinventory AS shopinventory
		LEFT JOIN $dbtech_vbshop_item AS item ON(item.itemid = shopinventory.itemid)
		LEFT JOIN $dbtech_vbshop_category AS category ON(category.categoryid = item.categoryid)
		WHERE shopinventory.itemid :itemIds
			AND shopinventory.active = \'1\'
		ORDER BY category.displayorder ASC, item.displayorder ASC
	', array(
		':itemIds' 		=> VBSHOP::$db->queryList($itemIds),
	));
	foreach ($items as $item)
	{
		if (!$shop = VBSHOP::$cache['shop'][$item['shopid']])
		{
			// Inactive shop
			continue;
		}

		if (!$shop['active'])
		{
			// Inactive shop
			continue;
		}
		
		if (!VBSHOP::checkPermissions($vbulletin->userinfo, $shop['permissions']))
		{
			// This isn't to be displayed
			continue;
		}

		// Store the category array
		$itemsByType[VBSHOP::$cache['item'][$item['itemid']]['itemtypeid']] [$item['itemid']] = array_merge(VBSHOP::$cache['item'][$item['itemid']], $item);
	}

	// Init this
	$threadItems = $threadItemPopups = '';

	foreach ($itemsByType as $itemtypeid => $items)
	{
		$itemList = '';
		foreach ($items as $item)
		{
			// Init this
			$disabled = '';
			
			if (!VBSHOP::check_price($item, $vbulletin->userinfo))
			{
				// Couldn't afford
				$disabled = $vbphrase['dbtech_vbshop_insufficient_funds'];
			}
			
			if ($item['stock'] == 0)
			{
				// Out of stock
				$disabled = $vbphrase['dbtech_vbshop_out_of_stock'];
			}
			
			if ($item['uniqueitem'] AND $purchases[$itemid])
			{
				// Already bought it
				$disabled = $vbphrase['dbtech_vbshop_already_owned_bought'];
			}
			
			// Ensure this is set
			$item['icon'] 		= ($item['icon'] 		? $item['icon'] 	: 'unknown.gif');
			$item['shopicon'] 	= ($item['shopicon'] 	? $item['shopicon'] : 'unknown_shop.gif');
			
			// Ensure this is set
			$item['currency'] 		= VBSHOP::$cache['currency'][$item['currencyid']]['title'];
			$item['owner'] 			= ($item['ownerid'] 	? $userids[$item['ownerid']] 	: $vbphrase['n_a']);				
			$item['giftable'] 		= ($item['giftable'] 	? $vbphrase['yes'] 				: $vbphrase['no']);				
			$item['unique'] 		= ($item['uniqueitem'] 	? $vbphrase['yes'] 				: $vbphrase['no']);				
			$item['duration'] 		= ($item['duration'] 	? $item['duration'] 			: $vbphrase['dbtech_vbshop_unlimited']);				
			$item['buybacktime'] 	= ($item['buybacktime'] ? $item['buybacktime'] 			: $vbphrase['dbtech_vbshop_unlimited']);				
			$item['maxstock'] 		= ($item['stock'] > -1 	? $item['maxstock'] 			: $vbphrase['dbtech_vbshop_unlimited']);				
			$item['stock'] 			= ($item['stock'] > -1 	? $item['stock'] 				: $vbphrase['dbtech_vbshop_unlimited']);	
			$item['maxstock'] 		= ($item['stock'] 		? $item['maxstock'] 			: 0);						
			$item['description'] 	= nl2br(htmlspecialchars_uni($item['description']));

			// Do item bits only if it's in the currently viewing shop
			$templater = vB_Template::create('dbtech_vbshop_threadcreate_itembit');
				$templater->register('item', 		$item);
				$templater->register('currency', 	VBSHOP::$cache['currency'][$item['currencyid']]);
				$templater->register('disabled', 	$disabled);
			$itemList .= $templater->render();

			// Do item bits only if it's in the currently viewing shop
			$templater = vB_Template::create('dbtech_vbshop_itempopup');
				$templater->register('item', 		$item);
			$threadItemPopups .= $templater->render();
		}

		// Template output
		$templater = vB_Template::create('dbtech_vbshop_threadcreate_itemgroup');
			$templater->register('phrase', VBSHOP::$cache['itemtype'][$itemtypeid]['title']);
			$templater->register('items', $itemList);
		$threadItems .= $templater->render();
	}

	$templater = vB_Template::create('dbtech_vbshop_threadcreate_wrapper');
		$templater->register('items', $threadItems);
	$posticons .= $templater->render();

	$footer .= $threadItemPopups . '<script type="text/javascript" src="' . VBSHOP::jQueryPath() . '"></script>' . VBSHOP::js('.hover', true, false);
}
while (false);
?>