<?php
session_start();
if (!isset($_SESSION['FirstName'])) {
	exit;
}
	 
include ('includes/db1config.php');
include ('../includes/mysql_connect.php'); 

/*
//trigger email to test script
$to="apicton@tastesystems.com";
$subject="test";
$message="save_footlink_order triggered";
$from = "apicton@apnetworking.com";
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
*/

//$query="insert into test_log (field1, field2) values ('save_footlink_order triggered.','') ";
//mysql_query($query);

 // $i = 0;

 // foreach ($_POST['page'] as $value) {
 //    // Execute statement:
 //      $qry="UPDATE footer_links SET sequence = $i WHERE recordkey = $value";
	//  //save qry to test log for troubleshooting
 // 	 $query="insert into test_log (field1, field2) values ('update footer_links','$qry') ";
 // 	 //mysql_query($query);
 // 	 mysql_query($query);
 //     $i++;
 // }

if (!isset($_POST['menuOrder'])) {
	 $query="insert into test_log (field1, field2) values ('Menu order not set.','') ";
	 mysql_query($query);
	die('Menu order not set.');
}

$menuOrder = str_replace('\"', '"', $_POST['menuOrder']);
$menuOrder = json_decode($menuOrder);

$i = 1;
$menuRecordkeys = array();
foreach ($menuOrder as $menuItem) {
	if (isset($menuItem->new) && $menuItem->new == 'true') {
		// new item
		addNewNavItem($menuItem);
	} else {
		$sql = "UPDATE footer_links SET sequence = '".$i."' WHERE recordkey = '".$menuItem->recordkey."'";
		mysql_query($sql) or die(mysql_error());
	}
	$i++;

	// check for child links
	/*
	if (isset($menuItem->children)) {
		foreach ($menuItem->children as $menuChild) {
			if (isset($menuChild->new) && $menuChild->new == 'true') {
				// new item
				addNewNavItem($menuChild, $menuItem->pageid);
			} else {
				$sql = "UPDATE footer_links SET parent_page_id = '".$menuItem->pageid."', sequence = '".$i."', active = 1 WHERE recordkey = '".$menuChild->recordkey."'";
				mysql_query($sql) or die(mysql_error());

				$menuRecordkeys[] = $menuChild->recordkey;
			}
			$i++;
		}
	}
	*/
	$menuRecordkeys[] = $menuItem->recordkey;
}

function addNewNavItem($menuItem, $parentPageId = null) {
	global $menuRecordkeys, $i;
	//$query="insert into test_log (field1, field2) values ('addNewNavItem triggered.','') ";
	// mysql_query($query);
	if ($menuItem->type == 'page') {
		// is new page

		$pageResult = mysql_query("SELECT pageTitle, permalink, pageOrder FROM content WHERE pageID = '".$menuItem->pageid."'") or die(mysql_error());
		if(mysql_num_rows($pageResult) > 0) {
			$page = mysql_fetch_assoc($pageResult);

			$sql = "INSERT INTO footer_links (link_text, url, sequence, pageid)
					VALUES ('".mysql_real_escape_string($page['pageTitle'])."', '".mysql_real_escape_string($page['permalink'])."', '".$i."', '".$menuItem->pageid."')";
			mysql_query($sql) or die(mysql_error());
			$recordkey = mysql_insert_id();

			//if (!empty($parentPageId)) {
			//	mysql_query("UPDATE footer_links SET parent_page_id = '".$parentPageId."' WHERE recordkey = '".$recordkey."'") or die(mysql_error());
			//}
			
			$menuRecordkeys[] = $recordkey;
		}
	} elseif ($menuItem->type == 'doc') {
		// is new pdf
		$button_text = $menuItem->filename;
		$button_text = str_ireplace('.pdf', '', $button_text);
		$button_text = str_replace('_', ' ', $button_text);
		$button_text = strtoupper($button_text);	
		$sql="Insert into footer_links (link_text, url, sequence, doc_link) values ('$button_text','".$menuItem->filename."','".$i."', 1)";
		mysql_query($sql) or die(mysql_error());
		$recordkey = mysql_insert_id();

		//if (!empty($parentPageId)) {
		//	mysql_query("UPDATE footer_links SET parent_page_id = '".$parentPageId."' WHERE recordkey = '".$recordkey."'") or die(mysql_error());
		//}

		$menuRecordkeys[] = $recordkey;
	}
}

// remove any not in array
$sql = "DELETE FROM footer_links WHERE recordkey NOT IN ('".implode("', '", $menuRecordkeys)."')";
// echo $sql;
mysql_query($sql) or die(mysql_error());

echo '1';
?>