<?php

/*********************************************************************************************
CartPlog Lite License
==============================================================================================
This software is as-is, no warranty of any kind, use at your own risk. You are granted free
use of this software. However, you are not permitted under any circumstances to redistribute
the software, whether in whole or in part. Also, the 'powered by' links must remain intact
and visible. Make sure to understand that this is *not GPL software. Do not redistribute it,
and do not use parts of it in other software. This software is copyrighted in its entirety
to 'calorie' a/k/a © ThinkDing LLC 2010, all rights reserved. The software author/entity are
in no way responsible for your site content. All copyright notices and 'powered by' links
must not be changed or removed. Do not use this software if obtained from a warez site!
*********************************************************************************************/

// ####################### SET PHP ENVIRONMENT ############################
error_reporting(E_ALL & ~E_NOTICE);

// #################### PRE-CACHE TEMPLATES AND DATA ######################
$phrasegroups = array('cartplogbackend', 'cartplogfrontend');
$specialtemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');

// ######################## CHECK ADMIN PERMISSIONS #######################
if (!can_administer('canadminforums'))
{
	print_cp_no_permission();
}

// ############################# LOG ACTION ###############################
log_admin_action();
?>

<script type="text/javascript">
<!--
function cartplog_left(str, n)
{
	if (n <= 0)
	{
		return '';
	}
	else if (n > String(str).length)
	{
		return str;
	}
	else
	{
		return String(str).substring(0, n);
	}
}
function cartplog_tickall(formobj, type, value)
{
	for (var i = 0; i < formobj.elements.length; i++)
	{
		var elm = formobj.elements[i];
		if (elm.type == 'checkbox')
		{
			if (cartplog_left(elm.name, String(type).length) == type)
			{
				elm.checked = value;
			}
		}
	}
}
//-->
</script>

<?php
// ######################## START MAIN SCRIPT #############################
$cartplog = array(); // do not remove !!!

print_cp_header($vbphrase['cartplog']);

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'viewcategories';
}

if ($_REQUEST['do'] == 'addcategory')
{
	print_form_header('cartplogacp', 'doaddcategory');
	construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
	construct_hidden_code('adminhash', ADMINHASH);

	print_table_header($vbphrase['cartplog_add_category']);
	print_input_row($vbphrase['title'], 'cartplogtitle', '');
	print_submit_row();
}

if ($_REQUEST['do'] == 'doaddcategory')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogtitle' => TYPE_STR
	));

	$db->query_write("INSERT INTO " . TABLE_PREFIX . "cartplog_categories (title)
		VALUES ('" . $db->escape_string($vbulletin->GPC['cartplogtitle']) . "')
	");

	print_table_start();
	print_table_header($vbphrase['cartplog']);
	print_description_row('<strong>' . $vbphrase['done'] . '</strong>');
	print_table_footer();

	print_cp_redirect("cartplogacp.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewcategories", 1);
}

if ($_REQUEST['do'] == 'editcategory')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogcatid' => TYPE_UINT
	));

	$cartplog_category_info = $db->query_first("SELECT title
		FROM " . TABLE_PREFIX . "cartplog_categories
		WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
	");

	print_form_header('cartplogacp', 'doeditcategory');
	construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
	construct_hidden_code('adminhash', ADMINHASH);

	construct_hidden_code('cartplogcatid', $vbulletin->GPC['cartplogcatid']);
	print_table_header($vbphrase['cartplog_edit_category']);
	print_input_row($vbphrase['title'], 'cartplogtitle', $cartplog_category_info['title']);
	print_submit_row();
}

if ($_REQUEST['do'] == 'doeditcategory')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogcatid' => TYPE_UINT,
		'cartplogtitle' => TYPE_STR
	));

	$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_categories
		SET title = '" . $db->escape_string($vbulletin->GPC['cartplogtitle']) . "'
		WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
	");

	print_table_start();
	print_table_header($vbphrase['cartplog']);
	print_description_row('<strong>' . $vbphrase['done'] . '</strong>');
	print_table_footer();

	print_cp_redirect("cartplogacp.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewcategories", 1);
}

if ($_REQUEST['do'] == 'deletecategory')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogcatid' => TYPE_UINT
	));

	print_form_header('cartplogacp', 'dodeletecategory');
	construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
	construct_hidden_code('adminhash', ADMINHASH);

	construct_hidden_code('cartplogcatid', $vbulletin->GPC['cartplogcatid']);
	print_table_header($vbphrase['cartplog_confirm']);
	print_description_row($vbphrase['cartplog_are_you_sure']);
	print_submit_row($vbphrase['yes'], '', 2, $vbphrase['no']);
}

if ($_POST['do'] == 'dodeletecategory')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'cartplogcatid' => TYPE_UINT
	));

	$db->query_write("DELETE FROM " . TABLE_PREFIX . "cartplog_categories
		WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
	");
	$db->query_write("DELETE FROM " . TABLE_PREFIX . "cartplog_products
		WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
	");

	print_table_start();
	print_table_header($vbphrase['cartplog']);
	print_description_row('<strong>' . $vbphrase['done'] . '</strong>');
	print_table_footer();

	print_cp_redirect("cartplogacp.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewcategories", 1);
}

if ($_REQUEST['do'] == 'addproduct')
{
	$cartplog_category_infos = $db->query_read("SELECT catid, title
		FROM " . TABLE_PREFIX . "cartplog_categories
		ORDER BY displayorder
	");

	$cartplog_category_list = array();

	while ($cartplog_category_info = $db->fetch_array($cartplog_category_infos))
	{
		$cartplog_catid = intval($cartplog_category_info['catid']);
		$cartplog_title = strval($cartplog_category_info['title']);
		$cartplog_category_list[$cartplog_catid] = $cartplog_title;
	}

	$db->free_result($cartplog_category_infos);

	if (empty($cartplog_category_list))
	{
		print_form_header('', '');
		construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
		construct_hidden_code('adminhash', ADMINHASH);

		print_table_header($vbphrase['cartplog']);
		print_description_row($vbphrase['cartplog_first_add_a_category']);
		print_table_footer();
	}
	else
	{
		print_form_header('cartplogacp', 'doaddproduct');
		construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
		construct_hidden_code('adminhash', ADMINHASH);

		print_table_header($vbphrase['cartplog_add_product']);
		print_input_row($vbphrase['title'], 'cartplogtitle', '');
		print_input_row($vbphrase['cartplog_http_link_to_thumb'], 'cartplogthumb', '');
		print_select_row($vbphrase['cartplog_category'], 'cartplogcatid', $cartplog_category_list, '', true);
//		print_input_row($vbphrase['cartplog_price'] . ' - ' . $vbphrase['cartplog_use_this_format'], 'cartplogprice', '');
		print_textarea_row($vbphrase['description'] . $vbphrase['cartplog_description_warning'], 'cartplogdescription', '');
		print_textarea_row($vbphrase['cartplog_options_one_per_line'], 'cartplogoptions', '');

		$cartplog_tickall = '<input type="checkbox" id="tickall" name="tickall" onclick="cartplog_tickall(this.form, \'cartplogprice\', this.checked)" />';
		print_table_break();
		print_table_header($vbphrase['cartplog_price'] . ' - ' . $vbphrase['cartplog_use_this_format'], 7);
		$cells = array($vbphrase['usergroup'], $cartplog_tickall . ' ' . $vbphrase['active'], $vbphrase['cartplog_aud'], $vbphrase['cartplog_cad'], $vbphrase['cartplog_eur'], $vbphrase['cartplog_gbp'], $vbphrase['cartplog_usd']);
		print_cells_row($cells, 1, 0, -7, 'middle');
		$cartplog_usergroups = $db->query_read("SELECT usergroupid, title FROM " . TABLE_PREFIX . "usergroup ORDER BY title");
		while ($cartplog_usergroup = $db->fetch_array($cartplog_usergroups))
		{
			$ugid = $cartplog_usergroup['usergroupid'];
			$cells = array();
			$cells[] = $cartplog_usergroup['title'];
			$cells[] = '<input class="bginput" type="checkbox" name="cartplogprice[' . $ugid . '][active]" id="active_' . $ugid . '" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][AUD]" id="aud_' . $ugid . '" size="5" value="" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][CAD]" id="cad_' . $ugid . '" size="5" value="" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][EUR]" id="eur_' . $ugid . '" size="5" value="" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][GBP]" id="gbp_' . $ugid . '" size="5" value="" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][USD]" id="usd_' . $ugid . '" size="5" value="" tabindex="1" />';
			print_cells_row($cells, 0, 0, -7);
		}

		print_submit_row('', '_default_', 7);
	}
}

if ($_REQUEST['do'] == 'doaddproduct')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogtitle' => TYPE_STR,
		'cartplogthumb' => TYPE_STR,
		'cartplogcatid' => TYPE_UINT,
		'cartplogprice' => TYPE_ARRAY_ARRAY,
		'cartplogdescription' => TYPE_STR,
		'cartplogoptions' => TYPE_STR
	));

	foreach ($vbulletin->GPC['cartplogprice'] AS $ugid => $price_array)
	{
		foreach ($price_array AS $key => $value)
		{
			if ($key == 'active') // only set if checkbox is ticked (value is 'on' in this case)
			{
				$vbulletin->GPC['cartplogprice'][$ugid][$key] = 1;
			}
			else
			{
				$vbulletin->GPC['cartplogprice'][$ugid][$key] = number_format($value, 2, '.', '');
			}
		}
	}
	// price was stored as floatval(number_format($vbulletin->GPC['cartplogprice'], 2, '.', ''))

	$db->query_write("INSERT INTO " . TABLE_PREFIX . "cartplog_products
		(title, thumb, catid, price, description, options)
		VALUES
			(
				'" . $db->escape_string($vbulletin->GPC['cartplogtitle']) . "',
				'" . $db->escape_string($vbulletin->GPC['cartplogthumb']) . "',
				" . intval($vbulletin->GPC['cartplogcatid']) . ",
				'" . $db->escape_string(serialize($vbulletin->GPC['cartplogprice'])) . "',
				'" . $db->escape_string($vbulletin->GPC['cartplogdescription']) . "',
				'" . $db->escape_string(serialize(explode("\n", $vbulletin->GPC['cartplogoptions']))) . "'
			)
	");

	$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_categories
		SET products = products + 1
		WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
	");

	print_table_start();
	print_table_header($vbphrase['cartplog']);
	print_description_row('<strong>' . $vbphrase['done'] . '</strong>');
	print_table_footer();

	print_cp_redirect("cartplogacp.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewproducts&amp;cartplogcatid=" . $vbulletin->GPC['cartplogcatid'], 1);
}

