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

class vBShop_Item_usertitlechange2 extends vBShop_Item_Core
{
	/**
	* The constructor
	*
	* @param	vBulletin	vBulletin registry
	* @param	array		Item info
	*/	
	public function __construct(&$registry, &$item)
	{
		parent::__construct($registry, $item);
		
		// Set purchase template
		//$this->config['purchasetemplate'] = 'dbtech_vbshop_configure_usertitlechange';
	}
	
	/**
	* 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;
		
		print_input_row($vbphrase['user_title'], 				'item[code][usertitle]', 			$code['usertitle']);
	}

	/**
	* Activate an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function activate($purchaseid, $recipient = NULL, $dorebuild = true)
	{
		// Ensure this is set
		$recipient = ($recipient === NULL ? $this->registry->userinfo : $recipient);

		if (!parent::activate($purchaseid, $recipient, $dorebuild))
		{
			// We can't activate this item
			return false;
		}
		
		if ($this->registry->options['ctMaxChars'] > 0)
		{
			// only trim title if changing custom title and not an admin
			$this->config['code']['usertitle'] = vbchop($this->config['code']['usertitle'], $this->registry->options['ctMaxChars']);
		}
		
		// Change the username
		$userdata =& datamanager_init('User', $this->registry, ERRTYPE_STANDARD);
			$userdata->set_existing($recipient);
			$userdata->set_usertitle($this->config['code']['usertitle'], false, array(), true, false);
		$userdata->save();		
		
		return true;
	}
	
	/**
	* Gifting an item
	*
	* @param	integer		Purchase ID
	* @param	array		Buyer info
	* @param	array		Recipient info
	* @param	string		The message from the buyer
	*/	
	public function gift($purchaseid, &$buyer, &$recipient, $message = '')
	{
		global $vbphrase;
		
		// We're done, we wanted to let the recipient configure it themselves
		$noalter = explode(',', $this->registry->config['SpecialUsers']['undeletableusers']);
		if (!is_array($noalter))
		{
			$noalter = array();
		}

		if ($buyer['userid'] != $recipient['userid'] AND in_array($recipient['userid'], $noalter))
		{
			// Woups, immune
			eval(standard_error(fetch_error('dbtech_vbshop_error_x', $vbphrase['dbtech_vbshop_recipient_immune'])));
		}
		
		// Ensure we have the necessary files
		require_once(DIR . '/includes/adminfunctions.php');
		require_once(DIR . '/includes/adminfunctions_options.php');
		
		$bitfielddefs =& fetch_bitfield_definitions('nocache|dbtech_vbshop_immunity');
		foreach ($bitfielddefs AS $key => $val)
		{
			if ($key == 'dbtech_vbshop_' . $this->config['itemtypeid'] AND $recipient['userid'] != $buyer['userid'] AND ((int)$recipient['dbtech_vbshop_immunity'] & (int)$val))
			{
				// Woups, immune
				eval(standard_error(fetch_error('dbtech_vbshop_error_x', $vbphrase['dbtech_vbshop_recipient_immune'])));
			}
		}
		
		// Do parent checking
		parent::gift($purchaseid, $buyer, $recipient, $message);
	}
}
?>