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

//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//	メッセージ一覧を取得する
//	関数名	：	getMessagesList
//	備考	：	
//	更新	：	2013/04/08 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function getMessagesList($conn_)
{
	$ret = array();
	
	$selectMsg = "SELECT msg_id, msg_info, msg_file_name FROM apc_common.messages";
	$selectMsg .= ";";
	Debug::write($insData);
	if (!($resMsg = pg_query($conn_, $selectMsg)))
	{
		return -1;
	}
	$row = pg_num_rows($resMsg);
	
	$ret[0]["msg_id"] = "-1";
	$ret[0]["msg_info"] = "利用しない";
	$ret[0]["msg_file_name"] = "-";
		
	for($iCount = 0; $iCount < $row; $iCount++)
	{
		$rowMsg = pg_fetch_array($resMsg, $iCount, PGSQL_ASSOC);
		$ret[$iCount + 1]["msg_id"] = $rowMsg["msg_id"];
		$ret[$iCount + 1]["msg_info"] = $rowMsg["msg_info"];
		$ret[$iCount + 1]["msg_file_name"] = $rowMsg["msg_file_name"];
	}
	return $ret;
}

//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//	エリアIDの頂点数を取得する
//	関数名	：	getMessagesList
//	備考	：	
//	更新	：	2013/04/08 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function getAreaIdList($conn_, $listId_)
{
	$ret = array();
	$selectIds = "SELECT area_id FROM qgis.area_list WHERE list_id='". $listId_. "'";
	if (!($resIds = pg_query($conn_, $selectIds)))
	{
		throw new Exception("エリアID一覧の取得に失敗<br />". $selectIds. "<br />". pg_last_error(). "<br />");
	}
	$row = pg_num_rows($resIds);
	
	if ($row != 0)
	{
		$selectPhase = "";
		for($iCount = 0; $iCount < $row; $iCount++)
		{
			$rowIds = pg_fetch_array($resIds, $iCount, PGSQL_ASSOC);
			$ret[$iCount]["id"] = $rowIds["area_id"];
			if ($iCount != 0)
			{
				$selectPhase .= ", ";
			}
			$selectPhase .= " COUNT(CASE WHEN area_id='". $ret[$iCount]["id"]. "' THEN 1 ELSE NULL END) as id_". $ret[$iCount]["id"]. "_cnt";
		}
		
		$selectVertex = "SELECT";
		$selectVertex .= $selectPhase;
		$selectVertex .= " FROM qgis.point_list";
		$selectVertex .= ";";
		Debug::write($selectVertex);
		if (!($resVertex = pg_query($conn_, $selectVertex)))
		{
			throw new Exception("頂点数の取得に失敗<br />". $selectVertex. "<br />". pg_last_error(). "<br />");
		}
		$rowVertex = pg_fetch_array($resVertex, 0, PGSQL_ASSOC);
		
		for($iCount = 0; $iCount < $row; $iCount++)
		{
			$key = "id_". $ret[$iCount]["id"]. "_cnt";
			$ret[$iCount]["vertex"] = $rowVertex[$key];
		}
		//Debug::writes($ret);
	}
	return $ret;
}

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

//===============================================
//	CommonUtilities
//	備考		：	
//	更新		：	2012/11/02 KMK-Teraoka
//===============================================
$eventsName = isset($_POST["events_name"]) ? htmlspecialchars($_POST["events_name"]) : "";

$PENTA_OCEAN = "五洋建設株式会社";
$TAIL_PENTA_OCEAN = "penta-ocean";
$SCRIPT_TITLE = "エリア編集 -". $eventsName. "-";
$CONST_ID = 4;
$APP_ID = 3;
$TOP_FORM = "../index.php";
$REGIST_FORM = "./area_regist.php";
$LIST_FORM = "./area_list.php";
$EDIT_FORM = "./area_edit.php";
$VIEW_FORM = "./area_view.php";
$DELETE_FORM = "./area_delete.php";
$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$AREA_TABLE = "qgis.area_list";

$VIEW_LIST = array(
	"0"		=>	array("head"	=>	"形式",				"object"	=>	"format",		"type"		=>	"1",		"control"		=>		"selectFormat",		"defval"	=>	"-"),
	"1"		=>	array("head"	=>	"優先度",			"object"	=>	"priority",		"type"		=>	"2",		"control"		=>		"selectPriority",	"defval"	=>	"-"),
	"2"		=>	array("head"	=>	"メッセージ1",		"object"	=>	"msg_id1",		"type"		=>	"3",		"control"		=>		"selectMsg1",		"defval"	=>	"-"),
	"3"		=>	array("head"	=>	"メッセージ2",		"object"	=>	"msg_id2",		"type"		=>	"3",		"control"		=>		"selectMsg2",		"defval"	=>	"-"),
	"4"		=>	array("head"	=>	"メッセージ3",		"object"	=>	"msg_id3",		"type"		=>	"3",		"control"		=>		"selectMsg3",		"defval"	=>	"-"),
	"5"		=>	array("head"	=>	"メッセージ4",		"object"	=>	"msg_id4",		"type"		=>	"3",		"control"		=>		"selectMsg4",		"defval"	=>	"-"),
	"6"		=>	array("head"	=>	"間隔[ms]",			"object"	=>	"msg_interval",	"type"		=>	"",			"control"		=>		"txtInterval",		"defval"	=>	"0"),
	"7"		=>	array("head"	=>	"無効[ms]",			"object"	=>	"msg_invalid",	"type"		=>	"",			"control"		=>		"txtInvalid",		"defval"	=>	"10000"),
	"8"		=>	array("head"	=>	"頂点数",			"object"	=>	"rect_vertex",	"type"		=>	"4",		"control"		=>		"txtRectVertex",	"defval"	=>	"4"),
	"9"		=>	array("head"	=>	"備考",				"object"	=>	"remarks",		"type"		=>	"",			"control"		=>		"txtRemarks",		"defval"	=>	""),
	"10"	=>	array("head"	=>	"更新時間",			"object"	=>	"upddatetime",	"type"		=>	"",			"control"		=>		"-",				"defval"	=>	"-"),
);

