<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created:	10/28/2014	ELL
//
	include ("check_session.php");

	$user_name = $_POST['user_name'];
	$AccessLevel = $_POST['AccessLevel'];
	$email = (isset($_POST['email']) ? $_POST['email'] : "");
	$first_name = (isset($_POST['first_name']) ? $_POST['first_name'] : "");
	$last_name = (isset($_POST['last_name']) ? $_POST['last_name'] : "");
	$table = $_POST['table'];
	$no_prefix = $_REQUEST['no_prefix'];
	
//	print_r($_POST);
//	exit;
	
	if (!isset($table) || ($table == "-not selected-") || ($table == ""))
	{
		echo MANAGE_USERS_NO_TABLE_TO_DELETE_USER_FROM_ERR . " " . CLICK_WORD . " <a href=\"manage_page_access.php\" title=\"" . HERE_WORD . "\">" . HERE_WORD . "</a> " . MANAGE_USERS_NO_TABLE_TO_ADD_USER_TO_2_ERR;
		exit;
	}

	if (isset($no_prefix) && ($no_prefix == 1))
		$prefix = "";
	else
		$prefix = BWB_TABLE_PREFIX;

	// Change password.
	$query = "UPDATE `" . $prefix . $table . "` SET";
	$comma = " ";
	$ignore_fields = array("website_user", "no_prefix", "table", "user_name");
	if (isset($_POST['website_user']) && ($_POST['website_user'] == 1))	{
		foreach ($_POST as $key => $val)
		{
			if (!in_array($key, $ignore_fields))	{
				$query .= $comma . "`" . $key . "` = '" . $val . "'";
				$comma = ", ";
			}
		}
	}
	else	{
		$query .= " `AccessLevel`='$AccessLevel'";
		if ($email != "")
			$query .= ", `email`='$email'";
		if ($first_name != "")
			$query .= ", `first_name`='$first_name'";
		if ($last_name != "")
			$query .= ", `last_name`='$last_name'";
	}
	
	$query .= " WHERE `UserID`='$user_name'";
	$cnt = $bwb_dbh->exec($query);
	
	if ($cnt < 1)
	{
		$query = "UPDATE `" . $prefix . $table . "` SET `AccessLevel`='$AccessLevel', `email`='$email' WHERE `UserID`='$user_name'";
		$cnt = $bwb_dbh->exec($query);
	}
	
	$_SESSION['last_user'] = $user_name;
	
	echo "user_name=$user_name@@@@@AccessLevel=$AccessLevel@@@@@email=$email@@@@@first_name=$first_name@@@@@last_name=$last_name";
?>
