<?php
do
{
	if ($this->condition)
	{
		// Only first post
		break;
	}

	$this->registry->input->clean_array_gpc('r', array(
		'threadbuy' 	=> TYPE_ARRAY,
	));

	foreach ($this->registry->GPC['threadbuy'] as $shopid => $items)
	{
		if (!$shop = VBSHOP::$cache['shop'][$shopid])
		{
			// Woups, immune
			continue;
		}

		if (!$shop['active'])
		{
			// Woups, immune
			continue;
		}

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

		foreach ($items as $itemid => $onoff)
		{
			if (!$item = VBSHOP::$cache['item'][$itemid])
			{
				// Woups, immune
				continue;
			}

			if (!$item['active'])
			{
				// Skip this
				continue;
			}

			if (!$item['threadpage'])
			{
				// Skip this
				continue;
			}
			
			if (!VBSHOP::checkPermissions($this->registry->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($this->registry->userinfo, $category['permissions']))
			{
				// This category isn't to be displayed
				continue;
			}
			
			if ($item['unique'] AND VBSHOP::$db->fetchOne('
				SELECT featureid
				FROM $dbtech_vbshop_purchase
				WHERE feature = ?
					AND featureid = ?
					AND userid = ?
			', array(
				'item',
				$item['itemid'],
				$this->registry->userinfo['userid'],
			)))
			{
				// This item isn't to be displayed
				continue;
			}

			if (!$inventory = VBSHOP::$db->fetchRow('
				SELECT item.itemid, item.categoryid, shopinventory.*
				FROM $dbtech_vbshop_shopinventory AS shopinventory
				LEFT JOIN $dbtech_vbshop_item AS item ON(item.itemid = shopinventory.itemid)
				WHERE shopinventory.itemid = ?
					AND shopinventory.shopid = ?
					AND shopinventory.active = \'1\'
			', array(
				$item['itemid'],
				$shop['shopid'],
			)))
			{
				// This item isn't to be displayed
				continue;
			}

			if (!VBSHOP::check_price($inventory, $this->registry->userinfo))
			{
				// Couldn't afford
				continue;
			}
			
			if (!$inventory['stock'])
			{
				// Out of stock
				continue;
			}	

			// Grab the full item info
			$item = array_merge($item, $inventory);
			
			 // Ensure this is set
			$item['quantity'] = 1;
			
			// Init the item
			$itemobj = VBSHOP::init_item($item);
			
			if (!$purchaseid = $itemobj->purchase($this->registry->userinfo))
			{
				// Purchase failed
				continue;
			}

			if (method_exists($itemobj, 'configure'))
			{
				// Force allow configure
				$itemobj->config['_overrideconfigerror'] = true;
				
				// Hack
				$_POST['code']['contentid'] = $this->fetch_field('threadid');

				// Do configuration
				$itemobj->configure($purchaseid);
			}
		}
	}
}
while (false);
?>