<?php
include "../../core.php";
saxue_checklogin();
saxue_getconfigs( "cate", "article" );
saxue_getconfigs( "configs", "article" );
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $GLOBALS['_REQUEST']['page'] = 1;
if ( empty( $_REQUEST['view'] ) || !in_array( $_REQUEST['view'], array( 'pending', 'deny', 'publish' ) ) ) $_REQUEST['view'] = 'publish';
include SAXUE_ROOT_PATH . "/model/article.php";
$article_handler = &saxuearticlehandler :: getinstance( "saxuearticlehandler" );
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'delete' && !empty( $_REQUEST['aid'] ) && is_numeric( $_REQUEST['aid'] ) ) {
		$article = $article_handler -> get( $_REQUEST['aid'] );
		if ( !is_object( $article ) ) {
				saxue_printfail( "ÎÄÕÂ²»´æÔÚ" );
		}
		$row = $article -> getvars( 'n' );
		if ( $row['status'] != 1 && $row['uid'] == $_SESSION['saxueUserId'] ) {
				include SAXUE_ROOT_PATH . "/model/article_data.php";
				$data_handler = saxuearticledatahandler :: getinstance( "saxuearticledatahandler" );
				$data_handler -> setdatatable( $row['tbnum'] );
				$data = $data_handler -> get( $_REQUEST['aid'] );
				$_tagids = $data -> getvar( 'tagids' );
				if ( !empty( $_tagids ) ) {
						include SAXUE_ROOT_PATH . "/model/tag.php";
						$tag_handler = &saxuetaghandler :: getinstance("saxuetaghandler");
						$_arr = explode( ',', $_tagids );
						foreach ( $_arr as $_tagid ) {
								$tag_handler -> updatefields( 'counts=counts-1', 'tagid=' . $tagid );
						}
				}
				saxue_delfile( $saxueConfigs['article']['txtdir'] . saxue_getsubdir( $_REQUEST['aid'] ) . '/' . $_REQUEST['aid'] . '.txt' );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "tag_article" ) . " WHERE aid=" . $_REQUEST['aid'] );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "article" ) . " WHERE aid=" . $_REQUEST['aid'] );
				$article_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "article_data_" . $row['tbnum'] ) . " WHERE aid=" . $_REQUEST['aid'] );
		}
		saxue_jumppage( '?view=' . $_REQUEST['view'] . '&menuid=' . $_REQUEST['view'] . '&page=' . $_REQUEST['page'] );
} 
$criteria = new criteriacompo( new criteria( 'uid', $_SESSION['saxueUserId'] ) );
switch( $_REQUEST['view'] ) {
		case 'publish':
				$criteria -> add( new criteria( 'status', 1 ) );
				break;
		case 'pending':
				$criteria -> add( new criteria( 'status', 0 ) );
				break;
		case 'deny':
				$criteria -> add( new criteria( 'status', 2 ) );
				break;
}
$criteria -> setsort( "addtime" );
$criteria -> setorder( "DESC" );
$criteria -> setlimit( $saxueConfigs['article']['userpnum'] );
$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['article']['userpnum'] );
$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'];
		$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' ) );
				} 
		} 
}
include SAXUE_WEB_PATH . "/member/header.php";
$saxueTpl -> assign_by_ref( "rows", $rows );
include SAXUE_ROOT_PATH . "/lib/util/page.php";
$jumppage = new saxuepage( $article_handler -> getcount( $criteria ), $saxueConfigs['article']['userpnum'], $_REQUEST['page'] );
$jumppage -> setlink( "", true, true );
$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
$saxueTset['saxue_contents_template'] = SAXUE_THEME_PATH . "/article/member/my.html";
include SAXUE_WEB_PATH . "/member/footer.php";