<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 12/14/2015
// Modified: 12/2/2016	ELL
// Consolidated all class tables into site_classes table indexed by style_sheet field.
// Consolidated all CSS tables into site_css table indexed by style_sheet field.
// Consolidated all menu items tables into menu_items.
	
	include ("check_session.php");
	include ("lib/build_css_string.php");

	$style_sheet = $_POST['style_sheet'];

	// Create new classes.
	$query = "DELETE FROM " . BWB_TABLE_PREFIX . "site_classes WHERE style_sheet = '" . $style_sheet . "_mobile'";
	$cnt = $bwb_dbh->exec($query);

	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_classes WHERE style_sheet = '" . $style_sheet . "'";
	$result = $bwb_dbh->query($query);
	$comma = ", ";
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$sql = "REPLACE INTO " . BWB_TABLE_PREFIX . "site_classes SET style_sheet = '" . $style_sheet . "_mobile'";
		foreach ($row as $field => $val)
		{
			if ($field != "style_sheet")	{
				$sql .= $comma . "`" . $field . "` = '" . $val . "'";
			}
		}
		$cnt = $bwb_dbh->exec($sql);
	}
	
	// Create new css.
	$query = "DELETE FROM " . BWB_TABLE_PREFIX . "site_css WHERE style_sheet = '" . $style_sheet . "_mobile'";
	$cnt = $bwb_dbh->exec($query);
	
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_css WHERE style_sheet = '" . $style_sheet . "'";
	$result = $bwb_dbh->query($query);
	$comma = ", ";
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$sql = "REPLACE INTO " . BWB_TABLE_PREFIX . "site_css SET style_sheet = '" . $style_sheet . "_mobile'";
		foreach ($row as $field => $val)
		{
			if ($field != "style_sheet")	{
				$sql .= $comma . "`" . $field . "` = '" . $val . "'";
			}
		}
		$cnt = $bwb_dbh->exec($sql);
	}
	
	// Copy custon site CSS if site.css.
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "custom_site_css WHERE name = '". $style_sheet . "'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$custom_css = addslashes(stripslashes($row['custom_css']));
		
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "custom_site_css WHERE name = '". $style_sheet . "_mobile'";
		$result = $bwb_dbh->query($query);
		if ($result->rowCount() < 1)
			$query = "INSERT INTO " . BWB_TABLE_PREFIX . "custom_site_css SET name = '" . $style_sheet . "_mobile', custom_css = '" . $custom_css . "'";
		else
			$query = "UPDATE " . BWB_TABLE_PREFIX . "custom_site_css SET custom_css = '" . $custom_css . "' WHERE name = '" . $style_sheet . "_mobile'";
		
		$cnt = $bwb_dbh->exec($query);
	}
	
	// Add to style sheets if new/non-existing mobile style sheet.
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "style_sheets WHERE name = '" . $style_sheet . "_mobile'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() < 1)	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "style_sheets WHERE name = '" . $style_sheet . "'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$description = $row['description'] . " Mobile";
		$show_class_editor = $row['show_class_editor'];
		$show_custom_styles = $row['show_custom_styles'];
		$sort_order = $row['sort_order'] + 1;
		
		$charset = $row['charset'];
		$import = $row['import'];
		$media_type_modifier = $row['media_type_modifier'];
		$media_type = $row['media_type'];
		$media_feature = $row['media_feature'];
		$media_feature_value = $row['media_feature_value'];

		$query = "INSERT INTO " . BWB_TABLE_PREFIX . "style_sheets SET `name` = '" . $style_sheet . "_mobile', `description` = '" . $description . "', show_class_editor = '" 
		. $show_class_editor . "', show_custom_styles = '" . $show_custom_styles . "', sort_order = '" . $sort_order . "', charset = '" . $charset . "', import = '" 
		. $import . "', media_type_modifier = '" . $media_type_modifier . "', media_type = '" . $media_type . "', media_feature = '" . $media_feature . "', media_feature_value = '" 
		. $media_feature_value . "'";
		$cnt = $bwb_dbh->exec($query);
	}
	/////////////////////////////////////////////
	// Build string for site.css file /////
	$css_string = BuildCSSString($style_sheet . "_mobile", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
			
	//Test to see if file already exists.
	$css_file = $style_sheet . "_mobile.css";
	
	$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);
	
	////////////////////////////////////////////////////////////
	// Create new item tables if menu style sheet.
	if (strpos($style_sheet, "menu") !== false)
	{
		// Create new classes.
		$query = "DELETE FROM " . BWB_TABLE_PREFIX . "site_classes WHERE style_sheet = '" . $style_sheet . "_mobile_item'";
		$cnt = $bwb_dbh->exec($query);
		
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_classes WHERE style_sheet = '" . $style_sheet . "_item'";
		$result = $bwb_dbh->query($query);
		$comma = ", ";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$sql = "REPLACE INTO " . BWB_TABLE_PREFIX . "site_classes SET style_sheet = '" . $style_sheet . "_mobile_item'";
			foreach ($row as $field => $val)
			{
				if ($field != "style_sheet")	{
					$sql .= $comma . "`" . $field . "` = '" . $val . "'";
				}
			}
			$cnt = $bwb_dbh->exec($sql);
		}
		
		// Create new css.
		$query = "DELETE FROM " . BWB_TABLE_PREFIX . "site_css WHERE style_sheet = '" . $style_sheet . "_mobile_item'";
		$cnt = $bwb_dbh->exec($query);
		
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_css WHERE style_sheet = '" . $style_sheet . "_item'";
		$result = $bwb_dbh->query($query);
		$comma = ", ";
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$sql = "REPLACE INTO " . BWB_TABLE_PREFIX . "site_css SET style_sheet = '" . $style_sheet . "_mobile_item'";
			foreach ($row as $field => $val)
			{
				if ($field != "style_sheet")	{
					$sql .= $comma . "`" . $field . "` = '" . $val . "'";
				}
			}
			$cnt = $bwb_dbh->exec($sql);
		}
		
		/////////////////////////////////////////////
		// Build string for site.css file /////
		$css_string = BuildCSSString($style_sheet . "_mobile_item", $bwb_dbh, BWB_BASE_DIR, BWB_IMAGE_DIR, BWB_TABLE_PREFIX);
				
		//Test to see if file already exists.
		$css_file = $style_sheet . "_mobile_item.css";
		
		$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);
	}
	
	if ($cnt === false)
		echo -1;
	else
		echo $style_sheet;
?>
