<?php
include "../core.php";
if( isset( $_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";
}
include SAXUE_WEB_PATH . "/header.php";
saxue_getconfigs( "cate", "article" );
$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/article/' . $saxueCate['article'][$_REQUEST['cid']]['templist'];
if ( $saxueCate['article'][$_REQUEST['cid']]['showchannel'] ) {
		if ( !file_exists( $saxueTset['saxue_page_template'] ) ) {
				$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/article/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_by_ref( "caterows", $saxueCate['article'] );
				// 分类属性
				$saxueTpl -> assign( "cid", $_REQUEST['cid'] );
				$saxueTpl -> assign( "cname", $saxueCate['article'][$_REQUEST['cid']]['cname'] );
				$saxueTpl -> assign( "curl", $saxueCate['article'][$_REQUEST['cid']]['curl'] );
				// 子分类列表
				$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 );
		} 
} else {
		if ( !file_exists( $saxueTset['saxue_page_template'] ) ) {
				$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/article/list.html';
		}
		if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
		$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", "article" );
				$pnum = $saxueCate['article'][$_REQUEST['cid']]['pnum'];
				if ( SAXUE_IN_MOBILE ) {
						$pnum = $saxueConfigs['article']['mobile_pnum'];
				} elseif ( empty( $pnum ) ) {
						$pnum = $saxueConfigs['article']['pnum'];
				}
				// 文章列表
				include SAXUE_ROOT_PATH . "/model/article.php";
				$article_handler = &saxuearticlehandler :: getinstance( "saxuearticlehandler" );
				if ( $saxueCate['article'][$_REQUEST['cid']]['child'] ) {
						$criteria = new criteriacompo( new criteria( 'pid', $_REQUEST['cid'] ) );
				} else {
						$criteria = new criteriacompo( new criteria( 'cid', $_REQUEST['cid'] ) );
				} 
				$criteria -> add( new criteria( 'status', 1 ) );
				$criteria -> setsort( "aid" );
				$criteria -> setorder( "DESC" );
				$criteria -> setlimit( $pnum );
				$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $pnum );
				$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'];
						if ( $rows[$k]['uid'] > 0 ) {
								$rows[$k]['uhome'] = saxue_geturl( "user_home", $rows[$k]['uid'] );
								$rows[$k]['uavatar'] = saxue_geturl( "user_avatar", $rows[$k]['uid'] );
						}
						$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'] );
										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 );
				// 分类属性
				$saxueTpl -> assign( "cid", $_REQUEST['cid'] );
				$saxueTpl -> assign( "cname", $saxueCate['article'][$_REQUEST['cid']]['cname'] );
				$saxueTpl -> assign( "curl", $saxueCate['article'][$_REQUEST['cid']]['curl'] );
				$saxueTpl -> assign( "cpage", $saxueCate['article'][$_REQUEST['cid']]['curl_page'] );
				// 父分类定位
				$catepos = array();
				$pid = $saxueCate['article'][$_REQUEST['cid']]['pid'];
				if ( $pid ) {
						$catepos[] = $saxueCate['article'][$pid];
				}
				$catepos[] = $saxueCate['article'][$_REQUEST['cid']];
				$saxueTpl -> assign_by_ref( "catepos", $catepos );
				// 子分类列表
				$catedir = array();
				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_by_ref( "catedir", $catedir );
				// 分页
				include SAXUE_ROOT_PATH . "/lib/util/page.php";
				if ( SAXUE_USE_CACHE ) {
						$pagecacheid = $_REQUEST['cid'];
						saxue_getcachevars( "articlelistlog" );
						if ( !is_array( $saxueArticlelistlog ) ) {
								$saxueArticlelistlog = array( );
						}
						if ( !isset( $saxueArticlelistlog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueArticlelistlog[$pagecacheid]['time'] ) {
								$saxueArticlelistlog[$pagecacheid] = array( "rows" => $article_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
								saxue_setcachevars( "articlelistlog", $saxueArticlelistlog );
						}
						$listrows = $saxueArticlelistlog[$pagecacheid]['rows'];
				} else {
						$listrows = $article_handler -> getcount( $criteria );
				}
				$jumppage = new saxuepage( $listrows, $pnum, $_REQUEST['page'], 5 );
				$jumppage -> setlink( $saxueCate['article'][$_REQUEST['cid']]['curl_page'] );
				$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
				$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
		} 
}
include( SAXUE_ROOT_PATH . "/common/funstat.php" );
saxue_visit_stat( $_REQUEST['cid'], 'article_cate', 'views', 'cid' );
include( SAXUE_WEB_PATH . "/footer.php" );
