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

// Load settings
$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/configs/settings.xml", 0, 0);
$O__pathData = $O__DBpath . "/pageTypes/" . $O__PT . "/data";
unset($O__fileData);

$O__PTATmsg = "";
$O__dataProfile = $O__URLpath[1];

function O__operation(){
	global $O__DBpath, $O__PTATmsg, $O__logNotes, $O__dataProfile, $O__pathData;
	
	$O__PTATmsgAddendum = "";
	$O__operation = $_POST["O__operation"];
	
	switch($O__operation){
		case "save":
			
			// Set up XML DOM with submitted values
			$O__fileData = new DomDocument("1.0", "UTF-8");
			$O__datasetInfo = $O__fileData->createElement("title", "");
				$O__datasetInfo->appendChild($O__fileData->createElement("title", htmlspecialchars(trim($_POST["O__title"]))));
				$O__datasetInfo->appendChild($O__fileData->createElement("dataset", htmlspecialchars(trim($_POST["O__dataset"]))));
				$O__datasetInfo->appendChild($O__fileData->createElement("delimited", htmlspecialchars(trim($_POST["O__delimited"]))));
				//$O__datasetInfo->appendChild($O__fileData->createElement("dataAction", htmlspecialchars(trim($_POST["O__dataAction"]))));
				$O__datasetInfo->appendChild($O__fileData->createElement("dataAction", htmlspecialchars(trim("countAll"))));
				$O__datasetInfo->appendChild($O__fileData->createElement("columnSource", htmlspecialchars(trim($_POST["O__columnSource"]))));
				//$O__datasetInfo->appendChild($O__fileData->createElement("columnSourceLabel", htmlspecialchars(trim($_POST["O__columnSourceLabel"]))));
				$O__datasetInfo->appendChild($O__fileData->createElement("columnTarget", htmlspecialchars(trim($_POST["O__columnTarget"]))));

			$O__fileData->appendChild($O__datasetInfo); // add tree to root XML
						
			// Backup old data and save new one
			O__backupData($O__pathData . "/" . $O__dataProfile . ".xml",0);
			O__XMLdata($O__pathData . "/" . $O__dataProfile . ".xml", $O__fileData, 0);
			unset($O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
			
			break;
	}
	
	// System message and logging
	$O__sysMsgHeadline = "Update Dataset";
	$O__sysMsgBody = "Success. '" . $O__dataProfile . "' and instructions have been updated. ";
	$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
	$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
}

function O__createOutput(){
	global $O__DBpath, $O__PTATmsg, $O__pathData, $O__dataProfile, $O__PT;
	
	// Load source data
	$O__fileData = O__XMLdata($O__pathData . "/" . $O__dataProfile . ".xml", 0, 0);
	$O__title = $O__fileData->title;
	$O__dataset = $O__fileData->dataset;
	$O__delimited = $O__fileData->delimited;
	//$O__dataAction = $O__fileData->dataAction;
	$O__dataAction = "countAll";
	$O__columnSource = $O__fileData->columnSource;
	//$O__columnSourceLabel = $O__fileData->columnSourceLabel;
	$O__columnTarget = $O__fileData->columnTarget;

	// Create UI output	
	$O__output = "<h2>Data Parser</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__pageForm\" id=\"O__pageForm\" action=\"/" . $O__PT . "/" . $O__dataProfile . "/edit\" method=\"post\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\">";	
	$O__output .= "<input type=\"hidden\" name=\"O__title\" value=\"" . $O__title  . "\">";

	$O__output .= "<b>Now editing:</b> <a href=\"/" . $O__PT . "/" . $O__title  . "/display\">View '" . $O__title. "'</a>";
	
	$O__output .= "<p><b>Dataset</b><br><textarea style=\"width:98%;\" rows=\"25\" name=\"O__dataset\" id=\"O__bodyCopy\" class=\"O__itemInfo\" >" . $O__dataset . "</textarea></p>";
	
	/*$O__output .= "<select name=\"O__dataAction\">";
	$O__output .= "<option value=\"countOne\"";
	if ($O__dataAction == "countOne"){ $O__output .= " selected"; }
	$O__output .= ">Count all the values for a single label</option>";
	$O__output .= "<option value=\"countAll\"";
	if ($O__dataAction == "countAll"){ $O__output .= " selected"; }
	$O__output .= ">Count all values</option>";
	$O__output .=  "</select>";*/
	
	$O__output .= "<p><b>Count values in column number</b><br><input type=\"text\" name=\"O__columnTarget\" value=\"" . $O__columnTarget . "\"></p>";		
	$O__output .= "<p><b>Use unique labels in column number</b><br><input type=\"text\" name=\"O__columnSource\" value=\"" . $O__columnSource . "\"></p>";
	
	$O__output .= "<p><b>Separator</b><br><i>Note: Use \"\\t\" for tab.</i><br><input type=\"text\" name=\"O__delimited\" value=\"" . $O__delimited . "\"></p>";
	
	$O__output .="<a href=\"#\" onclick=\"javascript:O__operation('save')\">Save</a>";
	$O__output .= "</form>";

	return $O__output;
}

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

<script>
function O__operation(O__action){
	var O__form = document.forms["O__pageForm"];
	O__form.O__operation.value = O__action;
	O__form.submit();
}
</script>
