<?php
if (!class_exists('vBShop_Item_Core'))
{
	die('No direct access allowed.');
}

class vBShop_Item_customicon extends vBShop_Item_Core
{
	/**
	* The constructor
	*
	* @param	vBulletin	vBulletin registry
	* @param	array		Item info
	*/	
	public function __construct(&$registry, &$item)
	{
		parent::__construct($registry, $item);
	}
	
	/**
	* Function to call before every action
	*/	
	public function action()
	{
		if (!parent::action())
		{
			// This type is inactive
			return false;
		}
		
		// We made it!
		return true;
	}
	
	/**
	* Prints out an AdminCP form
	*/	
	public function print_admin_form($code)
	{
		global $vbphrase;

		// Shop icons
		$customicons = array('' => $vbphrase['none']);
		$d = dir(DIR . '/dbtech/vbshop/images/customicons');
		while (false !== ($file = $d->read()))
		{
			if ($file != '.' AND $file != '..' AND $file != 'index.html')
			{
				// Store the icon
				$customicons['customicons/' . $file] = $file;
			}
		}
		$d->close();
		asort($customicons, SORT_STRING);

		print_select_row($vbphrase['dbtech_vbshop_custom_icon'], 			'item[code][customicon]',			$customicons,	$code['customicon']);
		print_select_row($vbphrase['dbtech_vbshop_custom_icon_position'], 	'item[code][position]',				array(
			'postbit_userinfo_left' 				=> 'postbit_userinfo_left',
			'postbit_userinfo_right_after_posts' 	=> 'postbit_userinfo_right_after_posts',
			'postbit_userinfo_right' 				=> 'postbit_userinfo_right',
			'postbit_messagearea_start' 			=> 'postbit_messagearea_start',
			'postbit_signature_start' 				=> 'postbit_signature_start',
			'postbit_signature_end' 				=> 'postbit_signature_end',
			'postbit_controls' 						=> 'postbit_controls',
			'icongroup' 							=> $vbphrase['dbtech_vbshop_custom_by_icongroup'],
		),																														$code['position']);
		print_input_row($vbphrase['dbtech_vbshop_custom_icon_group'], 		'item[code][group]',								$code['group']);
		print_input_row($vbphrase['dbtech_vbshop_custom_icon_limit'], 		'item[code][limit]',								isset($code['limit']) ? $code['limit'] : 5);
	}

	
	/**
	* Appends to a specified template hook
	*
	* @param	array		User info
	*/	
	public function postbit_display_complete($user)
	{
		if (!$this->action())
		{
			// We're not doin this
			return false;
		}

		if (!$this->config['code']['customicon'] OR !$this->config['code']['position'])
		{
			// We're not doin this
			return false;
		}

		if ($this->config['code']['position'] == 'icongroup')
		{
			// Override this
			$this->config['code']['position'] = 'dbtech_vbshop_icongroup_' . $this->config['code']['group'];
		}

		if (!isset(VBSHOP::$postbitcache3[$user['userid']]))
		{
			// Ensure this is an array
			VBSHOP::$postbitcache3[$user['userid']] = array(
				'icons' => array(),
				'countbyitemid' => array(),
				'countbyposition' => array(),
			);
		}

		if (!isset(VBSHOP::$postbitcache3[$user['userid']]['count'][$this->config['itemid']]))
		{
			// Ensure this is an array
			VBSHOP::$postbitcache3[$user['userid']]['count'][$this->config['itemid']] = array();
		}

		if (!isset(VBSHOP::$postbitcache3[$user['userid']]['countbyposition'][$this->config['code']['position']]))
		{
			// Ensure this is an array
			VBSHOP::$postbitcache3[$user['userid']]['countbyposition'][$this->config['code']['position']] = 0;
		}

		if (!isset(VBSHOP::$postbitcache3[$user['userid']]['icons'][$this->config['code']['position']]))
		{
			// Ensure this is an array
			VBSHOP::$postbitcache3[$user['userid']]['icons'][$this->config['code']['position']] = '';
		}

		if ($this->config['code']['limit'] AND VBSHOP::$postbitcache3[$user['userid']]['countbyposition'][$this->config['code']['position']] >= $this->config['code']['limit'])
		{
			// We have too many
			return false;
		}

		// Set the image
		VBSHOP::$postbitcache3[$user['userid']]['icons'][$this->config['code']['position']] .= '<img src="dbtech/vbshop/images/' . $this->config['code']['customicon'] . '" /> ';

		// Count towards the limit
		VBSHOP::$postbitcache3[$user['userid']]['countbyposition'][$this->config['code']['position']]++;
	}
}
?>