if ($_REQUEST['do'] == 'editproduct')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogproductid' => TYPE_UINT
	));

	$cartplog_category_infos = $db->query_read("SELECT catid, title
		FROM " . TABLE_PREFIX . "cartplog_categories
		ORDER BY displayorder
	");

	$cartplog_category_list = array();

	while ($cartplog_category_info = $db->fetch_array($cartplog_category_infos))
	{
		$cartplog_catid = intval($cartplog_category_info['catid']);
		$cartplog_title = strval($cartplog_category_info['title']);
		$cartplog_category_list[$cartplog_catid] = $cartplog_title;
	}

	$db->free_result($cartplog_category_infos);

	if (empty($cartplog_category_list))
	{
		print_form_header('', '');
		construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
		construct_hidden_code('adminhash', ADMINHASH);

		print_table_header($vbphrase['cartplog']);
		print_description_row($vbphrase['cartplog_first_add_a_category']);
		print_table_footer();
	}
	else
	{
		$cartplog_product_info = $db->query_first("SELECT title, thumb, catid, price, description, options
			FROM " . TABLE_PREFIX . "cartplog_products
			WHERE productid = " . intval($vbulletin->GPC['cartplogproductid']) . "
		");
		$cartplog_product_info_price = unserialize($cartplog_product_info['price']);

		print_form_header('cartplogacp', 'doeditproduct');
		construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
		construct_hidden_code('adminhash', ADMINHASH);

		construct_hidden_code('cartplogproductid', $vbulletin->GPC['cartplogproductid']);
		construct_hidden_code('cartplogcatidold', $cartplog_product_info['catid']);

		print_table_header($vbphrase['cartplog_edit_product']);
		print_input_row($vbphrase['title'], 'cartplogtitle', $cartplog_product_info['title']);
		print_input_row($vbphrase['cartplog_http_link_to_thumb'], 'cartplogthumb', $cartplog_product_info['thumb']);
		print_select_row($vbphrase['cartplog_category'], 'cartplogcatid', $cartplog_category_list, $cartplog_product_info['catid'], true);
//		print_input_row($vbphrase['cartplog_price'] . ' - ' . $vbphrase['cartplog_use_this_format'], 'cartplogprice', $cartplog_product_info['price']);
		print_textarea_row($vbphrase['description'] . $vbphrase['cartplog_description_warning'], 'cartplogdescription', $cartplog_product_info['description']);
		print_textarea_row($vbphrase['cartplog_options_one_per_line'], 'cartplogoptions', implode("\n", unserialize($cartplog_product_info['options'])));

		$cartplog_tickall = '<input type="checkbox" id="tickall" name="tickall" onclick="cartplog_tickall(this.form, \'cartplogprice\', this.checked)" />';
		print_table_break();
		print_table_header($vbphrase['cartplog_price'] . ' - ' . $vbphrase['cartplog_use_this_format'], 7);
		$cells = array($vbphrase['usergroup'], $cartplog_tickall . ' ' . $vbphrase['active'], $vbphrase['cartplog_aud'], $vbphrase['cartplog_cad'], $vbphrase['cartplog_eur'], $vbphrase['cartplog_gbp'], $vbphrase['cartplog_usd']);
		print_cells_row($cells, 1, 0, -7, 'middle');
		$cartplog_usergroups = $db->query_read("SELECT usergroupid, title FROM " . TABLE_PREFIX . "usergroup ORDER BY title");
		while ($cartplog_usergroup = $db->fetch_array($cartplog_usergroups))
		{
			$ugid = $cartplog_usergroup['usergroupid'];

			$cartplog_checked = '';
			if ($cartplog_product_info_price[$ugid]['active'])
			{
				$cartplog_checked = ' checked="checked"';
			}

			$cells = array();
			$cells[] = $cartplog_usergroup['title'];
			$cells[] = '<input class="bginput" type="checkbox" name="cartplogprice[' . $ugid . '][active]" id="active_' . $ugid . '"' . $cartplog_checked . ' />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][AUD]" id="aud_' . $ugid . '" size="5" value="' . $cartplog_product_info_price[$ugid]['AUD'] . '" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][CAD]" id="cad_' . $ugid . '" size="5" value="' . $cartplog_product_info_price[$ugid]['CAD'] . '" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][EUR]" id="eur_' . $ugid . '" size="5" value="' . $cartplog_product_info_price[$ugid]['EUR'] . '" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][GBP]" id="gbp_' . $ugid . '" size="5" value="' . $cartplog_product_info_price[$ugid]['GBP'] . '" tabindex="1" />';
			$cells[] = '<input class="bginput" type="text" name="cartplogprice[' . $ugid . '][USD]" id="usd_' . $ugid . '" size="5" value="' . $cartplog_product_info_price[$ugid]['USD'] . '" tabindex="1" />';
			print_cells_row($cells, 0, 0, -7);
		}

		print_submit_row('', '_default_', 7);
	}
}

if ($_REQUEST['do'] == 'doeditproduct')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogproductid' => TYPE_UINT,
		'cartplogcatidold' => TYPE_UINT,
		'cartplogtitle' => TYPE_STR,
		'cartplogthumb' => TYPE_STR,
		'cartplogcatid' => TYPE_UINT,
		'cartplogprice' => TYPE_ARRAY_ARRAY,
		'cartplogdescription' => TYPE_STR,
		'cartplogoptions' => TYPE_STR
	));

	foreach ($vbulletin->GPC['cartplogprice'] AS $ugid => $price_array)
	{
		foreach ($price_array AS $key => $value)
		{
			if ($key == 'active') // only set if checkbox is ticked (value is 'on' in this case)
			{
				$vbulletin->GPC['cartplogprice'][$ugid][$key] = 1;
			}
			else
			{
				$vbulletin->GPC['cartplogprice'][$ugid][$key] = number_format($value, 2, '.', '');
			}
		}
	}
	// price was stored as floatval(number_format($vbulletin->GPC['cartplogprice'], 2, '.', ''))

	$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_products
		SET title = '" . $db->escape_string($vbulletin->GPC['cartplogtitle']) . "',
			thumb = '" . $db->escape_string($vbulletin->GPC['cartplogthumb']) . "',
			catid = " . intval($vbulletin->GPC['cartplogcatid']) . ",
			price = '" . $db->escape_string(serialize($vbulletin->GPC['cartplogprice'])) . "',
			description = '" . $db->escape_string($vbulletin->GPC['cartplogdescription']) . "',
			options = '" . $db->escape_string(serialize(explode("\n", $vbulletin->GPC['cartplogoptions']))) . "'
		WHERE productid = " . intval($vbulletin->GPC['cartplogproductid']) . "
	");

	if ($vbulletin->GPC['cartplogcatidold'] != $vbulletin->GPC['cartplogcatid'])
	{
		$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_categories
			SET products = products - 1
			WHERE catid = " . intval($vbulletin->GPC['cartplogcatidold']) . "
		");
		$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_categories
			SET products = products + 1
			WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
		");
	}

	print_table_start();
	print_table_header($vbphrase['cartplog']);
	print_description_row('<strong>' . $vbphrase['done'] . '</strong>');
	print_table_footer();

	print_cp_redirect("cartplogacp.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewproducts&amp;cartplogcatid=" . $vbulletin->GPC['cartplogcatid'], 1);
}

