<?php
include($O__libPath . "/functions/server/O__tableMaker.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__storagePathMenus = $O__DBpath . "/pageTypes/" . $O__PT . O__XMLgetData($O__fileData,"/settings/setting[@id='storagePathMenus']/value");
unset($O__fileData);

$O__systemMsg = "";
$O__menu = $O__URLpath[1];
$O__menuItemCount = 0;

function O__operation(){
	global $O__DBpath, $O__systemMsg, $O__logNotes, $O__storagePathMenus, $O__menu, $O__PTATmsg;
	
	if(isset($_POST["O__menuItemCountFinal"])){
		// Collected submitted menu
		$O__menuItemCountFinal = $_POST["O__menuItemCountFinal"];
		
		// Create XML
		$O__fileData = new DomDocument("1.0", "UTF-8");
		$O__menuTree = $O__fileData->createElement("menu", "");

		// Add each menu item	
		for($i=0; $i<$O__menuItemCountFinal; $i++){
			$O__menuItem = $O__fileData->createElement("item", "");
				if(isset($_POST["active" . $i])){
					$O__menuItem->appendChild($O__fileData->createElement("active", "1"));
				} else {
					$O__menuItem->appendChild($O__fileData->createElement("active", "0"));		
				}
				
				$O__menuItem->appendChild($O__fileData->createElement("name", htmlspecialchars($_POST["name" . $i])));
				$O__menuItem->appendChild($O__fileData->createElement("info", htmlspecialchars($_POST["info" . $i])));
				$O__menuItem->appendChild($O__fileData->createElement("href", htmlspecialchars($_POST["href" . $i])));
				$O__menuItem->appendChild($O__fileData->createElement("path", $_POST["path" . $i]));
				$O__menuItem->appendChild($O__fileData->createElement("bin", $_POST["bin" . $i]));
				$O__menuItem->appendChild($O__fileData->createElement("args", htmlspecialchars($_POST["args" . $i])));
				$O__menuItem->appendChild($O__fileData->createElement("wait", $_POST["wait" . $i]));
				$O__menuItem->appendChild($O__fileData->createElement("img", $_POST["img" . $i]));
			$O__menuTree->appendChild($O__menuItem); // add menu item to tree		
		}
		
		$O__fileData->appendChild($O__menuTree); // add tree to root file

		// Backup old menu and save new one
		O__backupData($O__storagePathMenus . "/" . $O__menu . ".xml",0);
		O__XMLdata($O__storagePathMenus . "/" . $O__menu . ".xml", $O__fileData, 0);
		clearstatcache(); // clear cache so newest version can be loaded
		
		$O__sysMsgHeadline = "Menu Update";
		$O__sysMsgBody = "Success.  '" . $O__menu . "' has 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__PT, $O__systemMsg, $O__storagePathMenus, $O__menu, $O__menuItemCount, $O__PTATmsg;
	
	// Load menu data
	$O__fileData = O__XMLdata($O__storagePathMenus . "/" . $O__menu . ".xml", 0, 0);
	$O__menuItemCount = count($O__fileData);

	$O__output = "<h2>Kiosk Manager</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<p><b>Now editing:</b> '" . $O__menu . "'</p>";
	
	$O__output .= "<b>Add:</b> ";
	$O__output .= "<a href=\"javascript:O__menuItemAdd();\">New Item</a>";
	$O__output .= "<br><br>";
	$O__output .= "<form name=\"O__menuItemsForm\" id=\"O__menuItemsForm\" action=\"/" . $O__PT . "/" . $O__menu . "/edit\" method=\"post\">";	
	$O__output .= "<input type=\"hidden\" name=\"O__menuItemCountFinal\" value=\"\">";
	
	for($i=0;$i<count($O__fileData);$i++){
		$O__data[$i][0] = "<center><span id=\"numLabel" . $i. "\">" . $i . "</span><br><img id=\"imgPreview" . $i ."\" src=\"" . $O__fileData->item[$i]->img ."\" width=\"50\" height=\"50\"></center>";
		$O__data[$i][1] = "<table width=\"100%\">";
		$O__data[$i][1] .= "<tr><td colspan=\"2\"><b>Basic Details</b></td></tr>";
		$O__data[$i][1] .= "<tr><td>Title:</td><td><input type=\"text\" id=\"name" . $i ."\" name=\"name" . $i ."\" value=\"" . $O__fileData->item[$i]->name . "\"></td></tr>";
		$O__data[$i][1] .= "<tr><td>Description:</td><td><input type=\"text\" id=\"info" . $i ."\" name=\"info" . $i ."\" value=\"" . $O__fileData->item[$i]->info . "\"></td></tr>";
		$O__data[$i][1] .= "<tr><td>Image Path:</td><td><input type=\"text\" id=\"img" . $i ."\" name=\"img" . $i ."\" value=\"" . $O__fileData->item[$i]->img . "\"></td></tr>";
		
		// Use/ Active value
		$O__data[$i][1] .= "<tr><td>Active:</td><td><input type=\"checkbox\" id=\"active" . $i ."\" name=\"active" . $i ."\" ";
		if ($O__fileData->item[$i]->active == 1){
			$O__data[$i][1] .= "value=\"1\" checked=\"checked\"></td></tr>";
		} else {
			$O__data[$i][1] .= "value=\"0\"></td></tr>";
		}
		
		$O__data[$i][1] .= "</table><br><br>";
		$O__data[$i][1] .= "<table width=\"100%\">";
		$O__data[$i][1] .= "<tr><td colspan=\"2\"><b>Web Link Options</b></td></tr>";
		$O__data[$i][1] .= "<tr><td>Link:</td><td><input type=\"text\" id=\"href" . $i ."\" name=\"href" . $i ."\" value=\"" . $O__fileData->item[$i]->href . "\"></td></tr>";
		$O__data[$i][1] .= "</table><br><br>";
		$O__data[$i][1] .= "<table width=\"100%\">";
		$O__data[$i][1] .= "<tr><td colspan=\"2\"><b>Program Launcher Options</b></td></tr>";
		$O__data[$i][1] .= "<tr><td>Directory Path:</td><td><input type=\"text\" id=\"path" . $i ."\" name=\"path" . $i ."\" value=\"" . $O__fileData->item[$i]->path . "\"></td></tr>";
		$O__data[$i][1] .= "<tr><td>Executable:</td><td><input type=\"text\" id=\"bin" . $i ."\" name=\"bin" . $i ."\" value=\"" . $O__fileData->item[$i]->bin . "\"></td></tr>";
		$O__data[$i][1] .= "<tr><td>Options:</td><td><input type=\"text\" id=\"args" . $i ."\" name=\"args" . $i ."\" value=\"" . $O__fileData->item[$i]->args . "\"></td></tr>";
		$O__data[$i][1] .= "<tr><td>Launch Waitime:</td><td><input type=\"text\" id=\"wait" . $i ."\" name=\"wait" . $i ."\" value=\"" . $O__fileData->item[$i]->wait . "\"></td></tr>";
		$O__data[$i][1] .= "</table>";
		$O__data[$i][2] = "<a href=\"javascript:O__menuItemRemove(" . $i . ");\">Delete</a> | ";
		$O__data[$i][2] .= "<a href=\"javascript:O__menuItemMove(" . $i . ",'d');\">&#9660;</a> | ";
		$O__data[$i][2] .= "<a href=\"javascript:O__menuItemMove(" . $i . ",'u');\">&#9650;</a>";
	}

	$O__colLabels = array("#", "Menu Item","Actions");
	$O__output .="<a href=\"#\" onclick=\"javascript:O__operation()\">Save Menu</a> | <a href=\"/O__kiosk/" . $O__menu . "/display\">Display Menu</a> ";
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__menuItemsTable",0);
	$O__output .="<a href=\"#\" onclick=\"javascript:O__operation()\">Save Menu</a> | <a href=\"/O__kiosk/" . $O__menu . "/display\">Display Menu</a> ";
	$O__output .= "</form>";

	return $O__output;
}

O__operation();
$O__output = O__createOutput();
?>

<script>
var O__menuItemCount = <?php echo($O__menuItemCount); ?>;
	
function O__menuItemMove(num,direction){
	var tempActive,tempName,tempInfo,tempHref,tempPath,tempBin,tempArgs,tempWait,tempImg, tempImgPreview;	
	
	if (direction=="u"){ numTarget = num-1; }		
	if (direction=="d"){ numTarget = num+1; }

	if((numTarget >= 0)&&(numTarget < O__menuItemCount)){
	
		//First, copy target location values into temporary vars
		tempActive = document.getElementById("active" + numTarget).checked;
		tempName = document.getElementById("name" + numTarget).value;
		tempInfo = document.getElementById("info" + numTarget).value;
		tempHref = document.getElementById("href" + numTarget).value;
		tempPath = document.getElementById("path" + numTarget).value;
		tempBin = document.getElementById("bin" + numTarget).value;
		tempArgs = document.getElementById("args" + numTarget).value;
		tempWait = document.getElementById("wait" + numTarget).value;
		tempImg = document.getElementById("img" + numTarget).value;
		tempImgPreview = document.getElementById("imgPreview" + numTarget).src;
		
		//Next, copy source line values into target line; target values = source values
		document.getElementById("active" + numTarget).checked = document.getElementById("active" + num).checked;
		document.getElementById("name" + numTarget).value = document.getElementById("name" + num).value;
		document.getElementById("info" + numTarget).value = document.getElementById("info" + num).value;
		document.getElementById("href" + numTarget).value = document.getElementById("href" + num).value;
		document.getElementById("path" + numTarget).value = document.getElementById("path" + num).value;
		document.getElementById("bin" + numTarget).value = document.getElementById("bin" + num).value;
		document.getElementById("args" + numTarget).value = document.getElementById("args" + num).value;
		document.getElementById("wait" + numTarget).value = document.getElementById("wait" + num).value;
		document.getElementById("img" + numTarget).value = document.getElementById("img" + num).value;
		document.getElementById("imgPreview" + numTarget).src = document.getElementById("imgPreview" + num).src;
		
		
		//Finally, copy temporary values into source line; source values = temporary values
		document.getElementById("active" + num).checked = tempActive;
		document.getElementById("name" + num).value = tempName;
		document.getElementById("info" + num).value = tempInfo;
		document.getElementById("href" + num).value = tempHref;
		document.getElementById("path" + num).value = tempPath;
		document.getElementById("bin" + num).value = tempBin;
		document.getElementById("args" + num).value = tempArgs;
		document.getElementById("wait" + num).value = tempWait;
		document.getElementById("img" + num).value = tempImg;
		document.getElementById("imgPreview" + num).src = tempImgPreview;
	}
}

function O__menuItemAdd(){
	// First, move out the existing IDs to make space for the new one
	var numCount = O__menuItemCount-1;
	var O__menuItemsTable = document.getElementById("O__menuItemsTable");

	while(numCount>=0){
		var numNext = numCount+1;
	
		// Col 1
		document.getElementById("numLabel" + numCount).innerHTML = numNext;
		document.getElementById("numLabel" + numCount).setAttribute("name", "numLabel" + numNext);
			document.getElementById("numLabel" + numCount).id = "numLabel" + numNext;
		document.getElementById("imgPreview" + numCount).setAttribute("name", "imgPreview" + numNext);
			document.getElementById("imgPreview" + numCount).id = "imgPreview" + numNext;
		
		// Col 2
		document.getElementById("active" + numCount).setAttribute("name", "active" + numNext);
			document.getElementById("active" + numCount).id = "active" + numNext;
		document.getElementById("name" + numCount).setAttribute("name", "name" + numNext);
			document.getElementById("name" + numCount).id = "name" + numNext;
		document.getElementById("info" + numCount).setAttribute("name", "info" + numNext);
			document.getElementById("info" + numCount).id = "info" + numNext;
		document.getElementById("href" + numCount).setAttribute("name", "href" + numNext);
			document.getElementById("href" + numCount).id = "href" + numNext;
		document.getElementById("path" + numCount).setAttribute("name", "path" + numNext);
			document.getElementById("path" + numCount).id = "path" + numNext;
		document.getElementById("bin" + numCount).setAttribute("name", "bin" + numNext);
			document.getElementById("bin" + numCount).id = "bin" + numNext;
		document.getElementById("args" + numCount).setAttribute("name", "args" + numNext);
			document.getElementById("args" + numCount).id = "args" + numNext;
		document.getElementById("wait" + numCount).setAttribute("name", "wait" + numNext);
			document.getElementById("wait" + numCount).id = "wait" + numNext;
		document.getElementById("img" + numCount).setAttribute("name", "img" + numNext);
			document.getElementById("img" + numCount).id = "img" + numNext;
		
		// Col 3
		var O__updatedActionsCol = "<a href=\"javascript:O__menuItemRemove(" + numNext + ");\" onclick=\"\">Delete</a> | ";
		O__updatedActionsCol += "<a href=\"javascript:O__menuItemMove(" + numNext + ",'d');\">&#9660;</a> | ";
		O__updatedActionsCol += "<a href=\"javascript:O__menuItemMove(" + numNext + ",'u');\">&#9650;</a>";
		document.getElementById("O__menuItemsTable").rows[numCount+1].cells[2].innerHTML = O__updatedActionsCol;
		
		numCount--;
	}
	
	// Next, insert the new item row
	var O__newRow = O__menuItemsTable.insertRow(1);
	
		// Create: Col 1
		var O__newCellNum = O__newRow.insertCell(0);
		O__newCellNum.innerHTML = "<center><span id=\"numLabel0\">0</span><br><img id=\"imgPreview0\" src=\"/files/images/OutSCIRT_600x600.jpg\" width=\"50\" height=\"50\"></center>";
	
		// Create: Col 2
		var O__newCellItem = O__newRow.insertCell(1);
		O__itemCol = "<table width=\"100%\"><tr><td colspan=\"2\"><b>Basic Details</b></td></tr>";
		O__itemCol += "<tr><td>Title:</td><td><input type=\"text\" id=\"name0\" name=\"name0\" value=\"Insert Title\"></td></tr>";
		O__itemCol += "<tr><td>Description:</td><td><input type=\"text\" id=\"info0\" name=\"info0\" value=\"Please provide a description.\"></td></tr>";
		O__itemCol += "<tr><td>Image Path:</td><td><input type=\"text\" id=\"img0\" name=\"img0\" value=\"/files/images/OutSCIRT_600x600.jpg\"></td></tr>";
		O__itemCol += "<tr><td>Active:</td><td><input type=\"checkbox\" id=\"active0\" name=\"active0\" value=\"1\" checked=\"checked\"></td></tr></table><br><br>";
		O__itemCol += "<table width=\"100%\"><tr><td colspan=\"2\"><b>Web Link Options</b></td></tr>";
		O__itemCol += "<tr><td>Link:</td><td><input type=\"text\" id=\"href0\" name=\"href0\" value=\"/\"></td></tr></table><br><br>";
		O__itemCol += "<table width=\"100%\"><tr><td colspan=\"2\"><b>Program Launcher Options</b></td></tr>";
		O__itemCol += "<tr><td>Directory Path:</td><td><input type=\"text\" id=\"path0\" name=\"path0\" value=\"\"></td></tr>";
		O__itemCol += "<tr><td>Executable:</td><td><input type=\"text\" id=\"bin0\" name=\"bin0\"value=\"\"></td></tr>";
		O__itemCol += "<tr><td>Options:</td><td><input type=\"text\" id=\"args0\" name=\"args0\" value=\"\"></td></tr>"
		O__itemCol += "<tr><td>Launch Waitime:</td><td><input type=\"text\" id=\"wait0\" name=\"wait0\" value=\"\"></td></tr></table>";
		O__newCellItem.innerHTML = O__itemCol;
		
		// Create: Col 3
		var O__newCellActions = O__newRow.insertCell(2);
		var O__actionsCol = "<a href=\"javascript:O__menuItemRemove(0);\" onclick=\"\">Delete</a> | ";
		O__actionsCol += "<a href=\"javascript:O__menuItemMove(0,'d');\">&#9660;</a> | ";
		O__actionsCol += "<a href=\"javascript:O__menuItemMove(0,'u');\">&#9650;</a>";
		O__newCellActions.innerHTML = O__actionsCol;
		
	//Finally, increase total menu count
	O__menuItemCount++;
}

function O__menuItemRemove(num){
	// First, remove the row
	document.getElementById("O__menuItemsTable").deleteRow(num+1);
	
	// Next, close the number gap by moving in the existing IDs
	var numCount = num+1;
	var O__menuItemsTable = document.getElementById("O__menuItemsTable");

	while(numCount<O__menuItemCount){
		var numNext = numCount-1;
	
		// Col 1
		document.getElementById("numLabel" + numCount).innerHTML = numNext;
		document.getElementById("numLabel" + numCount).setAttribute("name", "numLabel" + numNext);
			document.getElementById("numLabel" + numCount).id = "numLabel" + numNext;
		document.getElementById("imgPreview" + numCount).setAttribute("name", "imgPreview" + numNext);
			document.getElementById("imgPreview" + numCount).id = "imgPreview" + numNext;
		
		// Col 2
		document.getElementById("active" + numCount).setAttribute("name", "active" + numNext);
			document.getElementById("active" + numCount).id = "active" + numNext;
		document.getElementById("name" + numCount).setAttribute("name", "name" + numNext);
			document.getElementById("name" + numCount).id = "name" + numNext;
		document.getElementById("info" + numCount).setAttribute("name", "info" + numNext);
			document.getElementById("info" + numCount).id = "info" + numNext;
		document.getElementById("href" + numCount).setAttribute("name", "href" + numNext);
			document.getElementById("href" + numCount).id = "href" + numNext;
		document.getElementById("path" + numCount).setAttribute("name", "path" + numNext);
			document.getElementById("path" + numCount).id = "path" + numNext;
		document.getElementById("bin" + numCount).setAttribute("name", "bin" + numNext);
			document.getElementById("bin" + numCount).id = "bin" + numNext;
		document.getElementById("args" + numCount).setAttribute("name", "args" + numNext);
			document.getElementById("args" + numCount).id = "args" + numNext;
		document.getElementById("wait" + numCount).setAttribute("name", "wait" + numNext);
			document.getElementById("wait" + numCount).id = "wait" + numNext;
		document.getElementById("img" + numCount).setAttribute("name", "img" + numNext);
			document.getElementById("img" + numCount).id = "img" + numNext;
		
		// Col 3
		var O__updatedActionsCol = "<a href=\"javascript:O__menuItemRemove(" + numNext + ");\" onclick=\"\">Delete</a> | ";
		O__updatedActionsCol += "<a href=\"javascript:O__menuItemMove(" + numNext + ",'d');\">&#9660;</a> | ";
		O__updatedActionsCol += "<a href=\"javascript:O__menuItemMove(" + numNext + ",'u');\">&#9650;</a>";
		document.getElementById("O__menuItemsTable").rows[numCount].cells[2].innerHTML = O__updatedActionsCol;
		
		numCount++;
	}
		
	//Finally, subtract total menu count
	O__menuItemCount--;
}

function O__operation(){
	var O__form = document.forms["O__menuItemsForm"];
	O__form.O__menuItemCountFinal.value = O__menuItemCount;
	O__form.submit();
}

</script>
