<?php
include "../core.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/index.html";
$saxueTset['saxue_page_cacheid'] = $_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 ) {
		saxue_getconfigs( "configs", 'member' );
		// 提取最新动态
		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();
		$criteria -> setsort( "doid" );
		$criteria -> setorder( "DESC" );
		$criteria -> setlimit( $saxueConfigs['member']['homepnum'] );
		$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['member']['homepnum'] );
		$doing_handler -> queryobjects( $criteria );
		$rows = $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;
		} 
		if ( $k > 0 ) {
				// 提取动态相关用户信息
				$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] );
						}
				}
		} 
		$saxueTpl -> assign_by_ref( "rows", $rows );
		// 分页
		include SAXUE_ROOT_PATH . "/lib/util/page.php";
		if ( SAXUE_USE_CACHE ) {
				saxue_getcachevars( 'spaceindex' );
				if ( !is_array( $saxueSpaceindex ) ) {
						$saxueSpaceindex = array( );
				}
				if ( !isset( $saxueSpaceindex['time'] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueSpaceindex['time'] ) {
						$saxueSpaceindex = array( "rows" => $doing_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
						saxue_setcachevars( 'spaceindex', $saxueSpaceindex );
				}
				$listrows = $saxueSpaceindex['rows'];
		} else {
				$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_WEB_PATH . "/footer.php";