<?php
/**
* Profile Block for vBShop
*
* @package vBShop
*/
class vB_ProfileBlock_vBShop extends vB_ProfileBlock
{
	/**
	* The name of the template to be used for the block
	*
	* @var string
	*/
	var $template_name = 'dbtech_vbshop_memberinfo_block';

	var $nowrap = true;

	/**
	* Variables to automatically prepare
	*
	* @var array
	*/
	var $auto_prepare = array();

	/**
	* Whether to return an empty wrapper if there is no content in the blocks
	*
	* @return bool
	*/
	function confirm_empty_wrap()
	{
		return false;
	}

	/**
	* Whether or not the block is enabled
	*
	* @return bool
	*/
	function block_is_enabled($id)
	{
		return (
			$this->registry->options['dbtech_vbshop_active'] /*AND
			!$this->profile->userinfo['dbtech_vbshop_excluded'] AND
			!($this->profile->userinfo['permissions']['dbtech_vbshoppermissions'] & $this->registry->bf_ugp_dbtech_vbshoppermissions['isexcluded'])*/
		? true : false);
	}

	/**
	* Prepare any data needed for the output
	*
	* @param	string	The id of the block
	* @param	array	Options specific to the block
	*/
	function prepare_output($id = '', $options = array())
	{
		global $show, $vbphrase, $vbshop, $template_hook, $vbulletin;
		
		if (intval($this->registry->versionnumber) == 3)
		{
			$this->nowrap = false;
		}
		
		// Ensure our purchases cache is up to specs
		VBSHOP::verify_purchases($this->profile->userinfo);
		
		// Build a better mouse trap
		$features 	= array();
		$lookup 	= array();
		foreach ((array)$this->profile->userinfo['dbtech_vbshop_purchase'] as $purchaseid => $purchase)
		{
			if (!$feature = VBSHOP::$cache[$purchase['feature']][$purchase['featureid']])
			{
				// Rebuild the cache automatically
				VBSHOP_CACHE::build($purchase['feature']);
				continue;
			}

			if (!$feature['active'])
			{
				// Inactive item
				continue;
			}
			
			if (!$purchase['active'])
			{
				// Inactive item
				continue;
			}
			
			if ($purchase['hidden'])
			{
				// Inactive item
				continue;
			}
			
			if ($feature['nodisplay'])
			{
				// Inactive item
				continue;
			}
			
			if ($purchase['userid'] != $this->profile->userinfo['userid'])
			{
				// We're not displaying stuff not intended for us
				continue;
			}
			
			// Cache the results
			$features[$purchase['feature']][$purchaseid] = $purchase;
			
			if ($purchase['buyer'] != $this->profile->userinfo['userid'])
			{
				// Lookup username
				$lookup[$purchase['buyer']] = '';
			}
		}
		
		if (!empty($lookup))
		{
			// Grab people who bought this for us
			$users_q = $this->registry->db->query_read_slave("
				SELECT userid, username
				FROM " . TABLE_PREFIX . "user
				WHERE userid IN(" . implode(',', array_keys($lookup)) . ")
			");
			while ($users_r = $this->registry->db->fetch_array($users_q))
			{
				// Store username
				$lookup[$users_r['userid']] = $users_r['username'];
			}
			$this->registry->db->free_result($users_q);
			unset($users_r);
		}
		
		foreach (array('item') AS $feature)
		{			
			// We're showing achievements
			$show["{$feature}_block"] = true;
			
			// Ensure this is an array
			$features[$feature] = (!is_array($features[$feature]) ? array() : $features[$feature]);
			
			// Emulate order by dateline desc
			krsort($features[$feature], SORT_NUMERIC);
			
			// Init some important vars
			$bits = array();
			foreach ($features[$feature] as $purchaseid => $purchase)
			{
				// Shorthand featureinfo
				$featureinfo = VBSHOP::$cache[$feature]["$purchase[featureid]"];
				
				if ($purchase['buyer'] == $this->profile->userinfo['userid'])
				{
					$phrase = construct_phrase(
						$vbphrase['dbtech_vbshop_item_purchased_x'],
						vbdate($this->registry->options['dateformat'], $purchase['dateline']) . ' ' . vbdate($this->registry->options['timeformat'], $purchase['dateline']),
						$this->profile->userinfo['username']
					);					
				}
				else
				{
					$phrase = construct_phrase(
						$vbphrase['dbtech_vbshop_gift_received_x_from_y'],
						vbdate($this->registry->options['dateformat'], $purchase['dateline']) . ' ' . vbdate($this->registry->options['timeformat'], $purchase['dateline']),
						$lookup["$purchase[buyer]"],
						($purchase['message'] ? '<br />' . $vbphrase['message'] . ': ' . $purchase['message'] : '')
					);					
				}
				
				// Ensure this is set
				$featureinfo['icon'] 		= ($featureinfo['icon'] 		? $featureinfo['icon'] 		: 'unknown.gif');
				$featureinfo['shopicon'] 	= ($featureinfo['shopicon'] 	? $featureinfo['shopicon'] 	: 'unknown_shop.gif');
				
				$templater = vB_Template::create('dbtech_vbshop_memberinfo_purchasebit');
					$templater->register('feature', $featureinfo);
					$templater->register('phrase', $phrase);
				$bits[$featureinfo['categoryid']] .= $templater->render();
			}
			
			$itembits = '';
			foreach ($bits as $categoryid => $item)
			{
				$templater = vB_Template::create('dbtech_vbshop_memberinfo_purchasebit_categorybit');
					$templater->register('category', VBSHOP::$cache['category'][$categoryid]['title']);
					$templater->register('item', $item);
				$itembits .= $templater->render();
			}
			
			$this->block_data["{$feature}s"] 		= $itembits;
			$this->block_data["{$feature}count"] 	= vb_number_format(count($features[$feature]));
		}
	}
}

/**
* Profile Block for vBShop
*
* @package vBShop
*/
class vB_ProfileBlock_vBShop_ProfileMusic extends vB_ProfileBlock
{
	/**
	* The name of the template to be used for the block
	*
	* @var string
	*/
	var $template_name = 'dbtech_vbshop_memberinfo_block_profilemusic';

