<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 4/28/2015	ELL
// 
// 
// 
	include ("check_session.php");
	
	$mode = $_POST['mode'];
	$copy_from_class = $_POST['copy_from_class'];
	$copy_to_class = trim($_POST['copy_to_class']);
	$style_sheet = $_POST['style_sheet'];
	if (isset($_POST['to_style_sheet']) && ($_POST['to_style_sheet'] != "") && ($_POST['to_style_sheet'] != "-not selected-"))
		$to_style_sheet = $_POST['to_style_sheet'];
	else
		$to_style_sheet = $_POST['style_sheet'];
	$class_table = $style_sheet . "_classes";
	$to_class_table = $to_style_sheet . "_classes";
	$site_css_table = $style_sheet . "_css";
	$to_site_css_table = $to_style_sheet . "_css";
	if (mode == "rename")
	{
		$css_file = $style_sheet . ".css";
		$custom_site_css_table = "custom_" . $style_sheet . "_css";
	}
	else
	{
		$css_file = $to_style_sheet . ".css";
		$custom_site_css_table = "custom_" . $to_style_sheet . "_css";
	}

	$replace = array(".", "#", " ", ",", ">", "&gt;", "+", "~");
	$search = array("PERIOD", "POUND", "SPACE", "COMMA", "GREATERTHAN", "GREATERTHAN", "PLUS", "TILDA");
	$copy_from_class = str_replace($search, $replace, $copy_from_class);
	$copy_to_class = str_replace($search, $replace, $copy_to_class);
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` WHERE `class`='" . $copy_from_class . "'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() < 1)
	{
		echo "-no data-";
		return;
	}
	if ($mode == "copy")
	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$description = $row['description'];
		$sort_order = $row['sort_order'];
		$subclass_sort_order = $row['subclass_sort_order'];
	}
	
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $to_class_table . "` WHERE `class`='" . $copy_to_class . "'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
	{
		echo "to class already exists";
		return;
	}
	
	if ($mode == "copy")
	{
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . $to_class_table . "` SET `class`='" . $copy_to_class . "', `description`='" . $description . "', `sort_order`='" . $sort_order . "', `subclass_sort_order`='" . $subclass_sort_order . "', `custom` = 1";
		$cnt = $bwb_dbh->exec($query);
	}
	elseif ($mode == "rename")
	{
		$query = "UPDATE `" . BWB_TABLE_PREFIX . $class_table . "` SET `class`='" . $copy_to_class . "' WHERE `class`='" . $copy_from_class . "'";
		$cnt = $bwb_dbh->exec($query);
	}
	else
	{
		echo "-no data-";
		exit;
	}
	
	unset($row);
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $site_css_table . "` WHERE `class`='" . $copy_from_class . "'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() < 1)
	{
		echo "-no data-";
		return;
	}
	if ($mode == "copy")
	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$sql = "INSERT INTO `" . BWB_TABLE_PREFIX . $to_site_css_table . "` SET `class` = '" . $copy_to_class . "'";
		foreach($row as $key => $val)
		{
			if ($key != 'class')
				$sql .= ", `" . $key . "` = '" . $val . "'";
		}
		$cnt = $bwb_dbh->exec($sql);
	}
	elseif ($mode == "rename")
	{
		$query = "UPDATE `" . BWB_TABLE_PREFIX . $site_css_table . "` SET `class`='" . $copy_to_class . "' WHERE `class`='" . $copy_from_class . "'";
		$cnt = $bwb_dbh->exec($query);
	}
	
	$search = array(".", "#", " ", ",", ">", "&gt;", "+", "~");
	$replace = array("PERIOD", "POUND", "SPACE", "COMMA", "GREATERTHAN", "GREATERTHAN", "PLUS", "TILDA");
	$class_str = str_replace($search, $replace, $copy_to_class);
	$_SESSION['last_class_edited'] = $class_str;
	
	if ($cnt < 1)
		echo "-no data-";
	else
		echo $cnt;
		
	/////////////////////////////////////////////
	// Build string for site.css file /////
	if ($mode == "copy")
		$site_css_table = $to_site_css_table;
	include("build_css_string.php");
	echo $css_string;

	////////////////////////////////////////////////////////
	/// Write menu items to menu.css file /////////////
	//Test to see if file already exists.
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if (BWB_BASE_DIR == '')
		$userfiles = '';
	else
		$userfiles = "/" . BWB_BASE_DIR;
		
	$file_name = $document_root . $userfiles . "/" . BWB_CSS_DIR . "/" . $css_file;
		
	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . time();
		if (!rename($file_name, $bkup_file_name))
			echo EDIT_CSS_COULD_NOT_RENAME_1_ERR . " $file_name " . EDIT_CSS_COULD_NOT_RENAME_2_ERR . " $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0644);
	}
	
	// Create file.
	$fh = fopen($file_name, 'x') or die(EDIT_CSS_CANT_CREATE_ERR . " $file_name: $php_errormsg");
	$result = (fputs($fh, $css_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
	
	/////////////////////////////////////////////////////////
	// Add custom styles to CKEditor style list. ///////////
	// Build string for fckstyles.xml file /////
	include("build_jason_style_string.php");
	
	// Rename styles.js file for backup
	$file_name = $document_root . "/ckeditor/styles.js";
	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . time();
		if (!rename($file_name, $bkup_file_name))
			echo EDIT_CSS_COULD_NOT_RENAME_1_ERR . " $file_name " . EDIT_CSS_COULD_NOT_RENAME_2_ERR . " $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0644);
	}
	
	// Create file.
	$fh = fopen($file_name, 'x') or die(EDIT_CSS_CANT_CREATE_ERR . " $file_name: $php_errormsg");
	$result = (fputs($fh, $jason_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
?>