<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 6/30/2012	ELL
// Revised: 11/3/2014	ELL
// Added first and last name fields.
// Revised: 3/24/2016	ELL
// Fixed bug with PDO not being able to continue to add fields upon failure but checking and setting flags first.
// 
//
//

	include ("check_session.php");

	$user_table = $_POST['user_table'];
	$_SESSION['last_user_table'] = $user_table;
	
	$query = "SHOW COLUMNS FROM " . $user_table;
	$result = $bwb_dbh->query($query);
	$got_password = false;
	$got_UsedID = false;
	$got_email = false;
	$got_AccessLevel = false;
	$got_Active = false;
	$got_Session = false;
	$got_IP = false;
	$got_welcome_message_page = false;
	$got_first_name = false;
	$got_last_name = false;
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		if ($row['Field'] == "password")
			$got_password = true;
		if ($row['Field'] == "UserID")
			$got_UsedID = true;
		if ($row['Field'] == "email")
			$got_email = true;
		if ($row['Field'] == "AccessLevel")
			$got_AccessLevel = true;
		if ($row['Field'] == "Active")
			$got_Active = true;
		if ($row['Field'] == "Session")
			$got_Session = true;
		if ($row['Field'] == "IP")
			$got_IP = true;
		if ($row['Field'] == "welcome_message_page")
			$got_welcome_message_page = true;
		if ($row['Field'] == "first_name")
			$got_first_name = true;
		if ($row['Field'] == "last_name")
			$got_last_name = true;
	}
	
	if (!$got_email)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `email` VARCHAR( 256 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_UsedID)	{
		$query = "ALTER TABLE `" . $user_table . "`ADD `UserID` VARCHAR( 64 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_password)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `password` VARCHAR( 256 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_AccessLevel)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `AccessLevel` VARCHAR( 64 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_Active)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `Active` tinyint(4) NOT NULL default '0';";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_Session)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `Session` VARCHAR( 32 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_IP)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `IP` VARCHAR( 16 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_welcome_message_page)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `welcome_message_page` VARCHAR( 255 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_first_name)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `first_name` VARCHAR( 128 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	if (!$got_last_name)	{
		$query = "ALTER TABLE `" . $user_table . "` ADD `last_name` VARCHAR( 128 ) NULL;";
		$cnt = $bwb_dbh->exec($query);
	}
	
	echo "Fields added";

?>