if ($_REQUEST['do'] == 'deleteproduct')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogproductid' => TYPE_UINT,
		'cartplogcatid' => TYPE_UINT
	));

	print_form_header('cartplogacp', 'dodeleteproduct');
	construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
	construct_hidden_code('adminhash', ADMINHASH);

	construct_hidden_code('cartplogproductid', $vbulletin->GPC['cartplogproductid']);
	construct_hidden_code('cartplogcatid', $vbulletin->GPC['cartplogcatid']);
	print_table_header($vbphrase['cartplog_confirm']);
	print_description_row($vbphrase['cartplog_are_you_sure']);
	print_submit_row($vbphrase['yes'], '', 2, $vbphrase['no']);
}

if ($_POST['do'] == 'dodeleteproduct')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'cartplogproductid' => TYPE_UINT,
		'cartplogcatid' => TYPE_UINT
	));

	$db->query_write("DELETE FROM " . TABLE_PREFIX . "cartplog_products
		WHERE productid = " . intval($vbulletin->GPC['cartplogproductid']) . "
	");

	$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_categories
		SET products = products - 1
		WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
	");

	print_table_start();
	print_table_header($vbphrase['cartplog']);
	print_description_row('<strong>' . $vbphrase['done'] . '</strong>');
	print_table_footer();

	print_cp_redirect("cartplogacp.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewcategories", 1);
}

