<?php
require 'inc.php';
$obj_data_back=new cls_ctl_data_back();

if($get_act=="del_bak"){
	$arr_return=$obj_data_back->on_del($get_back_name);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
	$get_act="back_list";
}
if($get_act=="repair"&&isset($get_me_table)&&$get_me_table!=""){
	$str_sql="repair table ".$get_me_table." quick";
	$str_err=$obj_db->execute($str_sql);
	if($str_err!=""){
		$obj_msg->alert($str_err);
	}else{
		$obj_msg->alert("成功修复表".$get_me_table);
	}
}
if($get_act=="optimize"&&isset($get_me_table)&&$get_me_table!=""){
	$str_sql="optimize table ".$get_me_table;
	$str_err=$obj_db->execute($str_sql);
	if($str_err!=""){
		$obj_msg->alert($str_err);
	}else{
		$obj_msg->alert("成功优化表".$get_me_table);
	}
	$get_act="";
}
if($get_act=="optimize_exe"&&isset($get_seltable)&&$get_seltable!=""){
	$str_msg="";
	foreach($get_seltable as $item){
		$str_sql="optimize table ".$item;
		$str_err=$obj_db->execute($str_sql);
		if($str_err!=""){
			$str_msg="优化失败";
		}
	}
	if($str_msg!=""){
		$obj_msg->alert($str_msg);
	}
	$get_act="optimize";
}

switch($get_act)
{	
	case "optimize":{
		
		$arr_list=array();
		$ii=0;
		$str_type = $obj_db->version() > '4.1' ? 'Engine' : 'Type';
		$str_sql="SHOW TABLE STATUS where  Data_free>0 and ".$str_type."='MyISAM' and name like '".DB_PRE."%'";
		$query = $obj_db->query($str_sql, 'SILENT');
		while($obj_rs = $obj_db->fetch_array($query)) {
			$arr_x=array();
			$ii++;
			$arr_x["index"]=$ii;
			$arr_x["name"]=$obj_rs["Name"];
			$arr_x["type"]=$obj_rs[$str_type];
			$arr_x["rows"]=$obj_rs["Rows"];
			$arr_x["data_length"]=$obj_rs["Data_length"];
			$arr_x["index_length"]=$obj_rs["Index_length"];
			$arr_x["data_free"]=$obj_rs["Data_free"];
			$arr_list[]=$arr_x;
		}
		include 'tpl/database.optimize.tpl.php';
		break;
	}
	case 'back_create':
	{
		if(!isset($get_selid)){
			$result = $obj_db->query("show table status");
			while($obj_rs = $obj_db->fetch_array($result))
			{
				$str_name=$obj_rs["Name"];
				if(stripos($str_name,"sys_admin_log")||stripos($str_name,"sys_session")) continue;//不备份这两张表
				$arr_table[]=$str_name;
			}
		}else{
			$arr_table=$get_selid;
		}
		if($get_back_name=="") $get_back_name=date("Y_m_d_H_i_s",TIME);
		$str_table=implode("|",$arr_table);
		include 'tpl/database.back_create.tpl.php';
	    break;
	}
	case 'back':
	{
		if($get_back_name=="") $get_back_name=date("Y_m_d_H_i_s",TIME);
		$str_table=$get_table;
		$lng_count=0;
		if($str_table!=""){
			$arr_table=explode("|",$str_table);
			$str_create_table=$arr_table[0];
			$lng_count=count($arr_table)-1;
			unset($arr_table[0]);
			if(count($arr_table)>0){
				$str_table=implode("|",$arr_table);
			}else{
				$str_table="";
			}
		}
		$arr_msg=$obj_data_back->on_back_create($get_back_name,$str_create_table);
		if($arr_msg["err"]!=""){
			$str_msg=$arr_msg["err"];
		}else{
			if($lng_count>0){
				$str_msg="剩余量：<font color='red'>".$lng_count."</font> 正在备份：".$str_create_table;
			}else{
				$str_msg="<font color='red'>完成！</font>";
			}
		}
		include 'tpl/database.back.tpl.php';
		break;
	}
	case 'back_row':
	{
		if($get_back_name=="") $get_back_name=date("Y_m_d_H_i_s",TIME);
		if(!isset($get_table)){
			$obj_msg->alert("备份表错误：请选择数据表！");
			exit;
		}
		$str_table=$get_table;
		(!isset($get_page))? $get_page=1:$get_page=intval($get_page);
		$arr_msg=$obj_data_back->on_back_insert($get_back_name,$str_table,$get_page);
		$get_page=$arr_msg["next_page"];
		include 'tpl/database.back_row.tpl.php';
		break;
	}
	case 'reset_list':
	{
		$arr_back=$obj_data_back->get_back_list();
		include 'tpl/database.reset_list.tpl.php';
		break;
	}
	case 'reset':
	{
		$str_back_name=$get_back_name;
		$arr_return=$obj_data_back->get_tables($str_back_name);
		if($arr_return["err"]==""){
			$arr_table=$arr_return["tables"];
		}else{
			$obj_msg->alert($arr_return["err"]);
			exit;
		}
		$str_table=implode("|",$arr_table);
		include 'tpl/database.reset.tpl.php';
		break;
	}
	case 'reset_table':
	{
		$str_back_name=$get_back_name;
		$str_table=$get_table;
		$arr_table=explode("|",$str_table);
		$str_create_table=$arr_table[0];
		$arr_msg=$obj_data_back->on_create($str_back_name,$str_create_table);
		if($arr_msg["err"]!=""){
			$obj_msg->alert($arr_msg["err"]);
			exit;
		}

		$lng_count=count($arr_table)-1;
		if($lng_count>0){
			$str_msg="剩余量：<font color='red'>".$lng_count."</font> 正在还原：".$str_create_table;
		}else{
			$str_msg="<font color='red'>完成！</font>";
		}

		unset($arr_table[0]);
		$str_table=implode("|",$arr_table);
		include 'tpl/database.reset_table.tpl.php';
		break;
	}
	case 'reset_row':
	{
		$str_back_name=$get_back_name;
		$str_table=$get_table;
		(!isset($get_page))? $get_page=1:$get_page=intval($get_page);
		$arr_msg=$obj_data_back->on_insert($str_back_name,$str_table,$get_page);
		if($arr_msg["err"]!=""){
			$obj_msg->alert($arr_msg["err"]);
			exit;
		}
		$get_page++;
		include 'tpl/database.reset_row.tpl.php';
		break;
	}
	default:
	{
		$arr_table=array();
		$result = $obj_db->query("show table status");
		while($obj_rs = $obj_db->fetch_array($result))
		{
			$lng_is=0;
			$str_name=$obj_rs["Name"];
			if(strlen($str_name)>9){
				$str_x=substr($str_name,-13);
				$str_y=substr($str_name,-11);
				//管理日志与登录记录不需要备份与还原
				if($str_x=="sys_admin_log"||$str_y=="sys_session"){
					$lng_is=1;
				}
			}
			if($lng_is==0) $arr_table[]=$obj_rs;
		}
		include 'tpl/database.tpl.php';
	}
}


?>
