<?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.
// Revised: 12/27/2014	ELL
// Added 'hover' subclass and fixed subclass descriptions for link set.
// Revised: 2/13/2015	ELL
// Added class selector operator handling.
// 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.
// 
// 
// 
	include ("check_session.php");

	$class = $_POST['class'];
	$subclass = $_POST['subclass'];
	$operator = $_POST['operator'];
	$selector_param = $_POST['selector_param'];
	$create_link_set = $_POST['create_link_set'];
	$style_sheet = $_REQUEST['style_sheet'];
	
	$no_op_selectors = array(":link",":hover",":visited",":active","::before","::after",":checked",":disabled",":enabled",":focus",":target","::selection",":read-only",":read-write",":not",":empty","::first-letter","::first-line",":first-child",":last-child",":nth-child",":nth-last-child",":only-child",":first-of-type",":last-of-type",":nth-of-type",":nth-last-of-type",":only-of-type",":in-range",":out-of-range",":valid",":invalid",":lang",":required",":optional",":root");
	
	$selectors_w_params = array(":not",":nth-child",":nth-last-child",":nth-of-type",":nth-last-of-type",":lang");
	
	//Get descriptions.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "site_classes` WHERE `style_sheet` = '$style_sheet' AND `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';
		$subclass_description = 'A:Link';
	}
	elseif (!in_array($subclass, $no_op_selectors))	{
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "site_classes` WHERE `style_sheet` = '$style_sheet' AND `class` = '$subclass'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$subclass_description = $row["description"];
	}
	else	{
		$subclass_description = str_replace(":", "", $subclass);
		$subclass_description = strtoupper(substr($subclass_description, 0, 1)) . substr($subclass_description, 1);
		if (in_array($subclass, $selectors_w_params))
			$subclass_description .= "(" . $selector_param . ")";
	}
	
	if (in_array($subclass, $no_op_selectors))
	{
		$class_subclass = $class . $subclass;
		if (in_array($subclass, $selectors_w_params))
			$class_subclass .= "(" . $selector_param . ")";
	}
	else
	{
		switch ($operator)	{
			case "":
				$operator = " ";
				break;
			case ",":
				$operator = ", ";
				break;
			case "&gt;":
				$operator = " &gt; ";
				break;
			case ">":
				$operator = " > ";
				break;
			case "+":
				$operator = " + ";
				break;
			case "~":
				$operator = " ~ ";
				break;
		}
		$class_subclass = $class . $operator . $subclass;
	}
		
	$description = $class_description . ": " . $subclass_description;
	$description = addslashes($description);
	
	$search = array(".", "#", " ", ",", ">", "&gt;", "+", "~");
	$replace = array("PERIOD", "POUND", "SPACE", "COMMA", "GREATERTHAN", "GREATERTHAN", "PLUS", "TILDA");
	$class_str = str_replace($search, $replace, $class_subclass);
	$_SESSION['last_class_edited'] = $class_str;
	
	// Add relationship to database.
	$query = "INSERT INTO `" . BWB_TABLE_PREFIX . "site_classes` SET `style_sheet` = '$style_sheet', `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';
		$subclass_description = 'A:Visited';
		
		$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 . "site_classes` SET `style_sheet` = '$style_sheet', `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';
		$subclass_description = 'A:Hover';
		
		$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 . "site_classes` SET `style_sheet` = '$style_sheet', `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';
		$subclass_description = 'A:Active';
		
		$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 . "site_classes` SET `style_sheet` = '$style_sheet', `class` = '$class_subclass', `description` = '$description', `sort_order` = $sort_order, `subclass_sort_order` = 500, `custom` = 1";
		$cnt = $bwb_dbh->exec($query);
	}
	
	echo 1;
?>
