<?php /*SVN-1324(5.0.09)*/
include_once("../../includes/var.php");
include_once("../../libs/utils.php");
include_once("../../libs/functions.php");

header("Content-Type: text/html;charset=".CS_ENCODING);

$sid = (isset($_GET["sid"])) ? $_GET["sid"] : "";
$mode = (isset($_GET["mode"])) ? $_GET["mode"] : "";
$MERI = (isset($_GET["MERI"])) ? $_GET["MERI"] : "";
$DERI = (isset($_GET["DERI"])) ? $_GET["DERI"] : "";
$DMASTERI = (isset($_GET["DMASTERI"])) ? $_GET["DMASTERI"] : "";
$DSRCERI = (isset($_GET["DSRCERI"])) ? $_GET["DSRCERI"] : "";
$mutp = (isset($_GET["mutp"])) ? $_GET["mutp"] : "";
switch ($mutp) {
	case "R": $mdir = "manage"; break;
	case "B": $mdir = "modules"; break;
	default: echo "System path error!"; return; break;
}

$parary = createsidobject($sid);
if (empty($parary)) {
	header("Location: ../../shell.php");
	exit();
}
$dbo = $parary["dbo"];$ln = $parary["ln"];$user = $parary["user"];
include_once("../../classes/class.Locale_DB.php");
$lo = new Locale($ln, $dbo, 1, $mode);

$install = get_cw_index_setup($sid, $mode, TYPE_FETCH_ARRAY);
$hstr = "<script>".
			"\$(function(){".
				"if(\$('#content').height()<=550){".
					"\$('#page_body').height(585);".
				"}else{".
					"\$('#page_body').height(\$('#content').height()+35);".
				"}".
			"});".
		"</script>";
if (empty($install)) {
	echo "<h2>未完成相關模組之底層設定!</h2>".$hstr;
	return;
}
if (isset($install[0]["MODNAME"]) && trim($install[0]["MODNAME"]) != "") {
	echo "<h2>模組相關設定已變更!請重新登入!</h2>".$hstr;
	return;
}

$mask = array();
$mask_file = "mask.ini";
if (file_exists($mask_file)) $index = parse_ini_file($mask_file, true);

$index["MODE"] = $install[0]["MODE"];
$index["DEFAULTTYPE"] = $install[0]["DEFAULTTYPE"];
$index["SERVERPATH"] = $install[0]["SERVERPATH"];
$index["PGSDIV"] = $install[0]["PGSDIV"];
$index["PGLDIV"] = $install[0]["PGLDIV"];
$index["PGTYPE"] = $install[0]["PGTYPE"];
$index["PGMTABLE"] = $install[0]["PGMTABLE"];
$index["PGFAQ"] = $install[0]["PGFAQ"];
$index["FDNAME"] = $install[0]["FDNAME"];
$index["QUERYSDC"] = $install[0]["QUERYSDC"];
$index["REPORTSDC"] = $install[0]["REPORTSDC"];
if (empty($index["FETYPE"])) $index["PGLOGE"] = 0;

if (!empty($index["DEFAULTTYPE"]) && !empty($index["PGFAQ"])) {
	$ccv_str = "";$data = array();
	$cara = array("sdcn"=>$index["DEFAULTTYPE"],"qury"=>$index["PGFAQ"]);
	if (!empty($index["FDNAME"])) $cara["fied"] = $index["FDNAME"];
	$data = get_cw_sdc_chgview($dbo, $cara, "", TYPE_FETCH_ARRAY);
	if (!empty($data)) {
		for ($i = 0; $i < count($data); $i++) {
			if ($data[$i]["CHGVIEWMODE"] == "0") {
				if ($data[$i]["CHGVIEWVALUE"] == "@") {
					$ccv_str .= "".$user["ERI"]."";
				} else if ($data[$i]["CHGVIEWVALUE"] == "~") {
					$ccv_str .= "".$ln."";
				} else if ($data[$i]["CHGVIEWVALUE"] == "!") {
					$ccv_str .= "".$MERI."";
				} else if ($data[$i]["CHGVIEWVALUE"] == "#") {
					$ccv_str .= "".$DERI."";
				} else if ($data[$i]["CHGVIEWVALUE"] == "$") {
					$ccv_str .= "".$DMASTERI."";
				} else if ($data[$i]["CHGVIEWVALUE"] == "%") {
					$ccv_str .= "".$DSRCERI."";
				} else if ($data[$i]["CHGVIEWVALUE"] == "^") {
					$ccv_str .= "0";
				} else {
					$ccv_str .= "".$data[$i]["CHGVIEWVALUE"]."";
				}
			} else {
				$ccv_str .= "error";
			}
			if (isset($data[$i+1]["CHGVIEWVALUE"])) $ccv_str .= ",";
		}
	}
	$index["CCVVAL"] = $ccv_str;
} else {
	$index["CCVVAL"] = "";
}

if (!empty($index["PGFAQ"])) {
	$odr_str = "";$data = array();
	$sql = "SET NOCOUNT ON;".
		   "EXEC dbo.PROC_CHANGE_VIEW_FIVEAFD '".$index["PGFAQ"]."';".
		   "SELECT * FROM VIEW_FIVEAFD ORDER BY SERNO;".
		   "SET NOCOUNT OFF;";
	$res = $dbo->execute($sql);
	$error = trim($dbo->getMessage());
	if ($error) echo "SQL: ".$sql."<br>".$error."<br>";
	$data = fetch_query_result($res, TYPE_FETCH_ARRAY);
	if (!empty($data)) {
		for ($i = 0; $i < count($data); $i++) {
			if ($data[$i]["QSORTMODE"] != "0") {
				if (!empty($odr_str)) $odr_str .= "&";
				$odr_str .= "S_".str_replace(".", "_", $data[$i]["QFNAME"])."=".$data[$i]["QSORTMODE"];
			}
		}
		if (empty($index["PGWHERE"])) $index["PGWHERE"] .= $odr_str;
		else $index["PGWHERE"] .= "&".$odr_str;
	}
}
$index["PGWHERE"] = "'".$index["PGWHERE"]."'";

include_once("../../classes/general/class.CSTemplate.php");
$cst = new CSTemplate();

$page = "index.html";
$cst->set("SID", $sid);
$cst->set("mode", $mode);
$cst->set("meri", $MERI);
$cst->set("user", $user);
$cst->set("locale_name", $ln);
$cst->set("locale_obj", $lo);
$cst->set("index", $index);
$cst->set("mutp", $mutp);
$cst->set("mdir", $mdir);
$cst->show($page);
?>