<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 7/30/2009	ELL
// Added ability to operate using different class tables.  This was done to give the ability
// to style calendar grids and event grids without adding 14 more classes to the site style list.
// Added feed thru to show/hide custom styles.
// Revised: 2/8/2012	ELL
// Added table prefix handling.
// Revised: 2/18/2013	ELL
// Added creating link set.
// 
// 
// 
// 
	include ("check_session.php");

	$class = $_POST['class'];
	$subclass = $_POST['subclass'];
	$create_link_set = $_POST['create_link_set'];
	$style_sheet = $_REQUEST['style_sheet'];
	$class_table = $style_sheet . "_classes";
	
	//Get descriptions.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` WHERE `class` = '$class'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$class_description = $row["description"];
	$sort_order = $row["sort_order"] + 1;
	
	if (isset($create_link_set))
		$subclass = 'a:link';
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` WHERE `class` = '$subclass'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$subclass_description = $row["description"];
	
	$class_subclass = $class . " " . $subclass;
	$description = $class_description . ": " . $subclass_description;
	$description = addslashes($description);
	
	$search = array(".", "#", " ", ",");
	$replace = array("PERIOD", "POUND", "SPACE", "COMMA");
	$class_str = str_replace($search, $replace, $class_subclass);
	$_SESSION['last_class_edited'] = $class_str;
	
	// Add relationship to database.
	$query = "INSERT INTO `" . BWB_TABLE_PREFIX . $class_table . "` SET `class` = '$class_subclass', `description` = '$description', `sort_order` = $sort_order, `subclass_sort_order` = 500, `custom` = 1";
	$cnt = $bwb_dbh->exec($query);
	
	if (isset($create_link_set))
	{
		$sort_order = $sort_order + 1;
		
		$subclass = 'a:visited';
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` WHERE `class` = 'a:visited'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$subclass_description = $row["description"];
		
		$class_subclass = $class . " " . $subclass;
		$description = $class_description . ": " . $subclass_description;
		$description = addslashes($description);
		
		$class_str = str_replace($search, $replace, $class_subclass);
		
		// Add relationship to database.
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . $class_table . "` SET `class` = '$class_subclass', `description` = '$description', `sort_order` = $sort_order, `subclass_sort_order` = 500, `custom` = 1";
		$cnt = $bwb_dbh->exec($query);
		
		//......................................
		$sort_order = $sort_order + 1;
		
		$subclass = 'a:hover';
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` WHERE `class` = 'a:hover'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$subclass_description = $row["description"];
		
		$class_subclass = $class . " " . $subclass;
		$description = $class_description . ": " . $subclass_description;
		$description = addslashes($description);
		
		$class_str = str_replace($search, $replace, $class_subclass);
		
		// Add relationship to database.
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . $class_table . "` SET `class` = '$class_subclass', `description` = '$description', `sort_order` = $sort_order, `subclass_sort_order` = 500, `custom` = 1";
		$cnt = $bwb_dbh->exec($query);
		
		//......................................
		$sort_order = $sort_order + 1;
		
		$subclass = 'a:active';
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . $class_table . "` WHERE `class` = 'a:active'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$subclass_description = $row["description"];
		
		$class_subclass = $class . " " . $subclass;
		$description = $class_description . ": " . $subclass_description;
		$description = addslashes($description);
		
		$class_str = str_replace($search, $replace, $class_subclass);
		
		// Add relationship to database.
		$query = "INSERT INTO `" . BWB_TABLE_PREFIX . $class_table . "` SET `class` = '$class_subclass', `description` = '$description', `sort_order` = $sort_order, `subclass_sort_order` = 500, `custom` = 1";
		$cnt = $bwb_dbh->exec($query);
	}
	
	header("Location: classes.php?style_sheet=$style_sheet&show_class_editor=1");
	
?>
