<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 11/9/2016	ELL
// 
// 
	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")
		$name .= "_mobile";

	//First check to see if style sheet exists.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "style_sheets` WHERE `name` = '$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 == "both"))
	{
		$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 = "CREATE TABLE IF NOT EXISTS " . BWB_TABLE_PREFIX . $name . "_classes SELECT * FROM ". BWB_TABLE_PREFIX . "menu1_classes";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . $name . "_classes` ADD PRIMARY KEY (`class`)";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . $name . "_classes`";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = "CREATE TABLE IF NOT EXISTS " . BWB_TABLE_PREFIX . $name . "_css SELECT * FROM ". BWB_TABLE_PREFIX . "site_css";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . $name . "_css` ADD PRIMARY KEY (`class`)";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . $name . "_css`";
		$cnt = $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 == "both"))
	{
		if ($style_sheet_types == "both")
			$name .= "_mobile";
		$description .= " Mobile";
		
		$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 = "CREATE TABLE IF NOT EXISTS " . BWB_TABLE_PREFIX . $name . "_classes SELECT * FROM ". BWB_TABLE_PREFIX . "menu1_classes";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . $name . "_classes` ADD PRIMARY KEY (`class`)";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . $name . "_classes`";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = "CREATE TABLE IF NOT EXISTS " . BWB_TABLE_PREFIX . $name . "_css SELECT * FROM ". BWB_TABLE_PREFIX . "site_css";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = " ALTER TABLE `" . BWB_TABLE_PREFIX . $name . "_css` ADD PRIMARY KEY (`class`)";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = "TRUNCATE TABLE `" . BWB_TABLE_PREFIX . $name . "_css`";
		$cnt = $cnt + $bwb_dbh->exec($query);
		
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "custom_site_css` SET `name` = '" . $name . "'";
		$cnt = $cnt + $bwb_dbh->exec($query);
	}
	
	echo $cnt;
?>
