<?php
include "../core.php";
if ( !saxue_checklogin( true ) ) {
	saxue_jumppage( '/space/' );
} 
// 我关注的用户
include SAXUE_ROOT_PATH . "/model/member_follow.php";
$follow_handler = &saxuememberfollowhandler :: getinstance( "saxuememberfollowhandler" );
$follow_handler -> queryobjects( new criteria( 'uid', $_SESSION['saxueUserId'] ) );
$ids = array();
while ( $v = $follow_handler -> getobject() ) {
	$ids[] = $v -> getvar( 'toid' );
} 
$rows = array();
include SAXUE_WEB_PATH . "/header.php";
if ( count( $ids ) > 0 ) {
	saxue_getconfigs( "configs", 'member' );
	// 我关注用户的动态
	if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
	saxue_getconfigs( "doingtype", 'member' );
	include SAXUE_ROOT_PATH . "/common/function.php";
	include SAXUE_ROOT_PATH . "/model/member_doing.php";
	$doing_handler = &saxuememberdoinghandler :: getinstance( "saxuememberdoinghandler" );
	$criteria = new criteriacompo( new criteria( 'uid', '(' . implode( ',', $ids ) . ')', 'IN' ) );
	$criteria -> setsort( "doid" );
	$criteria -> setorder( "DESC" );
	$criteria -> setlimit( $saxueConfigs['member']['homepnum'] );
	$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['member']['homepnum'] );
	$doing_handler -> queryobjects( $criteria );
	$aids = $uids = array();
	$k = 0;
	while ( $v = $doing_handler -> getobject() ) {
		$rows[$k] = $v -> getvars( 'n' );
		$rows[$k]['doname'] = $saxueDoingtype[$rows[$k]['doing']];
		$rows[$k]['dodate'] = saxue_getdate( $rows[$k]['dotime'] );
		$aids[] = $rows[$k]['aid'];
		$uids[] = $rows[$k]['uid'];
		++$k;
	} 
	// 我关注用户的用户信息
	$uids = array_unique( $uids );
	$sql = 'SELECT * FROM ' . saxue_dbprefix( "member_users" ) . ' WHERE uid IN(' . implode( ',', $uids ) . ')';
	$_res = $doing_handler -> db -> query( $sql );
	$users = array();
	while ( $v = $doing_handler -> getobject( $_res ) ) {
			$k = $v -> getvar( 'uid' );
			$users[$k] = $v -> getvars( 'n' );
			$users[$k]['uhome'] = saxue_geturl( "user_home", $users[$k]['uid'] );
			$users[$k]['uavatar'] = saxue_geturl( "user_avatar", $users[$k]['uid'], 's', $users[$k]['avatar'] );
	}
	// 我关注用户动态的相关文章信息
	saxue_getconfigs( "cate", 'article' );
	$aids = array_unique( $aids );
	$sql = 'SELECT * FROM ' . saxue_dbprefix( "article" ) . ' WHERE aid IN(' . implode( ',', $aids ) . ')';
	$_res = $doing_handler -> db -> query( $sql );
	$articles = $tables = array();
	while ( $v = $doing_handler -> getobject( $_res ) ) {
		$k = $v -> getvar( 'aid' );
		$articles[$k] = $v -> getvars( 'n' );
		$articles[$k]['cname'] = $saxueCate['article'][$articles[$k]['cid']]['cname'];
		$articles[$k]['curl'] = $saxueCate['article'][$articles[$k]['cid']]['curl'];
		$articles[$k]['uhome'] = saxue_geturl( "user_home", $articles[$k]['uid'] );
		$tables[$articles[$k]['tbnum']][] = $k;
	} 
	foreach ( $tables as $tbnum => $ids ) {
		$_res = $doing_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'article_data_' . $tbnum ) . " WHERE aid IN (" . implode( ',', $ids ) . ")" );
		while ( $v = $doing_handler -> getobject( $_res ) ) {
			$k = $v -> getvar( 'aid' );
			$articles[$k] = $articles[$k] + ( $v -> getvars( 'n' ) );
			$articles[$k]['intro'] = nl2br( $articles[$k]['intro'] ); 
			if ( $articles[$k]['isslide'] ) {
					$articles[$k]['album'] = explode( '|', $articles[$k]['album'] );
			}
			// 读取TAG
			$_tags = array();
			$_tagnum = 0;
			if ( strlen( $articles[$k]['tags'] ) > 0 ) {
				$_arr = explode( ',', $articles[$k]['tags'] );
				$_arrid = explode( ',', $articles[$k]['tagids'] );
				foreach ( $_arr as $_i => $_s ) {
					$_tags[$_tagnum]['name'] = $_s;
					$_tags[$_tagnum]['url'] = saxue_geturl( "tag_show", $_arrid[$_i] );
					++$_tagnum;
				} 
			} 
			$articles[$k]['taglist'] = $_tags;
		} 
	} 
	foreach ( $rows as $k => $v ) {
		if ( isset( $articles[$v['aid']] ) && isset( $users[$v['uid']] ) ) {
			$rows[$k]['article'] = $articles[$v['aid']];
			$rows[$k]['user'] = $users[$v['uid']];
		} else {
			unset( $rows[$k] );
		} 
	} 
	// 分页
	include SAXUE_ROOT_PATH . "/lib/util/page.php";
	$listrows = $doing_handler -> getcount( $criteria );
	$jumppage = new saxuepage( $listrows, $saxueConfigs['member']['homepnum'], $_REQUEST['page'], 5 );
	$jumppage -> setlink( '?page=<{$page}>' );
	$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
	$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
} 
include SAXUE_ROOT_PATH . "/model/member_users.php";
$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
$saxueUsers = $users_handler -> get( $_SESSION['saxueUserId'] );
$my = $saxueUsers -> getvars( "n" );
$my['uavatar'] = saxue_geturl( 'user_avatar', $_SESSION['saxueUserId'], 'l', $my['avatar'] );
$my['uhome'] = saxue_geturl( 'user_home', $_SESSION['saxueUserId'] );
$saxueTpl -> assign_by_ref( "my", $my ); 
$saxueTpl -> assign_by_ref( "rows", $rows ); 
$saxueTpl -> setcaching( 0 );
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . "/space/my.html";
include SAXUE_WEB_PATH . "/footer.php";