<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'users' );
include_once( SAXUE_ROOT_PATH . "/model/member_users.php" );
$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
if ( !empty( $_REQUEST['action'] ) && !empty( $_REQUEST['ids'] ) && is_array( $_REQUEST['ids'] ) && count( $_REQUEST['ids'] ) > 0 ) {
		switch ( $_REQUEST['action'] ) {
				case 'pass':
						$users_handler -> updatefields( array( 'status' => 1 ), 'uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'nopass':
						$users_handler -> updatefields( array( 'status' => 2 ), 'uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'delete':
						foreach ( $_REQUEST['ids'] as $uid ) {
								$basedir = saxue_uploadpath( 'avatar' ) . saxue_getsubdir( $uid );
								saxue_delfile( $basedir . "/" . $uid . '.jpg' );
								saxue_delfile( $basedir . "/" . $uid . 's.jpg' );
								saxue_delfile( $basedir . "/" . $uid . 'i.jpg' );
						}
						$users_handler -> db -> query( 'UPDATE ' . saxue_dbprefix( 'article' ) . ' SET uid=0 WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_account' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_bill' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_doing' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_follow' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ') OR toid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_message' ) . ' WHERE fromid IN(' . implode( ',', $_REQUEST['ids'] ) . ') OR toid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_oauth' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_money' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_score' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_userdata' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$users_handler -> db -> query( 'DELETE FROM ' . saxue_dbprefix( 'member_users' ) . ' WHERE uid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
		} 
		if ( empty( $_REQUEST['jumpurl'] ) ) {
				saxue_jumppage( 'users.php', LANG_DO_SUCCESS );
		} else {
				saxue_jumppage( $_REQUEST['jumpurl'], LANG_DO_SUCCESS );
		} 
} 
if ( empty( $_REQUEST['pagesize'] ) || !is_numeric( $_REQUEST['pagesize'] ) || !in_array( $_REQUEST['pagesize'], array( '10', '30', '50', '100' ) ) ) $_REQUEST['pagesize'] = 30;
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
$sql = "saxue_member_users AS a,saxue_member_userdata AS b WHERE 1";
if ( !empty( $_REQUEST['keytype'] ) && !empty( $_REQUEST['keyword'] ) ) {
		$sql .= " AND a." . $_REQUEST['keytype'] . "='" . $_REQUEST['keyword'] . "'";
} 
if ( isset( $_REQUEST['status'] ) && $_REQUEST['status'] != '' ) {
		$sql .= " AND a.status=" . $_REQUEST['status'];
} 
$sql .= " AND b.uid=a.uid";
$_res = $users_handler -> db -> query( "SELECT COUNT(*) FROM " . $sql );
if ( !$_res ) {
		$listrows = 0;
} else {
		list( $listrows ) = $users_handler -> db -> fetchrow( $_res );
} 
$sql = "SELECT a.*,b.* FROM " . $sql;
if ( isset( $_REQUEST['form_sort'] ) && !empty( $_REQUEST['form_sort'] ) ) {
		if ( $_REQUEST['form_sort'] == 'score' || $_REQUEST['form_sort'] == 'money' ) $sql .= " ORDER BY a." . $_REQUEST['form_sort'];
		else $sql .= " ORDER BY b." . $_REQUEST['form_sort'];
} else {
		$sql .= " ORDER BY a.uid";
} 
if ( isset( $_REQUEST['form_order'] ) && !empty( $_REQUEST['form_order'] ) ) {
		$sql .= " " . $_REQUEST['form_order'];
} else {
		$sql .= " DESC";
} 
$sql .= " LIMIT " . ( ( $_REQUEST['page'] - 1 ) * $_REQUEST['pagesize'] ) . ", " . $_REQUEST['pagesize'];
$res = $users_handler -> db -> query( $sql );
$rows = array();
$k = 0;
while ( $v = $users_handler -> getobject( $res ) ) {
		$rows[$k] = $v -> getvars( "n" );
		++$k;
} 
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTpl -> assign( "rowcount", $listrows );
include_once( SAXUE_ROOT_PATH . "/lib/util/page.php" );
$jumppage = new saxuepage( $listrows, $_REQUEST['pagesize'], $_REQUEST['page'] );
$jumppage -> setlink( "", true, true );
$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
$saxueTpl -> setcaching( 0 );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/users.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
