<?php
include "../core.php";
if ( empty( $_REQUEST['uid'] ) || !is_numeric( $_REQUEST['uid'] ) ) {
		include SAXUE_WEB_PATH . "/404.php";
} 
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
include SAXUE_WEB_PATH . "/header.php";
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . "/space/follow.html";
$saxueTset['saxue_page_cacheid'] = floor( $_REQUEST['uid'] / 1000 ) . '/' . $_REQUEST['uid'] . '_' . $_REQUEST['page'];
$page_used_cache = false;
if ( SAXUE_USE_CACHE ) {
		$cachedtime = $saxueTpl -> get_cachedtime( $saxueTset['saxue_page_template'], $saxueTset['saxue_page_cacheid'] );
		if ( SAXUE_NOW_TIME - $cachedtime < SAXUE_CACHE_LIFETIME ) {
				$page_used_cache = true;
		}
		if ( !$page_used_cache ) {
				$saxueTpl -> update_cachedtime( $saxueTset['saxue_page_template'], $saxueTset['saxue_page_cacheid'] );
				$saxueTpl -> setcaching( 2 );
		} else {
				$saxueTpl -> setcaching( 1 );
		}
		$saxueTpl -> setcachetime( 99999999 );
} else {
		$saxueTpl -> setcaching( 0 );
}
if ( !$page_used_cache ) {
		/* 用户信息 */
		include SAXUE_ROOT_PATH . "/model/member_users.php";
		$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
		$saxueUsers = $users_handler -> get( $_REQUEST['uid'] );
		if ( !is_object( $saxueUsers ) ) {
				include SAXUE_WEB_PATH . "/404.php";
		} 
		include SAXUE_ROOT_PATH . "/model/member_userdata.php";
		$userdata_handler = &saxuememberuserdatahandler :: getinstance( "saxuememberuserdatahandler" );
		$saxueUserdata = $userdata_handler -> get( $_REQUEST['uid'] );
		$user = $saxueUsers -> getvars( "n" ) + $saxueUserdata -> getvars( "n" );
		$user['uavatar'] = saxue_geturl( 'user_avatar', $_REQUEST['uid'], 'l', $user['avatar'] );
		$user['uhome'] = saxue_geturl( 'user_home', $_REQUEST['uid'] );
		$saxueTpl -> assign_by_ref( "user", $user );
		$saxueTpl -> assign( "uid", $_REQUEST['uid'] );
		/* 用户信息 */
		$saxueConfigs['member']['homeupnum'] = 20;
		include SAXUE_ROOT_PATH . "/model/member_follow.php";
		$follow_handler = &saxuememberfollowhandler :: getinstance( "saxuememberfollowhandler" );
		$criteria = new criteriacompo( new criteria( 'uid', $_REQUEST['uid'] ) );
		$criteria -> setsort( "fid" );
		$criteria -> setorder( "DESC" );
		$criteria -> setlimit( $saxueConfigs['member']['homeupnum'] );
		$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['member']['homeupnum'] );
		$follow_handler -> queryobjects( $criteria );
		$rows = $ids = array();
		while ( $v = $follow_handler -> getobject() ) {
				$k = $v -> getvar( 'toid' );
				$rows[$k]['followtime'] = $v -> getvar( 'followtime' );
				$ids[] = $k;
		}  
		if ( count( $ids ) > 0 ) {
				$sql = 'SELECT * FROM ' . saxue_dbprefix( "member_users" ) . ' WHERE uid IN(' . implode( ',', $ids ) . ')';
				$res = $follow_handler -> db -> query( $sql );
				while ( $v = $follow_handler -> getobject( $res ) ) {
						$k = $v -> getvar( 'uid' );
						$rows[$k] = $rows[$k] + ( $v -> getvars( 'n' ) );
						$rows[$k]['uhome'] = saxue_geturl( "user_home", $rows[$k]['uid'] );
						$rows[$k]['uavatar'] = saxue_geturl( "user_avatar", $rows[$k]['uid'], 'l', $rows[$k]['avatar'] );
				} 
				$sql = 'SELECT * FROM ' . saxue_dbprefix( "member_userdata" ) . ' WHERE uid IN(' . implode( ',', $ids ) . ')';
				$res = $follow_handler -> db -> query( $sql );
				while ( $v = $follow_handler -> getobject( $res ) ) {
						$k = $v -> getvar( 'uid' );
						$rows[$k] = $rows[$k] + ( $v -> getvars( 'n' ) );
				} 
		}
		$saxueTpl -> assign_by_ref( "rows", $rows );
		// 分页
		include SAXUE_ROOT_PATH . "/lib/util/page.php";
		if ( SAXUE_USE_CACHE ) {
				$pagecacheid = $_REQUEST['uid'];
				$_sub = floor( $_REQUEST['uid'] / 100 );
				saxue_getcachevars( $_sub, 'homefollowlog' );
				if ( !is_array( $saxueHomefollowlog ) ) {
						$saxueHomefollowlog = array( );
				}
				if ( !isset( $saxueHomefollowlog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueHomefollowlog[$pagecacheid]['time'] ) {
						$saxueHomefollowlog[$pagecacheid] = array( "rows" => $follow_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
						saxue_setcachevars( $_sub, $saxueHomefollowlog, 'homefollowlog' );
				}
				$listrows = $saxueHomefollowlog[$pagecacheid]['rows'];
		} else {
				$listrows = $follow_handler -> getcount( $criteria );
		}
		$jumppage = new saxuepage( $listrows, $saxueConfigs['member']['homeupnum'], $_REQUEST['page'], 5 );
		$jumppage -> setlink( saxue_geturl( "user_home", $_REQUEST['uid'], 'follow', '<{$page}>' ) );
		$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
		$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
		$saxueTpl -> assign( "spacenav", 'follow' );
}
include SAXUE_WEB_PATH . "/footer.php";