<?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 = "";
$O__blog = $O__URLpath[1];

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

	switch($O__operation){
		case "save":
			$O__published = $_POST["O__published"];
			
			// Set up XML DOM with submitted values
			$O__fileData = new DomDocument("1.0", "UTF-8");
			$O__blogInfo = $O__fileData->createElement("blog", "");
				$O__blogInfo->appendChild($O__fileData->createElement("published", $O__published));
				$O__blogInfo->appendChild($O__fileData->createElement("title", htmlspecialchars($_POST["O__title"])));
				$O__blogInfo->appendChild($O__fileData->createElement("author", htmlspecialchars($_POST["O__author"])));
				$O__fileData->appendChild($O__blogInfo);				

				$O__date = $O__fileData->createElement("date", "");
					$O__date->appendChild($O__fileData->createElement("month", $_POST["O__dateMonth"]));
					$O__date->appendChild($O__fileData->createElement("day", $_POST["O__dateDay"]));
					$O__date->appendChild($O__fileData->createElement("year", $_POST["O__dateYear"]));
					$O__blogInfo->appendChild($O__date);					
				
				$O__blogInfo->appendChild($O__fileData->createElement("summary", htmlspecialchars($_POST["O__summary"])));		
				$O__blogInfo->appendChild($O__fileData->createElement("bodyCopy", htmlspecialchars($_POST["O__bodyCopy"])));
				$O__blogInfo->appendChild($O__fileData->createElement("URLtitle", htmlspecialchars($_POST["O__URLtitle"])));
				$O__blogInfo->appendChild($O__fileData->createElement("tags", $_POST["O__tags"]));
				$O__blogInfo->appendChild($O__fileData->createElement("img", $_POST["O__img"]));
				$O__blogInfo->appendChild($O__fileData->createElement("imgPreview", $_POST["O__imgPreview"]));
				
				$O__metadata = $O__fileData->createElement("metadata", "");
					$O__metadata->appendChild($O__fileData->createElement("title", htmlspecialchars($_POST["O__metadataTitle"])));
					$O__metadata->appendChild($O__fileData->createElement("description", htmlspecialchars($_POST["O__metadataDescription"])));
					$O__metadata->appendChild($O__fileData->createElement("keywords", $_POST["O__metadataKeywords"]));
					$O__blogInfo->appendChild($O__metadata);
				
				$O__opengraph = $O__fileData->createElement("opengraph", "");
					$O__opengraph->appendChild($O__fileData->createElement("title", htmlspecialchars($_POST["O__opengraphTitle"])));
					$O__opengraph->appendChild($O__fileData->createElement("description", htmlspecialchars($_POST["O__opengraphDescription"])));
					$O__opengraph->appendChild($O__fileData->createElement("img", $_POST["O__opengraphImg"]));
					$O__opengraph->appendChild($O__fileData->createElement("URL", htmlspecialchars($_POST["O__opengraphURL"])));
					$O__opengraph->appendChild($O__fileData->createElement("sitename", htmlspecialchars($_POST["O__opengraphSitename"])));
					$O__blogInfo->appendChild($O__opengraph);

			$O__fileData->appendChild($O__blogInfo); // add tree to root file
			
			// Backup old data and save new one
			O__backupData($O__pathData . "/" . $O__blog . ".xml",0);
			O__XMLdata($O__pathData . "/" . $O__blog . ".xml", $O__fileData, 0);
			unset($O__fileData);
			clearstatcache(); // clear cache so newest version can be loaded
			
			// Take steps to publish/unpublish
			if ($O__published == 1) {
				// publish head
				$O__fileData = "<title>" . $_POST["O__metadataTitle"] . "</title>\n";
				$O__fileData .= "<meta name=\"description\" content=\"" . $_POST["O__metadataDescription"] . "\"> \n";
				$O__fileData .= "<meta name=\"keywords\" content=\"" . $_POST["O__metadataKeywords"] . "\">\n";
				$O__fileData .= "<meta property=\"og:title\" content=\"" . $_POST["O__opengraphTitle"] . "\"/>\n";
				$O__fileData .= "<meta property=\"og:description\" content=\"" . $_POST["O__opengraphDescription"] . "\"/>\n";
				$O__fileData .= "<meta property=\"og:image\" content=\"" . $_POST["O__opengraphImg"] . "\"/>\n";
				$O__fileData .= "<meta property=\"og:url\" content=\"" . $_POST["O__opengraphURL"] . "\"/>\n";
				$O__fileData .= "<meta property=\"og:site_name\" content=\"" . $_POST["O__opengraphSitename"] . "\"/>\n";
				O__dataWrite($O__pathHeads . "/" . $O__blog . ".php",$O__fileData);
				unset($O__fileData);

				// publish area
				$O__fileData = "";
				if($_POST["O__img"] != "") { $O__fileData .= "<img src=\"" . $_POST["O__img"] . "\" id=\"O__blogImg\">\n"; }
				$O__fileData .= "<h1>" . $_POST["O__title"] . "</h1>\n";
				if(($_POST["O__dateMonth"] != "") && ($_POST["O__dateDay"] != "") && ($_POST["O__dateYear"] != "")){ $O__fileData .= "<div id=\"O__blogDate\">" . date("F", mktime(0, 0, 0, $_POST["O__dateMonth"])) . " " . $_POST["O__dateDay"] . ", " . $_POST["O__dateYear"] . "</div>\n"; }
				if($_POST["O__author"] != "") { $O__fileData .= "<div id=\"O__blogAuthor\">By " . $_POST["O__author"] . "</div>\n"; }
				$O__fileData .= "<div id=\"O__blogSummary\">" . $_POST["O__summary"] . "</div>\n";
				$O__fileData .= "<div id=\"O__blogBody\">" . $_POST["O__bodyCopy"] . "</div>\n";
				O__dataWrite($O__pathAreas . "/" . $O__blog. ".php",$O__fileData);
				unset($O__fileData);		
				
				$O__PTATmsgAddendum .= " Blog is PUBLISHED.";
			} else if ($O__published == 0) {
				
				//delete head
				if (file_exists($O__pathHeads . "/" . $O__blog . ".php")){ unlink($O__pathHeads . "/" . $O__blog . ".php"); }
				
				// delete area
				if (file_exists($O__pathAreas . "/" . $O__blog . ".php")){ unlink($O__pathAreas . "/" . $O__blog . ".php"); }
				
				$O__PTATmsgAddendum .= " Blog is NOT published.";
			}
			
			break;
	}
	
	// System message and logging
	$O__sysMsgHeadline = "Save Blog";
	$O__sysMsgBody = "Success. '" . $O__blog . "' has been updated. " . $O__PTATmsgAddendum;
	$O__PTATmsg = O__sysMsg($O__sysMsgHeadline, $O__sysMsgBody, "O__sysMsgNeutral");
	$O__logNotes .= $O__sysMsgHeadline . ". " . $O__sysMsgBody;
}

