<?php
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//	自動でクラスを参照する
//	関数名	：	__autoload()
//	備考	：	参照先は./class/XXX.phpとする
//	更新	：	2012/10/19 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function __autoload($className)
{
	$fPath = "./class/" . str_replace("_", "/", $className) . ".php";
	require_once $fPath;
}

//===============================================
//	DEBUG_MODE
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
define("DEBUG_MODE", "0");
Debug::setMode(DEBUG_MODE);
if (DEBUG_MODE == 1)
{
	Stopwatch::start();
}

//===============================================
//	CommonUtilities
//	備考		：	
//	更新		：	2012/11/06 KMK-Teraoka
//===============================================
$PENTA_OCEAN = "五洋建設株式会社";
$TAIL_PENTA_OCEAN = "penta-ocean";
$SCRIPT_TITLE = "運行管理システム 警告音声管理";
$TOP_FORM = "../index.php";
$LIST_FORM = "./messages_list.php";
$EDIT_FORM = "./messages_edit.php";
$VIEW_FORM = "./messages_view.php";
$DELETE_FORM = "./messages_delete.php";
$REGIST_FORM = "./messages_regist.php";
$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$MESSAGES_TABLE = "qgis.messages";
$WHERE_PATTERN = array(
	"0"		=>	array("name"	=>	"警告音声ID",		"object"	=>	"msg_id",			"pattern"	=>	"all"),
	"1"		=>	array("name"	=>	"警告音声情報",		"object"	=>	"msg_info",			"pattern"	=>	"middle"),
	"2"		=>	array("name"	=>	"ファイル名称",		"object"	=>	"msg_file_name",	"pattern"	=>	"middle"),
	"3"		=>	array("name"	=>	"備考",				"object"	=>	"remarks",			"pattern"	=>	"middle"),
);
$VIEW_LIST = array(
	"0"		=>	array("head"	=>	"警告音声ID",			"object"	=>	"msg_id",			"type"		=>	"0",		"control"		=>		"-"),
//	"1"		=>	array("head"	=>	"工事ID",				"object"	=>	"const_id",			"type"		=>	"1",		"control"		=>		"txtConstId"),
//	"2"		=>	array("head"	=>	"アプリID",				"object"	=>	"app_id",			"type"		=>	"1",		"control"		=>		"txtAppId"),
	"1"		=>	array("head"	=>	"警告音声情報",			"object"	=>	"msg_info",			"type"		=>	"1",		"control"		=>		"txtMsgInfo"),
	"2"		=>	array("head"	=>	"ファイル名称",			"object"	=>	"msg_file_name",	"type"		=>	"1",		"control"		=>		"txtMsgFileName"),
	"3"		=>	array("head"	=>	"アップロードファイル",	"object"	=>	"local_file",		"type"		=>	"5",		"control"		=>		"fileMsgOgg"),
	"4"		=>	array("head"	=>	"備考",					"object"	=>	"remarks",			"type"		=>	"1",		"control"		=>		"txtRemarks"),
	"5"		=>	array("head"	=>	"更新日時",				"object"	=>	"upddatetime",		"type"		=>	"0",		"control"		=>		"-"),
);
$PAGE_LIMIT = 30;
$TMP_DIR = "./msg_tmp";
$UPLOAD_DIR = "./msg_files";
$CONST_ID = "1";
$APP_ID = "18";

//===============================================
//	HTML-HEAD-
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "<html>";
print "<head>";
print "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ansys/config/css/style.css\">";
print "<link rel=\"shortcut icon\" href=\"/ansys/config/favicon.ico\">";
print "<title>". $SCRIPT_TITLE. "</title>";
print "</head>";

//===============================================
//	HTML-BODY部開始-
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "<body>";
print "<center>";
print "<font size=\"3\">";
print "<b>";
print "". $SCRIPT_TITLE. "<br>";
print "【". $PENTA_OCEAN. "】";
print "</b>";
print "</font>";
print "</center>";
print "<hr />";

