<?php
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/list/page_templates.php - version 1.0
*/

$nSecureMe=1;

// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");

// bring in other required files
require("../../inc/config.php");
require("../inc/lang.php");
include("../inc/pageutil.php");
include("../inc/paginator.php");
include("../inc/calendar.php");
require("../inc/imgedit_gd.php");

$nShowPerPg = 999;

// Fix for "lost" pages: in sections that don't exist
nFixLostPages();

// if limited user, get page access to apply limitations
if(!$nAdminAccess){ $nPageIDAccessARRAY = nGetPageIDAccessARRAY($ValidUser); }

// Get passed-in vars
if(isset($_GET["pg"])){ $nPage = $_GET["pg"]; } else { $nPage = ""; }
if(!$nPage && isset($_POST["pg"])){ $nPage = $_POST["pg"]; }
if(isset($_GET["srt"])){ $nSort = $_GET["srt"]; } else { $nSort = ""; }
if(isset($_GET["ord"])){ $nSortOrder = $_GET["ord"]; } else { $nSortOrder = ""; }
if(isset($_GET["spp"])){ $nShowPerPgOverride = $_GET["spp"]; } else { $nShowPerPgOverride = ""; }
if($nShowPerPgOverride){ $nShowPerPg = $nShowPerPgOverride; }

// Setup SQL query for displayed listing
if(!$nSort){ $nSort="nName"; }  // default sort
if(!$nSortOrder){ $nSortOrder="ASC"; }  // default sort order
$nWhereARRAY[] = "nName='Page Templates'";

$nWhereSQL = "";
if(isset($nWhereARRAY)){ $nWhereSQL = "WHERE ".implode(" AND ",$nWhereARRAY); } else { $nWhereSQL = ""; }
$nOrderBySQL = "ORDER BY nParentID,$nSort $nSortOrder";
$nListingSQL = "SELECT * From nSection $nWhereSQL $nOrderBySQL";
?>
<div id="listContent" style="padding-top:0px;">
<?php
$nPassThru = "";
$nDivToUpdate = "ajaxPages";
$nPageInDiv = "list/page_templates.php";
$nCurrQueryStr = $_SERVER['QUERY_STRING'];
if($nCurrQueryStr){ $nFullPageInDiv = $nPageInDiv."?".$nCurrQueryStr; } else { $nFullPageInDiv = $nPageInDiv; }
$nPagingText = nGetPaging($nListingSQL,$nPage,$nShowPerPg,$nPassThru,$nDivToUpdate,$nPageInDiv);
if($nPassThru){ $nPassThruQry="?".$nPassThru; $nPassThru="&".$nPassThru; }
// for extrenal refresh requests
print "<script>refreshPages = function(){ loadContent('".$nDivToUpdate."','".$nFullPageInDiv."'); }</script>";
?>

<div id="pageDD">
	<?php echo nGetSectionList();?>
</div>

