<?php
include($O__libPath . "/functions/server/O__backupData.php");
include($O__libPath . "/functions/server/O__tableMaker.php");

$O__PTATmsg = "";
$O__JSsettings = array();
$O__settingsType = $O__URLpath[3];
$O__target = $O__URLpath[1];

switch($O__settingsType){
	case "indexer":
		$O__dataTarget = $O__DBpath . "/pageTypes/O__kiosk/data/indexers/" . $O__target . ".xml";
		break;
	
	case "PT":
		$O__dataTarget = $O__DBpath . "/pageTypes/" . $O__target . "/configs/settings.xml";
		break;
}
	

function O__operation(){
	global $O__DBpath, $O__PT, $O__PTATmsg, $O__logNotes, $O__target, $O__dataTarget;
	
	$O__operation = $_POST["O__operation"];

	switch($O__operation){
		case "save":
			// Parse out the settings package
			$O__settingsPackage = $_POST["O__settingsPackage"];
			$O__settingsPackageArray = explode("<O__delim:set>",$O__settingsPackage);
				
			// Create new XML
			$O__dataXML = new DomDocument("1.0", "UTF-8");
			$O__XMLroot = $O__dataXML->createElement("settings", "");
			
			// Create each setting
			for($i=0; $i < count($O__settingsPackageArray); $i++){
				$O__settingsPackageArrayValue = explode("<O__delim:value>",$O__settingsPackageArray[$i]);
				
				$O__indexerSetting = $O__dataXML->createElement("setting","");
					$O__indexerSetting->setAttribute("id", $O__settingsPackageArrayValue[0]);
					$O__XMLroot->appendChild($O__indexerSetting);
					
				$O__indexerSettingValue = $O__dataXML->createElement("value",$O__settingsPackageArrayValue[1]);
					$O__indexerSetting->appendChild($O__indexerSettingValue);
					
				$O__indexerSettingDescription = $O__dataXML->createElement("desc",$O__settingsPackageArrayValue[2]);
					$O__indexerSetting->appendChild($O__indexerSettingDescription);
			}
			
			// Wrap up and save XML
			$O__dataXML->appendChild($O__XMLroot);
			O__backupData($O__dataTarget,0);
			O__XMLdata($O__dataTarget, $O__dataXML, 0);
			unset($O__dataXML);
			clearstatcache(); // clear cache so newest version can be loaded
						
			$O__sysMsgHeadline = "Settings Update";
			$O__sysMsgBody = "Success. The settings for '" . $O__target . "' have been updated.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;		
			
			break;
	}

}

function O__createOutput(){
	global $O__DBpath, $O__PT, $O__AT, $O__PTATmsg, $O__dataTarget, $O__target, $O__JSsettings, $O__URLpath;
	
	// Create UI output
	$O__output = "<h2>Settings Editor</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__form\" id=\"O__form\" action=\"/" . $O__PT . "/" . $O__target . "/" . $O__AT . "/" . $O__URLpath[3] . "\" method=\"post\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__settingsPackage\" value=\"\">";
	
	$O__output .= "<b>Now editing:</b> '" . $O__target . "'";
	
	// Display settings
	$O__fileData = O__XMLdata($O__dataTarget, 0, 0);
	for ($i=0; $i< count($O__fileData->setting); $i++){
		$O__data[$i][0] = $O__fileData->setting[$i]["id"];
		$O__data[$i][1] = "<input type=\"text\" name=\"" . $O__fileData->setting[$i]["id"] . "\" value=\"" . $O__fileData->setting[$i]->value . "\">";
		$O__data[$i][2] = $O__fileData->setting[$i]->desc;
		$O__JSsettings[trim($O__fileData->setting[$i]["id"])] = htmlspecialchars(trim($O__fileData->setting[$i]->desc));
	}
			
	$O__colLabels = array("Setting","Value","Description");
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tableSettings",0);
	
	$O__output .="<p><a href=\"#\" onclick=\"javascript:O__operation('save')\">Save</a></p>";
	$O__output .= "</form>";
	
	return $O__output;
}

if(isset($_POST["O__operation"])){ O__operation(); }
$O__output = O__createOutput();
?>

<script>
function O__operation(O__action){	
	<?php
	$O__JSsettingLabel = "";
	$O__JSsettingDescription = "";
	foreach($O__JSsettings as $O__JSsettingsLabel => $O__JSsettingsDescription) {
		$O__JSsettingLabel .= "\"" . $O__JSsettingsLabel . "\",";
		$O__JSsettingDescription .= "\"" . $O__JSsettingsDescription . "\",";
	}
	?>
	
	// Set up form action
	var O__form = document.forms["O__form"];
	O__form.O__operation.value = O__action;
	
	// Set up values
	var O__settingsLabels = new Array(<?php echo($O__JSsettingLabel); ?>);
	var O__settingsValues = new Array();
	var O__settingsDescription = new Array(<?php echo($O__JSsettingDescription); ?>);		
	var O__settingsPackage = "";
	
	// Assembles the inputs
	var O__inputCount = 0;
	O__settingsInputValues = document.getElementsByTagName("input");
	for(i=0; i<O__settingsInputValues.length; i++){
		if (O__settingsInputValues[i].type == "text"){
			O__settingsValues[O__inputCount] = O__settingsInputValues[i].value;
			O__inputCount++;
		}
	}
	
	// Assemble settings package	
	for(i=0; i<O__settingsLabels.length; i++){
		O__settingsPackage += O__settingsLabels[i] + "<O__delim:value>" + O__settingsValues[i] + "<O__delim:value>" + O__settingsDescription[i];
		if (i < (O__settingsValues.length-1)){ O__settingsPackage += "<O__delim:set>"; }
	}

	O__form.O__settingsPackage.value = O__settingsPackage;

	O__form.submit();
}
</script>
