<?
// config
$nPluginPath = "../plugins/";

function nGetListPluginInclusions($nListInclusionPoint){
	global $DBName, $Link;
	if(!$nListInclusionPoint){ return; }
	$nPluginListInclARRAY = array();
	$Query = "SELECT * FROM nPluginInclusionPoint WHERE nInclusionType='List' AND nInclusionPoint='$nListInclusionPoint' ORDER BY nID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nPluginARRAY = array();
		$nPluginARRAY["nPluginID"] = $Row["nPluginID"];
		$nPluginARRAY["nPosition"] = $Row["nSetting1"];
		$nPluginARRAY["nColumnTitle"] = $Row["nSetting2"];
		$nPluginARRAY["nComponentFunction"] = $Row["nSetting3"];
		$nPluginListInclARRAY[] = $nPluginARRAY;
	}
	return $nPluginListInclARRAY;
}
function nGetBackOfficePagePluginInclusions($nInclusionPoint,$nLocation="",$nTabID=1){
	global $DBName, $Link;
	if(!$nInclusionPoint){ return; }
	$nPluginListInclARRAY = array();
	if($nLocation){ $nAddtSQL = "AND nSetting1='$nLocation'"; } else { $nAddtSQL = ""; }
	$Query = "SELECT * FROM nPluginInclusionPoint WHERE nInclusionType='BackOfficePage' AND nInclusionPoint='$nInclusionPoint' $nAddtSQL ORDER BY nID";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nPluginARRAY = array();
		$nPluginARRAY["nPluginID"] = $Row["nPluginID"];
		$nPluginARRAY["nPosition"] = $Row["nSetting1"];
		$nPluginARRAY["nTabTitle"] = $Row["nSetting2"];
		$nPluginARRAY["nComponentFunction"] = $Row["nSetting3"];
		$nPluginARRAY["nTabID"] = $nTabID;
		$nPluginListInclARRAY[] = $nPluginARRAY;
		$nTabID++;
	}
	return $nPluginListInclARRAY;
}
function nRunBackOfficePagePlugins($nPluginInclusionPoint,$nLocation=""){
	global $nPluginPath;
	if(!$nPluginInclusionPoint){ return; }
	$nPluginCode = "";
	$nPluginListInclARRAY = nGetBackOfficePagePluginInclusions($nPluginInclusionPoint,$nLocation);
	if($nPluginListInclARRAY){
		foreach($nPluginListInclARRAY AS $nPluginARRAY){
			$nPluginID = $nPluginARRAY["nPluginID"];
			$nComponentFunction = $nPluginARRAY["nComponentFunction"];
			// load plugin
			$nMasterFilePath = "../$nPluginPath$nPluginID/$nPluginID.php";
			if(file_exists("$nMasterFilePath")){
				$nIncludeBH = include_once "$nMasterFilePath"; // send include into variable so it's output isn't viewable, but it's functions and vars are available
				if(function_exists($nPluginID)){ eval("echo ".$nPluginID."();"); }
				if(function_exists($nPluginID."_".$nComponentFunction)){ eval("\$nPluginCode = ".$nPluginID."_".$nComponentFunction."();"); }
			}
		}
	}
	return $nPluginCode;
}
function nInsertPluginTabIncl($nPluginListInclARRAY,$nTabARRAY){
	if(!$nTabARRAY){ return; }
	foreach($nPluginListInclARRAY AS $nPluginARRAY){
		$nPosition = $nPluginARRAY["nPosition"];
		$nTabTitle = $nPluginARRAY["nTabTitle"];
		$nTabID = $nPluginARRAY["nTabID"];
		// if tab title already exists, just replace it
		$nNewItemARRAY = array();
		$nNewItemARRAY["$nTabTitle"] = array($nTabID,$nPosition);
		if(isset($nTabARRAY["$nTabTitle"]) && $nTabARRAY["$nTabTitle"]){
			// don't do anything... we'd just be replacing the title with the same thing
		} else if(isset($nTabARRAY["$nPosition"]) && $nTabARRAY["$nPosition"]){
			// if tab position matches one already in the header, drop in the new header after it
			$nTabTitleARRAY = array_keys($nTabARRAY);
			$nInsertAfter = array_search($nPosition,$nTabTitleARRAY)+1;
			$nFirstPartARRAY = array_slice($nTabARRAY,0,$nInsertAfter);
			$nSecondPartARRAY = array_slice($nTabARRAY,$nInsertAfter);
			$nTabARRAY = array_merge($nFirstPartARRAY,$nNewItemARRAY);
			$nTabARRAY = array_merge($nTabARRAY,$nSecondPartARRAY);
		} else if(isset($nPluginARRAY["$nPosition"]) && !$nPluginARRAY["$nPosition"]){
			// if position is empty, just add it to the front of the array
			$nTabARRAY = array_merge($nNewItemARRAY,$nTabARRAY);
		}
	}
	return $nTabARRAY;
}
function nInsertPluginHdrIncl($nPluginListInclARRAY,$nColumnARRAY){
	if(!$nColumnARRAY){ return; }
	foreach($nPluginListInclARRAY AS $nPluginARRAY){
		$nPosition = $nPluginARRAY["nPosition"];
		$nColumnTitle = $nPluginARRAY["nColumnTitle"];
		// if column title already exists, just replace it
		$nNewItemARRAY = array();
		$nNewItemARRAY["$nColumnTitle"] = array("","","","","");
		if(isset($nColumnARRAY["$nColumnTitle"]) && $nColumnARRAY["$nColumnTitle"]){
			// don't do anything... we'd just be replacing the title with the same thing
		} else if(isset($nColumnARRAY["$nPosition"]) && $nColumnARRAY["$nPosition"]){
			// if column position matches one already in the header, drop in the new header after it
			$nColumnTitleARRAY = array_keys($nColumnARRAY);
			$nInsertAfter = array_search($nPosition,$nColumnTitleARRAY)+1;
			$nFirstPartARRAY = array_slice($nColumnARRAY,0,$nInsertAfter);
			$nSecondPartARRAY = array_slice($nColumnARRAY,$nInsertAfter);
			$nColumnARRAY = array_merge($nFirstPartARRAY,$nNewItemARRAY);
			$nColumnARRAY = array_merge($nColumnARRAY,$nSecondPartARRAY);
		} else if(isset($nPluginARRAY["$nPosition"]) && !$nPluginARRAY["$nPosition"]){
			// if position is empty, just add it to the front of the array
			$nColumnARRAY = array_merge($nNewItemARRAY,$nColumnARRAY);
		}
	}
	return $nColumnARRAY;
}
function nInsertPluginRowIncl($nPluginListInclARRAY,$nColumnARRAY,$nItemID){
	global $nPluginPath;
	
	if(!$nColumnARRAY){ return; }
	foreach($nPluginListInclARRAY AS $nPluginARRAY){
		$nPosition = $nPluginARRAY["nPosition"];
		$nColumnTitle = $nPluginARRAY["nColumnTitle"];
		$nPluginID = $nPluginARRAY["nPluginID"];
		$nComponentFunction = $nPluginARRAY["nComponentFunction"];
		// load plugin
		$nMasterFilePath = "../$nPluginPath$nPluginID/$nPluginID.php";
		if(file_exists("$nMasterFilePath")){
			$nIncludeBH = include_once "$nMasterFilePath"; // send include into variable so it's output isn't viewable, but it's functions and vars are available
			if(function_exists($nPluginID)){ eval("echo ".$nPluginID."();"); }
			if(function_exists($nPluginID."_".$nComponentFunction)){ eval("\$nData = ".$nPluginID."_".$nComponentFunction."($nItemID);"); }
		} else { echo "does not exist: $nMasterFilePath<BR>"; return $nColumnARRAY; }
		// if column title already exists, just replace it
		$nNewItemARRAY = array();
		$nNewItemARRAY["$nColumnTitle"] = array("$nData","","","","");
		if(isset($nColumnARRAY["$nColumnTitle"]) && $nColumnARRAY["$nColumnTitle"]){
			$nColumnTitleARRAY = array_keys($nColumnARRAY);
			$nInsertAfter = array_search($nPosition,$nColumnTitleARRAY)+1;
			$nFirstPartARRAY = array_slice($nColumnARRAY,0,$nInsertAfter-1);
			$nSecondPartARRAY = array_slice($nColumnARRAY,$nInsertAfter);
			$nColumnARRAY = array_merge($nFirstPartARRAY,$nNewItemARRAY);
			$nColumnARRAY = array_merge($nColumnARRAY,$nSecondPartARRAY);
		} else if(isset($nColumnARRAY["$nPosition"]) && $nColumnARRAY["$nPosition"]){
			// if column position matches one already in the header, drop in the new header after it
			$nColumnTitleARRAY = array_keys($nColumnARRAY);
			$nInsertAfter = array_search($nPosition,$nColumnTitleARRAY)+1;
			$nFirstPartARRAY = array_slice($nColumnARRAY,0,$nInsertAfter);
			$nSecondPartARRAY = array_slice($nColumnARRAY,$nInsertAfter);
			$nColumnARRAY = array_merge($nFirstPartARRAY,$nNewItemARRAY);
			$nColumnARRAY = array_merge($nColumnARRAY,$nSecondPartARRAY);
		} else if(isset($nPluginARRAY["$nPosition"]) && !$nPluginARRAY["$nPosition"]){
			// if position is empty, just add it to the front of the array
			$nColumnARRAY = array_merge($nNewItemARRAY,$nColumnARRAY);
		}
	}
	return $nColumnARRAY;
}
function nBuildHeaderHTML($nColumnARRAY){
	$nHeaderCode = "";
	if($nColumnARRAY){
		foreach($nColumnARRAY AS $nColumnName => $nColumnAttrARRAY){
			$nAttr = "";
			if($nColumnAttrARRAY[0]){ $nAttr.=" width=\"{$nColumnAttrARRAY[0]}\""; }
			if($nColumnAttrARRAY[1]){ $nAttr.=" align=\"{$nColumnAttrARRAY[1]}\""; }
			if($nColumnAttrARRAY[2]){ $nAttr.=" class=\"{$nColumnAttrARRAY[2]}\""; }
			if($nColumnAttrARRAY[3]){ $nAttr.=" style=\"{$nColumnAttrARRAY[3]}\""; }
			if($nColumnName=="#Checkbox#"){ $nColumnName = "<input type=\"checkbox\" name=\"checkMaker\" value=\"0\" onClick=\"checkUncheckAll(this);\">"; }
			$nHeaderCode.="<th$nAttr>$nColumnName</th>";
		}
	}
	return $nHeaderCode;
}
function nBuildHeaderDataTableJS($nColumnARRAY){
	$nHeaderCode = "";
	$nHeaderCodeARRAY = array();
	if($nColumnARRAY){
		foreach($nColumnARRAY AS $nColumnName => $nColumnAttrARRAY){
			if($nColumnAttrARRAY[4]){ $nDataTableJS = $nColumnAttrARRAY[4]; } else { $nDataTableJS = "null"; }
			$nHeaderCodeARRAY[] = $nDataTableJS;
		}
	}
	if($nHeaderCodeARRAY){ $nHeaderCode = implode(",",$nHeaderCodeARRAY); }
	return $nHeaderCode;
}
function nBuildRowHTML($nColumnARRAY){
	$nRowCode = "";
	if($nColumnARRAY){
		foreach($nColumnARRAY AS $nColumnName => $nColumnAttrARRAY){
			$nAttr = "";
			if($nColumnAttrARRAY[1]){ $nAttr.=" width=\"{$nColumnAttrARRAY[1]}\""; }
			if($nColumnAttrARRAY[2]){ $nAttr.=" align=\"{$nColumnAttrARRAY[2]}\""; }
			if($nColumnAttrARRAY[3]){ $nAttr.=" class=\"{$nColumnAttrARRAY[3]}\""; }
			if($nColumnAttrARRAY[4]){ $nAttr.=" style=\"{$nColumnAttrARRAY[4]}\""; }
			$nRowCode.="<td$nAttr nowrap>{$nColumnAttrARRAY[0]}</td>";
		}
	}
	return $nRowCode;
}
function nBuildTabHTML($nTabARRAY){
	$nTabsCode = "";
	if($nTabARRAY){
		foreach($nTabARRAY AS $nTabName => $nTabARRAY){
			$nTabID = $nTabARRAY[0];
			$nTabPosition = $nTabARRAY[1];
			$nTabsCode.="<li><a href=\"#tabs-$nTabID\">$nTabName</a></li>";
		}
	}
	return $nTabsCode;
}
function nBuildTabBodyHTML($nTabARRAY,$nPluginInclusionPoint){
	$nTabsBodyCode = "";
	if($nTabARRAY && $nPluginInclusionPoint){
		foreach($nTabARRAY AS $nTabName => $nTabARRAY){
			$nTabID = $nTabARRAY[0];
			$nTabPosition = $nTabARRAY[1];
			// if tab has a position, it's a plugin, so run it's associated function and place in Content
			if($nTabPosition){
				$nTabBody = nRunBackOfficePagePlugins($nPluginInclusionPoint,$nTabPosition);
				$nTabsBodyCode.="<div id=\"tabs-$nTabID\">$nTabBody</div>";
			}
		}
	}
	return $nTabsBodyCode;
}
?>