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

class vBShop_Item_threadtitlestyle2 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_stylepreset';
		
		// HTML safe
		$this->config['code']['color'] = htmlspecialchars_uni($this->config['code']['color']);
		$this->config['code']['glow'] = htmlspecialchars_uni($this->config['code']['glow']);
		$this->config['code']['shadow'] = htmlspecialchars_uni($this->config['code']['shadow']);
	}
	
	/**
	* 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;
		
		/*DBTECH_PRO_START*/
		print_yes_no_row($vbphrase['dbtech_vbshop_singleuse_threadstyle'], 	'item[code][singleuse]', 		$code['singleuse']);
		/*DBTECH_PRO_END*/
		print_checkbox_row($vbphrase['dbtech_vbshop_bold'], 				'item[code][bold]', 			$code['bold']);
		print_checkbox_row($vbphrase['dbtech_vbshop_italic'], 				'item[code][italic]', 			$code['italic']);
		print_checkbox_row($vbphrase['dbtech_vbshop_underline'], 			'item[code][underline]', 		$code['underline']);
		print_input_row($vbphrase['dbtech_vbshop_color'], 					'item[code][color]', 			$code['color']);
		print_checkbox_row($vbphrase['dbtech_vbshop_enable_glow'], 			'item[code][glow_onoff]', 		$code['glow_onoff']);
		print_input_row($vbphrase['dbtech_vbshop_glow_colour'], 			'item[code][glow]', 			$code['glow']);
		print_checkbox_row($vbphrase['dbtech_vbshop_enable_shadow'], 		'item[code][shadow_onoff]', 	$code['shadow_onoff']);
		print_input_row($vbphrase['dbtech_vbshop_shadow_colour'], 			'item[code][shadow]', 			$code['shadow']);
	}

	/**
	* Whether we can configure an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Purchase info
	*/		
	public function can_configure($purchaseid, $purchase = null)
	{
		/*DBTECH_PRO_START*/
		if ($this->config['code']['singleuse'])
		{
			// Check otherwise
			return parent::can_configure($purchaseid, $purchase);
		}
		/*DBTECH_PRO_END*/

		return false;
	}

	/**
	* Configure an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function configure($purchaseid, $recipient = null)
	{
		global $vbphrase;
		
		if (!$this->can_configure($purchaseid))
		{
			// Can't configure this item
			return false;
		}
		
		// Ensure this is set
		$recipient = (!$recipient ? $this->registry->userinfo : $recipient);
		
		// Get the form code
		$code 			= $this->registry->input->clean_gpc('p', 'code', 			TYPE_ARRAY);
		$noConfigure 	= $this->registry->input->clean_gpc('p', '_noConfigure', 	TYPE_BOOL);
		
		if ($noConfigure)
		{
			// We're done, we wanted to let the recipient configure it themselves
			return true;
		}
		
		$values = array();
		foreach ($code as $style => &$value)
		{
			$output = $value;
			if (is_numeric($value))
			{
				if ($value == 0)
				{
					$output = $vbphrase['no'];
				}
				else if ($value == 1)
				{
					$output = $vbphrase['yes'];
				}				
			}
			else if (!$value)
			{
				$output = 'N/A';
			}
			else
			{
				$output = $value = preg_replace('/[^A-Za-z0-9 #(),]/', '', $value);
			}
			
			// Set the values
			$values[] = $vbphrase['dbtech_vbshop_' . $style] . ': ' . $output;
		}
		if ($this->config['notifications_config'])
		{
			$userids = explode(',', $this->config['notifications_config']);
			foreach ((array)$userids as $userid)
			{
				// Send PM
				VBSHOP::sendPM(
					$userid,
					construct_phrase($vbphrase['dbtech_vbshop_new_configuration_x'],
						$this->config['title']
					),
					construct_phrase($vbphrase['dbtech_vbshop_x_just_configured_y'],
						$recipient['username'],
						$this->config['title'],
						implode("\n", $values)
					)
				);
			}
		}
		
		// Update flag
		$this->registry->db->query_write("
			UPDATE " . TABLE_PREFIX . "dbtech_vbshop_purchase
			SET configuration = " . $this->registry->db->sql_prepare(trim(serialize($code))) . "
			WHERE purchaseid = " . intval($purchaseid) . "
		");
		
		// Build purchase cache
		VBSHOP::build_purchases($recipient);
	}

	/**
	* Actions that override the musername / usertitle
	*
	* @param	array		User info
	*/	
	public function fetch_mthread(&$thread)
	{
		if (!$this->config['active'])
		{
			// We're not doin this
			return false;
		}

		if ($this->config['code']['singleuse'] AND $thread['threadid'] != $this->config['configuration']['contentid'])
		{
			// We're not doin this
			return false;
		}

		// By default, no open tag
		$this->config['code']['opentag'] = '';
		
		if ($this->config['code']['bold'])
		{
			// Bolded username
			$this->config['code']['opentag'] .= 'font-weight:bold; ';
		}
		
		if ($this->config['code']['italic'])
		{
			// Bolded username
			$this->config['code']['opentag'] .= 'font-style:italic; ';
		}
		
		if ($this->config['code']['underline'])
		{
			// Bolded username
			$this->config['code']['opentag'] .= 'text-decoration:underline; ';
		}
		
		if ($this->config['code']['color'])
		{
			// Bolded username
			$this->config['code']['opentag'] .= 'color:' . $this->config['code']['color'] . '; ';
		}
		
		if ($this->config['code']['glow_onoff'])
		{
			$this->config['code']['opentag'] .= 'text-shadow: 0px 0px 0.2em ' . $this->config['code']['glow'] . ', 0px 0px 0.2em ' . $this->config['code']['glow'] . ', 0px 0px 0.2em ' . $this->config['code']['glow'] . '; ';
			//  filter:progid:DXImageTransform.Microsoft.Glow(Color=#FF0000, Strength=5); _height:1px; 
		}
		
		if ($this->config['code']['shadow_onoff'])
		{
			$this->config['code']['opentag'] .= 'text-shadow:2px 2px 4px ' . $this->config['code']['shadow'] . '; ';
		}
		
		if ($this->config['code']['opentag'])
		{
			// Close the tag
			//$this->config['code']['opentag'] = '<span style="' . $this->config['code']['opentag'] . '">';
			//$this->config['code']['closetag'] = '</span>';
		}
		
		if (!empty($this->config['code']['opentag']))
		{
			// We've got musername
			$thread['dbtech_vbshop_style'] .= $this->config['code']['opentag'];
			//$post['message'] = $this->config['code']['opentag'] . $post['message'] . $this->config['code']['closetag'];			
		}
	}	
}
?>