<style>
.ui-state-highlight{ background:#fff url('assets/images/icons/icon_placeholder.gif') repeat-x top center; float:left; display:block; width:64px: height:71px; position: relative; margin: 0px 10px 10px 0px; }
</style>
<script type="text/javascript">
// Handle sorting and order saving
$("<?php if($nSortableARRAY){ echo implode(",",$nSortableARRAY); } ?>").sortable({
	distance: 20,
	items: "div.pageIcon", // only icons are sortable
	connectWith: ".groupDD", // allow icons to be sorted into different sections
	placeholder: "ui-state-highlight",
	forcePlaceholderSize: true,
	update: function(event, ui) {
		var dataString = "group=" + $(this).attr("id") + "&order=" + $(this).sortable('toArray').toString();
		// save updated order
		var url = "process/page_order.php";
		$.ajax({  
			type: "POST", url: url, data: dataString,  
			error: function(msg,msg2,errorMsg){ alert('error returned in call to '+url+': '+errorMsg); },
			success: function(msg){ }
		});
		$(this).effect("highlight", {}, 1500);
	}
});
</script>


<?php
function nGetSectionList(){
	global $DBName,$Link,$nListingSQL,$nAdminAccess;
	global $nShowPerPg,$nTotalRec,$nStartRec,$nEndRec,$nTotalPg,$nSortableARRAY;
	if(!$nListingSQL){ return ""; }
	$nCurrRec = 0;
	$nTable = "";
	mysql_select_db($DBName); $Result = mysql_query($nListingSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCurrRec++;
		if($nCurrRec>=$nStartRec && $nCurrRec<=$nEndRec && $nCurrRec<=$nTotalRec){
			$nID = $Row["nID"];
			$nName = $Row["nName"];
			$nParentID = $Row["nParentID"];
			if($nParentID){ $nParentName = nGetSectionName($nParentID); } else { $nParentName = ""; }
			if($nParentName){ $nParentCODE = "<span class=\"caption\">$nParentName &gt;</span> "; } else { $nParentCODE = ""; }
			$nPagesCODE = nGetPages($nID);
			if($nPagesCODE){ $nDropOnEmpty = "dropOnEmpty: false"; } else { $nDropOnEmpty = "dropOnEmpty: true"; }
			if(!$nAdminAccess && !$nPagesCODE){ $nHasSectionAccess = 0; } else { $nHasSectionAccess = 1; }
			if($nHasSectionAccess){
				$nSortableARRAY[] = "#section_$nID"; // add to sortables
				$nTable.= <<<CODE
				<div id="section_$nID" class="groupDD clearfix">
					<h1 class="handle">$nParentCODE$nName</h1>
					$nPagesCODE
				</div>
				<div style="clear:both;"></div>
CODE;
			}
		}
	}
	/*
	if($nAdminAccess){
		// Add Unclassified section if needed
		$nPagesCODE = nGetPages(0);
		if($nPagesCODE){
			$nSortableARRAY[] = "#section_0"; // add to sortables
			$nTable.= <<<CODE
				<div id="section_0" class="groupDD clearfix">
					<h1 class="handle caption"><em>Unclassified</em></h1>
					$nPagesCODE
				</div>
				<div style="clear:both;"></div>
CODE;
		}
	}
	*/
	if(!$nTable){ $nTable = "<tr><td height=\"200\" align=\"center\" class=\"caption\" colspan=\"5\"><em>No data available to display.</em></td></tr>"; }
	return $nTable;
}

function nGetPages($nSectionID){
	global $DBName,$Link,$nAdminAccess,$nPageIDAccessARRAY,$_SERVER;
	if($nSectionID===""){ return ""; }
	$nBrowserType = $_SERVER["HTTP_USER_AGENT"];
	$nIsIE = strpos($nBrowserType," MSIE ");
	$nCurrRec = 0;
	$nTable = "";
	$nSQL = "SELECT * FROM nPage WHERE nSectionID=$nSectionID ORDER BY nOrderID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCurrRec++;
		$nID = $Row["nID"];
		$nNavTitle = $Row["nNavTitle"];
		$nTitle = $Row["nTitle"];
		$nBGColor = $Row["nBGColor"];
		$nBaseColor = $Row["nBaseColor"];
		$nBGImgID = $Row["nBGImgID"];
		$nType = $Row["nType"];
		
		if(!$nBGColor){ $nBGColor = "#007bb6"; } // default blue
		switch($nType){
			case "Homepage": $nAddIcon = "<div id=\"edit\"><img src=\"assets/images/icons/icon_home.png\" width=\"26\" height=\"26\" border=\"0\"></div>"; break;
			case "Search": $nAddIcon = "<div id=\"edit\"><img src=\"assets/images/icons/icon_search.png\" width=\"26\" height=\"26\" border=\"0\"></div>"; break;
			case "Link": $nAddIcon = "<div id=\"edit\"><img src=\"assets/images/icons/icon_link.png\" width=\"26\" height=\"26\" border=\"0\"></div>"; break;
			case "Template": $nAddIcon = "<div id=\"edit\"><img src=\"assets/images/icons/icon_template.png\" width=\"26\" height=\"26\" border=\"0\"></div>"; break;
			default: $nAddIcon = ""; break;
		}
		if($nType!="Homepage" && $nAdminAccess){
			$nAddIcon.="<div id=\"delete\" style=\"display:none;\"><a href=\"dialog/page_delete.php?id=$nID\" title=\"Delete Template\" class=\"modal\"><img src=\"assets/images/icons/icon_delete.png\" alt=\"\" width=\"23\" height=\"23\" border=\"0\" id=\"img_$nID\"></a></div>";
			if($nIsIE!==false){	// if browser is IE...
				$nHoverCODE = "onMouseEnter=\"$('#icon_$nID #delete').show();return false;\" onMouseLeave=\"$('#icon_$nID #delete').hide();return false;\"";
			} else {
				$nHoverCODE = "onMouseOver=\"$('#icon_$nID #delete').show();return false;\" onMouseOut=\"$('#icon_$nID #delete').hide();return false;\"";
			}
		} else {
			$nHoverCODE = "";
		}
		
		$nIcnBGClr = $nBGColor;
		$nIcnTxt = $nNavTitle;
		$nIcnTag = "<div class=\"boxIcon\" style=\"background-color:$nIcnBGClr;\"><p>$nIcnTxt</p></div>";
		
		if(!$nAdminAccess && in_array($nID,$nPageIDAccessARRAY)){ $nHasPageAccess = 1; } else { $nHasPageAccess = 0; }
		if($nAdminAccess){ $nHasPageAccess = 1; }
		if($nHasPageAccess){
			if($nType=="Link"){
				$nIcnTag = "<a href=\"dialog/page_add.php?id=$nID\" title=\"Edit Page Settings\" class=\"modal\">$nIcnTag</a>";
			} else {
				$nIcnTag = "<a href=\"page_edit.php?id=$nID\" title=\"$nIcnTxt\">$nIcnTag</a>";
			}
			$nTable.= <<<CODE
				<div id="icon_$nID" class="pageIcon" $nHoverCODE>$nIcnTag<div id="editStamps" style="width:30px;">$nAddIcon</div></div>
CODE;
		}
	}
	return $nTable;
}