$FORMAT_LIST = array(
	"0"		=>	array("value"	=>	"1",		"view"		=>	"侵入時発生(矩形)"),
);

$PRIORITY_LIST = array(
	"0"		=>	array("value"	=>	"1",		"view"		=>	"警告"),
);
$PAGE_LIMIT = 30;

//===============================================
//	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/10/18 KMK-Teraoka
	//===============================================
	if (DEBUG_MODE == 1)
	{
		extract($_POST);
		extract($_GET);
		Debug::writes($_POST);
	}
	$listId = isset($_POST["list_id"]) ? htmlspecialchars($_POST["list_id"]) : "";
	if (strcmp($listId, "") == 0)
	{
		throw new Exception("登録IDが指定されていません");
	}
	$areaId = isset($_POST["area_id"]) ? htmlspecialchars($_POST["area_id"]) : "";
	if (strcmp($areaId, "") == 0)
	{
		throw new Exception("エリアIDが指定されていません");
	}
	
	//===============================================
	//	メッセージ一覧を取得する
	//	備考		：	
	//	更新		：	2013/04/08 KMK-Teraoka
	//===============================================
	$msgList = getMessagesList($conn);
	$vertexList = getAreaIdList($conn, $listId);
	
	//===============================================
	//	WHERE句の作成
	//	備考		：	
	//	更新		：	2012/10/18 KMK-Teraoka
	//===============================================
	$whereObj = "";
	$whereObj .= " WHERE list_id='". $listId. "' AND area_id='". $areaId. "'";
	Debug::write($whereObj);
	
	//===============================================
	//	見出し
	//	備考		：	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/19 KMK-Teraoka
	//===============================================
	$selData = "SELECT * FROM ". $AREA_TABLE;
	if (strcmp($whereObj, "") != 0)
	{
		$selData .= $whereObj;
	}
	$selData .= ";";
	Debug::write($selData);
	if (!($resData = pg_query($conn, $selData)))
	{
		throw new Exception("アプリID：[". $listId. "]のデータ取得に失敗しました<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 1:
				$row = count($FORMAT_LIST);
				for($jCount = 0; $jCount < $row; $jCount++)
				{
					if ($rowData[$VIEW_LIST[$iCount]["object"]] == $FORMAT_LIST[$jCount]["value"])
					{
						print $FORMAT_LIST[$jCount]["view"];
						break;
					}
				}
				break;
			case 2:
				$row = count($PRIORITY_LIST);
				for($jCount = 0; $jCount < $row; $jCount++)
				{
					if ($rowData[$VIEW_LIST[$iCount]["object"]] == $PRIORITY_LIST[$jCount]["value"])
					{
						print $PRIORITY_LIST[$jCount]["view"];
						break;
					}
				}
				break;
			case 3:
				$row = count($msgList);
				for($jCount = 0; $jCount < $row; $jCount++)
				{
					if ($rowData[$VIEW_LIST[$iCount]["object"]] == $msgList[$jCount]["msg_id"])
					{
						print $msgList[$jCount]["msg_info"];
						break;
					}
				}
				break;
			case 4:
				$row = count($vertexList);
				for($jCount = 0; $jCount < $row; $jCount++)
				{
					if ($rowData["area_id"] == $vertexList[$jCount]["id"])
					{
						if (strcmp($rowData[$VIEW_LIST[$iCount]["object"]], $vertexList[$jCount]["vertex"]) == 0)
							print "<font color=\"#000000\">";
						else
							print "<font color=\"#FF0000\">";
						
						print $rowData[$VIEW_LIST[$iCount]["object"]]. "(". $vertexList[$jCount]["vertex"]. ")";
						print "</font>";
						break;
					}
				}
				break;
			default:
				print $rowData[$VIEW_LIST[$iCount]["object"]];
				break;
		}
		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=\"area_id\" value=\"". $areaId. "\">";
	print "<input type=\"hidden\" name=\"list_id\" value=\"". $listId. "\">";
	print "<input type=\"hidden\" name=\"events_name\" value=\"". $eventsName. "\">";
	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=\"list_id\" value=\"". $listId. "\">";
print "<input type=\"hidden\" name=\"events_name\" value=\"". $eventsName. "\">";
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>";
?>
