<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 6/26/09		ELL
// Added heigh, width and background-image styles.
// Revised: 8/3/2009	ELL
// Added ability to operate using different class tables and different CSS 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 explicit defining of "text_decoration: none" so that this can be called out explicitly instead of implied only if <a> tag is being used.
// If omitted and no other text decoration is called for, style will simply not define text decoration.  This will be usefull to keep from breaking
// inherited text decorations from nested classes.
// Added switch to turn off custom styles.
// Added text shadow effects.
// Added feed thru for show/hide class editor.
// Revised: 8/24/2009	ELL
// Reworked to work with new AJAX updator that only updates one class block at a time.
// Revised: 10/15/2009	ELL
// Added line-height, float, border-style, outline-style, border-collapse and white-space.  Added inline-block to display.
// Renamed z-index, overflow and clip vars to not have prepended "position_".
// Revised: 2/10/2012	ELL
// Added table prefix handling.  Added references to CSS folder configuration.
//
	include ("check_session.php");
		
	$style_sheet = $_REQUEST['style_sheet'];
	$class_table = $style_sheet . "_classes";
	$css_table = $style_sheet . "_css";
	$css_file = $style_sheet . ".css";
	$show_class_editor = $_REQUEST['show_class_editor'];
	$submit = $_POST['submit'];
	
	
	include ("build_sql_string.php");
	
	//Get data from preview table and move to live table.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "site_css_preview`";
	$result = mysql_query($query, $bwb_link);
	while ($row = mysql_fetch_array($result))
	{
		$class = $row["class"];
		$sub_class_of = $row["sub_class_of"];
		$use_table_data = $row["use_table_data"];
		$font = $row["font"];
		$font_size = $row["font_size"];
		$text_color = $row["text_color"];
		$padding_top = $row["padding_top"];
		$padding_right = $row["padding_right"];
		$padding_bottom = $row["padding_bottom"];
		$padding_left = $row["padding_left"];
		$weight = $row["weight"];
		$italic = $row["italic"];
		$underline = $row["underline"];
		$overline = $row["overline"];
		$blink = $row["blink"];
		$linethrough = $row["linethrough"];
		$font_stretch = $row["font_stretch"];
		$oblique = $row["oblique"];
		$small_caps = $row["small_caps"];
		$no_text_decoration = $row["small_caps"];
		$shadow = $row["shadow"];
		$shadow_color = $row["shadow_color"];
		$shadow_x_offset = $row["shadow_x_offset"];
		$shadow_y_offset = $row["shadow_y_offset"];
		$shadow_blur = $row["shadow_blur"];
		$line_height = $row["line_height"];
		$line_height_units = $row["line_height_units"];
		
		$align = $row["align"];
		$vertical_align = $row["vertical_align"];
		$bkgd_color = $row["bkgd_color"];
		$bkgd_opacity = $row["bkgd_opacity"];
		$border = $row["border"];
		$border_color = $row["border_color"];
		$border_style = $row["border_style"];
		$outline_style = $row["outline_style"];
		$border_collapse = $row["border_collapse"];
		$border_sides = $row["border_sides"];
		
		$margin_top = $row["margin_top"];
		$margin_right = $row["margin_right"];
		$margin_left = $row["margin_left"];
		$margin_bottom = $row["margin_bottom"];
		$display = $row["display"];
		$width = $row["width"];
		$width_units = $row["width_units"];
		$height = $row["height"];
		$height_units = $row["height_units"];
		$background_image = $row["background_image"];
		$background_attachment = $row["background_attachment"];
		$background_repeat = $row["background_repeat"];
		$background_position = $row["background_position"];
		$background_position_x = $row["background_position_x"];
		$background_position_y = $row["background_position_y"];
		$background_position_units = $row["background_position_units"];
		
		$margin_top_units = $row["margin_top_units"];
		$margin_top_auto = $row["margin_top_auto"];
		$margin_right_units = $row["margin_right_units"];
		$margin_right_auto = $row["margin_right_auto"];
		$margin_left_units = $row["margin_left_units"];
		$margin_left_auto = $row["margin_left_auto"];
		$margin_bottom_units = $row["margin_bottom_units"];
		$margin_bottom_auto = $row["margin_bottom_auto"];
		
		$position = $row["position"];
		$position_left = $row["position_left"];
		$position_left_units = $row["position_left_units"];
		$position_top = $row["position_top"];
		$position_top_units = $row["position_top_units"];
		$position_right = $row["position_right"];
		$position_right_units = $row["position_right_units"];
		$position_bottom = $row["position_bottom"];
		$position_bottom_units = $row["position_bottom_units"];
		$z_index = $row["z_index"];
		$overflow = $row["overflow"];
		$clip_top = $row["clip_top"];
		$clip_top_units = $row["clip_top_units"];
		$clip_right = $row["clip_right"];
		$clip_right_units = $row["clip_right_units"];
		$clip_bottom = $row["clip_bottom"];
		$clip_bottom_units = $row["clip_bottom_units"];
		$clip_left = $row["clip_left"];
		$clip_left_units = $row["clip_left_units"];
		$float = $row["float"];
		$white_space = $row["white_space"];
		
		$list_style_type = $row["list_style_type"];
		$list_style_position = $row["list_style_position"];
		$list_style_image = $row["list_style_image"];
		
		$table_layout = $row["table_layout"];
		
		$query2 = "SELECT `use_table_data` FROM " . BWB_TABLE_PREFIX . $css_table . " WHERE `class` = '" . $class . "'";
		$result2 = mysql_query($query2, $bwb_link);
		$row_count = mysql_num_rows($result2);
			
		if ($row_count > 0)
		{
			//Update class record.
			$condition = "`class` = '" . $class . "'";

		$query2 = BuildSQLString(BWB_TABLE_PREFIX . $css_table, "UPDATE", $condition, "", $use_table_data, $font, $font_size, $text_color, $padding_top, $padding_right, $padding_bottom, $padding_left, $weight, $italic,
		$underline, $overline, $blink, $linethrough, $font_stretch, $oblique, $small_caps, $no_text_decoration, $shadow, $shadow_color, $shadow_x_offset,
		$shadow_y_offset, $shadow_blur, $line_height, $line_height_units, $align, $vertical_align, $bkgd_color, $bkgd_opacity, $border, $border_color,
		$border_style, $outline_style, $border_collapse, $border_sides, $margin_top, $margin_top_units, $margin_top_auto, $margin_right, $margin_right_units,
		$margin_right_auto, $margin_left, $margin_left_units, $margin_left_auto, $margin_bottom, $margin_bottom_units, $margin_bottom_auto,
		$display, $width, $width_units, $height, $height_units, $position, $position_left, $position_left_units, $position_top, $position_top_units,
		$position_right, $position_right_units, $position_bottom, $position_bottom_units, $z_index, $overflow,
		$clip_top, $clip_top_units, $clip_right, $clip_right_units, $clip_bottom, $clip_bottom_units, $clip_left, $clip_left_units,
		$background_image, $background_attachment, $background_repeat, $background_position, $background_position_x, $background_position_y,
		$background_position_units, $list_style_type, $list_style_position, $list_style_image, $float, $white_space, $table_layout);
		}
		else
		{
			//Insert new data into table. 
		$insert_key = "class='" . $class . "', ";

		$query2 = BuildSQLString(BWB_TABLE_PREFIX . $css_table, "INSERT INTO", "", $insert_key, $use_table_data, $font, $font_size, $text_color, $padding_top, $padding_right, $padding_bottom, $padding_left, $weight, $italic,
		$underline, $overline, $blink, $linethrough, $font_stretch, $oblique, $small_caps, $no_text_decoration, $shadow, $shadow_color, $shadow_x_offset,
		$shadow_y_offset, $shadow_blur, $line_height, $line_height_units, $align, $vertical_align, $bkgd_color, $bkgd_opacity, $border, $border_color,
		$border_style, $outline_style, $border_collapse, $border_sides, $margin_top, $margin_top_units, $margin_top_auto, $margin_right, $margin_right_units,
		$margin_right_auto, $margin_left, $margin_left_units, $margin_left_auto, $margin_bottom, $margin_bottom_units, $margin_bottom_auto,
		$display, $width, $width_units, $height, $height_units, $position, $position_left, $position_left_units, $position_top, $position_top_units,
		$position_right, $position_right_units, $position_bottom, $position_bottom_units, $z_index, $overflow,
		$clip_top, $clip_top_units, $clip_right, $clip_right_units, $clip_bottom, $clip_bottom_units, $clip_left, $clip_left_units,
		$background_image, $background_attachment, $background_repeat, $background_position, $background_position_x, $background_position_y,
		$background_position_units, $list_style_type, $list_style_position, $list_style_image, $float, $white_space, $table_layout);
		}

		$result2 = mysql_query($query2, $bwb_link);
		
	} // end while
	
	
	if ($style_sheet == "site")
	{
		//Get custom data from preview table.
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "custom_site_css_preview`";
		$result = mysql_query($query, $bwb_link);
		$row = mysql_fetch_array($result);
		$custom_css = $row["custom_css"];
		
		//Start by deleting all old data in table.
		$query = "TRUNCATE TABLE " . BWB_TABLE_PREFIX . "custom_site_css";
		$result = mysql_query($query, $bwb_link);
		
		//Insert new data into table.
		$query = "INSERT INTO " . BWB_TABLE_PREFIX . "custom_site_css (custom_css) VALUES('" . $custom_css . "')";	
		$result = mysql_query($query, $bwb_link);
	}


	/////////////////////////////////////////////
	// Build string for site.css file /////
	
	$site_css_table = $css_table;
	$custom_site_css_table = "custom_site_css";
	include("build_css_string.php");

	////////////////////////////////////////////////////////
	/// Write CSS to CSS file /////////////
	//Test to see if file already exists.
	$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 . mktime();
		if (!rename($file_name, $bkup_file_name))
			echo "Could not rename $file_name to $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0755);
	}
	
	// Create file.
	$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
	$result = (fputs($fh, $css_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0755);
	
	if ($submit == "Save and go back to Editor")
		header("Location: edit_css.php?style_sheet=$style_sheet");
	else
		header("Location: main.php?mode=css");
	
?>
