<?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/article.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'] );
		/* 用户信息 */
		saxue_getconfigs( "configs", 'member' );
		saxue_getconfigs( "cate", 'article' );
		include SAXUE_ROOT_PATH . "/model/article.php";
		$article_handler = &saxuearticlehandler :: getinstance( "saxuearticlehandler" );
		$criteria = new criteriacompo( new criteria( 'uid', $_REQUEST['uid'] ) );
		$criteria -> add( new criteria( 'status', 1 ) );
		$criteria -> setsort( "aid" );
		$criteria -> setorder( "DESC" );
		$criteria -> setlimit( $saxueConfigs['member']['homepnum'] );
		$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['member']['homepnum'] );
		$article_handler -> queryobjects( $criteria );
		$rows = $tables = array();
		while ( $v = $article_handler -> getobject() ) {
				$k = $v -> getvar( 'aid' );
				$rows[$k] = $v -> getvars( 'n' );
				$rows[$k]['cname'] = $saxueCate['article'][$rows[$k]['cid']]['cname'];
				$rows[$k]['curl'] = $saxueCate['article'][$rows[$k]['cid']]['curl'];
				$tables[$rows[$k]['tbnum']][] = $k;
		}  
		if ( count( $tables ) > 0 ) {
				foreach ( $tables as $tbnum => $ids  ) {
						$_res = $article_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'article_data_' . $tbnum ) . " WHERE aid IN (" . implode( ',', $ids ) . ")" );
						while ( $v = $article_handler -> getobject( $_res ) ) {
								$k = $v -> getvar( 'aid' );
								$rows[$k] = $rows[$k] + ( $v -> getvars( 'n' ) );
								$rows[$k]['intro'] = nl2br( $rows[$k]['intro'] );
								$rows[$k]['uhome'] = saxue_geturl( "user_home", $rows[$k]['uid'] );
								if ( $rows[$k]['isslide'] ) {
										$rows[$k]['album'] = explode( '|', $rows[$k]['album'] );
								}
								// 读取TAG
								$_tags = array();
								$_tagnum = 0;
								if ( strlen( $rows[$k]['tags'] ) > 0 ) {
										$_arr = explode( ',', $rows[$k]['tags'] );
										$_arrid = explode( ',', $rows[$k]['tagids'] );
										foreach ( $_arr as $_i => $_s ) {
												$_tags[$_tagnum]['name'] = $_s;
												$_tags[$_tagnum]['url'] = saxue_geturl( "tag_show", $_arrid[$_i] );
												++$_tagnum;
										} 
								}
								$rows[$k]['taglist'] = $_tags;
						} 
				} 
		}
		$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, 'homearticlelog' );
				if ( !is_array( $saxueHomearticlelog ) ) {
						$saxueHomearticlelog = array( );
				}
				if ( !isset( $saxueHomearticlelog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueHomearticlelog[$pagecacheid]['time'] ) {
						$saxueHomearticlelog[$pagecacheid] = array( "rows" => $article_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
						saxue_setcachevars( $_sub, $saxueHomearticlelog, 'homearticlelog' );
				}
				$listrows = $saxueHomearticlelog[$pagecacheid]['rows'];
		} else {
				$listrows = $article_handler -> getcount( $criteria );
		}
		$jumppage = new saxuepage( $listrows, $saxueConfigs['member']['homepnum'], $_REQUEST['page'], 5 );
		$jumppage -> setlink( saxue_geturl( "user_home", $_REQUEST['uid'], 'article', '<{$page}>' ) );
		$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
		$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
		$saxueTpl -> assign( "spacenav", 'article' );
}
include SAXUE_WEB_PATH . "/footer.php";