<?php 
// #########################################
// 名称：银行日志 For 3.5
// 版本：1.0.0
// 时间：2005-12-18
// 作者：沙滩凉鞋
// 电邮：ceo@gameach.com
// #########################################
// ###############基本设置开始################
// 设置错误级别
error_reporting(E_ALL & ~E_NOTICE);
// 定义基本常量
define('CVS_REVISION', '$RCSfile: bankkog.php,v $ - $Revision: 1.48 $');
// 预读必要模板
$phrasegroups = array('logging','bank');
$specialtemplates = array();
// 调用必要文件
require_once( './global.php' );
// 检查管理权限
if (!can_administer('canadmincron'))
{
	print_cp_no_permission();
}
// 记录管理动作
log_admin_action();
// 设置默认动作
print_cp_header($vbphrase['bank_logs']);

if (empty($_REQUEST['do']))
{
	$_REQUEST['do'] = 'choose';
}
// ###############文件开始################
// 查看日志
if ($_REQUEST['do'] == 'view')
{
	$vbulletin->input->clean_array_gpc('r', array(
		'perpage'	=> TYPE_INT,
		'logid'	=> TYPE_INT,
		'orderby'	=> TYPE_STR,
		'user'	=> TYPE_STR,
		'page'		=> TYPE_INT
	));

	if (empty($vbulletin->GPC['perpage']))
	{
		$vbulletin->GPC['perpage'] = 15;
	}
	$sqlconds = 'WHERE 1=1 ';
	if (!empty($vbulletin->GPC['logid']))
	{
		$sqlconds .= " \nAND bank_log.operate = " . $vbulletin->GPC['logid'];
	}

	if (!empty($vbulletin->GPC['user']))
	{
		if ( !$userid = $db->query_first( "
			SELECT `user`.`userid`, `user`.`username`
			FROM `" . TABLE_PREFIX . "user` AS `user`
			WHERE `user`.`username` = '".$vbulletin->GPC['user']."'
		" ) )
		{
			print_stop_message('no_users_matched_your_query');
		}
		else
		{
			$sqlconds .= " \nAND bank_log.userid = " . $userid['userid'];
		}
	}

	$counter = $db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "bank_log AS bank_log $sqlconds");
	$totalpages = ceil($counter['total'] / $vbulletin->GPC['perpage']);

	if (empty($vbulletin->GPC['page']))
	{
		$vbulletin->GPC['page'] = 1;
	}

	$startat = ($vbulletin->GPC['page'] - 1) * $vbulletin->GPC['perpage'];

	switch($vbulletin->GPC['orderby'])
	{
		case 'operate':
			$order = 'operate ASC, logtime DESC';
			break;
		case 'sum':
			$order = 'sum ASC, logtime DESC';
			break;
		case 'date':
		default:
			$order = 'logtime DESC';
	}

	$logs = $db->query_read("
		SELECT bank_log.*,user.username
		FROM " . TABLE_PREFIX . "bank_log AS bank_log
		LEFT JOIN " . TABLE_PREFIX . "user AS user ON (bank_log.userid = user.userid)
		$sqlconds
		ORDER BY $order
		LIMIT $startat, " . $vbulletin->GPC['perpage']
	);

	if ($db->num_rows($logs))
	{
		if ($vbulletin->GPC['page'] != 1)
		{
			$prv = $vbulletin->GPC['page'] - 1;
			$firstpage = "<input type=\"button\" class=\"button\" tabindex=\"1\" value=\"&laquo; " . $vbphrase['first_page'] . "\" onclick=\"window.location='banklog.php?" . $vbulletin->session->vars['sessionurl'] . "do=view" .
				"&logid=" . $vbulletin->GPC['logid'] .
				"&pp=" . $vbulletin->GPC['perpage'] .
				"&orderby=" . $vbulletin->GPC['orderby'] . "&page=1'\">";
			$prevpage = "<input type=\"button\" class=\"button\" tabindex=\"1\" value=\"&lt; " . $vbphrase['prev_page'] . "\" onclick=\"window.location='banklog.php?" . $vbulletin->session->vars['sessionurl'] . "do=view" .
				"&logid=" . $vbulletin->GPC['logid'] .
				"&pp=" . $vbulletin->GPC['perpage'] .
				"&orderby=" . $vbulletin->GPC['orderby'] . "&page=$prv'\">";
		}

		if ($vbulletin->GPC['page'] != $totalpages)
		{
			$nxt = $vbulletin->GPC['page'] + 1;
			$page_button = "banklog.php?" . $vbulletin->session->vars['sessionurl'] . "do=view&logid=" . $vbulletin->GPC['logid'] . "&pp=" . $vbulletin->GPC['perpage'] . "&orderby=" . $vbulletin->GPC['orderby'];
			$nextpage = "<input type=\"button\" class=\"button\" tabindex=\"1\" value=\"" . $vbphrase['next_page'] . " &gt;\" onclick=\"window.location='$page_button&page=$nxt'\">";
			$lastpage = "<input type=\"button\" class=\"button\" tabindex=\"1\" value=\"" . $vbphrase['last_page'] . " &raquo;\" onclick=\"window.location='$page_button&page=$totalpages'\">";
		}

		print_form_header('banklog', 'remove');
		print_description_row(construct_link_code($vbphrase['restart'], "banklog.php?" . $vbulletin->session->vars['sessionurl'] . ""), 0, 6, 'thead', $stylevar['right']);
		print_table_header(construct_phrase($vbphrase['bank_log_viewer_page_x_y_there_are_z_total_log_entries'], vb_number_format($vbulletin->GPC['page']), vb_number_format($totalpages), vb_number_format($counter['total'])), 6);

		$headings = array();
		$headings[] = $vbphrase['id'];
		$headings[] = "<a href=\"banklog.php?" . $vbulletin->session->vars['sessionurl'] . "do=view" .
			"&logid=" . $vbulletin->GPC['logid'] .
			"&pp=" . $vbulletin->GPC['perpage'] .
			"&orderby=operate" .
			"&page=" . $vbulletin->GPC['page'] . "\" title=\"" . $vbphrase['order_by_action'] . "\">" . $vbphrase['action'] . "</a>";
		$headings[] = "<a href=\"banklog.php?" . $vbulletin->session->vars['sessionurl'] . "do=view" .
			"&logid=" . $vbulletin->GPC['logid'] .
			"&pp=" . $vbulletin->GPC['perpage'] .
			"&orderby=sum" .
			"&page=" . $vbulletin->GPC['page'] . "\" title=\"" . $vbphrase['order_by_sum'] . "\">" . $vbphrase['bank_money'] . "</a>";
		$headings[] = "<a href=\"banklog.php?" . $vbulletin->session->vars['sessionurl'] . "do=view" .
			"&logid=" . $vbulletin->GPC['logid'] .
			"&pp=" . $vbulletin->GPC['perpage'] .
			"&orderby=date" .
			"&page=" . $vbulletin->GPC['page'] . "\" title=\"" . $vbphrase['order_by_date'] . "\">" . $vbphrase['date'] . "</a>";
		$headings[] = $vbphrase['bank_user'];
		$headings[] = $vbphrase['bank_remark'];
		print_cells_row($headings, 1);

		$operates = explode( "|", $vbphrase['bank_operates'] );
		while ($log = $db->fetch_array($logs))
		{
			$cell = array();
			$cell[] = $log['logid'];
			$cell[] = $operates[$log[operate]];
			$cell[] = $log['sum'];
			$cell[] = '<span class="smallfont">' . vbdate($vbulletin->options['logdateformat'], $log['logtime']) . '</span>';
			$cell[] = $log['username'];
			$cell[] = $log['note'];

			print_cells_row($cell, 0, 0, -6);
		}

		print_table_footer(6, "$firstpage $prevpage &nbsp; $nextpage $lastpage");
	}
	else
	{
		print_stop_message('no_matches_found');
	}
}

// ###################### Start prune log #######################
if ($_POST['do'] == 'prunelog')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'logid' 	=> TYPE_INT,
		'user'	=> TYPE_STR,
		'daysprune' => TYPE_INT
	));

	$sqlconds = '';
	if ($vbulletin->GPC['logid'])
	{
		$sqlconds = "\nAND operate = " . $vbulletin->GPC['logid'];
	}

	$datecut = TIMENOW - (86400 * $vbulletin->GPC['daysprune']);

	$logs = $db->query_first("SELECT COUNT(*) AS total FROM " . TABLE_PREFIX . "bank_log WHERE logtime < $datecut $sqlconds");

	if ($logs['total'])
	{
		print_form_header('banklog', 'doprunelog');
		construct_hidden_code('datecut', $datecut);
		construct_hidden_code('logid', $vbulletin->GPC['logid']);
		print_table_header($vbphrase['prune_bank_log']);
		print_description_row(construct_phrase($vbphrase['are_you_sure_you_want_to_prune_x_log_entries_from_bank_log'], vb_number_format($logs['total'])));
		print_submit_row($vbphrase['yes'], 0, 0, $vbphrase['no']);
	}
	else
	{
		print_stop_message('no_matches_found');
	}
}

