<?php
/*************************************************************\
+-------------------------------------------------------------+
| 通灵佛教网在线烧香程序 System                                 |
| Copyright (c) 2003-2010 Alfred China                        |
| All rights reserved.                                        |
+-------------------------------------------------------------+
\*************************************************************/
require_once('chk.php');
require_once('netgou.php');

function ng_delete_profile($user) {
        global $sql_tbl;
        db_query("DELETE FROM $sql_tbl[users] WHERE uid='$uid'");
}

if ($REQUEST_METHOD == "POST") {
	
	if ($mode == "delete") {
		
		if (is_array($users_to_delete)) {
			foreach ($users_to_delete as $k=>$v) {

				db_query("DELETE FROM $sql_tbl[users] WHERE uid ='$k'");
					
			}
		}
		$users_to_delete = '';
		showmsg("删除成功","users.php?redo=Y");		
	}
}

$sort_string = " $sql_tbl[users].uid DESC";
		
if (!empty($sort)) {

	$direction = ($sort_direction == 1) ? "DESC" : "ASC";
	switch ($sort) {
		case "uid":
			$sort_string = " $sql_tbl[users].uid $direction";
			break;
		case "username":
			$sort_string = " $sql_tbl[users].username $direction";
			break;
		case "name":
			$sort_string = " $sql_tbl[users].hxname $direction";
			break;
		case "usertype":
			$sort_string = " $sql_tbl[users].usertype $direction";
			break;
		case "email":
			$sort_string = " $sql_tbl[users].email $direction";
			break;
		case "first_login":
			$sort_string = " $sql_tbl[users].uid $direction";
			break;
		case "loginip":
			$sort_string = " $sql_tbl[users].loginip $direction";
			break;
		case "score":
			$sort_string = " $sql_tbl[users].score $direction";
			break;
		case "deposit":
			$sort_string = " $sql_tbl[users].deposit $direction";
			break;
		case "logindtm":
			$sort_string = " $sql_tbl[users].logindtm $direction";
			break;
		case "consumes":
			$sort_string = " $sql_tbl[users].consumes $direction";
			break;
		case "logins":
			$sort_string = " $sql_tbl[users].logins $direction";
			break;
	}
}
		
if ($redo) {
	
	$total_items = ng_query_first_cell("SELECT COUNT($sql_tbl[users].uid) FROM $sql_tbl[users]");
	if ($total_items > 0) {
					
		$objects_per_page = 50;
		$total_nav_pages = ceil($total_items/$objects_per_page);
		if($page > $total_nav_pages)
			$page = $total_nav_pages;
								
		$pageurl = "users.php?mode=search&redo=Y";
		include_once ng_dir."/include/navigation.php";
			
		$users = ng_query("SELECT $sql_tbl[users].* FROM $sql_tbl[users] ORDER BY $sort_string LIMIT $first_page, $objects_per_page");
			
		$first_item = $first_page+1;
		$last_item = min($first_page+$objects_per_page, $total_items);
	}
	
}else{
	
	
	if ($mode == "search") {
	
		$search_condition = " WHERE 1=1 ";
		$txturl = "";
		
		$membership && $search_condition .= " AND $sql_tbl[users].membership='".$membership."' ";
		$membership && $txturl .= "&membership=$membership";
		
		$userkind && $search_condition .= " AND $sql_tbl[users].userkind='".$userkind."' ";
		$userkind && $txturl .= "&userkind=$userkind";
		
		if($substring){
			$substring && $txturl .= "&substring=$substring";
			$search_condition .= " AND $sql_tbl[users].username LIKE '%".$substring."%'";
			$search_condition .= " OR $sql_tbl[users].hxname LIKE '%".$substring."%'";
			$search_condition .= " OR $sql_tbl[users].email LIKE '%".$substring."%'";
		}
		
		$start_date && $search_condition .= " AND $sql_tbl[users].regdtm>='".strtotime($start_date)."'";
		$end_date && $search_condition .= " AND $sql_tbl[users].regdtm<='".strtotime($end_date)."'";
		
		switch ($date_period) {
			case "D":
				$search_condition .= " AND $sql_tbl[users].regdtm>='".strtotime("today")."'";
				break;
			case "W":
				$search_condition .= " AND $sql_tbl[users].regdtm>='".strtotime("last monday")."'";
				break;
			case "M":
				$search_condition .= " AND $sql_tbl[users].regdtm>='".mktime(0,0,0,date("m"),1,date("Y"))."'";
				break;
			case "Y":
				$search_condition .= " AND $sql_tbl[users].regdtm>='".mktime(0,0,0,1,1,date("Y"))."'";
				break;
			case "7D":
				$search_condition .= " AND $sql_tbl[users].regdtm>='".strtotime("-1 week")."'";
				break;
			case "30D":
				$search_condition .= " AND $sql_tbl[users].regdtm>='".strtotime("-1 month")."'";
				break;
			case "365D":
				$search_condition .= " AND $sql_tbl[users].regdtm>='".strtotime("-1 year")."'";
				break;
			default:
				break;
		}
		$start_date && $txturl .= "&start_date=$start_date";
		$end_date && $txturl .= "&end_date=$end_date";
		$date_period && $txturl .= "&date_period=$date_period";
			
		$total_items = ng_query_first_cell("SELECT COUNT(uid) FROM $sql_tbl[users]".$search_condition);
	
		if ($total_items > 0) {
			
			$objects_per_page = 50;	
			$total_nav_pages = ceil($total_items/$objects_per_page);
			$pageurl = "users.php?mode=search&".$txturl;
			include_once ng_dir."/include/navigation.php";
			
			$users = ng_query("SELECT $sql_tbl[users].* FROM $sql_tbl[users] $search_condition ORDER BY $sort_string LIMIT $first_page, $objects_per_page");
	
			$first_item = $first_page+1;
			$last_item = min($first_page+$objects_per_page, $total_items);
		}
		
	} 
}

$ng_obstart && function_exists('ob_gzhandler') ? ob_start('ob_gzhandler') : ob_start();
$ng_obstart ? $ng_gzip="Gzip enabled":$ng_gzip="Gzip disabled";

require_once PrintTemp('users');
require_once "./footer.php";

$output=str_replace(array('<!--<!--<!---->','<!--<!---->','<!---->'),array('','',''),ob_get_contents());
ob_end_clean();
echo $output;
flush;
exit;
?>