<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 11/9/2016	ELL
// Revised: 2/28/2018	ELL
// Converted from each style sheet using individual classes and css tables to using site_classes and site_css tables.
// Added copy mode.
// 
// 
	include ("check_session.php");

	$old_name = $_REQUEST['old_name'];
	$new_name = $_REQUEST['new_name'];
	$new_desc = $_REQUEST['new_desc'];
	$mode = $_REQUEST['mode'];
	if (isset($_REQUEST['new_exclude']) && ($_REQUEST['new_exclude'] != 0))
		$new_exclude = 1;
	else
		$new_exclude = 0;
	if (isset($_REQUEST['new_show_custom_styles']) && ($_REQUEST['new_show_custom_styles'] != 0))
		$new_show_custom_styles = 1;
	else
		$new_show_custom_styles = 0;

	if ($mode == "rename")
		$query = "UPDATE `" . BWB_TABLE_PREFIX . "style_sheets` SET `name` = '$new_name', `description` = '$new_desc', `exclude` = '$new_exclude', `show_custom_styles` = '$new_show_custom_styles' WHERE `name` = '$old_name'";
	elseif ($mode == "copy")	{
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "style_sheets` WHERE `name` = '$old_name'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$sort_order = $row['sort_order'] + 1;
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "style_sheets` SET `name` = '$new_name', `description` = '$new_desc', `exclude` = '$new_exclude', `show_custom_styles` = '$new_show_custom_styles', `custom` = 1, `sort_order` = '$sort_order'";
	}
	else	{
		echo "ERROR: rename/copy mode not specified. Cannot continue.";
		exit;
	}
	$cnt = $bwb_dbh->exec($query);

	if ($mode == "copy")
	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_classes WHERE style_sheet = '" . $old_name . "'";
		$result = $bwb_dbh->query($query);
		$comma = ", ";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			
			$sql = "REPLACE INTO " . BWB_TABLE_PREFIX . "site_classes SET style_sheet = '" . $new_name . "'";
			
			foreach ($row as $field => $val)
			{
				if ($field != "style_sheet")	{
					$sql .= $comma . "`" . $field . "` = '" . $val . "'";
				}
			}
			$cnt = $cnt + $bwb_dbh->exec($sql);
		}
	}
	elseif ($mode == "rename")
	{
		$sql = "UPDATE " . BWB_TABLE_PREFIX . "site_classes SET style_sheet = '" . $new_name . "' WHERE style_sheet = '" . $old_name . "'";
		$cnt = $cnt + $bwb_dbh->exec($sql);
	}
	
	if ($mode == "copy")
	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_css WHERE style_sheet = '" . $old_name . "'";
		$result = $bwb_dbh->query($query);
		$comma = ", ";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			
			$sql = "REPLACE INTO " . BWB_TABLE_PREFIX . "site_css SET style_sheet = '" . $new_name . "'";
			
			foreach ($row as $field => $val)
			{
				if ($field != "style_sheet")	{
					$sql .= $comma . "`" . $field . "` = '" . $val . "'";
				}
			}
			$cnt = $cnt + $bwb_dbh->exec($sql);
		}
	}
	elseif ($mode == "rename")
	{
		$sql = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET style_sheet = '" . $new_name . "' WHERE style_sheet = '" . $old_name . "'";
		$cnt = $cnt + $bwb_dbh->exec($sql);
	}
	
	echo $cnt;
?>