function nGetSectionName($nRecID){
	global $DBName,$Link;
	$nSQL = "SELECT nName,nParentID FROM nSection WHERE nID='$nRecID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nName = $Row["nName"];
		$nParentID = $Row["nParentID"];
	}
	if($nParentID){ $nParentName = nGetSectionName($nParentID)." &gt; "; } else { $nParentName = ""; }
	return "$nParentName$nName";
}

function nFixLostPages(){
	global $DBName,$Link;
	$nSQL = "SELECT nID,nSectionID FROM nPage";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nID"];
		$nSectionID = $Row["nSectionID"];
		if(!nSectionExists($nSectionID)){
			// Set page nSectionID to unclassified section
			$Query = "UPDATE nPage SET nSectionID='0' WHERE nID='$nPageID'";
			mysql_select_db($DBName);
			mysql_query($Query, $Link);
		}
	}
}
function nSectionExists($nSectionID){
	global $DBName,$Link;
	if(!$nSectionID){ return 0; }
	$nSQL = "SELECT nID FROM nSection WHERE nID=$nSectionID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nCount = mysql_num_rows($Result);
	return $nCount;
}
function nGetPageIDAccessARRAY($ValidUser){
	global $DBName,$Link;
	if(!$ValidUser){ return; }
	// Get user ID
	$nSQL = "SELECT nID FROM nEditor WHERE nUser='$ValidUser' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nEditorID = $Row["nID"];
	}
	$nDataARRAY = array();
	$nSQL = "SELECT nPageID FROM xEditorPage WHERE nEditorID=$nEditorID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nPageID"];
		$nDataARRAY[] = $nPageID;
	}
	return $nDataARRAY;
}
?>
<?php require("../../inc/db_close.php"); ?>