if ($_REQUEST['do'] == 'viewcategories')
{
	$cartplog_category_infos = $db->query_read("SELECT *
		FROM " . TABLE_PREFIX . "cartplog_categories
		ORDER BY displayorder
	");

	$cartplog_category_list = array();
	$cartplog_category_count = array();
	$cartplog_category_order = array();

	while ($cartplog_category_info = $db->fetch_array($cartplog_category_infos))
	{
		$cartplog_catid = intval($cartplog_category_info['catid']);
		$cartplog_title = htmlspecialchars_uni($cartplog_category_info['title']);
		$cartplog_count = intval($cartplog_category_info['products']);
		$cartplog_order = intval($cartplog_category_info['displayorder']);
		$cartplog_category_list[$cartplog_catid] = $cartplog_title;
		$cartplog_category_count[$cartplog_catid] = $cartplog_count;
		$cartplog_category_order[$cartplog_catid] = $cartplog_order;
	}

	$db->free_result($cartplog_category_infos);

	if (empty($cartplog_category_list))
	{
		print_form_header('', '');
		construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
		construct_hidden_code('adminhash', ADMINHASH);

		print_table_header($vbphrase['cartplog']);
		print_description_row($vbphrase['cartplog_first_add_a_category']);
		print_table_footer();
	}
	else
	{
		print_form_header('cartplogacp', 'dodisplayorder');
		construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
		construct_hidden_code('adminhash', ADMINHASH);

		print_table_header($vbphrase['cartplog'], 4);
		print_description_row($vbphrase['if_you_change_display_order'], 0, 4);
		print_cells_row(array($vbphrase['cartplog_category'], $vbphrase['cartplog_products'], '<nobr>' . $vbphrase['display_order'] . '</nobr>', $vbphrase['controls']), 1, '', -1);

		foreach ($cartplog_category_list AS $cartplog_catid => $cartplog_title)
		{
			$bgclass = fetch_row_bgclass();

			echo '
				<tr>
					<td class="' . $bgclass . '" width="100%"><a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=editcategory&amp;cartplogcatid=' . $cartplog_catid . '">' . $cartplog_title . '</a></td>
					<td class="' . $bgclass . '"><a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=viewproducts&amp;cartplogcatid=' . $cartplog_catid . '">' . $cartplog_category_count[$cartplog_catid] . '</a></td>
					<td class="' . $bgclass . '"><input class="bginput" type="text" name="cartplogdisplayorder[' . $cartplog_catid . ']" value="' . $cartplog_category_order[$cartplog_catid] . '" size="5" /></td>
					<td class="' . $bgclass . '"><nobr><a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=editcategory&amp;cartplogcatid=' . $cartplog_catid . '">' . $vbphrase['edit'] . '</a> <a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=deletecategory&amp;cartplogcatid=' . $cartplog_catid . '">' . $vbphrase['delete'] . '</a></nobr></td>
				</tr>
			';
		}

		print_table_footer(4, '<input type="submit" value="' . $vbphrase['save_display_order'] . '" />' . construct_button_code($vbphrase['cartplog_add_category'], 'cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=addcategory'));
	}
}

if ($_REQUEST['do'] == 'viewproducts')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogcatid' => TYPE_UINT
	));

	$cartplog_product_infos = $db->query_read("SELECT *
		FROM " . TABLE_PREFIX . "cartplog_products
		WHERE catid = " . intval($vbulletin->GPC['cartplogcatid']) . "
	");

	print_form_header('', '');
	construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
	construct_hidden_code('adminhash', ADMINHASH);

	print_table_header($vbphrase['cartplog'], 4);
	print_cells_row(array($vbphrase['cartplog_thumb'], $vbphrase['info'], $vbphrase['options'], $vbphrase['controls']), 1, '', -1);

	while ($cartplog_product_info = $db->fetch_array($cartplog_product_infos))
	{
		$bgclass = fetch_row_bgclass();

		if ($cartplog_product_info['thumb'])
		{
			$cartplog_product_info['thumb'] = '<img style="' . $vbulletin->options['cartplog_thumbcss'] . '" src="' . strval($cartplog_product_info['thumb']) . '" alt="" border="0" />';
		}
		else
		{
			$cartplog_product_info['thumb'] = '<img style="' . $vbulletin->options['cartplog_thumbcss'] . '" src="../' . $vbulletin->options['cleargifurl'] . '" alt="" border="0" />';
		}

		echo '
			<tr>
				<td class="' . $bgclass . '" valign="top">' . $cartplog_product_info['thumb'] . '</td>
				<td class="' . $bgclass . '" valign="top"><a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=editproduct&amp;cartplogproductid=' . intval($cartplog_product_info['productid']) . '">' . htmlspecialchars_uni($cartplog_product_info['title']) . '</a><div class="smallfont">' . strval($cartplog_product_info['description']) . '</div></td>
				<td class="' . $bgclass . '" valign="top">' . nl2br(implode("\n", array_map('htmlspecialchars_uni', unserialize($cartplog_product_info['options'])))) . '</td>
				<!-- <td class="' . $bgclass . '" valign="top">' . round(floatval($cartplog_product_info['price']), 2) . '</td> -->
				<td class="' . $bgclass . '" align="center" valign="top"><nobr><a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=editproduct&amp;cartplogproductid=' . intval($cartplog_product_info['productid']) . '">' . $vbphrase['edit'] . '</a> <a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=deleteproduct&amp;cartplogproductid=' . intval($cartplog_product_info['productid']) . '&amp;cartplogcatid=' . intval($cartplog_product_info['catid']) . '">' . $vbphrase['delete'] . '</a></nobr></td>
			</tr>
		';
	}

	if (!$db->num_rows($cartplog_product_infos))
	{
		print_description_row($vbphrase['cartplog_first_add_a_product'], false, 4);
	}

	$db->free_result($cartplog_product_infos);

	print_table_footer(4, construct_button_code($vbphrase['cartplog_edit_category'], 'cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=editcategory&amp;cartplogcatid=' . intval($vbulletin->GPC['cartplogcatid'])));
}

