<?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/
 * 
 * FILE: manage/edit/userpage.php - version 1.0
*/

// $nSecureMe = 0;

// 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/calendar.php");
include("../inc/rc4.php");

// Get passed-in vars
$nRecID = (isset($_GET['id']) ? $_GET['id'] : NULL);
if(!$nRecID){ $nRecID = (isset($_POST['id']) ? $_POST['id'] : NULL); }
$nEditMode = (isset($_GET['editMode']) ? $_GET['editMode'] : NULL);
if(!$nEditMode){ $nEditMode = (isset($_POST['editMode']) ? $_POST['editMode'] : NULL); }

// Additional security check if this account is a limited user (give only self access)
if(!$nAdminAccess && $nRecID!=$UID){ exit; }

// AJAX Specific vars
$nPassThru = "?id=$nRecID";
$nDivToUpdate = "ajaxUserPageEdit"; // must be same as parent edit page
$nPageInDiv = "edit/userpage.php";

// Get info
if($nRecID){
	$nSQL = "SELECT * FROM nEditor WHERE nID='$nRecID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nID = $Row["nID"];
		$nAdmin = $Row["nAdmin"];
		$nAccessPages = $Row["nAccessPages"];
		$nAccessLibrary = $Row["nAccessLibrary"];
		$nAccessSettings = $Row["nAccessSettings"];
	} else { echo "<div align=\"center\" style=\"padding:20px;\"><em>"._("No record found.")."</em></div>"; }
	$nAllPagesARRAY = nGetAllPagesARRAY();
} else { echo "<div align=\"center\" style=\"padding:20px;\"><em>"._("No record available. Invalid ID.")."</em></div>"; }
?>

<?php if(!$nEditMode){ ?>

		<div style="height:200px;overflow:auto;">
			<strong><?php if($nRecID!=$UID){?><?php echo _("This user has access to these areas:"); ?><?} else {?><?php echo _("You have access to these areas:"); ?><?php } ?></strong><BR><strong></strong>
			<div style="padding:8px;">
			<?php
			if(!$nAdmin && $nAccessPages){ echo "<a href=\"pages.php\">"._("Pages")."</a><BR>"; }
			if(!$nAdmin && $nAccessLibrary){ echo "<a href=\"library.php\">"._("Library")."</a><BR>"; }
			if(!$nAdmin && $nAccessSettings){ echo "<a href=\"settings.php\">"._("Settings")."</a><BR>"; }
			
			if($nAdmin){ echo "<div align=\"center\" style=\"margin:14px 0;\"><em class=\"caption\">"._("Administrators have access to all areas.")."</em></div>"; }
			
			$nNoAccess = 0;
			if(!$nAdmin && (!$nAccessPages && !$nAccessLibrary && !$nAccessSettings)){ $nNoAccess = 1; echo "<div align=\"center\" style=\"margin:23px 0 23px 0;\"><em class=\"caption\">"._("No access to any areas.")."</em></div>"; }
			?>
			</div><BR>
			
			<strong><?php if($nRecID!=$UID){?><?php echo _("This user has access to these pages:"); ?><?} else {?><?php echo _("You have access to these pages:"); ?><?php } ?></strong><BR>
			<div style="padding:8px;">
			<?php
			$nPageList = nGetEditablePages($nRecID);
			if((!$nPageList && !$nAdmin) || $nNoAccess){ echo "<div align=\"center\" style=\"margin:23px 0 23px 0\"><em class=\"caption\">"._("No access to any pages.")."</em></div>"; }
			if($nAdmin){ echo "<div align=\"center\" style=\"margin:14px 0;\"><em class=\"caption\">"._("Administrators have access to all pages.")."</em></div>"; }
			?>
			<?php if($nPageList && !$nAdmin){ ?><ul style="line-height:14px;"><?php echo $nPageList;?></ul><?php } ?>
			</div>
		</div>
		
		<?php if(!$nAdmin && $nAdminAccess){ ?>
		<div style="clear:both;">&nbsp;</div>
		<div align="center" id="buttonBox">
			<input type="button" name="nEdit" value="<?php echo _("Edit Page Access"); ?>" class="button blue" onClick="loadContent('<?php echo $nDivToUpdate?>','<?php echo $nPageInDiv.$nPassThru?>&editMode=1');">
		</div>
		<?php } ?>

<?php } ?>

