<?php
include "../core.php";
if( !empty( $_REQUEST['alias'] ) ) {
		saxue_getconfigs( "catealias", "article" );
		if ( isset( $saxueCatealias['article'][$_REQUEST['alias']] ) ) {
				$GLOBALS['_REQUEST']['cid'] = $saxueCatealias['article'][$_REQUEST['alias']];
		}
		if ( empty( $_REQUEST['cid'] ) ) {
				include SAXUE_WEB_PATH . "/404.php";
		}
}
if( !isset( $_REQUEST['cid'] ) ) {
		$GLOBALS['_REQUEST']['cid'] = 0;
}
saxue_getconfigs( "cate", "article" );
include( SAXUE_WEB_PATH . "/header.php" );
if ( isset( $saxueCate['article'][$_REQUEST['cid']] ) && $saxueCate['article'][$_REQUEST['cid']]['child'] ) {
		$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/tag/channel.html';
		$saxueTset['saxue_page_cacheid'] = $_REQUEST['cid'];
		if ( SAXUE_USE_CACHE ) {
				$saxueTpl -> setcaching( 1 );
		} else {
				$saxueTpl -> setcaching( 0 );
		} 
		if ( !SAXUE_USE_CACHE || !$saxueTpl -> is_cached( $saxueTset['saxue_page_template'], $saxueTset['saxue_page_cacheid'] ) ) {
				// 分类属性
				$saxueTpl -> assign( "cid", $_REQUEST['cid'] );
				$saxueTpl -> assign( "curl", saxue_geturl( "tag_list", $_REQUEST['cid'] ) );
				$saxueTpl -> assign( "cname", $saxueCate['article'][$_REQUEST['cid']]['cname'] );
				// 子分类列表
				$catedir = array();
				$childs = explode( ',', $saxueCate['article'][$_REQUEST['cid']]['arrchild'] );
				foreach ( $childs as $child ) {
						if ( $child != $_REQUEST['cid'] ) $catedir[$child] = $saxueCate['article'][$child];
				}
				$saxueTpl -> assign_by_ref( "catedir", $catedir );
		} 
		include( SAXUE_WEB_PATH . "/footer.php" );
		exit;
}
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/tag/list.html';
$saxueTset['saxue_page_cacheid'] = $_REQUEST['cid'] . '_' . $_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", "tag" );
		// 查询TAG列表
		include SAXUE_ROOT_PATH . "/model/tag.php";
		$tag_handler = &saxuetaghandler :: getinstance( "saxuetaghandler" );
		$criteria = new criteriacompo();
		if ( !empty( $_REQUEST['cid'] ) ) {
				if ( $saxueCate['article'][$_REQUEST['cid']]['child'] ) {
						$criteria -> add( new criteria( 'cid', '(' . $saxueCate['article'][$_REQUEST['cid']]['arrchild'] . ')', 'IN' ) );
				} else {
						$criteria -> add( new criteria( 'cid', $_REQUEST['cid'] ) );
				} 
		}
		$criteria -> setsort( "tagid" );
		$criteria -> setorder( "DESC" );
		$criteria -> setlimit( $saxueConfigs['tag']['pnum'] );
		$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['tag']['pnum'] );
		$tag_handler -> queryobjects( $criteria );
		$rows = array();
		$k = 0;
		$colors = intval( $saxueConfigs['tag']['colornum'] );
		if ( $colors <= 1 ) $colors = 0;
		$sizes = intval( $saxueConfigs['tag']['sizenum'] );
		if ( $sizes <= 1 ) $sizes = 0;
		while ( $v = $tag_handler -> getobject() ) {
				$rows[$k] = $v -> getvars( 'n' );
				$rows[$k]['url'] = saxue_geturl( "tag_show", $rows[$k]['tagid'] );
				if ( $colors ) $rows[$k]['color'] = mt_rand( 1, $colors );
				if ( $sizes ) $rows[$k]['size'] = mt_rand( 1, $sizes );
				++$k;
		} 
		$saxueTpl -> assign_by_ref( "rows", $rows );
		// 分类属性
		$catepos = array();
		$catedir = array();
		$curl = saxue_geturl( "tag_list", $_REQUEST['cid'] );
		$cname = $saxueCate['article'][$_REQUEST['cid']]['cname'];
		if ( !empty( $_REQUEST['cid'] ) ) {
				$pid = $saxueCate['article'][$_REQUEST['cid']]['pid'];
				if ( $pid ) {
						$catepos['0']['cid'] = $pid;
						$catepos['0']['cname'] = $saxueCate['article'][$pid]['cname'];
						$catepos['0']['curl'] = saxue_geturl( "tag_list", $pid );
				}
				$catepos['1']['cid'] = $_REQUEST['cid'];
				$catepos['1']['cname'] = $cname;
				$catepos['1']['curl'] = $curl;
				if ( $saxueCate['article'][$_REQUEST['cid']]['child'] ) {
						$childs = explode( ',', $saxueCate['article'][$_REQUEST['cid']]['arrchild'] );
						foreach ( $childs as $child ) {
								if ( $child != $_REQUEST['cid'] ) $catedir[$child] = $saxueCate['article'][$child];
						}
				} elseif ( isset( $saxueCate['article'][$pid] ) && $saxueCate['article'][$pid]['child'] ) {
						$childs = explode( ',', $saxueCate['article'][$pid]['arrchild'] );
						foreach ( $childs as $child ) {
								if ( $child != $pid ) $catedir[$child] = $saxueCate['article'][$child];
						}
				}
		}
		$saxueTpl -> assign( "cid", $_REQUEST['cid'] );
		$saxueTpl -> assign( "curl", $curl );
		$saxueTpl -> assign( "cname", $cname );
		$saxueTpl -> assign_by_ref( "catepos", $catepos );
		$saxueTpl -> assign_by_ref( "catedir", $catedir );
		// 分页
		include SAXUE_ROOT_PATH . "/lib/util/page.php";
		if ( SAXUE_USE_CACHE ) {
				$pagecacheid = $_REQUEST['cid'];
				saxue_getcachevars( "taglistlog" );
				if ( !is_array( $saxueTaglistlog ) ) {
						$saxueTaglistlog = array( );
				}
				if ( !isset( $saxueTaglistlog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueTaglistlog[$pagecacheid]['time'] ) {
						$saxueTaglistlog[$pagecacheid] = array( "rows" => $tag_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
						saxue_setcachevars( "taglistlog", $saxueTaglistlog );
				}
				$listrows = $saxueTaglistlog[$pagecacheid]['rows'];
		} else {
				$listrows = $tag_handler -> getcount( $criteria );
		}
		$jumppage = new saxuepage( $listrows, $saxueConfigs['tag']['pnum'], $_REQUEST['page'] );
		$jumppage -> setlink( saxue_geturl( "tag_list", $_REQUEST['cid'], '<{$page}>' ) );
		$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
		$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
} 
include( SAXUE_WEB_PATH . "/footer.php" );