<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'follow' );
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;
include SAXUE_ROOT_PATH . "/model/member_follow.php";
$follow_handler = &saxuememberfollowhandler :: getinstance( "saxuememberfollowhandler" );
if ( !empty( $_REQUEST['action'] ) && $_REQUEST['action'] == 'delete' && !empty( $_REQUEST['ids'] ) && is_array( $_REQUEST['ids'] ) && count( $_REQUEST['ids'] ) > 0 ) {	
		$follow_handler -> queryobjects( new criteria( 'fid', '(' . implode( ',', $_REQUEST['ids'] ) . ')', 'IN' ) );
		$follows = $fans = array();
		while ( $v = $follow_handler -> getobject() ) {
				$row = $v -> getvars( "n" );
				if ( isset( $follows[$row['uid']] ) ) ++$follows[$row['uid']];
				else $follows[$row['uid']] = 1;
				if ( isset( $fans[$row['toid']] ) ) ++$fans[$row['toid']];
				else $fans[$row['toid']] = 1;
		}
		include_once( SAXUE_ROOT_PATH . "/model/member_users.php" );
		$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
		foreach ( $follows as $uid => $count ) {
				$users_handler -> updatefields( 'follows=follows-' . $count, 'uid=' . $uid );
		}
		foreach ( $fans as $uid => $count ) {
				$users_handler -> updatefields( 'fans=fans-' . $count, 'uid=' . $uid );
		}

		$follow_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "member_follow" ) . " WHERE fid IN(" . implode( ',', $_REQUEST['ids'] ) . ")" );
		if ( empty( $_REQUEST['jumpurl'] ) ) {
				saxue_jumppage( 'follow.php', LANG_DO_SUCCESS );
		} else {
				saxue_jumppage( $_REQUEST['jumpurl'], LANG_DO_SUCCESS );
		}
}
$criteria = new criteriacompo();
if ( !empty( $_REQUEST['uid'] ) ) {
		$criteria -> add( new criteria( 'uid', $_REQUEST['uid'] ) );
} 
if ( !empty( $_REQUEST['toid'] ) ) {
		$criteria -> add( new criteria( 'toid', $_REQUEST['toid'] ) );
} 
$criteria -> setsort( "fid" );
$criteria -> setorder( "DESC" );
$criteria -> setlimit( $_REQUEST['pagesize'] );
$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $_REQUEST['pagesize'] );
$follow_handler -> queryobjects( $criteria );
$rows = array();
$k = 0;
while ( $v = $follow_handler -> getobject() ) {
		$rows[$k] = $v -> getvars( "n" );
		++$k;
} 
include SAXUE_ADMIN_PATH . "/header.php";
$saxueTpl -> assign_by_ref( "rows", $rows );
include SAXUE_ROOT_PATH . "/lib/util/page.php";
$jumppage = new saxuepage( $follow_handler -> getcount( $criteria ), $_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/follow.html";
include SAXUE_ADMIN_PATH . "/footer.php";
