<?php
/* Your vbBux / vbPlaza Addon Title */
/* Any credits you want */

/* initialize variables */
$hidden_fields = '';
$stepid = $item['step'];
$queryfields = $tabledata = array();
$processed = $item_can_be_removed = false;

/* process any step evaluation code */
if (!empty($item['stepevalcode']["$stepid"]))
{
	/* run this code */
	eval($item['stepevalcode']["$stepid"]);
}

/* verify its not processed already */
if (!$processed)
{
	/* verify any required fields */
	vbplaza_verify_all_fields($item);

	/* check for weird table names */
	if ($item['tablename'] != 'user')
	{
		/* grab the data from the table */
		if (!empty($item['wherevalue']))
		{
			/* grab the data specifically */
			$tabledata = $vbulletin->db->query_first("SELECT * FROM " . TABLE_PREFIX . $item['tablename'] . " WHERE " . $item['wherename'] . " = '" . $item['wherevalue'] . "'");
		}
		else
		{
			/* grab the data now */
			$tabledata = $vbulletin->db->query_first("SELECT * FROM " . TABLE_PREFIX . $item['tablename']);
		}
	}
	else
	{
		/* use the logged in user for the table data */
		$tabledata = $vbulletin->userinfo;
	}

	/* verify it has found the table data */
	if (empty($tabledata) || !is_array($tabledata))
	{
		/* display the error */
		eval(standard_error(fetch_error('vbplaza_invalid_table_name')));
	}

	/* check set type 1 for removable items */
	if ($item['settype'] == 1)
	{
		/* check if this item is enabled */
		if (!empty($tabledata[$item['setname']]))
		{
			/* check for purchasing */
			if ($item['step'] < 11)
			{
				/* setup the step to remove
				$item['step'] = 11;
				$item['nextstep'] = $item['step'] + 1;
			}
			else
			{
				/* this item can be removed */
				$item_can_be_removed = true;
			}
		}
	}
	/* check set type 2 for removable items */
	else if ($item['settype'] == 2)
	{
		/* check if this item is enabled */
		if ($tabledata[$item['setname']] & intval($item['setvalue']))
		{
			/* check for purchasing */
			if ($item['step'] < 11)
			{
				/* setup the step to remove
				$item['step'] = 11;
				$item['nextstep'] = $item['step'] + 1;
			}
			else
			{
				/* this item can be removed */
				$item_can_be_removed = true;
			}
		}
	}
	/* check set type 3 for removable items */
	else if ($item['settype'] == 3)
	{
		/* check if this item is enabled */
		if ($tabledata[$item['setname']] == intval($item['setvalue']))
		{
			/* check for purchasing */
			if ($item['step'] < 11)
			{
				/* setup the step to remove
				$item['step'] = 11;
				$item['nextstep'] = $item['step'] + 1;
			}
			else
			{
				/* this item can be removed */
				$item_can_be_removed = true;
			}
		}
	}
	/* check set type 6 for removable items */
	else if ($item['settype'] == 6)
	{
		/* check if this item is enabled */
		if ($tabledata[$item['setname']] > 0)
		{
			/* check for purchasing */
			if ($item['step'] < 11)
			{
				/* setup the step to remove
				$item['step'] = 11;
				$item['nextstep'] = $item['step'] + 1;
			}
			else
			{
				/* this item can be removed */
				$item_can_be_removed = true;
			}
		}
	}

	/* process step 1 */
	if ($item['step'] == 1)
	{
		/* validate the cost */
		vbplaza_validate_cost($item);

		/* processs any input fields */
		vbplaza_build_inputfields($item);

		/* setup the page title */
		$pagetitle = $vbphrase['vbplaza_purchase'] . " " . $item['title'];

		/* build the proper template */
		eval('$current .= "' . fetch_template('vbplaza_item_confirmation') . '";');
	}
	/* process step 2 */
	else if ($item['step'] == 2)
	{
		/* verify the item is not being flooded */
		vbplaza_item_check_for_flood($item);

		/* check for a valid finalcost */
		if (!is_numeric($item['finalcost']))
		{
			/* display the error */
			eval(standard_error(fetch_error('vbplaza_invalid_final_cost')));
		}

		/* validate the cost */
		vbplaza_validate_cost($item);

		/* build the query fields */
		$query_fields_entry = vbplaza_build_purchase_item_query(1, $item);

		/* check if the table is the points table */
		if ($item['tablename'] == $vbulletin->options['vbbux_points_table'])
		{
			/* add the arrays together */
			$queryfields = array_merge($queryfields, $query_fields_entry);

			/* update the purchase for this user */
			vbplaza_user_purchase_update($vbulletin->userinfo, $item, $queryfields);
		}
		else
		{
			/* send off the query to update the table */
			$vbulletin->db->query_write(@vbplaza_fetch_query_sql($query_fields_entry, $item['tablename'], "WHERE " . $item['wherename'] . " = '" . $item['wherevalue'] . "'"));

			/* update the purchase for this user */
			vbplaza_user_purchase_update($vbulletin->userinfo, $item);
		}

		/* update the quantity sold */
		vbplaza_update_quantity_sold($item);

		/* update the item datastore */
		vbplaza_build_item_datastore();

		/* setup the page title */
		$pagetitle = $vbphrase['vbplaza_purchase'] . " " . $item['title'];

		/* build the proper template */
		eval('$current .= "' . fetch_template('vbplaza_item_confirmed') . '";');
	}
	/* process step 11 */
	else if ($item['step'] == 11)
	{
		/* setup the page title */
		$pagetitle = $vbphrase['vbplaza_remove'] . " " . $item['title'];

		/* build the proper template */
		eval('$current .= "' . fetch_template('vbplaza_item_removal') . '";');
	}
	/* process step 12 */
	else if ($item['step'] == 12)
	{
		/* check if this item is already removed */
		if (!$item_can_be_removed)
		{
			/* display the error */
			eval(standard_error(fetch_error('vbplaza_item_was_already_removed')));
		}

		/* build the query fields */
		$query_fields_entry = vbplaza_build_purchase_item_query(0, $item);

		/* check if the table is the points table */
		if ($item['tablename'] == $vbulletin->options['vbbux_points_table'])
		{
			/* add the arrays together */
			$queryfields = array_merge($queryfields, $query_fields_entry);
		}
		else
		{
			/* send off the query to update the table */
			$vbulletin->db->query_write(@vbplaza_fetch_query_sql($query_fields_entry, $item['tablename'], "WHERE " . $item['wherename'] . " = '" . $item['wherevalue'] . "'"));
		}

		/* remove this purchase from the user */
		$item['removal'] = true;
		vbplaza_user_purchase_update($vbulletin->userinfo, $item, $queryfields);

		/* setup the page title */
		$pagetitle = $vbphrase['vbplaza_remove'] . " " . $item['title'];

		/* build the proper template */
		eval('$current .= "' . fetch_template('vbplaza_item_removed') . '";');
	}
}
?>