<?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_components.php - version 1.01
*/

$nSecureMe=1;

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

// bring in other required files
include("../inc/calendar.php");
require("../../inc/config.php");
require("../inc/lang.php");
require("../../inc/tmplutil.php");
require("../../inc/resize-class.php");

$nShowPerPg = 999;

// Get passed-in vars
$nRecID = (isset($_GET['id']) ? $_GET['id'] : NULL);
if(!$nRecID){ $nRecID = (isset($_POST['id']) ? $_POST['id'] : NULL); }
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; }

// Globals
$nSortableCreateARRAY = array();
$nSortableARRAY = array();
$nOpenerTag="[#"; $nCloserTag="#]"; $nTagParamDelim="_"; $nTagSubParamDelim="=";
?>
<div id="listContent" style="padding-top:0px;">
<?php
$nPassThru = "";
$nDivToUpdate = "ajaxPageEdit";
$nPageInDiv = "list/page_components.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>refreshPageComps = function(){ loadContent('".$nDivToUpdate."','".$nFullPageInDiv."'); }</script>";
?>
	
<div id="pageEditor">
	
	<?php echo nGetPageLayoutAndComps();?>
	
</div>

<style>
.ui-state-highlight{ position:relative; background-color:#ccc; margin:10px; padding:4px 8px 4px 0px; display:block;-webkit-border-radius:4px; -moz-border-radius:4px; border-radius:4px; border:1px solid #fff; padding:1px 2px; }
</style>
<script type="text/javascript">
// Handle sorting and order saving
$("<?php if($nSortableARRAY){ echo implode(",",$nSortableARRAY); } ?>").sortable({
	distance: 20,
	items: "div.component", // only components are sortable
	connectWith: ".pageColumn", // allow components to be sorted into different columns
	placeholder: "ui-state-highlight",
	forcePlaceholderSize: true,
	update: function(event, ui) {
		var dataString = "group=" + $(this).attr("id") + "&order=" + $(this).sortable('toArray').toString() + "&pid=<?php echo $nRecID;?>";
		// save updated order
		var url = "edit/component_order.php";
		$.ajax({  
			type: "POST", url: url, data: dataString,  
			error: function(msg,msg2,errorMsg){ alert('error'); alert('error returned in call to '+url+': '+errorMsg); },
			success: function(msg){  }
		});
		$(this).effect("highlight", {}, 1500);
	}
});
</script>


<?php
function nGetPageLayoutAndComps(){
	global $nOpenerTag,$nCloserTag,$nTagParamDelim,$nTagSubParamDelim,$nThemePath;
	global $nRecID;
	if(!$nRecID){ return; }
	$nCurrentTheme = nGetActiveThemePath();
	$nTmplLayoutPath = "../../tmpl/{$nCurrentTheme}layouts/";
	// Set template file
	$nType = nGetPageType($nRecID);
	$nFileName = "";
	switch($nType){
		case "Homepage": $nFileName = "home.html"; break;
		case "Content": $nFileName = "content.html"; break;
		case "Search": $nFileName = "search.html"; break;
		case "Template": $nFileName = "content.html"; break;
		case "Link": $nFileName = ""; break;
		case "cat": $nFileName = "cat.html"; break;
		case "prod": $nFileName = "prod.html"; break;
		case "grp": $nFileName = "grp.html"; break;
		default: $nFileName = strtolower($nType).".html"; break;
	}
	$nFullTmplLayoutPath = "$nTmplLayoutPath$nFileName";
	if(!$nFileName){
		exit("<div align=\"center\" style=\"padding:20px;\">No layout is available for the <strong style=\"text-transform:uppercase;\">$nType</strong> page type.<BR><BR>Please check your system configuration or switch to a different template.</div>");
	}
	if(file_exists($nFullTmplLayoutPath) && is_file($nFullTmplLayoutPath)){ $nTemplateCode = file_get_contents($nFullTmplLayoutPath); } else {
		exit("<div align=\"center\" style=\"padding:20px;\">A layout for this template does not exist: $nFullTmplLayoutPath.<BR><BR>Please check your system configuration or switch to a different template.</div>");
	}
	// Parse layout template
	if($nTemplateCode){
		do{
			$nOpenerPos = strpos($nTemplateCode,$nOpenerTag);
			$nCloserPos = strpos($nTemplateCode,$nCloserTag,$nOpenerPos);
			if($nOpenerPos!==false && $nCloserPos!==false){
				$nInnerTag = substr($nTemplateCode,($nOpenerPos+strlen($nOpenerTag)),($nCloserPos-strlen($nCloserTag)-$nOpenerPos));
				$nReplacement = "";
				$nInnerTagARRAY = explode($nTagParamDelim,$nInnerTag);
				$nTemplateItem = $nInnerTagARRAY[0];
				unset($nInnerTagARRAY[0]);
				switch($nTemplateItem){
					case "compColumnComps":
						$nReplacement = managerGetColumnComps($nTemplateItem,$nInnerTagARRAY);
						break;
				}
				$nTemplateCode = str_replace(($nOpenerTag.$nInnerTag.$nCloserTag),$nReplacement,$nTemplateCode);
			}
		} while($nOpenerPos!==false);
	}
	return $nTemplateCode;
}

function nGetActiveThemePath(){
	global $DBName,$Link;
	$nSettingsARRAY = array();
	$nSQL = "SELECT * FROM nSettings WHERE nSetting='activeTemplate'";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nActiveTmplPath = $Row["nValue"];
	}
	return $nActiveTmplPath;
}

