<?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__pathData = $O__DBpath . "/pageTypes/" . $O__PT . "/data";
$O__pathHeads = $O__DBpath . "/pageTypes/" . $O__PT . "/heads";
$O__pathAreas = $O__DBpath . "/pageTypes/" . $O__PT . "/areas";
unset($O__fileData);

$O__PTATmsg = "";

function O__operation(){
	global $O__DBpath, $O__PTATmsg, $O__logNotes, $O__pathData, $O__pathHeads, $O__pathAreas;
	
	$O__operation = $_POST["O__operation"];
	$O__target = $_POST["O__target"];
	$O__source = $_POST["O__source"];
	$O__sysMsgValue = $O__source;

	switch($O__operation){
		case "create":			
			
			// Check if file exists
			if(file_exists($O__pathData . "/" . $O__source. ".xml")){					
				// Set system message & log
				$O__sysMsgHeadline = "Create Blog";
				$O__sysMsgBody = "Failed. Same name already exists.  Did not create '" . $O__sysMsgValue . "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			} else {				
				// Create new
				$O__fileData = new DomDocument("1.0", "UTF-8");
				$O__fileInfo = $O__fileData->createElement("blog", "");
					$O__fileInfo->appendChild($O__fileData->createElement("title", $O__source));
					$O__fileInfo->appendChild($O__fileData->createElement("URLtitle", $O__source));
					$O__fileData->appendChild($O__fileInfo); // add tree to root file	
				O__XMLdata($O__pathData . "/" . $O__source . ".xml", $O__fileData, 0);
				unset($O__fileData);
			
				// Set system message & log
				$O__sysMsgHeadline = "Create Blog";
				$O__sysMsgBody = "Success.  Created '" . $O__sysMsgValue . "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			}
			
			break;
			
		case "rename":
			// Check if exists
			if(file_exists($O__pathData . "/" . $O__source. ".xml")){					
				// Set system message & log
				$O__sysMsgHeadline = "Rename URL";
				$O__sysMsgBody = "Failed.  One with the same name exists.  Did not rename URL '" . $O__sysMsgValue . "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgAlert");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			} else {
				// backup old source
				O__backupData($O__pathData . "/" . $O__target . ".xml",0);				
				
				// load source data					
				$O__fileData = O__XMLdata($O__pathData . "/" . $O__target . ".xml", 0, 0);
				
				// rename head
				if(file_exists($O__pathHeads . "/" . $O__target . ".php")){ // if published
					rename($O__pathHeads . "/" . $O__target . ".php", $O__pathHeads . "/" . $O__source . ".php"); // head file
				}
				
				// rename areas
				if(file_exists($O__pathAreas . "/" . $O__target . ".php")){ // if published
					rename($O__pathAreas . "/" . $O__target . ".php", $O__pathAreas . "/" . $O__source . ".php"); // area file
				}
									
				// rename target data name to source data name
				rename($O__pathData . "/" . $O__target . ".xml", $O__pathData . "/" . $O__source . ".xml");
				
				// update source data
				$O__fileData->URLtitle = $O__source;
				O__XMLdata($O__pathData . "/" . $O__source . ".xml", $O__fileData, 0);

				// cleanup
				unset($O__fileData);
				clearstatcache(); // clear cache so newest version can be loaded		
	
				// Set system message & log
				$O__sysMsgHeadline = "Rename URL";
				$O__sysMsgBody = "Success.  Renamed '" . $O__target . "' to '" . $O__source . "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			}
			
			break;
			
		case "delete":
			if(file_exists($O__pathData . "/" . $O__target. ".xml")){
				// backup source data				
				O__backupData($O__pathData . "/" . $O__target . ".xml",1);
							
				// delete published head
				if (file_exists($O__pathHeads . "/" . $O__target . ".php")){ unlink($O__pathHeads . "/" . $O__target . ".php"); }
				
				// delete published area
				if (file_exists($O__pathAreas . "/" . $O__target . ".php")){ unlink($O__pathAreas . "/" . $O__target . ".php"); }
	
				//$O__sysMsgValue = $O__target;
				
				$O__sysMsgHeadline = "Delete Page";
				$O__sysMsgBody = "Success.  Deleted '" . $O__target. "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			} else {
				$O__sysMsgHeadline = "Delete Page";
				$O__sysMsgBody = "Failed.  Could not find '" . $O__target. "'.";
				$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
				$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
			}		

			break;
	}
	
}

function O__createOutput(){
	global $O__PT, $O__AT, $O__PTATmsg, $O__pathData;
	
	$O__output = "<h2>Blogs</h2>";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__formManage\" 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__operation\" value=\"\">";
	$O__output .= "</form>";
	
	$O__output .= "<p><b>Create:</b> ";
	$O__output .= "<a href=\"#\" onclick=\"javascript:O__operation('create','');\">New</a></p>";

	$O__colLabels = array("Blog","URL Name","Published","Display Date","Last Updated","Action");
	$O__filesArray = O__dirToArray($O__pathData);
	sort($O__filesArray);
	
	if (count($O__filesArray)){
		for($i=0;$i<count($O__filesArray);$i++){
			$O__fileName = substr($O__filesArray[$i][0],0,-4);
			$O__fileData = O__XMLdata($O__pathData . "/" . $O__fileName . ".xml", 0, 0);
			$O__published = $O__fileData->published;

			if($O__published == 1){
				$O__data[$i][0] = "<a href=\"/" . $O__PT . "/" . $O__fileName . "/display\" target=\"_new\">" . $O__fileData->title . "</a>";
				$O__data[$i][1] = "<a href=\"/" . $O__PT . "/" . $O__fileName . "/display\" target=\"_new\">" . $O__fileData->URLtitle . "</a>";
				$O__data[$i][2] = "Yes";
			} else {
				$O__data[$i][0] = $O__fileData->title;
				$O__data[$i][1] = $O__fileData->URLtitle;
				$O__data[$i][2] = "No";
			}
			
			if($O__fileData->date->month){
				$O__data[$i][3] = date("F", mktime(0, 0, 0, (int)$O__fileData->date->month)) . " ";
				if ($O__fileData->date->day != 0){ $O__data[$i][3] .= $O__fileData->date->day . ", "; }
				$O__data[$i][3] .= $O__fileData->date->year;
			} else {
				$O__data[$i][3] = "Not defined.";
			}
			
			$O__data[$i][4] = date("F d, Y H:i:s", filemtime($O__pathData . "/" . $O__filesArray[$i][0]));
			$O__data[$i][5] = "<a href=\"/" . $O__PT . "/" . $O__fileName . "/edit\">Edit</a> | ";
			$O__data[$i][5] .= "<a href=\"#\" onclick=\"javascript:O__operation('rename','" .  $O__fileName ."');\">Rename URL</a> | ";
			$O__data[$i][5] .= "<a href=\"#\" onclick=\"javascript:O__operation('delete','" .  $O__fileName ."');\">Delete</a>";
		}
		unset($O__fileData);
	} else {
		$O__data[0][0] = "None found.";
		$O__data[0][1] = "";
		$O__data[0][2] = "";
		$O__data[0][3] = "";
		$O__data[0][4] = "";
		$O__data[0][5] = "";
	}
	
	
	$O__output .= O__tableMaker($O__colLabels,$O__data,"O__table","O__tableMenus",0);
	$O__output .= "<br>";
	unset($O__data);

	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__formManage"];
	O__form.O__target.value = O__target;
	
	switch (O__action){
		case "create":
			var O__input = prompt("Please enter a title.");
			if (O__input == ""){
				alert("Please enter a name.");
			} else if (O__input != null){
				O__form.O__source.value = O__input.replace(/ /g, "-");
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;

		case "rename":
			var O__input = prompt("The current URL name is '" + O__target + "'.  Please enter a URL name.");
			if (O__input == ""){
				alert("Please enter a URL name."); 
			} else if (O__input != null){
				O__form.O__source.value = O__input;
				O__form.O__target.value = O__target;
				O__form.O__operation.value = O__action;
				O__form.submit();
			}
			break;
			
		case "delete":
			var O__input = confirm("Are you sure you want to DELETE '" + 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>
