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

$nAdminOnlyPg = 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");

// config
$nRefreshCall = "nLoadUsersTable();";

// AJAX Specific vars
$nPassThru = "";
$nDivToUpdate = "editModal";
$nPageInDiv = "dialog/user_add.php";

// Get language settings
$nSettingsARRAY = array();
$nSQL = "SELECT * FROM nSettings";
mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
while($Row = mysql_fetch_array($Result)){
	$nSetting = $Row["nSetting"];
	$nValue = $Row["nValue"];
	$nSettingsARRAY[$nSetting] = $nValue;
}

// Defaults
$nUserLang = $nSettingsARRAY["defLanguage"];
?>

<div id="<?php echo $nDivToUpdate;?>">
<?php
include("../inc/form_class.php");

$nFormName = "FormUserAdd";
$nForm1 = new Form();
$nForm1->Name = $nFormName;
$nForm1->Action = $_SERVER['PHP_SELF'];
$nForm1->CancelText = _("Cancel");
$nForm1->CancelRedirect = "$.colorbox.close();";
$nForm1->CancelRedirectIsJS = true;
$nForm1->SubmitText = _("Add New User");
$nForm1->AddToDBTable = "nEditor";
$nForm1->DontProcess = "nSendEmail";
$nForm1->DBLink = $Link;
$nForm1->nUseADODB = false;
// AJAX settings
$nForm1->SubmitIsJS = true;
$nForm1->SubmitToJS = "return submitForm('$nDivToUpdate','$nPageInDiv$nPassThru','$nFormName');return false;";
// Form styling
$nForm1->FormClass = "form no-border";
$nForm1->CleanMarkup = true;
$nForm1->SubmitClass = "button blue";
$nForm1->CancelClass = "button";
$nForm1->ButtonTemplate = "<div class=\"clearfix grey-highlight\"><div class=\"input no-label\">[#Buttons#]</div></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();

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

$nFormField = new FormField("text","nUser",_("Login Username:"));
$nFormField->Caption = _("(max. 50 characters)");
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormField = $nForm1->AddValidation("minlength",$nFormField,4);
$nFormField = $nForm1->AddValidation("maxlength",$nFormField,50);
$nFormField->Class = "large";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("password","nPass",_("Login Password:"));
$nFormField->Caption = _("(max. 16 characters)");
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormField = $nForm1->AddValidation("minlength",$nFormField,4);
$nFormField = $nForm1->AddValidation("maxlength",$nFormField,16);
//$nFormField = $nForm1->AddEncoding("RC4",$nFormField);
$nFormField->Class = "large";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("select","nAdmin",_("Access Level:"));
$nEditorText = _("Editor");
$nAdminText = _("Administrator");
$nFormField->DataARRAY = array(
	"0" => $nEditorText,
	"1" => $nAdminText);
$nFormField->SelectedARRAY = array("0" => "selected"); // default
$nFormField->Class = "large";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("select","nUserLang",_("Language:"));
$nFormField->DataARRAY = array(
	"en" => "English",
	"es" => "Espa&ntilde;ol");
if($nUserLang){ $nFormField->SelectedARRAY = array("$nUserLang" => "selected"); }
$nFormField->Class = "large";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nEmail",_("E-mail Address:"));
if(isset($nEmail)){ $nFormField->Value = $nEmail; }
$nFormField = $nForm1->AddValidation("email",$nFormField);
$nFormField = $nForm1->AddValidation("required",$nFormField);
$nFormField->Class = "xlarge";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("checkbox","nSendEmail","");
$nFormField->DataARRAY = array(
	"nSendEmail" => _("Send this new user an email with their login information now.<BR><BR>")
);
$nFormField->SelectedARRAY["nSendEmail"] = "selected";
$nFormField->LabelStyle = "width:auto;";
$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){
	// Post-processing
	$nUserID = $nForm1->InsertID;
	$nEmail = $_POST["nEmail"];
	$nSendEmail = (isset($_POST['nSendEmail']) ? $_POST['nSendEmail'] : NULL);
	$nUser = $_POST["nUser"];
	$nPass = $_POST["nPass"];
	$nSettingsARRAY = nGetSiteSettingsARRAY();
	$nCoName = $nSettingsARRAY["coName"];
	$nSiteName = $nSettingsARRAY["siteName"];
	$nFullSitePath = $nSettingsARRAY["fullSitePath"];
	if($nSendEmail){
		//////////////////////////////
		$nSubject = _("Your login information for")." $nSiteName";
		$nHdr  = 'MIME-Version: 1.0' . "\r\n";
		$nHdr .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
		$nHdr .= 'From: Web Pro Manager <info@thewebpro.com>' . "\r\n";
		// Mail it
		$nBody = _("<strong>You are now an editor of the $nCoName website!</strong><BR><BR>A new user has been setup for you:")."<BR><BR><div align=\"center\" style=\"font-size:larger;\">"._("Username").": <strong style=\"font-size:larger;color:#777;\">$nUser</strong><BR>"._("Password").": <strong style=\"font-size:larger;color:#777;\">$nPass</strong></div><BR>"._("You can login here:")." <a href=\"".$nFullSitePath."manage/\">".$nFullSitePath."manage/</a><BR><BR>"._("Once logged in, you can change your username/password.")."<BR><BR>";
		if(!@mail($nEmail,$nSubject,$nBody,$nHdr)){ echo "<strong style=\"color:red;\">"._("UNABLE TO EMAIL: There was a problem e-mailing the new user login information.  Please check your server settings.")."</strong><BR><BR>"; }
		//////////////////////////////
	}
	echo "<div align=\"center\" id=\"buttonBox\"><BR>";
		echo "<strong>"._("A new user has been succesfully created!")."</strong><BR><BR>";
		echo _("What do you want to do next?")."<BR><BR>";
		echo "<input type=\"button\" class=\"button blue\" name=\"nEdit\" value=\""._("Edit this User")."\" onClick=\"document.location.href='user_modify.php?usr=".$_POST["nID"]."'\" class=\"formSubmit\"> ";
		echo "<input type=\"button\" class=\"button\" name=\"nClose\" value=\""._("Close")."\" onClick=\"$.colorbox.close();\" class=\"formCancel\"> ";
	echo "</div>";
	
	// log system event
	$nSystemEventDesc = "New user created: [nUserID:$nUserID]";
	$Query = "INSERT INTO nSystemEventLog (nEventTime,nEventDesc,nEditorUser) VALUES ('".date("Y-m-d H:i:s")."','$nSystemEventDesc','$ValidUser')";
	mysql_select_db($DBName); mysql_query($Query, $Link);
	
	echo "<script>$.colorbox.resize(); $nRefreshCall</script>";
} else {
	echo $nFormToDisplay;
	echo "<script>$.colorbox.resize();</script>";
}
?>
</div>
<?php
function nGetNextUserNum(){
	global $DBName,$Link;
	$nSQL = "SELECT Max(nID) AS nID FROM nEditor";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nID = $Row["nID"] + 1;
	}
	return $nID;
}
function nGetSiteSettingsARRAY(){
	global $DBName,$Link;
	$nSettingsARRAY = array();
	$nSQL = "SELECT * FROM nSettings";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nSetting = $Row["nSetting"];
		$nValue = $Row["nValue"];
		$nSettingsARRAY[$nSetting] = $nValue;
	}
	return $nSettingsARRAY;
}
?>
<?php require("../../inc/db_close.php"); ?>
