<?php
function cache_datatable() {
		global $query_handler;
		$sql = "SHOW TABLE STATUS LIKE '" . SAXUE_DB_PREFIX . "_article_data_%'";
		$res = $query_handler -> db -> query( $sql );
		$rows = array();
		while ( $row = $query_handler -> db -> fetcharray( $res ) ) {
				$id = str_replace( SAXUE_DB_PREFIX . '_article_data_', '', $row['Name'] );
				if ( !is_numeric( $id ) || $id < 1 ) continue;
				$rows['article'][$id] = $row['Name'];
		} 
		saxue_setconfigs( "datatable", $rows, "article" );
} 
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'arttable' );
@set_time_limit( 0 );
@session_write_close();
saxue_includedb();
$query_handler = saxuequeryhandler :: getinstance( "SaxueQueryHandler" );
if ( !empty( $_REQUEST['action'] ) ) {
		switch ( $_REQUEST['action'] ) {
				case 'new':
						$tbnum = intval( $_POST['tbnum'] );
						if ( $tbnum < 1 ) {
								saxue_printfail( '数据表格式错误' );
						}
						$res = $query_handler -> db -> query( "SHOW TABLE STATUS LIKE '" . SAXUE_DB_PREFIX . "_article_data_" . $tbnum . "'" );
						$obj = $query_handler -> getobject( $res ); 
						if ( is_object( $obj ) ) {
								saxue_printfail( '数据表『' . SAXUE_DB_PREFIX . "_article_data_" . $tbnum . '』已存在' );
						}
						$res = $query_handler -> db -> query( "CREATE TABLE " . SAXUE_DB_PREFIX . "_article_data_" . $tbnum . " LIKE " . SAXUE_DB_PREFIX . "_article_data_1" );
						if ( !$res ) {
								saxue_printfail( '创建数据表失败' );
						}
						cache_datatable();
						saxue_jumppage( 'table.php', LANG_DO_SUCCESS );
				case 'delete':
						$tbnum = intval( $_REQUEST['tbnum'] );
						$res = $query_handler -> db -> query( "SHOW TABLE STATUS LIKE '" . SAXUE_DB_PREFIX . "_article_data_" . $tbnum . "'" );
						$row = $query_handler -> db -> fetcharray( $res ); 
						if ( !$row ) {
								saxue_printfail( '数据表『' . SAXUE_DB_PREFIX . "_article_data_" . $tbnum . '』不存在' );
						} else if ( $row['Rows'] > 0 ) {
								saxue_printfail( '数据表『' . SAXUE_DB_PREFIX . "_article_data_" . $tbnum . '』有数据，不能删除' );
						}
						$res = $query_handler -> db -> query( "DROP TABLE IF EXISTS " . SAXUE_DB_PREFIX . "_article_data_" . $tbnum );
						if ( !$res ) {
								saxue_printfail( '删除数据表失败' );
						}
						cache_datatable();
						saxue_jumppage( 'table.php', LANG_DO_SUCCESS );
				case 'cache':
						cache_datatable();
						saxue_jumppage( 'table.php', LANG_DO_SUCCESS );
		}
}
$sql = "SHOW TABLE STATUS LIKE '" . SAXUE_DB_PREFIX . "_article_data_%'";
$res = $query_handler -> db -> query( $sql );
$tablerows = array();
$totaltable = 0;
$totalsize = 0;
$totalrows = 0;
$totalindex = 0;
$totalfree = 0;
while ( $row = $query_handler -> db -> fetcharray( $res ) ) {
		$id = str_replace( SAXUE_DB_PREFIX . '_article_data_', '', $row['Name'] );
		if ( !is_numeric( $id ) || $id < 1 ) continue;
		$tablerows[$id] = saxue_funtoarray( "saxue_htmlstr", $row );
		$totalrows += $row['Rows'];
		$totalsize += $row['Data_length'];
		$totalindex += $row['Index_length'];
		$totalfree += $row['Data_free'];
		++$totaltable;
} 
include_once( SAXUE_ADMIN_PATH . "/header.php" );
saxue_getconfigs( "configs", "article" );
$saxueTpl -> assign( "tbnum", $saxueConfigs['article']['tbnum'] ); 
$saxueTpl -> assign( "nexttable", $totaltable + 1 ); 
$saxueTpl -> assign( "tablepre", SAXUE_DB_PREFIX . '_article_data_' ); 
$saxueTpl -> assign( "totaltable", $totaltable ); 
$saxueTpl -> assign( "totalrows", $totalrows );
if ( 1048576 < $totalsize ) {
		$totalsize = sprintf( "%0.1fM", $totalsize / 1048576 );
} else if ( 1024 < $totalsize ) {
		$totalsize = sprintf( "%0.1fK", $totalsize / 1024 );
} 
$saxueTpl -> assign( "totalsize", $totalsize );
if ( 1048576 < $totalindex ) {
		$totalindex = sprintf( "%0.1fM", $totalindex / 1048576 );
} else if ( 1024 < $totalindex ) {
		$totalindex = sprintf( "%0.1fK", $totalindex / 1024 );
} 
$saxueTpl -> assign( "totalindex", $totalindex );
if ( 1048576 < $totalfree ) {
		$totalfree = sprintf( "%0.1fM", $totalfree / 1048576 );
} else if ( 1024 < $totalfree ) {
		$totalfree = sprintf( "%0.1fK", $totalfree / 1024 );
} 
$saxueTpl -> assign( "totalfree", $totalfree );
$saxueTpl -> assign_by_ref( "tablerows", $tablerows );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/table.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );