<?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 . '/slide/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" );
		if ( SAXUE_IN_MOBILE ) {
				$pnum = $saxueConfigs['article']['mobile_spnum'];
		} else {
				$pnum = $saxueConfigs['article']['spnum'];
		}
		// 文章列表
		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 -> add( new criteria( 'isslide', 0, '>' ) );
		$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]['scurl'] = $saxueCate['article'][$rows[$k]['cid']]['scurl'];
				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_by_ref( "caterows", $saxueCate['article'] );
		$saxueTpl -> assign( "cid", $_REQUEST['cid'] );
		$saxueTpl -> assign( "cname", $saxueCate['article'][$_REQUEST['cid']]['cname'] );
		$saxueTpl -> assign( "scurl", $saxueCate['article'][$_REQUEST['cid']]['scurl'] );
		$saxueTpl -> assign( "spage", $saxueCate['article'][$_REQUEST['cid']]['scurl_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( "slidelistlog" );
				if ( !is_array( $saxueSlidelistlog ) ) {
						$saxueSlidelistlog = array( );
				}
				if ( !isset( $saxueSlidelistlog[$pagecacheid] ) || SAXUE_CACHE_LIFETIME < SAXUE_NOW_TIME - $saxueSlidelistlog[$pagecacheid]['time'] ) {
						$saxueSlidelistlog[$pagecacheid] = array( "rows" => $article_handler -> getcount( $criteria ), "time" => SAXUE_NOW_TIME );
						saxue_setcachevars( "slidelistlog", $saxueSlidelistlog );
				}
				$listrows = $saxueSlidelistlog[$pagecacheid]['rows'];
		} else {
				$listrows = $article_handler -> getcount( $criteria );
		}
		$jumppage = new saxuepage( $listrows, $pnum, $_REQUEST['page'], 5 );
		$jumppage -> setlink( $saxueCate['article'][$_REQUEST['cid']]['scurl_page'] );
		$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
		$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
} 
include( SAXUE_WEB_PATH . "/footer.php" );