<?php if($nEditMode){ ?>
		
		<div style="">
<?php
include("../inc/form_class.php");

$nFormName = "FormUserEdit";
$nForm1 = new Form();
//$nForm1->DEBUG = true;
$nForm1->Name = $nFormName;
$nForm1->Action = $_SERVER['PHP_SELF'];
if($nRecID){
	$nForm1->SubmitText = _("Update");
	$nForm1->CancelText = _("Cancel");
	$nForm1->CancelRedirect = "loadContent('$nDivToUpdate','$nPageInDiv$nPassThru');";
	$nForm1->CancelRedirectIsJS = true;
	// Form DB settings
	//$nForm1->ModifyDBTable = "nEditor";
	//$nForm1->ModifyDBField = "nID";
	//$nForm1->ModifyDBID = "'$nRecID'";
	//$nForm1->DBLink = $Link;
	//$nForm1->nUseADODB = false;
	// AJAX settings
	$nForm1->SubmitToJS = "return submitForm('$nDivToUpdate','$nPageInDiv$nPassThru',this.name);return false;";
}
$nForm1->DontProcess = "id,editMode,nPageAccess,nSectionAccess";
// Form styling
$nForm1->FormClass = "form no-border";
$nForm1->CleanMarkup = true;
$nForm1->SubmitClass = "button blue";
$nForm1->CancelClass = "button";
$nForm1->ButtonTemplate = "<div class=\"clearfix\">[#Buttons#]</div>\n";
//$nForm1->FieldTemplate = "<div id=\"[#RowID#]\" class=\"clearfix\" style=\"[#RowStyle#]\">[#Label#]<div class=\"input\" style=\"[#ItemStyle#]\">[#BeforeField#][#Field#][#AfterField#]<span class=\"info\">[#Caption#]</span></div></div>\n";
//$nForm1->GroupTemplate = "<div id=\"[#ID#]\" style=\"[#Style#]\" class=\"[#Class#]\">\n[#Name#][#FieldCode#]\n</div>\n";

// Create groups
$nFormGroup1 = new FormGroup();
$nFormGroup1->Style = "display:block; height:250px; overflow:auto;";

// Create fields
$nFormField = new FormField("hidden","id");
$nFormField->Value = $nRecID;
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("hidden","editMode");
$nFormField->Value = "1";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("checkbox","nSectionAccess","<strong>"._("This user has access to these areas:")."</strong>");
$nPagesText = _("Pages");
$nLibraryText = _("Library");
$nSettingsText = _("Settings");
$nFormField->DataARRAY = array(
	"nAccessPages" => "$nPagesText<BR>",
	"nAccessLibrary" => "$nLibraryText<BR>",
	"nAccessSettings" => "$nSettingsText<BR>");
if($nAccessPages){ $nFormField->SelectedARRAY["nAccessPages"] = "selected"; }
if($nAccessLibrary){ $nFormField->SelectedARRAY["nAccessLibrary"] = "selected"; }
if($nAccessSettings){ $nFormField->SelectedARRAY["nAccessSettings"] = "selected"; }
$nFormField->RowSize = "Half";
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("checkbox","nPageAccess","<strong>"._("This user has access to these pages:")."</strong>");
$nFormField->DataARRAY = $nAllPagesARRAY;
$nFormField->SelectedARRAY = nGetEditablePagesARRAY($nRecID);
$nFormField->RowSize = "Half";
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormGroup1->AddField($nFormField);

// Finalize and display form
$nForm1->AddGroup($nFormGroup1);
$nFormToDisplay = $nForm1->GetForm();

// AJAX redirect if form submitted w/o error, otherwise display form (again, if necessary)
if($nForm1->HasProcessed){
	$nAccessPages = (isset($_POST["nAccessPages"]) ? $_POST["nAccessPages"] : NULL);
	$nAccessLibrary = (isset($_POST["nAccessLibrary"]) ? $_POST["nAccessLibrary"] : NULL);
	$nAccessSettings = (isset($_POST["nAccessSettings"]) ? $_POST["nAccessSettings"] : NULL);
	
	if($nAccessPages){ $nAccessPages = 1; }
	if($nAccessLibrary){ $nAccessLibrary = 1; }
	if($nAccessSettings){ $nAccessSettings = 1; }
	
	$Query = "UPDATE nEditor SET nAccessPages='$nAccessPages',nAccessLibrary='$nAccessLibrary',nAccessSettings='$nAccessSettings' WHERE nID=$nRecID LIMIT 1";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
	
	// delete all current page access from this editor (start from zero)
	$nSQL = "DELETE FROM xEditorPage WHERE nEditorID=$nRecID";
	mysql_select_db($DBName);
	mysql_query($nSQL, $Link);
	// go through POST and add page access as needed
	foreach($_POST AS $key => $item){
		$nKeyARRAY = explode("_",$key);
		if($nKeyARRAY[0]=="pageid"){
			// save page
			$nSQL = "INSERT INTO xEditorPage (nPageID,nEditorID) VALUES (".$nKeyARRAY[1].",$nRecID)";
			mysql_select_db($DBName);
			mysql_query($nSQL, $Link);
		}
	}
	echo "<script>loadContent('$nDivToUpdate','$nPageInDiv$nPassThru');</script>";
} else {
	echo $nFormToDisplay;
}
?>
		</div>

<?php } ?>
<?php
function nGetEditablePages($nEditorID){
	global $DBName,$Link;
	$nCode = "";
	$nSQL = "SELECT nPageID,nNavTitle FROM xEditorPage LEFT JOIN nPage ON nPageID=nPage.nID WHERE nEditorID=$nEditorID ORDER BY nType,nNavTitle";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nPageID"];
		$nPageName = $Row["nNavTitle"];
		$nCode.="<li><a href=\"page_edit.php?id=$nPageID\">$nPageName</a></li>";
	}
	return $nCode;
}
function nGetEditablePagesARRAY($nEditorID){
	global $DBName,$Link;
	$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"];
		$nPageName = nGetPageName($nPageID);
		$nDataARRAY["pageid_"."$nPageID"] = $nPageName."<BR>";
	}
	asort($nDataARRAY, SORT_STRING);
	return $nDataARRAY;
}
function nGetAllPagesARRAY(){
	global $DBName,$Link;
	$nDataARRAY = array();
	$nSQL = "SELECT nID,nNavTitle FROM nPage ORDER BY nType,nNavTitle";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nID"];
		$nNavTitle = $Row["nNavTitle"];
		$nDataARRAY["pageid_"."$nPageID"] = $nNavTitle."<BR>";
	}
	asort($nDataARRAY, SORT_STRING);
	return $nDataARRAY;
}
function nGetPageName($nPageID){
	global $DBName,$Link;
	if(!$nPageID){ return; }
	$nNavTitle = "";
	$nSQL = "SELECT nNavTitle FROM nPage WHERE nID=$nPageID LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nNavTitle = $Row["nNavTitle"];
	}
	return $nNavTitle;
}
function nGetUserIDFromName($nUserName){
	global $DBName,$Link;
	if(!$nUserName){ return; }
	$Query = "SELECT nID FROM nEditor WHERE nUser='$nUserName' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	if($Row = mysql_fetch_array($Result)) {
		$nUserID = $Row["nID"];
	}
	return $nUserID;
}
?>
<?php require("../../inc/db_close.php"); ?>