	var $nowrap = true;

	/**
	* Variables to automatically prepare
	*
	* @var array
	*/
	var $auto_prepare = array();

	/**
	* Whether to return an empty wrapper if there is no content in the blocks
	*
	* @return bool
	*/
	function confirm_empty_wrap()
	{
		return false;
	}

	/**
	* Whether or not the block is enabled
	*
	* @return bool
	*/
	function block_is_enabled($id)
	{
		return (
			$this->registry->options['dbtech_vbshop_active'] /*AND
			!$this->profile->userinfo['dbtech_vbshop_excluded'] AND
			!($this->profile->userinfo['permissions']['dbtech_vbshoppermissions'] & $this->registry->bf_ugp_dbtech_vbshoppermissions['isexcluded'])*/
		? true : false);
	}

	/**
	* Prepare any data needed for the output
	*
	* @param	string	The id of the block
	* @param	array	Options specific to the block
	*/
	function prepare_output($id = '', $options = array())
	{
		global $show, $vbphrase, $vbshop, $template_hook, $vbulletin;
		
		if (intval($this->registry->versionnumber) == 3)
		{
			$this->nowrap = false;
		}
		
		// Ensure our purchases cache is up to specs
		VBSHOP::verify_purchases($this->profile->userinfo);
		
		// Build a better mouse trap
		$features 	= array();
		$lookup 	= array();
		foreach ((array)$this->profile->userinfo['dbtech_vbshop_purchase'] as $purchaseid => $purchase)
		{
			if (!$item = VBSHOP::$cache[$purchase['feature']][$purchase['featureid']])
			{
				// Rebuild the cache automatically
				continue;
			}
			
			if (!$purchase['active']/* OR !VBSHOP::$cache[$purchase['feature']][$purchase['featureid']]['active']*/)
			{
				// Inactive item
				continue;
			}
			
			if ($purchase['hidden'])
			{
				// Inactive item
				continue;
			}
			
			if ($purchase['userid'] != $this->profile->userinfo['userid'])
			{
				// We're not displaying stuff not intended for us
				continue;
			}

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

			// Grab ourselves an array
			$purchase['configuration'] = @unserialize($purchase['configuration']);

			if (!$purchase['configuration']['url'] OR pathinfo(parse_url($purchase['configuration']['url'], PHP_URL_PATH), PATHINFO_EXTENSION) != 'mp3')
			{
				// Skip this
				continue;
			}

			// Set URL
			$this->block_data['mp3url'] = $purchase['configuration']['url'];
			break;
		}
	}
}
?>