function managerGetColumnComps($nItem,$nFullParamARRAY=""){
	global $DBName,$Link,$nTagSubParamDelim,$nRecID;
	$nColumnID = nGetParam("COLID",$nFullParamARRAY);
	$nTitle = nGetParam("TITLE",$nFullParamARRAY);
	if(!$nColumnID || !$nRecID){ return; }
	if(!$nTitle){ $nTitle = "Column $nColumnID"; }
	// Get components for this column
	$nColumnComps = nGetColumn($nColumnID,$nRecID);
	$nColumnCode = "<div id=\"pageColumn_$nColumnID\" class=\"pageColumn\"><h6 class=\"handleCompFalse\">$nTitle</h6>$nColumnComps</div><div style=\"clear:both;\"></div>";
	return $nColumnCode;
}

function nGetColumn($nColumnID,$nPageID){
	global $DBName,$Link;
	global $nSortableCreateARRAY, $nSortableARRAY, $nDefaultLang;
	if($nColumnID===""){ return ""; }
	$nCurrRec = 0;
	$nTable = "";
	$nSQL = "SELECT xComponentPage.nID AS xID,nComponentID,nContentID,nOrderID FROM xComponentPage LEFT JOIN nComponent ON nComponentID=nComponent.nCompFile WHERE nComponent.nActive=1 AND nColumnID=$nColumnID AND nPageID=$nPageID ORDER BY nOrderID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCurrRec++;
		$nID = $Row["xID"];
		$nCompID = $Row["nComponentID"];
		$nContentID = $Row["nContentID"];
		$nOrderID = $Row["nOrderID"];
		$nCompName = nGetComponentName($nCompID);
		$nCompTitle = strip_tags(nGetComponentTitle($nContentID));
		$nCompFile = $nCompID;// nGetComponentFile($nCompID);
		$nCompCode = tmplComp("",array("CID=$nContentID","CFILE=$nCompFile","LANG=$nDefaultLang","EDIT=1"));
		$nCompCloneARRAY = nGetComponentCloneARRAY($nID);
		$nCloneAmt = count($nCompCloneARRAY)-1;
		if($nCloneAmt>0 && $nContentID){ $nCompName="$nCompName <a title=\"Cloned on $nCloneAmt other pages.\" style=\"text-decoration:none;\">(clone)</a>"; }
		$nCompName = "<div style=\"padding-left:15px;\"><strong>$nCompName</strong></div>";
		$nTable.= <<<CODE
			<div id="component_$nID" class="component">
				<table width="100%" border="0" cellspacing="0" cellpadding="0" id="componentTable">
				  <tr style="color:#fff;">
					<td id="componentTitle" valign="top" style="padding:2px 0;"><span class="handleComp">$nCompName</span></td>
					<td width="80" align="right" valign="top" style="padding:2px 6px;">
						<a href="javascript:;" onClick="openEditCompDialog($nID);"><img src="assets/images/icons/edit_module.gif" alt="" width="11" height="11" hspace="3" border="0"></a> 
						<a href="dialog/component_duplicate.php?id=$nID" title="Duplicate Component" class="modal"><img src="assets/images/icons/copy.png" alt="" width="14" height="14" hspace="3" border="0"></a>
						<a href="dialog/component_delete.php?id=$nID" title="Delete Component" class="modal"><img src="assets/images/icons/action4.gif" alt="" width="14" height="13" hspace="3" border="0"></a>
					</td>
				  </tr>
				  <tr style="background-color:#fff;">
				  	<td colspan="2"><div style="padding:12px;overflow:hidden;" class="handleComp">$nCompCode</div></td>
				  </tr>
				</table>
			</div>
CODE;
	}
	$nSortableARRAY[] = "#pageColumn_$nColumnID"; // add to sortables
	return $nTable;
}
function nGetComponentTitle($nContentID){
	global $DBName,$Link,$nDefaultLang;
	if(!$nContentID){ return; }
	$nTitle = "";
	$Query = "SELECT nText FROM nTranslate WHERE nItemID='$nContentID' AND nLang='$nDefaultLang' AND nType='comp' AND nOrderID='1'";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nTitle = $Row["nText"];
	}
	return $nTitle;
}
function nGetComponentCloneARRAY($nRecID){
	global $DBName,$Link;
	if(!$nRecID){ return; }
	// Get associated content ID
	$nSQL = "SELECT * FROM xComponentPage WHERE nID=$nRecID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nContentID = $Row["nContentID"];
		$nCompID = $Row["nComponentID"];
	}
	// Get all component/content clones
	$nCloneARRAY = array();
	$nSQL = "SELECT nPageID FROM xComponentPage WHERE nComponentID='$nCompID' AND nContentID='$nContentID'";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCloneARRAY[] = $Row["nPageID"];
	}
	return $nCloneARRAY;
}
function nGetTranslation($nLangCode,$nItemID,$nField,$nType){
	global $DBName,$Link;
	if(!$nLangCode || !$nItemID || !$nField || !$nType){ return; }
	$nTranslation = "";
	$Query = "SELECT $nField AS nField FROM nTranslate WHERE nLang='$nLangCode' AND nItemID='$nItemID' AND nType='$nType'";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	if ($Row = mysql_fetch_array($Result)) {
		$nTranslation = $Row["nField"];
	}
	return $nTranslation;
}
function nConvertIntChars($nText){
	if(!$nText){ return; }
	// lowercase
	$nText = str_replace("ú","&uacute;",$nText);
	$nText = str_replace("í","&iacute;",$nText);
	$nText = str_replace("ó","&oacute;",$nText);
	$nText = str_replace("é","&eacute;",$nText);
	$nText = str_replace("á","&aacute;",$nText);
	$nText = str_replace("ñ","&ntilde;",$nText);
	// uppercase
	$nText = str_replace("Ú","&Uacute;",$nText);
	$nText = str_replace("Í","&Iacute;",$nText);
	$nText = str_replace("Ó","&Oacute;",$nText);
	$nText = str_replace("É","&Eacute;",$nText);
	$nText = str_replace("Á","&Aacute;",$nText);
	$nText = str_replace("Ñ","&Ntilde;",$nText);
	return $nText;
}
?>
<?php require("../../inc/db_close.php"); ?>
