<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'pending' );
saxue_getconfigs( "configs", "article" );
saxue_getconfigs( "datatable", "article" );
saxue_getconfigs( "cate", "article" );
if ( empty( $_REQUEST['tbnum'] ) || !array_key_exists( $_REQUEST['tbnum'], $saxueDatatable['article'] ) ) {
		$_REQUEST['tbnum'] = $saxueConfigs['article']['tbnum'];
}
include SAXUE_ROOT_PATH . "/model/article.php";
$article_handler = &saxuearticlehandler :: getinstance( "saxuearticlehandler" );
if ( !empty( $_REQUEST['action'] ) ) {
		if ( empty( $_REQUEST['ids'] ) || !is_array( $_REQUEST['ids'] ) || count( $_REQUEST['ids'] ) == 0 ) {
				saxue_printfail( '请选择文章' );
		}
		@set_time_limit( 0 );
		switch ( $_REQUEST['action'] ) {
				case 'setlevel':
						$level = intval( $_REQUEST['level'] );
						$article_handler -> updatefields( array( 'level' => $level ), 'aid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'checkno':
						$article_handler -> updatefields( array( 'status' => 2 ), 'aid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'checkok':
						include SAXUE_ROOT_PATH . "/common/funarticle.php";
						$score = $saxueConfigs['article']['postscore'];
						$money = $saxueConfigs['article']['postmoney'];
						if ( $score > 0 || $money > 0 ) {
								include SAXUE_ROOT_PATH . "/model/member_users.php";
								$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
						}
						$dosql = '';
						$article_handler -> updatefields( array( 'status' => 1 ), 'aid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						$article_handler -> queryobjects( new criteria( 'aid', '(' . implode( ',', $_REQUEST['ids'] ) . ')', 'IN' ) );
						while ( $v = $article_handler -> getobject() ) {
								$row = $v -> getvars( "n" );
								if ( $row['uid'] > 0 ) {
										$dosql .= ',(' . $row['uid'] . ', ' . $row['aid'] . ', ' . $row['addtime'] . ', 1)';
										if ( $score > 0 ) $users_handler -> changescore( $row['uid'], $score, 100, $row['aid'] );
										if ( $money > 0 ) $users_handler -> changemoney( $row['uid'], $money, 100, $row['aid'] );
								}
								if ( $saxueConfigs['article']['url_detail'] > 2 && $row['ischapter'] > 0 ) {
										RepackArticle( $row['aid'], array( 'makechapter' => 1 ), true );
								}
						} 
						PreNextArticle( $_REQUEST['ids'], $_REQUEST['tbnum'], true );
						// 动态处理
						if ( !empty( $dosql ) ) {
								$dosql = 'INSERT INTO ' . saxue_dbprefix( 'member_doing' ) . '(uid, aid, dotime, doing) VALUES ' . substr( $dosql, 1 );
								$article_handler -> db -> query( $dosql );
						}
						// 百度主动推送
						if ( $saxueConfigs['article']['baidupush'] == 1 ) {
								$urls = array();
								$data_handler -> setdatatable( $_REQUEST['tbnum'] );
								$data_handler -> queryobjects( new criteria( 'aid', '(' . implode( ',', $_REQUEST['ids'] ) . ')', 'IN' ) );
								while ( $v = $data_handler -> getobject() ) {
										$url = $v -> getvar( "url" );
										$urls[] = SAXUE_URL . $url;
										if ( SAXUE_MOBILE_OPEN == 1 ) $urls[] = SAXUE_MURL . $url;
								} 
								BaiduPush( $urls );
						}
						break;
				case 'movecate':
						$tocid = intval( $_REQUEST['tocid'] );
						if ( $saxueCate['article'][$tocid]['child'] ) {
								saxue_printfail( '清选择子分类' );
						}
						if ( $saxueCate['article'][$tocid]['pid'] ) {
								$topid = $saxueCate['article'][$tocid]['pid'];
						} else {
								$topid = $tocid;
						}
						$article_handler -> updatefields( array( 'cid' => $tocid, 'pid' => $topid ), 'aid IN(' . implode( ',', $_REQUEST['ids'] ) . ')' );
						break;
				case 'delete':
						include SAXUE_ROOT_PATH . "/common/funarticle.php";
						DelArticle( $_REQUEST['ids'], $_REQUEST['tbnum'] );
						break;
		}
		if ( empty( $_REQUEST['jumpurl'] ) ) {
				saxue_jumppage( 'article.php', LANG_DO_SUCCESS );
		} else {
				saxue_jumppage( $_REQUEST['jumpurl'], LANG_DO_SUCCESS );
		}
}
if ( empty( $_REQUEST['pagesize'] ) || !is_numeric( $_REQUEST['pagesize'] ) || !in_array( $_REQUEST['pagesize'], array( '20', '30', '50', '100' ) ) ) $_REQUEST['pagesize'] = 30;
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
if ( empty( $_REQUEST['status'] ) || !is_numeric( $_REQUEST['status'] ) || !in_array( $_REQUEST['status'], array( '0', '2' ) ) ) $_REQUEST['status'] = 0;
$datatable = $saxueDatatable['article'][$_REQUEST['tbnum']];
$ids = array();
$checkids = false;
if ( !empty( $_REQUEST['keytype'] ) && !empty( $_REQUEST['keyword'] ) && in_array( $_REQUEST['keytype'], array( 'title', 'author', 'copyfrom' ) ) ) {
		if ( $_REQUEST['searchtype'] == 2 ) {
				$sql = 'SELECT aid FROM ' . $datatable . ' WHERE ' . $_REQUEST['keytype'] . " = '" . $_REQUEST['keyword'] . "'";
		} elseif ( $_REQUEST['searchtype'] == 1 ) {
				$sql = 'SELECT aid FROM ' . $datatable . ' WHERE ' . $_REQUEST['keytype'] . " LIKE '" . $_REQUEST['keyword'] . "%'";
		} else {
				$sql = 'SELECT aid FROM ' . $datatable . ' WHERE ' . $_REQUEST['keytype'] . " LIKE '%" . $_REQUEST['keyword'] . "%'";
		}
		$res = $article_handler -> db -> query( $sql );
		while ( $v = $article_handler -> getobject( $res ) ) {
				$ids[] = $v -> getvar( 'aid' );
		}
		$checkids = true;
}
$rows = array();
if ( !$checkids || count( $ids ) > 0 ) {
		$criteria = new criteriacompo();
		if ( $checkids ) {
				$criteria -> add( new criteria( 'aid', '(' . implode( ',', $ids ) . ')', 'IN' ) );
		}
		if ( !empty( $_REQUEST['cid'] ) ) {
				if ( $saxueCate['article'][$_REQUEST['cid']]['pid'] ) {
						$criteria -> add( new criteria( 'cid', $_REQUEST['cid'] ) );
				} else {
						$criteria -> add( new criteria( 'pid', $_REQUEST['cid'] ) );
				} 
		}
		if ( !$checkids && !empty( $_REQUEST['keytype'] ) && !empty( $_REQUEST['keyword'] ) ) {
				$criteria -> add( new criteria( $_REQUEST['keytype'], intval( $_REQUEST['keyword'] ) ) );
		}
		$criteria -> add( new criteria( 'status', $_REQUEST['status'] ) );
		$criteria -> add( new criteria( 'tbnum', $_REQUEST['tbnum'] ) );
		if ( isset( $_REQUEST['form_sort'] ) && !empty( $_REQUEST['form_sort'] ) ) {
				$criteria -> setsort( $_REQUEST['form_sort'] );
		} else {
				$criteria -> setsort( "aid" );
		} 
		if ( isset( $_REQUEST['form_order'] ) && !empty( $_REQUEST['form_order'] ) ) {
				$criteria -> setorder( $_REQUEST['form_order'] );
		} else {
				$criteria -> setorder( "DESC" );
		} 
		$criteria -> setlimit( $_REQUEST['pagesize'] );
		$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $_REQUEST['pagesize'] );
		$article_handler -> queryobjects( $criteria );
		$aids = array();
		while ( $v = $article_handler -> getobject() ) {
				$k = $v -> getvar( 'aid' );
				$aids[] = $k;
				$rows[$k] = $v -> getvars( 'n' );
				$rows[$k]['cname'] = $saxueCate['article'][$rows[$k]['cid']]['cname'];
				$rows[$k]['curl'] = $saxueCate['article'][$rows[$k]['cid']]['curl'];
				$rows[$k]['pname'] = $saxueCate['article'][$rows[$k]['pid']]['cname'];
				$rows[$k]['purl'] = $saxueCate['article'][$rows[$k]['pid']]['curl'];
		}  
		if ( count( $aids ) > 0 ) {
				$res = $article_handler -> db -> query( "SELECT * FROM " . $datatable . " WHERE aid IN (" . implode( ',', $aids ) . ")" );
				while ( $v = $article_handler -> getobject( $res ) ) {
						$k = $v -> getvar( 'aid' );
						$rows[$k] = $rows[$k] + ( $v -> getvars( 'n' ) );
				}
		}
		$listrows = $article_handler -> getcount( $criteria );
} else {
		$listrows = 0;
}
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTpl -> assign_by_ref( "tables", $saxueDatatable['article'] );
include_once( SAXUE_ROOT_PATH . "/lib/util/page.php" );
$jumppage = new saxuepage( $listrows, $_REQUEST['pagesize'], $_REQUEST['page'] );
$jumppage -> setlink( "", true, true );
$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
include_once( SAXUE_ROOT_PATH . "/lib/util/tree.php" );
$tree = new tree;
$tree -> icon = array( '&nbsp;<span class="f_tree">│</span>&nbsp;', '&nbsp;<span class="f_tree">├─</span> ', '&nbsp;<span class="f_tree">└─</span> ' );
$tree -> nbsp = '&nbsp;';
$tree -> mid = 'cid';
$tree -> pid = 'pid';
$tree -> init( $saxueCate['article'] );
$str = "<option value=\$cid \$selected>\$spacer\$cname</option>";
$cates = $tree -> get_tree( 0, $str, intval( $_REQUEST['cid'] ) );
$saxueTpl -> assign( "cates", $cates );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/pending.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );