<?php
include("CheckSession.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="馬戲團" />
<title>馬戲團</title>
</head>
<?php
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");

require_once("class/FileConfig.php");
require_once("class/FileOperation.php");

$actionOp = $_POST["actionOp"];
$id = $_POST["id"];
$CFileCount = $_POST["CFileCount"];

$CUser = "";
if($_POST["AId"] == null || $_POST["AId"] == "")
{
	$CUser = $_SESSION['userId'];
}
else
{
	$CUser = $_POST["AId"];
}

$fo = new FileOperation();

$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
				
if($actionOp == "Insert")
{
	for($x=1;$x<=$CFileCount;$x++)
	{
	  $tempFileName = "myfile".$x;;
	  $tempMemo = "CMemo".$x;;
	  $tempMemo = $_POST[$tempMemo];	  
	  	  
		if($_FILES[$tempFileName]['name'] != null && $_FILES[$tempFileName]['name'] != "")
		{
		  $orgfilName = $_FILES[$tempFileName]['name'];
			$newfilName = Date('YmdHis').$_FILES[$tempFileName]['name'];
			$baseNewFileName = $fo->get_basename(rtrim($newfilName, '/'));
			$baseOrgFileName = $fo->get_basename(rtrim($orgfilName, '/'));
			$useuploadfile = $_FILECONFIG['UsedDir'].basename($newfilName);
			$backupuploadNewfile = $_FILECONFIG['BackUpDir'].basename($newfilName);
			$backupuploadOrgfile = $_FILECONFIG['BackUpDir'].basename($orgfilName);

			
			if(!move_uploaded_file($_FILES[$tempFileName]['tmp_name'], iconv("utf-8", "big5", $backupuploadOrgfile)))
			{
				echo '<script type="text/javascript">';
  			echo 'alert("檔案上傳失敗!!!!");';
  			echo 'location.href="FileDataIndex.php?page=1"';
  			echo '</script>';
  			exit();
			}
			if(!copy(iconv("utf-8", "big5", $backupuploadOrgfile), iconv("utf-8", "big5", $backupuploadNewfile)))
			{
				echo '<script type="text/javascript">';
  			echo 'alert("檔案複製失敗!!!!");';
  			echo 'location.href="FileDataIndex.php?page=1"';
  			echo '</script>';
  			exit();
			}
			if(!copy(iconv("utf-8", "big5", $backupuploadNewfile), iconv("utf-8", "big5", $useuploadfile)))
			{
				echo '<script type="text/javascript">';
  			echo 'alert("檔案備份失敗!!!!");';
  			echo 'location.href="FileDataIndex.php?page=1"';
  			echo '</script>';
  			exit();
			}		
						
			$insertSql = "INSERT INTO FileData (CMemo, CNewFileName,COrgFileName,CCreateDateTime,CUpdateDateTime,CCreateAcc,CRealCreator)"
	              ."VALUES ('".$db->mysql_real_escape_string($tempMemo)."'"
	              .",'".$db->mysql_real_escape_string($newfilName)."'"
	              .",'".$db->mysql_real_escape_string($orgfilName)."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".$db->mysql_real_escape_string($CUser)."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['userId'])."')";
			$db->query($insertSql);
		}
	}
  $db->close_db();	
	echo '<script type="text/javascript">';
  echo 'alert("新增成功!!!!");';
  echo 'location.href="FileDataIndex.php?page=1"';
  echo '</script>';
}
else
{
	$tempMemo = $_POST["CMemo"];
	
	$updateSql = "UPDATE FileData set CMemo = '".$db->mysql_real_escape_string($tempMemo)."'";
	
	if($_FILES['myfile']['name'] != null && $_FILES['myfile']['name'] != "")
	{
		$orgfilName = $_FILES['myfile']['name'];
		$newfilName = Date('YmdHis').$_FILES['myfile']['name'];
		$baseNewFileName = $fo->get_basename(rtrim($newfilName, '/'));
		$baseOrgFileName = $fo->get_basename(rtrim($orgfilName, '/'));
		$useuploadfile = $_FILECONFIG['UsedDir'].basename($newfilName);
		$backupuploadNewfile = $_FILECONFIG['BackUpDir'].basename($newfilName);
		$backupuploadOrgfile = $_FILECONFIG['BackUpDir'].basename($orgfilName);
		if(!move_uploaded_file($_FILES['myfile']['tmp_name'], iconv("utf-8", "big5", $backupuploadOrgfile)))
		{
			echo '<script type="text/javascript">';
  		echo 'alert("檔案上傳失敗!!!!");';
  		echo 'location.href="FileDataIndex.php?page=1"';
  		echo '</script>';
  		exit();
		}
		if(!copy(iconv("utf-8", "big5", $backupuploadOrgfile), iconv("utf-8", "big5", $backupuploadNewfile)))
		{
			echo '<script type="text/javascript">';
  		echo 'alert("檔案複製失敗!!!!");';
  		echo 'location.href="FileDataIndex.php?page=1"';
  		echo '</script>';
  		exit();
		}
		if(!copy(iconv("utf-8", "big5", $backupuploadNewfile), iconv("utf-8", "big5", $useuploadfile)))
		{
			echo '<script type="text/javascript">';
  		echo 'alert("檔案備份失敗!!!!");';
  		echo 'location.href="FileDataIndex.php?page=1"';
  		echo '</script>';
  		exit();
		}
		
		$updateSql = $updateSql.",CNewFileName = '".$db->mysql_real_escape_string($newfilName)."'";
		$updateSql = $updateSql.",COrgFileName = '".$db->mysql_real_escape_string($orgfilName)."'";
	}
	$updateSql = $updateSql.",CUpdateDateTime = '".Date('Y-m-d H:i:s')."'";
	if($_SESSION['username'] == "admin")
	{
	 		$updateSql = $updateSql.",CCreateAcc = '".$db->mysql_real_escape_string($CUser)."'";
	}                        
	$updateSql = $updateSql." where CId = ".$id;
	$db->query($updateSql);
  $db->close_db();	
	echo '<script type="text/javascript">';
  echo 'alert("更新成功!!!!");';
  echo 'location.href="FileDataIndex.php?page=1"';
  echo '</script>';
}
?>
</html>
