<?php
include "../core.php";
if( isset( $_REQUEST['alias'] ) ) {
		saxue_getconfigs( "catealias", "topic" );
		if ( isset( $saxueCatealias['topic'][$_REQUEST['alias']] ) ) {
				$GLOBALS['_REQUEST']['cid'] = $saxueCatealias['topic'][$_REQUEST['alias']];
		}
}
if ( empty( $_REQUEST['cid'] ) ) {
		include SAXUE_WEB_PATH . "/404.php";
}
saxue_getconfigs( "cate", "topic" );
saxue_getconfigs( "configs", "topic" );
include( SAXUE_WEB_PATH . "/header.php" );
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/topic/list.html';
$page_used_cache = false;
if ( $saxueConfigs['topic']['pnum'] ) {
		if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
		$saxueTset['saxue_page_cacheid'] = $_REQUEST['cid'] . '_' . $_REQUEST['page'];
		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 );
		}
} else {
		$saxueTset['saxue_page_cacheid'] = $_REQUEST['cid'];
		if ( SAXUE_USE_CACHE && $saxueTpl -> is_cached( $saxueTset['saxue_page_template'], $saxueTset['saxue_page_cacheid'] ) ) {
				$page_used_cache = true;
		}
}
if ( !$page_used_cache ) {
		// 主题列表
		include SAXUE_ROOT_PATH . "/model/topic.php";
		$topic_handler = &saxuetopichandler :: getinstance( "saxuetopichandler" );
		$criteria = new criteriacompo( new criteria( 'cid', $_REQUEST['cid'] ) );
		$criteria -> add( new criteria( 'status', 1 ) );
		$criteria -> setsort( "addtime" );
		$criteria -> setorder( "DESC" );
		if ( $saxueConfigs['topic']['pnum'] ) {
				$criteria -> setlimit( $saxueConfigs['topic']['pnum'] );
				$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['topic']['pnum'] );
		}
		$topic_handler -> queryobjects( $criteria );
		$rows = $ids = array();
		while ( $v = $topic_handler -> getobject() ) {
				$k = $v -> getvar( 'topicid' );
				$ids[] = $k;
				$rows[$k] = $v -> getvars( 'n' );
		} 
		if ( $ids ) {
				$_res = $topic_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'topic_data' ) . " WHERE topicid IN (" . implode( ',', $ids ) . ")" );
				while ( $v = $topic_handler -> getobject( $_res ) ) {
						$k = $v -> getvar( 'topicid' );
						$rows[$k] = $rows[$k] + ( $v -> getvars( 'n' ) );
						$rows[$k]['intro'] = nl2br( $rows[$k]['intro'] );
				} 
		}
		$saxueTpl -> assign_by_ref( "rows", $rows );
		// 分类属性
		$saxueTpl -> assign_by_ref( "caterows", $saxueCate['topic'] );
		$saxueTpl -> assign( "cid", $_REQUEST['cid'] );
		$saxueTpl -> assign( "cname", $saxueCate['topic'][$_REQUEST['cid']]['cname'] );
		$saxueTpl -> assign( "curl", $saxueCate['topic'][$_REQUEST['cid']]['curl'] );
		$saxueTpl -> assign( "cpage", $saxueCate['topic'][$_REQUEST['cid']]['curl_page'] );
		// 分页
		if ( $saxueConfigs['topic']['pnum'] ) {
				include SAXUE_ROOT_PATH . "/lib/util/page.php";
				if ( SAXUE_USE_CACHE ) {
						$pagecacheid = $_REQUEST['cid'];
						saxue_getcachevars( "topiclistlog" );
						if ( !is_array( $saxueTopiclistlog ) ) {
								$saxueTopiclistlog = array( );
						}
						if ( !isset( $saxueTopiclistlog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueTopiclistlog[$pagecacheid]['time'] ) {
								$saxueTopiclistlog[$pagecacheid] = array( "rows" => $topic_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
								saxue_setcachevars( "topiclistlog", $saxueTopiclistlog );
						}
						$listrows = $saxueTopiclistlog[$pagecacheid]['rows'];
				} else {
						$listrows = $topic_handler -> getcount( $criteria );
				}
				$jumppage = new saxuepage( $listrows, $saxueConfigs['topic']['pnum'], $_REQUEST['page'] );
				$jumppage -> setlink( $saxueCate['topic'][$_REQUEST['cid']]['curl_page'] );
				$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
				$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
		} else {
				$saxueTpl -> assign( "totalpage", 1 );
		}
} 
include( SAXUE_WEB_PATH . "/footer.php" );