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

// Setup
$O__pathFile = $O__filePaths[$O__URLpath[1]][0] . $O__PTATinputs["path"];
$O__pathFilename = basename($O__pathFile);
$O__PTATmsg = "";
$O__editorType = "";
if(isset($O__PTATinputs["editor"])){ $O__editorType = $O__PTATinputs["editor"]; }

// Load settings
$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/configs/settings.xml", 0, 0);
$O__editorID = O__XMLgetData($O__fileData,"/settings/setting[@id='editorID']/value");
$O__editorName = O__XMLgetData($O__fileData,"/settings/setting[@id='editorName']/value");
unset($O__fileData);

function O__operation(){
	global $O__pathFile, $O__pathFilename, $O__PTATmsg, $O__logNotes;

	$O__operation = $_POST["O__operation"];

	switch($O__operation){
		case "save":						
			// Backup file
			O__backupData($O__pathFile,0);
			
			// Make sure POST is not completely empty
			$O__fileData = $_POST["O__textEditor"];
			if ($O__fileData == ""){ $O__fileData = " "; }
			
			// Update file
			O__dataWrite($O__pathFile,$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
			$O__sysMsgHeadline = "Save File";
			$O__sysMsgBody = "Success.  '" . $O__pathFilename . "' 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__filePaths, $O__pathFile, $O__PT, $O__PTATmsg, $O__PTATinputs, $O__editorID, $O__editorName, $O__editorType,$O__URLpath;
	
	$O__output = "<h2>Textpad</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<b>Editing:</b> " . "/" . $O__filePaths[$O__URLpath[1]][1] . $O__PTATinputs["path"];
	$O__output .= "<form name=\"O__form\" action=\"/" . $O__PT . "/" . $O__URLpath[1] . "/?path=" . $O__PTATinputs["path"];
	if ($O__editorType != ""){ $O__output .= "&editor=" . $O__editorType; }
	$O__output .= "\" method=\"post\">";
	$O__output .= "<textarea id=\"" . $O__editorID . "\" name=\"" . $O__editorName . "\" style=\"width:98%;\" class=\"O__itemInfo\" wrap=\"on\">" . O__dataRead($O__pathFile) . "</textarea>";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\"><br/>";
	$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>
<?php include($O__libPath . "/functions/client/O__sizer.php"); ?>
window.addEventListener("load", function(){ O__sizer("O__body","<?php echo($O__editorID); ?>",.9,200); });
window.addEventListener("resize", function(){ O__sizer("O__body","<?php echo($O__editorID); ?>",.9,200); });

function O__operation(O__action){
	var O__form = document.forms["O__form"];
	
	switch (O__action){
		case "save":
			var O__input = confirm("Are you sure you want to save '<?php echo($O__pathFilename); ?>'?");
			if (O__input == 1){
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
	}

}
</script>
