<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 11/13/2008	ELL
// Added data type handling for text boxes as well as drop downs.
// Revised: 6/17/2009	ELL
// Added capability to handle checkbox data type, i.e. if checked, set value to 1, otherwise 0.
// Revised: 2/10/2012	ELL
// Added table prefix handling.
// Revised:	1/23/2015	ELL
// Modified to refresh main menu only if bit is set to do so in options table for option that was changed.
// Converted save operation to work as AJAX form post using JQuery.
// 
	include ("check_session.php");
	
	$option_name = array();
	$option_value = array();
	$data_type = array();
	$refresh_main = array();
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "options` ORDER BY `option_name`";
	$result = $bwb_dbh->query($query);
	$num_results = $result->rowCount();
	$k = 0;
	$last_option_name = "-none-";
	for ($i = 0; $i < $num_results; $i++)
	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		if ($last_option_name != $row["option_name"])
		{
			$option_name[$k] = $row["option_name"];
			$data_type[$k] = $row["data_type"];
			$refresh_main[$k] = $row["refresh_main"];
			$option_value[$k] = $_POST[$option_name[$k]];
			if ($data_type[$k] == 2) //checkbox type
			{
				if (isset($option_value[$k]))
					$option_value[$k] = 1;
				else
					$option_value[$k] = 0;
			}
			$last_option_name = $option_name[$k];
			$k++;
		}
	}
	
	$do_refresh = 0;
	//Insert new data into table.
	for ($i = 0; $i < $k; $i++)
	{
		$query2 = "SELECT * FROM `" . BWB_TABLE_PREFIX . "options_selected` WHERE `option_name` = '$option_name[$i]'";
		$result2 = $bwb_dbh->query($query2);
		if ($result2->rowCount() > 0)
			$query = "UPDATE `" . BWB_TABLE_PREFIX . "options_selected` SET `option_value` = '$option_value[$i]' WHERE `option_name` = '$option_name[$i]'";
		else
			$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "options_selected` (`option_name`, `option_value`) VALUES ('$option_name[$i]', '$option_value[$i]')";
		$cnt = $bwb_dbh->exec($query);
		if (($cnt !== false) && ($cnt > 0) && ($refresh_main[$i] == 1))
			$do_refresh = 1;
	}

	// Set flags and print results.
	if ($do_refresh == 1)	{	
		$_SESSION['button_group'] = "other_functions";
		$_SESSION['button'] = "btn_users_site_options";
		$_SESSION['sub_button'] = "btn_site_options";
	}
	echo "options_saved=1:refresh_main=$do_refresh";
	
?>