<?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/11/06 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 = "apc_common.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"	=>	"警告音声情報",		"object"	=>	"msg_info",			"type"		=>	"1",		"control"		=>		"txtMsgInfo"),
	"2"		=>	array("head"	=>	"ファイル名称",		"object"	=>	"msg_file_name",	"type"		=>	"0",		"control"		=>		"-"),
	"3"		=>	array("head"	=>	"備考",				"object"	=>	"remarks",			"type"		=>	"1",		"control"		=>		"txtRemarks"),
	"4"		=>	array("head"	=>	"更新日時",			"object"	=>	"upddatetime",		"type"		=>	"0",		"control"		=>		"-"),
);
$PAGE_LIMIT = 30;
$TMP_DIR = "./msg_tmp";
$UPLOAD_DIR = "./msg_files";

//===============================================
//	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/18 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);
		Debug::writes($_POST);
	}
	$editId = isset($_POST["editId"]) ? htmlspecialchars($_POST["editId"]) : "";
	if (strcmp($editId, "") == 0)
	{
		throw new Exception("警告音声IDが指定されていません");
	}
	$selSearch = isset($_POST["selSearch"]) ? htmlspecialchars($_POST["selSearch"]) : "";
	$txtSearchObj = isset($_POST["txtSearchObj"]) ? htmlspecialchars($_POST["txtSearchObj"]) : "";
	$viewPage = isset($_POST["hPage"]) ? htmlspecialchars($_POST["hPage"]) : 1;
	
	//===============================================
	//	WHERE句の作成
	//	備考		：	
	//	更新		：	2012/10/18 KMK-Teraoka
	//===============================================
	$whereObj = "";
	$whereObj .= " WHERE msg_id='". $editId. "'";
	Debug::write($whereObj);
	
	//===============================================
	//	見出し
	//	備考		：	Table内TableでBorderを細くする
	//	更新		：	2012/11/06 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/11/06 KMK-Teraoka
	//===============================================
	$selData = "SELECT * FROM ". $MESSAGES_TABLE;
	if (strcmp($whereObj, "") != 0)
	{
		$selData .= $whereObj;
	}
	$selData .= ";";
	Debug::write($selData);
	if (!($resData = pg_query($conn, $selData)))
	{
		throw new Exception("警告音声ID：[". $editId. "]のデータ取得に失敗しました<br />". $selData. "<br />". pg_last_error(). "<br />");
	}
	$rowData = pg_fetch_array($resData, 0, PGSQL_ASSOC);
	
	//===============================================
	//	管理メッセージの表示
	//	備考		：	
	//	更新		：	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\">";
	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:
				print $rowData[$VIEW_LIST[$iCount]["object"]];
				break;
			case 1:
				print $rowData[$VIEW_LIST[$iCount]["object"]];
				break;
			case 2:
				print $rowData[$VIEW_LIST[$iCount]["object"]];
				break;
			case 3:
				print $rowData[$VIEW_LIST[$iCount]["object"]];
				break;
			default:
				throw new Exception("リストに存在しません");
				break;
		}
		
		//===============================================
		//	物理ファイルの存在を確認
		//	備考		：	
		//	更新		：	2012/11/06 KMK-Teraoka
		//===============================================
		if ($iCount == 2)
		{
			if (!file_exists($UPLOAD_DIR. "/". mb_convert_encoding($rowData[$VIEW_LIST[$iCount]["object"]], "SJIS", "UTF-8")))
			{
				print "<font color=\"#ff0000\">";
				print "<br />※ファイルが存在しません";
				print "</font>";
			}
		}
		
		print "</td>";
		print "</tr>";
	}
	print "</table>";
	print "</td>";
	print "</tr>";
	print "</table>";
	print "</form>";
	print "<br />";
	
	//===============================================
	//	編集リンク
	//	備考		：	
	//	更新		：	2012/10/23 KMK-Teraoka
	//===============================================
	print "<form name=\"cForm_1_2\" method=\"post\" action=\"". $EDIT_FORM. "\" style=\"display:inline\">";
	print "<a href=\"javascript:cForm_1_2.submit()\">編集</a>";
	print "<input type=\"hidden\" name=\"editId\" value=\"". $editId. "\">";
	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 "&nbsp;|&nbsp;";
}
catch(Exception $exc)
{
	print "<font color=\"#FF0000\">";
	print "【スクリプトエラー】<br />";
	print $exc->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>";
print "<br />";

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

//===============================================
//	HTML終了処理
//	備考		：	
//	更新		：	2012/10/18 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>";
?>