function O__createOutput(){
	global $O__DBpath, $O__PTATmsg, $O__pathData, $O__blog, $O__PT;
	
	// Load source data
	$O__fileData = O__XMLdata($O__pathData . "/" . $O__blog. ".xml", 0, 0);
	$O__published = $O__fileData->published;
	$O__title = $O__fileData->title;
	$O__author = $O__fileData->author;
	$O__dateMonth = $O__fileData->date->month;
	$O__dateDay = $O__fileData->date->day;
	$O__dateYear = $O__fileData->date->year;
	$O__summary = $O__fileData->summary;
	$O__bodyCopy = $O__fileData->bodyCopy;
	$O__tags = $O__fileData->tags;
	$O__URLtitle = $O__fileData->URLtitle;
	$O__img = $O__fileData->img;
	$O__imgPreview = $O__fileData->imgPreview;
	$O__metadataTitle = $O__fileData->metadata->title;
	$O__metadataDescription = $O__fileData->metadata->description;
	$O__metadataKeywords = $O__fileData->metadata->keywords;
	$O__opengraphTitle = $O__fileData->opengraph->title;
	$O__opengraphDescription = $O__fileData->opengraph->description;
	$O__opengraphImg = $O__fileData->opengraph->img;
	$O__opengraphURL = $O__fileData->opengraph->URL;
	$O__opengraphSitename = $O__fileData->opengraph->sitename;
	
	// Create UI output
	$O__output = "<h2>Blogs</h2>";
	$O__output .= "<b>Now editing:</b> '" . $O__blog . "'";
	$O__output .= $O__PTATmsg;
	$O__output .= "<form name=\"O__blogForm\" id=\"O__blogForm\" action=\"/" . $O__PT . "/" . $O__blog . "/edit\" method=\"post\">";
	$O__output .= "<input type=\"hidden\" name=\"O__operation\" value=\"\">";
	$O__output .= "<input type=\"hidden\" name=\"O__URLtitle\" value=\"" . $O__URLtitle ."\">";
	
	$O__output .= "<h2>Edit Blog</h2>";
	
	if (isset($_SERVER["HTTPS"])) { $O__serverProtocol = "https://"; } else { $O__serverProtocol = "http://"; }
	$O__output .= "<p><b>URL</b><br><a href=\"" . $O__serverProtocol . $_SERVER["HTTP_HOST"] . "/" . $O__PT . "/" . $O__blog . "/display\" target=\"_new\">" . $O__serverProtocol . $_SERVER["HTTP_HOST"] . "/" . $O__PT . "/" . $O__blog. "/display</a></p>";
	
	$O__output .= "<p><b>Published:</b><br>";
	$O__output .= "<select name=\"O__published\">";
	$O__output .= "<option value=\"0\"";
		if ($O__published == 0) { $O__output .= " selected"; }	
	$O__output .=  ">No</option>";
	$O__output .= "<option value=\"1\"";
		if ($O__published == 1) { $O__output .= " selected"; }	
	$O__output .= ">Yes</option>";
	$O__output .=  "</select>";

	$O__output .= "<p><b>Title</b><br><input type=\"text\" name=\"O__title\" value=\"" . $O__title . "\"></p>";
	$O__output .= "<p><b>Author</b><br><input type=\"text\" name=\"O__author\" value=\"" . $O__author . "\"></p>";
	
	$O__output .= "<p><b>Date</b><br>";	
		
		 // create month dropdown
		$O__output .= "<select name=\"O__dateMonth\">";
		for($i=1; $i<=12; $i++){
			$O__output .= "<option value=\"" . $i . "\"";
			if($i == $O__dateMonth){ $O__output .= " selected"; }
			$O__output .= ">" . date("F", mktime(0, 0, 0, $i)) . "</option>";
		}
		$O__output .= "</select>";
		
		 // create day dropdown
		$O__output .= "<select name=\"O__dateDay\">";
		$O__output .= "<option value=\"0\">None</option>";
		for($i=1; $i<=31; $i++){
			$O__output .= "<option value=\"" . $i . "\"";
			if($i == $O__dateDay){ $O__output .= " selected"; }
			$O__output .= ">" . $i . "</option>";
		}
		$O__output .= "</select>";
		
		 // create year dropdown
		$O__output .= "<select name=\"O__dateYear\">";
		for($i=(int)date("Y"); $i>=2000; $i--){
			$O__output .= "<option value=\"" . $i . "\"";
			if($i == $O__dateYear){ $O__output .= " selected"; }
			$O__output .= ">" . $i . "</option>";
		}
		$O__output .= "</select>";
	
	$O__output .= "<p><b>Summary</b><br><textarea style=\"width:98%;\" name=\"O__summary\" class=\"O__itemInfo\" >" . $O__summary . "</textarea></p>";
	$O__output .= "<p><b>Body</b><br><textarea style=\"width:98%;\" rows=\"25\" name=\"O__bodyCopy\" id=\"O__bodyCopy\" class=\"O__itemInfo\" >" . $O__bodyCopy . "</textarea></p>";
	
	$O__output .= "<p><b>Tags</b><br><input type=\"text\" name=\"O__tags\" value=\"" . $O__tags . "\"></p>";
	
	$O__output .= "<p><b>Image</b><br><input type=\"text\" name=\"O__img\" value=\"" . $O__img . "\"></p>";
	
	$O__output .= "<p><b>Image Preview</b><br><input type=\"text\" name=\"O__imgPreview\" value=\"" . $O__imgPreview . "\"></p>";
		
	$O__output .= "<h2>Metadata</h2>";
	$O__output .= "<p><b>Title</b><br><input type=\"text\" name=\"O__metadataTitle\" value=\"" . $O__metadataTitle . "\"></p>";
	$O__output .= "<p><b>Description</b><br><input type=\"text\" name=\"O__metadataDescription\" value=\"" . $O__metadataDescription . "\"></p>";
	$O__output .= "<p><b>Keywords</b><br><input type=\"text\" name=\"O__metadataKeywords\" value=\"" . $O__metadataKeywords . "\"></p>";
		
	$O__output .= "<h2>Open Graph</h2>";
	$O__output .= "<p><b>Title</b><br><input type=\"text\" name=\"O__opengraphTitle\" value=\"" . $O__opengraphTitle . "\"></p>";
	$O__output .= "<p><b>Description</b><br><input type=\"text\" name=\"O__opengraphDescription\" value=\"" . $O__opengraphDescription . "\"></p>";
	$O__output .= "<p><b>Image</b><br><input type=\"text\" name=\"O__opengraphImg\" value=\"" . $O__opengraphImg . "\"></p>";
	$O__output .= "<p><b>Blog URL</b><br><input type=\"text\" name=\"O__opengraphURL\" value=\"" . $O__opengraphURL . "\"></p>";
	$O__output .= "<p><b>Site Name</b><br><input type=\"text\" name=\"O__opengraphSitename\" value=\"" . $O__opengraphSitename . "\"></p>";
	
	$O__output .="<a href=\"#\" onclick=\"javascript:O__operation('save')\">Save Blog</a>";	
	$O__output .= "</form>";
	
	return $O__output;
}

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

<script>
function O__operation(O__action){
	var O__form = document.forms["O__blogForm"];
	O__form.O__operation.value = O__action;
	O__form.submit();
}
</script>
