<?
/*
 * 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/plugin_settings.php - version 1.0
*/

$nAdminOnlyPg = 1;

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

// bring in other required files
require("../inc/config.php");
require("inc/lang.php");
include("inc/calendar.php");
require("inc/form_class.php");
require("inc/pluginutil.php");

// set page defaults
$nLoadJS = ""; // used for changing title onload

$nPluginPath = "../plugins/";

// Get passed-in variables
$nPluginNum = (isset($_GET["id"]) ? $_GET["id"] : NULL);
if(!$nPluginNum){ $nPluginNum = (isset($_POST["id"]) ? $_POST["id"] : NULL); }

// Get plugin settings and check for errors
$nErrorARRAY = array();

if($nPluginNum){
	$nPluginID = nGetPluginIDFromNum($nPluginNum);
	if($nPluginID){
		$nPluginName = nGetPluginNameFromID($nPluginID);
		// get settings form from plugin
		$nSettingsFunction = nGetSettingsFunction($nPluginID);
		if($nSettingsFunction){
			$nSettingsFunction = $nPluginID."_".$nSettingsFunction;
			$nMasterFilePath = "$nPluginPath$nPluginID/$nPluginID.php";
			if(file_exists("$nMasterFilePath")){
				$nIncludeBH = include "$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($nSettingsFunction)){ eval("\$nSettingsForm = ".$nSettingsFunction."();"); } else { $nErrorARRAY[] = "This plugin is missing a critical function (".$nSettingsFunction.") and the settings cannot be loaded."; }
			} else { $nErrorARRAY[] = "This plugin is missing a critical file: $nPluginID.php"; }
		} else { $nErrorARRAY[] = "This plugin is missing a critical settings function and the settings cannot be loaded."; }
	}
} else {
	$nErrorARRAY[] = "Invalid Plugin ID.";
}
?>
<?php include("inc/nav/header.php"); ?>

<div id="titlediv">
    <div class="clearfix container" id="pattern">
        <div class="row">
            <div class="col_12">
                <ul class="breadcrumbs hor-list">
                    <li><a href="home.php"><?php echo _("Dashboard"); ?></a></li>
                    <li><a href="plugins.php"><?php echo _("Plugins"); ?></a></li>
					<li><a href="plugins.php?id=<?php echo $nPluginNum; ?>"><?php echo _("Plugin Settings"); ?></a></li>
                </ul>
                <h1><?php echo _("Plugin Settings"); ?></h1>
            </div>
        </div>
    </div>
</div>

<div class="container" id="actualbody">

	<div class="row clearfix">
	    <div class="widget clearfix">
			<div class="view-switcher">
			    <h2><span id="pluginsTableTitle"><?php echo _("Settings"); ?>: <? echo $nPluginName; ?></span></h2>
			</div>
	        <div class="widget_inside">
	            <!--
				<div style="text-align:right;">
					<button onClick="openEditDialog('comp',0,'Add New Plugin');" class="icon" data-icon-primary="ui-icon-plus" style="margin-top:-60px;">Add New Plugin</button>
				</div>
				//-->
				<span id="notify" class="notification done" style="display:none;"></span>
				<div class="col_12" id="pluginsTable">
				
					<?if($nErrorARRAY){?>
					<div align="center">
						<strong>The settings for this plugin were unable to be loaded for the following reasons:</strong><BR>
						<ul><? foreach($nErrorARRAY as $nErrorMsg){ echo "<li>$nErrorMsg</li>"; } ?></ul>
					</div>
					<? } else { ?>
						<?=$nSettingsForm;?>
					<? } ?>
				
				</div>
	        </div>
	    </div>
	</div>
    
</div>

<?php include("inc/nav/footer.php") ?>
<?php
function nGetPluginIDFromNum($nPluginNum){
	global $DBName, $Link;
	if(!$nPluginNum){ return; }
	$Query = "SELECT nPluginID FROM nPlugins WHERE nID='$nPluginNum'";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	$nCurrentRecord = 0;
	while ($Row = mysql_fetch_array($Result)) {
		$nPluginID = $Row["nPluginID"];
	}
	return $nPluginID;
}
function nGetPluginNameFromID($nPluginID){
	global $DBName, $Link;
	if(!$nPluginID){ return; }
	$Query = "SELECT nName FROM nPlugins WHERE nPluginID='$nPluginID'";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	$nCurrentRecord = 0;
	while ($Row = mysql_fetch_array($Result)) {
		$nPluginName = $Row["nName"];
	}
	return $nPluginName;
}
function nGetSettingsFunction($nPluginID){
	global $DBName, $Link;
	if(!$nPluginID){ return; }
	$nSQL = "SELECT nFunction FROM nPluginSettingsForm WHERE nPluginID='$nPluginID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nFunction = $Row["nFunction"];
	}
	return $nFunction;
}
function getPluginPath(){
	global $nPluginPath;
	return $nPluginPath;
}
?>
<?php
// disconnect from mysql
mysql_close($Link);
?>
