<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'artupdate' );
saxue_getconfigs( 'cate', 'article' );
if ( isset( $_REQUEST['dosubmit'] ) ) {
		if ( !isset( $_REQUEST['url'] ) && !isset( $_REQUEST['prenext'] ) ) {
				saxue_printfail( '至少选择一个需要更新的内容' );
		}
		$page = isset( $_REQUEST['page'] ) ? intval( $_REQUEST['page'] ) : 1;
		$total = isset( $_REQUEST['total'] ) ? intval( $_REQUEST['total'] ) : 0;
		$pagesize = !empty( $_REQUEST['pagesize'] ) ? intval( $_REQUEST['pagesize'] ) : 100;
		if ( !is_array( $_REQUEST['catids'] ) ) {
				$catids = $_REQUEST['catids'];
		} elseif ( $_REQUEST['catids'][0] > 0 ) {
				$catids = implode( ',', $_REQUEST['catids'] );
		} else {
				$catids = '';
		} 
		include( SAXUE_ROOT_PATH . "/model/article.php" );
		$article_handler = saxuearticlehandler :: getinstance( "saxuearticlehandler" );
		include SAXUE_ROOT_PATH . "/model/article_data.php";
		$data_handler = saxuearticledatahandler :: getinstance( "saxuearticledatahandler" );
		$criteria = new criteriacompo();
		if ( !empty( $catids ) ) {
				$criteria -> add( new criteria( 'cid', '(' . $catids . ')', 'IN' ) );
		}
		$url = '?dosubmit=1&catids=' . $catids . '&type=' . $_REQUEST['type'] . '&pagesize=' . $pagesize;
		if ( isset( $_REQUEST['url'] ) ) {
				$url .= '&url=1';
		}
		if ( isset( $_REQUEST['prenext'] ) ) {
				$url .= '&prenext=1';
		}
		switch ( $_REQUEST['type'] ) {
				case 'lastinput':
						$criteria -> setsort( "aid" );
						$criteria -> setorder( "DESC" );
						break;
				case 'date':
						if ( !empty( $_REQUEST['fromtime'] ) ) {
								$criteria -> add( new criteria( 'addtime', strtotime( $_REQUEST['fromtime'] . ' 00:00:00' ), '>=' ) );
								$url .= '&fromtime=' . $_REQUEST['fromtime'];
						}
						if ( !empty( $_REQUEST['totime'] ) ) {
								$criteria -> add( new criteria( 'addtime', strtotime( $_REQUEST['totime'] . ' 23:59:59' ), '<=' ) );
								$url .= '&totime=' . $_REQUEST['totime'];
						}
						break;
				case 'id':
						if ( !empty( $_REQUEST['fromid'] ) ) {
								$criteria -> add( new criteria( 'aid', intval( $_REQUEST['fromid'] ), '>=' ) );
								$url .= '&fromid=' . $_REQUEST['fromid'];
						}
						if ( !empty( $_REQUEST['toid'] ) ) {
								$criteria -> add( new criteria( 'aid', intval( $_REQUEST['toid'] ), '<=' ) );
								$url .= '&toid=' . $_REQUEST['toid'];
						}
						break;
		} 
		$criteria -> setlimit( $pagesize );
		$criteria -> setstart( ( $page - 1 ) * $pagesize );
		// 更新
		$article_handler -> queryobjects( $criteria );
		while ( $v = $article_handler -> getobject() ) {
				$row = $v -> getvars( 'n' );
				$up = array();
				$isup = false;
				if ( isset( $_REQUEST['url'] ) ) {
						$up['surl'] = saxue_geturl( 'slide_show', $row['aid'], $row['cid'], $row['addtime'], $row['isslide'] );
						if ( $row['ischapter'] || $row['isslide'] != 1  ) {
								$up['url'] = saxue_geturl( 'article_show', $row['aid'], $row['cid'], $row['addtime'], $row['ischapter'] );
						} else {
								$up['url'] = $up['surl'];
						}
						$isup = true;
				}
				if ( isset( $_REQUEST['prenext'] ) && $row['status'] == 1 ) {
						$_res = $article_handler -> db -> query( "SELECT aid FROM " . saxue_dbprefix( "article" ) . " WHERE aid<" . $row['aid'] . " AND status=1 AND tbnum=" . $row['tbnum'] . " ORDER BY aid DESC LIMIT 1" );
						$_row = $article_handler -> db -> fetcharray( $_res ); 
						$up['preid'] = intval( $_row['aid'] );
						$_res = $article_handler -> db -> query( "SELECT aid FROM " . saxue_dbprefix( "article" ) . " WHERE aid>" . $row['aid'] . " AND status=1 AND tbnum=" . $row['tbnum'] . " ORDER BY aid ASC LIMIT 1" );
						$_row = $article_handler -> db -> fetcharray( $_res ); 
						$up['nextid'] = intval( $_row['aid'] );
						$isup = true;
				}
				if ( $isup ) {
						$data_handler -> setdatatable( $row['tbnum'] );
						$data_handler -> updatefields( $up, 'aid=' . $row['aid'] );
				}
		}
		if ( empty( $total ) ) {
				$total = $article_handler -> getcount( $criteria );
		} 
		$pages = ceil( $total / $pagesize );
		if ( $pages > $page ) {
				$msg = '总文章数：' . $total . '，已更新 ' . ( $page * $pagesize ) . ' 篇，正在跳转到下一页';
				++$page;
				$url .= '&total=' . $total . '&page=' . $page;
				saxue_jumppage( $url, $msg, 1 );
		} 
		saxue_jumppage( 'update.php', '批量更新完成', 1 );
} 
include SAXUE_ROOT_PATH . "/lib/util/tree.php";
$tree = new tree;
$tree -> icon = array( '&nbsp;│&nbsp;', '&nbsp;├─&nbsp;', '&nbsp;└─&nbsp;' );
$tree -> nbsp = '&nbsp;';
$tree -> mid = 'cid';
$tree -> pid = 'pid';
$tree -> init( $saxueCate['article'] );
$str = "<option value='\$cid'>\$spacer\$cname</option>";
$cates = $tree -> get_tree( 0, $str );
include SAXUE_ADMIN_PATH . "/header.php";
$saxueTpl -> assign( "cates", $cates );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/update.html";
include SAXUE_ADMIN_PATH . "/footer.php";
