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

$O__userIDtarget  = $O__URLpath[1];
$O__output = "";

function O__operation(){
	global $O__DBpath, $O__usersDataPath, $O__userIDtarget, $O__PTATmsg, $O__logNotes;
	if (isset($_POST["O__name"])){ $O__name = $_POST["O__name"]; }
	$O__target = $_POST["O__target"];
	$O__source = $_POST["O__source"];
	$O__operation = $_POST["O__operation"];
	
	switch($O__operation){			
		case "pageTypeAdd":
			$O__fileData = O__XMLdata($O__usersDataPath . "groups/" . $O__userIDtarget . ".xml", 0, 0);
			
			// Add new permission
			$O__permissionAdd = $O__fileData->permissions->addChild("pageType");
				$O__permissionAdd->addChild("name",$O__source);
				$O__permissionAdd->addChild("actions","none");
			$O__fileData = $O__fileData->asXML();
			O__dataWrite($O__usersDataPath . "groups/" . $O__userIDtarget . ".xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
					
			$O__sysMsgHeadline = "PageType Add";
			$O__sysMsgBody = "Success. '" . $O__source . "' pagetype has been added.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "pageTypeRemove":
			$O__fileData = O__XMLdata($O__usersDataPath . "groups/" . $O__userIDtarget . ".xml", 0, 0);
			
			for ($j=0; $j<count($O__fileData->permissions->pageType); $j++){
				if($O__fileData->permissions->pageType[$j]->name == $O__target){
					unset($O__fileData->permissions->pageType[$j]);
					break;
				}
			}
			
			$O__fileData = $O__fileData->asXML();			
			O__dataWrite($O__usersDataPath . "groups/" . $O__userIDtarget . ".xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
					
			$O__sysMsgHeadline = "PageType Remove";
			$O__sysMsgBody = "Success. '" . $O__target . "' pagetype has been removed.";
			$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
			$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			break;
			
		case "pageTypeUpdate":
			
			$O__fileData = O__XMLdata($O__usersDataPath . "groups/" . $O__userIDtarget . ".xml", 0, 0);
			
			for ($j=0; $j<count($O__fileData->permissions->pageType); $j++){
				if($O__fileData->permissions->pageType[$j]->name == $O__target){
					$O__fileData->permissions->pageType[$j]->actions = $O__source;
					break;
				}
			}
			
			$O__fileData = $O__fileData->asXML();			
			O__dataWrite($O__usersDataPath . "groups/" . $O__userIDtarget . ".xml",$O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
			
			$O__sysMsgHeadline = "PageType Actions Update";
			$O__sysMsgBody = "Success. Actions for '" . $O__target . "' pagetype 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__usersDataPath, $O__userIDtarget, $O__PT, $O__AT, $O__PTATmsg;

	$O__fileData = O__XMLdata($O__usersDataPath . "groups/" . $O__userIDtarget . ".xml", 0, 0);
		
	$O__output = "<h2>Users & Groups Manager</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<b>Now editing group:</b> '" . $O__userIDtarget . "'";
	$O__output .= "<form name=\"O__userEdit\" action=\"/" . $O__PT . "/" . $O__userIDtarget . "/" . $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__operation\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__userIDtarget\" value=\"" . $O__userIDtarget . "\">";
	
	// Permissions
	$O__output .= "<br><b>Add:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('pageTypeAdd');\">PageType</a>";
	$O__output .= "<br><br>";
	for ($i=0; $i<count($O__fileData->permissions->pageType); $i++){
		$O__data[$i][0] = $O__fileData->permissions->pageType[$i]->name;
		$O__data[$i][1] = "<input type=\"text\" name=\"" . $O__fileData->permissions->pageType[$i]->name . "-actions\" id=\"" . $O__fileData->permissions->pageType[$i]->name . "-actions\" value=\"" . $O__fileData->permissions->pageType[$i]->actions . "\">";
		$O__data[$i][2] = "<a href=\"#\" onclick=\"javascript:O__operation('pageTypeUpdate','" . $O__fileData->permissions->pageType[$i]->name . "');\">Update</a> | ";
		$O__data[$i][2] .= "<a href=\"#\" onclick=\"javascript:O__operation('pageTypeRemove','" . $O__fileData->permissions->pageType[$i]->name . "');\">Delete</a>";
	}
	
	$O__colLabels = array("PageType","Actions","Action");
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tablePageTypes",0);
	//$O__output .="<input type=\"button\" value=\"Update PageTypes\" onclick=\"O__operation('pageTypesUpdate');\">";
	$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){
	var O__form = document.forms["O__userEdit"];
	O__form.O__target.value = O__target;
	
	switch (O__action){
		case "pageTypeAdd":
			var O__input = prompt("Please enter a PageType.");
			if (O__input == ""){
				alert("You need to enter a name for a PageType.  Please try again.");
			} else if (O__input != null){
				O__form.O__source.value = O__input;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
		case "pageTypeUpdate":
			var O__input = confirm("Are you sure you want to update permissions for the PageType '" + O__target + "'?");
			if (O__input == 1){
				O__actionsNew = O__target + "-actions";
				O__form.O__source.value = document.getElementById(O__actionsNew).value;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
		case "pageTypeRemove":
			var O__input = confirm("Are you sure you want to remove permissions for the PageType '" + O__target + "'?");
			if (O__input == 1){
				O__form.O__target.value = O__target;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;	
	}

}
</script>