try
{
	//================================================
	//	データベース接続
	//	備考		：	
	//	更新		：	2012/10/19 KMK-Teraoka
	//================================================
	$conn = pg_connect("
		host	=	". $HOST. "
		dbname	=	". $DB. "
		user	=	". $USER. "
		password=	". $PASS. "
	");
	if (!$conn)
	{
		throw new Exception("データベースへの接続に失敗しました");
	}
	
	//===============================================
	//	POST、GETパラメータの受け取り
	//	備考		：	
	//	更新		：	2012/11/06 KMK-Teraoka
	//===============================================
	if (DEBUG_MODE == 1)
	{
		extract($_POST);
		extract($_GET);
		extract($_FILES);
		Debug::writes($_POST, $_FILES);
	}
	$selSearch = isset($_POST["selSearch"]) ? htmlspecialchars($_POST["selSearch"]) : "";
	$txtSearchObj = isset($_POST["txtSearchObj"]) ? htmlspecialchars($_POST["txtSearchObj"]) : "";
	$viewPage = isset($_POST["hPage"]) ? htmlspecialchars($_POST["hPage"]) : 1;
	$btnUpdate = isset($_POST["btnUpdate"]) ? htmlspecialchars($_POST["btnUpdate"]) : "";
	if (strcmp($btnUpdate, "確認") == 0)
	{
		$txtMsgInfo = isset($_POST["txtMsgInfo"]) ? htmlspecialchars($_POST["txtMsgInfo"]) : "";
		$txtMsgFileName = isset($_POST["txtMsgFileName"]) ? htmlspecialchars($_POST["txtMsgFileName"]) : "";
		if (strcmp($txtMsgFileName, "") == 0)
		{
			$txtMsgFileName = $fileName;
		}
		else if (!preg_match("/\\.ogg$/i", $txtMsgFileName))
		{
			$txtMsgFileName .= ".ogg";
		}
		if (file_exists($UPLOAD_DIR. "/". $txtMsgFileName))
		{
			$txtMsgFileName = str_replace(".ogg", "_". date("Ymd_His"). ".ogg", $txtMsgFileName);
		}
		$txtRemarks = isset($_POST["txtRemarks"]) ? htmlspecialchars($_POST["txtRemarks"]) : "";
		
		//===============================================
		//	アップロードされたファイルを確認
		//	一時保存ディレクトリへコピー
		//	備考		：	
		//	更新		：	2012/11/06 KMK-Teraoka
		//===============================================
		if ($_FILES["fileMsgOgg"]["error"] == 0)
		{
			$fileName = $_FILES["fileMsgOgg"]["name"];
			if (file_exists($_FILES["fileMsgOgg"]["tmp_name"]))
			{
				if (preg_match("/\\.ogg$/i", $fileName))
				{
					move_uploaded_file($_FILES["fileMsgOgg"]["tmp_name"], mb_convert_encoding($TMP_DIR. "/". $txtMsgFileName, "SJIS", "UTF-8"));
					chmod(mb_convert_encoding($TMP_DIR. "/". $txtMsgFileName, "SJIS", "UTF-8"), 0777);
				}
				else
				{
					throw new Exception("アップロード可能な警告音声のファイル形式は「ogg」のみです");
				}
			}
			else
			{
				throw new Exception("アップロードに失敗しました");
			}
		}
		else
		{
			throw new Exception("ファイルの指定がありません");
		}
		
		$confMode = 1;
		$confValue = array(
			"id"			=>	"",
			"msg_info"		=>	$txtMsgInfo,
			"msg_file_name"	=>	$txtMsgFileName,
			"local_file"	=>	$_FILES["fileMsgOgg"]["name"],
			"remarks"		=>	$txtRemarks,
			"upddatetime"	=>	""
		);
		$mgrMsg = "下記内容で更新します<br />";
	}
	else if (strcmp($btnUpdate, "登録") == 0)
	{
		$regMsgInfo = isset($_POST["regMsgInfo"]) ? htmlspecialchars($_POST["regMsgInfo"]) : "";
		$regMsgFileName = isset($_POST["regMsgFileName"]) ? htmlspecialchars($_POST["regMsgFileName"]) : "";
		$regRemarks = isset($_POST["regRemarks"]) ? htmlspecialchars($_POST["regRemarks"]) : "";
		
		$insData = "INSERT INTO ". $MESSAGES_TABLE. " (";
		$insData .= "const_id, app_id, msg_info, msg_file_name, remarks, upddatetime, regdatetime";
		$insData .= ") VALUES (";
		$insData .= "'". $CONST_ID. "','". $APP_ID. "', '". $regMsgInfo. "', '". $regMsgFileName. "', '". $regRemarks. "', current_timestamp, current_timestamp";
		$insData .= ")";
		$insData .= ";";
		
		Debug::write($insData);
		if (!($resData = pg_query($conn, $insData)))
		{
			throw new Exception("データの登録に失敗しました<br />". $insData. "<br />". pg_last_error(). "<br />");
		}
		//===============================================
		//	アップロードファイルを一時保存ディレクトリから移動
		//	備考		：	
		//	更新		：	2012/11/06 KMK-Teraoka
		//===============================================
		Debug::write($TMP_DIR. "/". $regMsgFileName, $UPLOAD_DIR. "/". $regMsgFileName);
		if(!rename(mb_convert_encoding($TMP_DIR. "/". $regMsgFileName, "SJIS", "UTF-8"), mb_convert_encoding($UPLOAD_DIR. "/". $regMsgFileName, "SJIS", "UTF-8")))
		{
			throw new Exception("ファイルの移動に失敗しました");
		}
		$confMode = 2;
		$selInsData = "SELECT * FROM ". $MESSAGES_TABLE. "";
		$selInsData .= " ORDER BY msg_id DESC";
		$selInsData .= " LIMIT 1";
		$selInsData .= ";";
		if (!($resInsData = pg_query($conn, $selInsData)))
		{
			throw new Exception("最新データの取得に失敗しました<br />". $selInsData. "<br />". pg_last_error(). "<br />");
		}
		$rowInsData = pg_fetch_array($resInsData, 0, PGSQL_ASSOC);
		$mgrMsg = "警告音声ID[". $rowInsData["msg_id"]. "]：警告音声の登録を行いました<br />";
	}
	
	//===============================================
	//	見出し
	//	備考		：	Table内TableでBorderを細くする
	//	更新		：	2012/10/18 KMK-Teraoka
	//===============================================
	print "<table width=\"100%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"0\">";
	print "<tr>";
	print "<td>";
	print "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\">";
	print "<tr bgcolor=\"#99CCFF\">";
	print "<td align=\"left\">";
	print "警告音声登録";
	print "</td>";
	print "</tr>";
	print "</table>";
	print "</td>";
	print "</tr>";
	print "</table>";
	print "<br />";
	
	//===============================================
	//	管理メッセージの表示
	//	備考		：	
	//	更新		：	2012/10/18 KMK-Teraoka
	//===============================================
	print "<font color=\"#FF0000\">";
	print $mgrMsg;
	print "</font>";
	
	//===============================================
	//	一覧の表示
	//	備考		：	
	//	更新		：	2012/10/18 KMK-Teraoka
	//===============================================
	print "<form action=\"". $_SERVER['PHP_SELF']. "\" method=\"post\" style=\"display:inline\" enctype=\"multipart/form-data\">";
	print "<table width=\"45%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"0\">";
	print "<tr>";
	print "<td>";
	print "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\">";
	for($iCount = 0; $iCount < count($VIEW_LIST); $iCount++)
	{
		print "<tr bgcolor=\"#FFFFFF\" align=\"left\">";
		print "<td bgcolor=\"#99CCFF\" style=\"width:30%;height:25px;\">";
		print $VIEW_LIST[$iCount]["head"];
		print "</td>";
		
		print "<td bgcolor=\"#FFFFFF\" style=\"width:70%\">";
		switch($VIEW_LIST[$iCount]["type"])
		{
			case 0:
				if ($confMode == 2)
				{
					print $rowInsData[$VIEW_LIST[$iCount]["object"]];
				}
				else
				{
					print "-";
				}
				break;
			case 1:
				if ($confMode == 1)
				{
					print $confValue[$VIEW_LIST[$iCount]["object"]];
				}
				else if ($confMode == 2)
				{
					print $rowInsData[$VIEW_LIST[$iCount]["object"]];
				}
				else
				{
					print "<input type=\"text\" name=\"". $VIEW_LIST[$iCount]["control"]. "\" value=\"\" style=\"width:100%\">";
				}
				break;
			case 2:
				if ($confMode == 1)
				{
					print $confValue[$VIEW_LIST[$iCount]["object"]];
				}
				else if ($confMode == 2)
				{
					print $rowInsData[$VIEW_LIST[$iCount]["object"]];
				}
				else
				{
					print "<input type=\"text\" name=\"". $VIEW_LIST[$iCount]["control"]. "_y\" value=\"\" style=\"width:34px;ime-mode:inactive\" maxlength=\"4\">";
					print "-";
					print "<input type=\"text\" name=\"". $VIEW_LIST[$iCount]["control"]. "_m\" value=\"\" style=\"width:21px;ime-mode:inactive\" maxlength=\"2\">";
					print "-";
					print "<input type=\"text\" name=\"". $VIEW_LIST[$iCount]["control"]. "_d\" value=\"\" style=\"width:21px;ime-mode:inactive\" maxlength=\"2\">";
					print "&nbsp;";
					print "<input type=\"text\" name=\"". $VIEW_LIST[$iCount]["control"]. "_h\" value=\"\" style=\"width:21px;ime-mode:inactive\" maxlength=\"2\">";
					print ":";
					print "<input type=\"text\" name=\"". $VIEW_LIST[$iCount]["control"]. "_i\" value=\"\" style=\"width:21px;ime-mode:inactive\" maxlength=\"2\">";
					print ":";
					print "<input type=\"text\" name=\"". $VIEW_LIST[$iCount]["control"]. "_s\" value=\"\" style=\"width:21px;ime-mode:inactive\" maxlength=\"2\">";
				}
				break;
			case 3:
				if ($confMode == 2)
				{
					print $rowInsData[$VIEW_LIST[$iCount]["object"]];
				}
				else
				{
					print "-";
				}
				break;
			case 4:
				if ($confMode == 1)
				{
					print $confValue[$VIEW_LIST[$iCount]["object"]];
				}
				else if ($confMode == 2)
				{
					print $rowInsData[$VIEW_LIST[$iCount]["object"]];
				}
				else
				{
					print "<input type=\"checkbox\" name=\"". $VIEW_LIST[$iCount]["control"]. "\" value=\"1\">";
				}
				break;
			case 5:
				if ($confMode == 1)
				{
					print $confValue[$VIEW_LIST[$iCount]["object"]];
				}
				else if ($confMode == 2)
				{
					print "-";
				}
				else
				{
					print "<input type=\"file\" name=\"". $VIEW_LIST[$iCount]["control"]. "\" style=\"width:100%;\" id=\"submit_button\">";
				}
				break;
			default:
				throw new Exception("リストに存在しません");
				break;
		}
		print "</td>";
		print "</tr>";
	}
	print "</table>";
	print "</td>";
	print "</tr>";
	print "</table>";
	
	//===============================================
	//	確認、登録ボタン
	//	備考		：	
	//	更新		：	2012/10/19 KMK-Teraoka
	//===============================================
	if ($confMode == 1)
	{
		print "<input type=\"submit\" name=\"btnUpdate\" value=\"登録\" style=\"width:60px\" id=\"submit_button\">";
		print "&nbsp;";
		print "<input type=\"submit\" name=\"btnUpdate\" value=\"修正\" style=\"width:60px\" id=\"submit_button\">";
		
		print "<input type=\"hidden\" name=\"regMsgInfo\" value=\"". $confValue["msg_info"]. "\">";
		print "<input type=\"hidden\" name=\"regMsgFileName\" value=\"". $confValue["msg_file_name"]. "\">";
		print "<input type=\"hidden\" name=\"regRemarks\" value=\"". $confValue["remarks"]. "\">";
		print "<br />";
	}
	else if ($confMode == 2)
	{
		
	}
	else
	{
		print "<input type=\"submit\" name=\"btnUpdate\" value=\"確認\" style=\"width:60px\" id=\"submit_button\">";
		print "<br />";
	}
	print "<input type=\"hidden\" name=\"selSearch\" value=\"". $selSearch. "\">";
	print "<input type=\"hidden\" name=\"txtSearchObj\" value=\"". $txtSearchObj. "\">";
	print "<input type=\"hidden\" name=\"hPage\" value=\"". $viewPage. "\">";
	print "</form>";
	print "<br />";
}
catch(Exception $exe)
{
	print "<font color=\"#FF0000\">";
	print "【スクリプトエラー】<br />";
	print $exe->getMessage(). "<br />";
	print "</font>";
}

//===============================================
//	一覧へ戻るリンク
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
print "<form name=\"listForm\" method=\"post\" action=\"". $LIST_FORM. "\" style=\"display:inline\">";
print "<a href=\"javascript:listForm.submit()\">一覧</a>";
print "<input type=\"hidden\" name=\"selSearch\" value=\"". $selSearch. "\">";
print "<input type=\"hidden\" name=\"txtSearchObj\" value=\"". $txtSearchObj. "\">";
print "<input type=\"hidden\" name=\"hPage\" value=\"". $viewPage. "\">";
print "</form>";

//===============================================
//	Postgresql終了処理
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
pg_close($conn);

//===============================================
//	HTML終了処理
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
print "<hr />";
print "<font size=\"3\" color=\"#000000\" face=\"Arial\">";
print "<center>";
print "<b>";
print "<i>";
print $TAIL_PENTA_OCEAN;
print "</i>";
print "</b>";
print "</center>";
print "</font>";
print "</body>";
print "</html>";
?>
