<?php
include "../core.php";
saxue_checkpower( 'attachs' );
include SAXUE_ROOT_PATH . "/model/attachs.php";
$attachs_handler = saxueattachshandler :: getinstance( "saxueattachshandler" );
if ( !empty( $_REQUEST['action'] ) ) {
		@set_time_limit( 0 );
		switch ( $_REQUEST['action'] ) {
				case 'delete':
						if ( empty( $_REQUEST['ids'] ) || !is_array( $_REQUEST['ids'] ) || count( $_REQUEST['ids'] ) == 0 ) {
								saxue_printfail( '请选择附件记录' );
						}
						$attachs_handler -> db -> query( "DELETE FROM " . saxue_dbprefix( "attachs" ) . " WHERE attachid IN(" . implode( ',', $_REQUEST['ids'] ) . ")" );
						break;
				case 'delattachs':
						if ( empty( $_REQUEST['ids'] ) || !is_array( $_REQUEST['ids'] ) || count( $_REQUEST['ids'] ) == 0 ) {
								saxue_printfail( '请选择附件记录' );
						}
						include SAXUE_ROOT_PATH . '/aliyun/oss_php52/sdk.class.php';
						$attachs_handler -> delattachs( $_REQUEST['ids'] );
						break;
				case 'clearzero':
						include SAXUE_ROOT_PATH . '/aliyun/oss_php52/sdk.class.php';
						$attachs_handler -> delarticle( 0 );
						break;
		}
		if ( empty( $_REQUEST['jumpurl'] ) ) {
				saxue_jumppage( 'attachs.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;
$criteria = new criteriacompo();
if ( isset( $_REQUEST['aid'] ) && $_REQUEST['aid'] != '' ) {
		$criteria -> add( new criteria( 'aid', intval( $_REQUEST['aid'] ) ) );
}
if ( !empty( $_REQUEST['savein'] ) ) {
		$criteria -> add( new criteria( 'savein', $_REQUEST['savein'] ) );
}
$criteria -> setsort( "attachid" );
$criteria -> setorder( "DESC" );
$criteria -> setlimit( $_REQUEST['pagesize'] );
$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $_REQUEST['pagesize'] );
$attachs_handler -> queryobjects( $criteria );
$rows = $aids = array();
$k = 0;
$savein = array( 'local' => '本地服务器', 'oss' => 'OSS服务器', 'ftp' => 'FTP服务器' );
while ( $v = $attachs_handler -> getobject() ) {
		$rows[$k] = $v -> getvars( 'n' );
		$rows[$k]['savein'] = $savein[$rows[$k]['savein']];
		if ( $rows[$k]['aid'] > 0 ) $aids[] = $rows[$k]['aid'];
		++$k;
} 
$aids = array_unique( $aids );
$sql = 'SELECT * FROM ' . saxue_dbprefix( "article" ) . ' WHERE  aid IN(' . implode( ',', $aids ) . ')';
$res = $attachs_handler -> db -> query( $sql );
$articles = $tables = array();
while ( $v = $attachs_handler -> getobject( $res ) ) {
		$k = $v -> getvar( 'aid' );
		$articles[$k] = $v -> getvars( 'n' );
		$tables[$articles[$k]['tbnum']][] = $k;
} 
if ( count( $tables ) > 0 ) {
		foreach ( $tables as $tbnum => $ids  ) {
				$_res = $attachs_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'article_data_' . $tbnum ) . " WHERE aid IN (" . implode( ',', $ids ) . ")" );
				while ( $v = $attachs_handler -> getobject( $_res ) ) {
						$k = $v -> getvar( 'aid' );
						$articles[$k] = $articles[$k] + ( $v -> getvars( 'n' ) );
				} 
		} 
}
foreach ( $rows as $k => $v ) {
		if ( isset( $articles[$v['aid']] ) ) {
				$rows[$k]['article'] = $articles[$v['aid']];
				$rows[$k]['isarticle'] = 1;
		} else {
				$rows[$k]['isarticle'] = 0;
		}
}
include SAXUE_ADMIN_PATH . "/header.php";
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTpl -> assign_by_ref( "savein", $savein );
include SAXUE_ROOT_PATH . "/lib/util/page.php";
$jumppage = new saxuepage( $attachs_handler -> getcount( $criteria ), $_REQUEST['pagesize'], $_REQUEST['page'] );
$jumppage -> setlink( "", true, true );
$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
$saxueTset['saxue_contents_template'] = SAXUE_ADMIN_PATH . "/templates/attachs_db.html";
include SAXUE_ADMIN_PATH . "/footer.php";