if ($_REQUEST['do'] == 'viewtransactions')
{
	$cartplog_transaction_infos = $db->query_read("SELECT cartplog_transactions.*, user.username
		FROM " . TABLE_PREFIX . "cartplog_transactions AS cartplog_transactions, " . TABLE_PREFIX . "user AS user
		WHERE cartplog_transactions.userid = user.userid
		ORDER BY cartplog_transactions.dateline DESC
	");

	print_form_header('', '');
	construct_hidden_code('s', $vbulletin->session->vars['sessionhash']);
	construct_hidden_code('adminhash', ADMINHASH);

	print_table_header($vbphrase['cartplog']);
	print_cells_row(array($vbphrase['info']), 1, '', -1);

	while ($cartplog_transaction_info = $db->fetch_array($cartplog_transaction_infos))
	{
		$bgclass = fetch_row_bgclass();
		$cartplog_basket = '';

		$cartplog_transaction_info['products'] = unserialize($cartplog_transaction_info['products']);
		if (!is_array($cartplog_transaction_info['products']))
		{
			$cartplog_transaction_info['products'] = array();
		}
		foreach ($cartplog_transaction_info['products'] AS $cartplog_transaction_productid => $cartplog_transaction_array)
		{
			$cartplog['productid'] = intval($cartplog_transaction_productid);
			$cartplog['quantity'] = intval($cartplog_transaction_array['quantity']);
			$cartplog['thumb'] = strval($cartplog_transaction_array['thumb']);
			$cartplog['title'] = htmlspecialchars_uni($cartplog_transaction_array['title']);
			$cartplog['price'] = number_format(round(floatval($cartplog_transaction_array['price']), 2), 2, '.', '');
			$cartplog['currency'] = strval($cartplog_transaction_array['currency']);
			$cartplog['options'] = nl2br(htmlspecialchars_uni($cartplog_transaction_array['options']));
			$cartplog['lineprice'] = number_format(round($cartplog['quantity'] * $cartplog['price'], 2), 2, '.', '');

			if ($cartplog['thumb'])
			{
				$cartplog['thumb'] = '<img style="' . $vbulletin->options['cartplog_thumbcss'] . '" src="' . $cartplog['thumb'] . '" alt="" border="0" />';
			}
			else
			{
				$cartplog['thumb'] = '<img style="' . $vbulletin->options['cartplog_thumbcss'] . '" src="../' . $vbulletin->options['cleargifurl'] . '" alt="" border="0" />';
			}

			$cartplog_basket .= '
				<tr>
					<td class="' . $bgclass . '"><a href="cartplogacp.php?' . $vbulletin->session->vars['sessionurl'] . 'do=editproduct&amp;cartplogproductid=' . $cartplog['productid'] . '">' . $cartplog['productid'] . '</a></td>
					<td class="' . $bgclass . '">' . $cartplog['thumb'] . '</td>
					<td class="' . $bgclass . '">' . $cartplog['title'] . '<div class="smallfont">' . $cartplog['options'] . '</div></td>
					<td class="' . $bgclass . '">' . $cartplog['price'] . '</td>
					<td class="' . $bgclass . '">' . $cartplog['quantity'] . '</td>
					<td class="' . $bgclass . '">' . $cartplog['lineprice'] . ' ' . $cartplog['currency'] . '</td>
				</tr>
			';
		}

		$cartplog_basket = '
			<table border="0" cellpadding="6" cellspacing="1" align="center" width="100%">
			<tr>
				<td class="' . $bgclass . '">' . $vbphrase['cartplog_id'] . '</td>
				<td class="' . $bgclass . '">' . $vbphrase['cartplog_thumb'] . '</td>
				<td class="' . $bgclass . '">' . $vbphrase['info'] . '</td>
				<td class="' . $bgclass . '">' . $vbphrase['cartplog_price'] . '</td>
				<td class="' . $bgclass . '">' . $vbphrase['cartplog_quantity'] . '</td>
				<td class="' . $bgclass . '">' . $vbphrase['total'] . '</td>
			</tr>
			' . $cartplog_basket . '
			</table>
		';

		echo '
			<tr>
				<td class="' . $bgclass . '" width="100%">
					<div>' . $vbphrase['username'] . ': <a href="user.php?' . $vbulletin->session->vars['sessionurl'] . 'do=edit&amp;u=' . intval($cartplog_transaction_info['userid']) . '">' . $cartplog_transaction_info['username'] . '</a></div>
					<div>' . $vbphrase['cartplog_session_id'] . ': ' . htmlspecialchars_uni($cartplog_transaction_info['sessionid']) . '</div>
					<div>' . $vbphrase['cartplog_transaction_id'] . ': <a href="https://www.paypal.com/us/vst/id=' . urlencode($cartplog_transaction_info['txnid']) . '" target="_blank">' . htmlspecialchars_uni($cartplog_transaction_info['txnid']) . '</a></div>
					<div>' . $vbphrase['total'] . ': ' . number_format(round(floatval($cartplog_transaction_info['total']), 2), 2, '.', '') . ' ' . $cartplog['currency'] . '</div>
					<div>' . $vbphrase['status'] . ': ' . htmlspecialchars_uni($cartplog_transaction_info['status']) . '</div>
					<div>' . $vbphrase['date'] . ': ' . vbdate($vbulletin->options['dateformat'], intval($cartplog_transaction_info['dateline'])) . '</div>
				</td>
			</tr>
			<tr>
				<td class="' . $bgclass . '">
					' . $cartplog_basket . '
				</td>
			</tr>
		';
	}

	if (!$db->num_rows($cartplog_transaction_infos))
	{
		print_description_row($vbphrase['cartplog_no_transactions']);
	}

	$db->free_result($cartplog_transaction_infos);

	print_table_footer();
}

if ($_REQUEST['do'] == 'dodisplayorder')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'cartplogdisplayorder' => TYPE_ARRAY
	));

	$cartplog_sql = '';
	foreach ($vbulletin->GPC['cartplogdisplayorder'] AS $cartplog_catid => $cartplog_displayorder)
	{
		$cartplog_sql .= "WHEN " . intval($cartplog_catid) . " THEN " . intval($cartplog_displayorder) . "\n";
	}

	if ($cartplog_sql)
	{
		$db->query_write("UPDATE " . TABLE_PREFIX . "cartplog_categories
			SET displayorder = CASE catid
			$cartplog_sql ELSE displayorder END
		");
	}

	print_table_start();
	print_table_header($vbphrase['cartplog']);
	print_description_row('<strong>' . $vbphrase['done'] . '</strong>');
	print_table_footer();

	print_cp_redirect("cartplogacp.php?" . $vbulletin->session->vars['sessionurl'] . "do=viewcategories", 1);
}

print_cp_footer();

?>