<?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.
// 
// 
	include ("check_session.php");

	$name = $_REQUEST['name'];
	$description = $_REQUEST['description'];
	$style_sheet_types = $_REQUEST['style_sheet_types'];
	if (isset($_REQUEST['exclude']) && ($_REQUEST['exclude'] != 0))
		$exclude = 1;
	else
		$exclude = 0;
	if ($style_sheet_types == "mobile")
		$search_name .= "_mobile";
	elseif ($style_sheet_types == "touchscreen")
		$search_name .= "_touchscreen";

	//First check to see if style sheet exists.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "style_sheets` WHERE `name` = '$search_name'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
	{
		echo "-style sheet already exists-";
		exit;
	}
	
	// Get highest sort order.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "style_sheets` ORDER BY sort_order DESC";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$sort_order = intval($row['sort_order']) + 10;
	
	// Add style sheet to database.
	if (($style_sheet_types == "desktop") || ($style_sheet_types == "all"))
	{
		$search = array("_mobile", "_touchscreen");
		$name = str_replace($search, "", $name);
		
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "style_sheets` SET `name` = '$name', `description` = '$description', show_class_editor = 1, show_custom_styles = 1, `sort_order` = $sort_order, `custom` = 1, `exclude` = $exclude";
		$cnt = $bwb_dbh->exec($query);
		
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "custom_site_css` SET `name` = '" . $name . "'";
		$cnt = $cnt + $bwb_dbh->exec($query);
	}
	
	if (($style_sheet_types == "mobile") || ($style_sheet_types == "all"))
	{
		$add_name = $name;
		$add_description = $description;
		if (strpos($add_name, "_mobile") === false)
			$add_name .= "_mobile";
		if (strpos($add_description, "Mobile") === false)
			$add_description .= " Mobile";
		
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "style_sheets` SET `name` = '$add_name', `description` = '$add_description', show_class_editor = 1, show_custom_styles = 1, `sort_order` = $sort_order, `custom` = 1, `exclude` = $exclude";
		$cnt = $bwb_dbh->exec($query);
		
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "custom_site_css` SET `name` = '" . $add_name . "'";
		$cnt = $cnt + $bwb_dbh->exec($query);
	}
	
	if (($style_sheet_types == "touchscreen") || ($style_sheet_types == "all"))
	{
		$add_name = $name;
		$add_description = $description;
		if (strpos($add_name, "_touchscreen") === false)
			$add_name .= "_touchscreen";
		if (strpos($add_description, "Touchscreen") === false)
			$add_description .= " Touchscreen";
		
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "style_sheets` SET `name` = '$add_name', `description` = '$add_description', show_class_editor = 1, show_custom_styles = 1, `sort_order` = $sort_order, `custom` = 1, `exclude` = $exclude";
		$cnt = $bwb_dbh->exec($query);
		
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "custom_site_css` SET `name` = '" . $add_name . "'";
		$cnt = $cnt + $bwb_dbh->exec($query);
	}
	
	echo $cnt;
?>
