<?php
$page_title = "Pages on your site List";
include('includes/header.php');
// error_reporting(E_ALL);
// ini_set('display_errors', 1);
?>
<script src="../assets/scripts/functions.js"></script>
<?php
		
/* Function menu_showNested
 * @desc Create inifinity loop for nested list from database
 * @return echo string
*/
function menu_showNested($parentID) 
{
	global $dbc;
	$sql = "SELECT * FROM tblMenuItems inner join tblContentNavigationLink on tblMenuItems.intMenuItemID = tblContentNavigationLink.intMenuItemID WHERE intParentMenuID='$parentID' AND tblMenuItems.ynDeleted = 0 AND tblContentNavigationLink.ynDeleted = 0 ORDER BY intOrderNum";
	$result = mysqli_query($dbc, $sql) or mysqli_error($dbc);
	$numRows = mysqli_num_rows($result);
	
	if ($numRows > 0) 
	{
		echo "\n";
		echo "<ol class='dd-list'>\n";
		while($row = mysqli_fetch_array($result)) 
		{
			echo "\n";
				
			echo "<li class='dd-item' data-id='{$row['intMenuItemID']}'>\n";
			echo "<div class='dd-handle'><span  class='moveArrow'></span>".$row['strMenuItem']."</div>";
			echo "<div class='dd-bkg'>";
			// echo '<a href="edit-content.php?c='.$row['intContentID'].'">Edit:'.$row['strMenuItem'].'</a></div>';
			echo '<a href="edit-content-user.php?c='.$row['intContentID'].'">Edit Content</a></div>';
			echo '<span class="li_button">
					<a title="Edit Menu" href="edit-menu.php?id='.$row['intMenuItemID'].'">
						<button class="edit fg-button-option">
							<span class="ui-icon ui-icon-pencil"></span>
						</button>
					</a>
					<a onclick="return confirm(\'Are you sure you want to remove this menu?\');" title="Delete Menu" href="delete-menu.php?i='.$row['intMenuItemID'].'">
						<button class="delete fg-button-option">
							<span class="ui-icon ui-icon-trash"></span>
						</button>
					</a>
				</span>';
				
					// Run this function again (it would stop running when the mysql_num_result is 0
					menu_showNested($row['intMenuItemID']);
				
			echo "</li>\n";
		}
		echo "</ol>\n";
	}
}
		
// mysqli_free_result($result);
		
		
	
## Show the top parent elements from DB
######################################
$sql = "SELECT *, tblMenuItems.intMenuItemID as menuID FROM tblMenuItems left join tblContentNavigationLink on tblMenuItems.intMenuItemID = tblContentNavigationLink.intMenuItemID 
	WHERE intParentMenuID=0 AND tblMenuItems.ynDeleted = 0
ORDER BY intOrderNum";
// echo $sql;
$result = mysqli_query($dbc, $sql) or die(mysqli_error($dbc));
$numRows = mysqli_num_rows($result);

echo '<div id="listing">';
echo '<h1>Pages on your Site</h1>
		<div class="row">
			<a href="add-menu.php" title = "Create a New Menu Item and Page" class="buttonsLINK">ADD NEW</a>
			<a href="exportdb.php?p=c" title="Export the Information to a Spreadsheet" class="buttonsLINK">EXPORT CSV</a>
		</div>
	 	<p class="row pad10TB"><strong>Menu Item</strong>
		<strong>Options</strong></p>';
echo "<div class='cf nestable-lists'>\n";
echo "<div class='dd' id='nestableMenu'>\n\n";
echo "<ol class='dd-list'>\n";
		
while($row = mysqli_fetch_array($result)) 
{
	$contentID = $row['intContentID'];
	if ($contentID == "")
	{
		$contentID = '0';
	}

	$menuID =  $row['menuID'];

	echo "\n";
	echo "<li class='dd-item' data-id='{$row['intMenuItemID']}'>";
	echo "<div class='dd-handle' ><span  class='moveArrow'></span> ".$row['strMenuItem']."</div>";
	echo "<div class='dd-bkg'>";
	if ($contentID == 0)
	{
		echo '<a href="add-content-user.php?id='.$menuID.'&c=1">Add Content</a>';
	}
	else
	{
		echo '<a href="edit-content-user.php?c='.$contentID.'">Edit Content</a>';
	}
	echo '</div>';
	echo '<span class="li_button">
			<a title="Edit Menu Item" href="edit-menu.php?id='.$menuID.'">
				<button class="edit fg-button-option">
					<span class="ui-icon ui-icon-pencil"></span>
				</button>
			</a>
			<a onclick="return confirm(\'Are you sure you want to remove this menu?\');" title="Delete Menu Item" href="delete-menu.php?i='.$menuID.'">
				<button class="delete fg-button-option">
					<span class="ui-icon ui-icon-trash"></span>
				</button>
			</a>
		</span>';
				
			
	menu_showNested($menuID);
				
	echo "</li>\n";
}
			
echo "</ol>\n\n";
echo "</div>\n";
echo "</div>\n\n";

// mysqli_free_result($result);

		
// Feedback div for update hierarchy to DB
// IMPORTANT: This needs to be here! But you can remove the style
echo "<div id='sortDBfeedback' style='border:1px solid #eaeaea; padding:10px; margin:15px;'></div>\n";


// Script output for debuug
//echo "<textarea id='nestableMenu-output'></textarea>";

?>

<script src="../assets/scripts/jquery.nestable.js"></script>
<script>

$(document).ready(function()
{
	
	/* The output is ment to update the nestableMenu-output textarea
	 * So this could probably be rewritten a bit to only run the menu_updatesort function onchange
	*/
	
	var updateOutput = function(e)
	{
		var list   = e.length ? e : $(e.target),
			output = list.data('output');
		if (window.JSON) {
			output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
			menu_updatesort(window.JSON.stringify(list.nestable('serialize')));
		} else {
			output.val('JSON browser support required for this demo.');
		}
	};
	
	// activate Nestable for list menu
	$('#nestableMenu').nestable({
		group: 1
	})
	.on('change', updateOutput);

	
	
	// output initial serialised data
	updateOutput($('#nestableMenu').data('output', $('#nestableMenu-output')));

	$('#nestable-menu').on('click', function(e)
	{
		var target = $(e.target),
			action = target.data('action');
		if (action === 'expand-all') {
			$('.dd').nestable('expandAll');
		}
		if (action === 'collapse-all') {
			$('.dd').nestable('collapseAll');
		}
	});

	$('#nestable3').nestable();

});
</script>

</div>
<?php
	include('includes/footer.php');
?>
