<?php
include "../core.php";
$tagid = intval( $_REQUEST['tagid'] );
if ( empty( $tagid ) ) {
		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_cacheid'] = floor( intval( $tagid ) / 1000 ) . '/' . $tagid . '_' . $_REQUEST['page'];
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/tag/detail.html';
$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 ) {
		// 查询TAG
		include SAXUE_ROOT_PATH . "/model/tag.php";
		$tag_handler = &saxuetaghandler :: getinstance("saxuetaghandler");
		$tag = $tag_handler -> get( $tagid );
		if ( !is_object( $tag ) ) {
				include SAXUE_WEB_PATH . "/404.php";
		} 
		$row = $tag -> getvars( "n" );
		$saxueTpl -> assign_by_ref( "row", $row );
		saxue_getconfigs( "cate", "article" );
		saxue_getconfigs( "configs", "tag" );
		// 查询TAG文章，速度慢时需优化查询
		$where = saxue_dbprefix( 'tag_article' ) . " a INNER JOIN " . saxue_dbprefix( 'article' ) . " b ON a.aid=b.aid WHERE a.tagid=" . $row['tagid'] . " AND b.status=1";
		$sql = "SELECT b.* FROM " . $where . " ORDER BY b.aid DESC LIMIT " . ( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['tag']['tpnum'] ) . ", " . $saxueConfigs['tag']['tpnum'];
		$res = $tag_handler -> db -> query( $sql );
		$articles = $tables = array();
		while ( $v = $tag_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'];
				if ( $articles[$k]['uid'] > 0 ) {
						$articles[$k]['uhome'] = saxue_geturl( "user_home", $articles[$k]['uid'] );
						$articles[$k]['uavatar'] = saxue_geturl( "user_avatar", $articles[$k]['uid'] );
				}
				$tables[$articles[$k]['tbnum']][] = $k;
		} 
		if ( count( $tables ) > 0 ) {
				foreach ( $tables as $tbnum => $ids  ) {
						$_res = $tag_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'article_data_' . $tbnum ) . " WHERE aid IN (" . implode( ',', $ids ) . ")" );
						while ( $v = $tag_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;
						} 
				} 
		}
		$saxueTpl -> assign_by_ref( "articles", $articles );
		// 分页
		include SAXUE_ROOT_PATH . "/lib/util/page.php";
		if ( SAXUE_USE_CACHE ) {
				$pagecacheid = $tagid;
				$_sub = floor( $tagid / 100 );
				saxue_getcachevars( $_sub, 'tagarticlelog' );
				if ( !is_array( $saxueTagarticlelog ) ) {
						$saxueTagarticlelog = array( );
				}
				if ( !isset( $saxueTagarticlelog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueTagarticlelog[$pagecacheid]['time'] ) {
						$_res = $tag_handler -> db -> query( "SELECT COUNT(*) FROM " . $where );
						if ( !$_res ) {
								$listrows = 0;
						} else {
								list( $listrows ) = $tag_handler -> db -> fetchrow( $_res );
						} 
						$saxueTagarticlelog[$pagecacheid] = array( "rows" => $listrows, "time" => SAXUE_NOW_TIME );
						saxue_setcachevars( $_sub, $saxueTagarticlelog, 'tagarticlelog' );
				}
				$listrows = $saxueTagarticlelog[$pagecacheid]['rows'];
		} else {
				$_res = $tag_handler -> db -> query( "SELECT COUNT(*) FROM " . $where );
				if ( !$_res ) {
						$listrows = 0;
				} else {
						list( $listrows ) = $tag_handler -> db -> fetchrow( $_res );
				} 
		}
		$jumppage = new saxuepage( $listrows, $saxueConfigs['tag']['tpnum'], $_REQUEST['page'] );
		$jumppage -> setlink( saxue_geturl( "tag_show", $tagid, '<{$page}>' ) );
		$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
		$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
} 
include SAXUE_ROOT_PATH . "/common/funstat.php";
saxue_visit_stat( $tagid, 'tag', 'views', 'tagid' );
include( SAXUE_WEB_PATH . "/footer.php" );