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

$O__output = "";

function O__operation(){
	global $O__DBpath, $O__PT, $O__PTATmsg, $O__logNotes;
	if (isset($_POST["O__target"])){ $O__target = (int)$_POST["O__target"]; }
	if (isset($_POST["O__source"])){ $O__source = $_POST["O__source"]; }
	if (isset($_POST["O__from"])){ $O__from = $_POST["O__from"]; }
	if (isset($_POST["O__to"])){ $O__to = $_POST["O__to"]; }
	$O__operation = $_POST["O__operation"];
	
	switch($O__operation){			
		case "aliasAdd":
			$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/aliases.xml", 0, 0);
			
			// Add new alias
			$O__permissionAdd = $O__fileData->addChild("alias");
				$O__permissionAdd->addChild("from","FROM");
				$O__permissionAdd->addChild("to","TO");
			$O__fileData = $O__fileData->asXML();
			O__dataWrite($O__DBpath . "/pageTypes/" . $O__PT . "/data/aliases.xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
					
			$O__sysMsgHeadline = "Alias Add";
			$O__sysMsgBody = "Success. New empty alias entry has been added.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "aliasRemove":
			$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/aliases.xml", 0, 0);
			unset($O__fileData->alias[$O__target]);
			$O__fileData = $O__fileData->asXML();
			O__dataWrite($O__DBpath . "/pageTypes/" . $O__PT . "/data/aliases.xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
					
			$O__sysMsgHeadline = "Alias Remove";
			$O__sysMsgBody = "Success. Alias #" . $O__target . " '" . $O__from . "'(from) -> '" . $O__to . "'(to) has been removed.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "aliasUpdate":
			$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/aliases.xml", 0, 0);
			$O__fileData->alias[$O__target]->from = $O__from;
			$O__fileData->alias[$O__target]->to = $O__to;
			$O__fileData = $O__fileData->asXML();
			O__dataWrite($O__DBpath . "/pageTypes/" . $O__PT . "/data/aliases.xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
			
			$O__sysMsgHeadline = "Alias Update";
			$O__sysMsgBody = "Success. Alias '" . $O__target . "' has 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__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/data/aliases.xml", 0, 0);
		
	$O__output = "<h2>Aliases Manager</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__userEdit\" action=\"/" . $O__PT . "/default/" . $O__AT . "\" method=\"post\">";
	$O__output .= "<input type=\"hidden\" name=\"O__target\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__source\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__from\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__to\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\">";
	
	// Aliases
	$O__output .= "<b>Add:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('aliasAdd');\">Alias</a>";
	for ($i=0; $i<count($O__fileData->alias); $i++){
		$O__data[$i][0] = "<input type=\"text\" name=\"" . $i . "-from\" id=\"" . $i . "-from\" value=\"" . $O__fileData->alias[$i]->from . "\">";
		$O__data[$i][1] = "<input type=\"text\" name=\"" . $i . "-to\" id=\"" . $i . "-to\" value=\"" . $O__fileData->alias[$i]->to . "\">";
		$O__data[$i][2] = "<a href=\"#\" onclick=\"javascript:O__operation('aliasUpdate','" . $i . "','" . $O__fileData->alias[$i]->from . "','" . $O__fileData->alias[$i]->to . "');\">Update</a> | ";
		$O__data[$i][2] .= "<a href=\"#\" onclick=\"javascript:O__operation('aliasRemove','" . $i . "','" . $O__fileData->alias[$i]->from . "','" . $O__fileData->alias[$i]->to . "');\">Delete</a>";
	
	}
	$O__colLabels = array("From","To","Action");
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tablePageTypes",0);
	$O__output .= "</form>";
	
	return $O__output;
}

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

<script>
function O__operation(O__action, O__target, O__from, O__to){
	var O__form = document.forms["O__userEdit"];
	O__form.O__target.value = O__target;
	
	switch (O__action){
		case "aliasAdd":
			var O__input = confirm("Are you sure you want to create a new alias entry?");
			if (O__input == 1){
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "aliasUpdate":
			var O__input = confirm("Are you sure you want to update the alias '" + O__from + "' pointing to '" + O__to + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__fromNew = O__target + "-from";
					O__form.O__from.value = document.getElementById(O__fromNew).value;
				O__toNew = O__target + "-to";
					O__form.O__to.value = document.getElementById(O__toNew).value;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "aliasRemove":
			var O__input = confirm("Are you sure you want to remove the alias '" + O__from + "' pointing to '" + O__to + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.O__from.value = O__from;
				O__form.O__to.value = O__to;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
	}

}
</script>
