<?php
session_start();
if (!isset($_SESSION['FirstName'])) {
	exit;
}
/*
//trigger email to test script
$to="apicton@tastesystems.com";
$subject="test";
$message="message triggered";
$from = "apicton@apnetworking.com";
$headers = "From:" . $from;
mail($to, $subject, $message, $headers);
*/
include ('includes/db1config.php');
include ('../includes/mysql_connect.php');
// $i = 0;

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

if (!isset($_POST['menuOrder'])) {
	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
		$menuRecordkeys = addNewNavItem($menuItem, null, $menuRecordkeys);
	} else {
		$sql = "UPDATE nav_slider_mobile SET nav_order = '".$i."', parent_page_id = 0, active = 1 WHERE recordkey = '".$menuItem->recordkey."'";
		mysql_query($sql) or die(mysql_error());
		$menuRecordkeys[$menuItem->curPageNum] = $menuItem->recordkey;
	}
	$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 nav_slider_mobile SET parent_page_id = '".$menuItem->pageid."', nav_order = '".$i."', active = 1 WHERE recordkey = '".$menuChild->recordkey."'";
				mysql_query($sql) or die(mysql_error());

				$menuRecordkeys[$menuChild->curPageNum] = $menuChild->recordkey;
			}
			$i++;
		}
	}

}


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

function addNewNavItem($menuItem, $parentPageId = null, $menuRecordkeys) {
	global $i;

	// Get default color theme
	$sql = "SELECT *
			FROM color_themes
			ORDER BY default_option DESC 
			LIMIT 1";
	$result = mysql_query($sql) or die(mysql_error());

	$colorTheme = mysql_fetch_assoc($result);

	if ($menuItem->type == 'page') {
		// is new page

		$pageResult = mysql_query("SELECT pageTitle, alias, 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 nav_slider_mobile (
						button_text, button_url, nav_order, pageid, icon_text, background_color, text_color
					) VALUES (";
						if($page['alias']>""){
							$button_text = mysql_real_escape_string($page['alias']);
						} else {
							$button_text = mysql_real_escape_string($page['pageTitle']);
						}
						
						$sql .= "'".mysql_real_escape_string($button_text)."', 						
						'".mysql_real_escape_string($page['permalink'])."', 
						'".$i."', 
						'".$menuItem->pageid."', 
						'".mysql_real_escape_string($button_text)."', 
						'".mysql_real_escape_string($colorTheme['background_color'])."', 
						'".mysql_real_escape_string($colorTheme['text_color'])."'
					)";
			mysql_query($sql) or die(mysql_error());
			$recordkey = mysql_insert_id();

			if (!empty($parentPageId)) {
				mysql_query("UPDATE nav_slider_mobile SET parent_page_id = '".$parentPageId."' WHERE recordkey = '".$recordkey."'") or die(mysql_error());
			}
			
			$menuRecordkeys[$menuItem->curPageNum] = $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 
				nav_slider_mobile (
					button_text, button_url, nav_order, navdoc, active, custom, icon_text, background_color, text_color
				) VALUES (
					'$button_text',
					'".$menuItem->filename."',
					'".$i."',
					'1',
					'1', 
					'0', 
					'".mysql_real_escape_string($button_text)."', 
					'".mysql_real_escape_string($colorTheme['background_color'])."', 
					'".mysql_real_escape_string($colorTheme['text_color'])."'
				)";
		mysql_query($sql) or die(mysql_error());
		$recordkey = mysql_insert_id();

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

		$menuRecordkeys[$menuItem->curPageNum] = $recordkey;
	} elseif ($menuItem->type == 'social') {
		$sql = "SELECT *
				FROM social_networks
				WHERE recordkey = '".$menuItem->socialid."'";
		$result = mysql_query($sql) or die(mysql_error());

		if (mysql_num_rows($result) > 0) {
			$socialNetwork = mysql_fetch_assoc($result);

			$sql = "INSERT INTO nav_slider_mobile (
						button_text, button_url, nav_order, socialnetworkid, icon_image
					) VALUES (
						'".mysql_real_escape_string($socialNetwork['network_name'])."', 
						'".mysql_real_escape_string($socialNetwork['page_url'])."', 
						'".$i."', 
						'".$menuItem->socialid."', 
						'".mysql_real_escape_string($socialNetwork['icon_name'])."'
					)";
			mysql_query($sql) or die(mysql_error());
			$recordkey = mysql_insert_id();

			if (!empty($parentPageId)) {
				mysql_query("UPDATE nav_slider_mobile SET parent_page_id = '".$parentPageId."' WHERE recordkey = '".$recordkey."'") or die(mysql_error());
			}
			
			$menuRecordkeys[$menuItem->curPageNum] = $recordkey;
		}
	}

	return $menuRecordkeys;
}
?>