// ###################### Start do prune log #######################
if ($_POST['do'] == 'doprunelog')
{
	$vbulletin->input->clean_array_gpc('p', array(
		'logid'  => TYPE_INT,
		'datecut' => TYPE_INT
	));

	$sqlconds = '';
	if (!empty($vbulletin->GPC['logid']))
	{
		$sqlconds = "\nAND operate = " . $vbulletin->GPC['logid'];
	}


	if (!empty($vbulletin->GPC['user']))
	{
		if ( !$userid = $db->query_first( "
			SELECT `user`.`userid`, `user`.`username`
			FROM `" . TABLE_PREFIX . "user` AS `user`
			WHERE `user`.`username` = '".$vbulletin->GPC['user']."'
		" ) )
		{
			print_stop_message('no_users_matched_your_query');
		}
		else
		{
			$sqlconds .= "\nAND bank_log.userid = " . $userid['userid'];
		}
	}

	$db->query_write("DELETE FROM " . TABLE_PREFIX . "bank_log WHERE logtime < " . $vbulletin->GPC['datecut'] . " $sqlconds");

	define('CP_REDIRECT', 'banklog.php?do=choose');
	print_stop_message('pruned_bank_log_successfully');
}

// ###################### Start modify #######################
if ($_REQUEST['do'] == 'choose')
{
	$operates = explode( "|", $vbphrase['bank_operates'] );
	$filelist = array();
	foreach ( $operates as $key => $value )
	{
		$filelist[$key] = $value;
	}
	$filelist[0] = $vbphrase['all_bank_operations'];

	$perpage = array(5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 40 => 40, 50 => 50, 100 => 100);
	$orderby = array('date' => $vbphrase['date'], 'opration' => $vbphrase['action'], 'sum' => $vbphrase['bank_money']);

	print_form_header('banklog', 'view');
	print_table_header($vbphrase['bank_log_viewer']);

	print_select_row($vbphrase['log_entries_to_show_per_page'], 'perpage', $perpage, 15);
	print_input_row($vbphrase['bank_user'], 'user', '');
	print_select_row($vbphrase['action'], 'logid', $filelist);
	print_select_row($vbphrase['order_by'], 'orderby', $orderby);

	print_submit_row($vbphrase['view'], 0);

	print_form_header('banklog', 'prunelog');
	print_table_header($vbphrase['prune_bank_log']);
	print_select_row($vbphrase['action'], 'logid', $filelist);
	print_input_row($vbphrase['bank_user'], 'user', '');
	print_input_row($vbphrase['remove_entries_older_than_days'], 'daysprune', 30);
	print_submit_row($vbphrase['prune'], 0);
}

print_cp_footer();
?>