<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 10/29/2018		ELL

include_once ("check_session.php");

echo "Starting update...<br /><br />";

$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "version";
$result = $bwb_dbh->query($query);
$row = $result->fetch(PDO::FETCH_ASSOC);
$version = $row['version'];

if ($version == "6.5.50")
{
	echo "Searching for width_prefix...<br /><br />";
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_css WHERE (width_prefix <> '-not selected-') AND (width_prefix <> '')";
	$result = $bwb_dbh->query($query);
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$style_sheet = $row['style_sheet'];
		$class = $row['class'];
		$query2 = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_css WHERE style_sheet = '$style_sheet' AND `class` = '$class'";
		$result2 = $bwb_dbh->query($query2);
		$row2 = $result2->fetch(PDO::FETCH_ASSOC);
		$width = $row2['width'];
		$width_units = $row2['width_units'];
		
		if (($row['width_prefix'] == "min") || ($row['width_prefix'] == "min-max"))	{
			echo "Copying min-width...<br />";
			$query3 = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET min_width = '$width', min_width_units = '$width_units', width = -1 WHERE style_sheet = '$style_sheet' AND `class` = '$class'";
		}
		elseif ($row['width_prefix'] == "max")	{
			echo "Copying max-width...<br />";
			$query3 = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET max_width = '$width', max_width_units = '$width_units', width = -1 WHERE style_sheet = '$style_sheet' AND `class` = '$class'";
		}
		if (($row['width_prefix'] == "min") || ($row['width_prefix'] == "max") || ($row['width_prefix'] == "min-max"))
			$cnt = $bwb_dbh->exec($query3);
	}
	
	echo "<br />Searching for height_prefix...<br /><br />";
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_css WHERE (height_prefix <> '-not selected-') AND (height_prefix <> '')";
	$result = $bwb_dbh->query($query);
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$id = $row['id'];
		$query2 = "SELECT * FROM " . BWB_TABLE_PREFIX . "site_css WHERE style_sheet = '$style_sheet' AND `class` = '$class'";
		$result2 = $bwb_dbh->query($query2);
		$row2 = $result2->fetch(PDO::FETCH_ASSOC);
		$height = $row2['height'];
		$height_units = $row2['height_units'];
		
		if (($row['height_prefix'] == "min") || ($row['height_prefix'] == "min-max"))	{
			echo "Copying min-height...<br />";
			$query3 = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET min_height = '$height', min_height_units = '$height_units', height = -1 WHERE style_sheet = '$style_sheet' AND `class` = '$class'";
		}
		elseif ($row['height_prefix'] == "max")	{
			echo "Copying max-height...<br />";
			$query3 = "UPDATE " . BWB_TABLE_PREFIX . "site_css SET max_height = '$height', max_height_units = '$height_units', height = -1 WHERE style_sheet = '$style_sheet' AND `class` = '$class'";
		}
		if (($row['height_prefix'] == "min") || ($row['height_prefix'] == "max") || ($row['height_prefix'] == "min-max"))
			$cnt = $bwb_dbh->exec($query3);
	}
	
	echo "<br />Dropping width_prefix and height_prefix...<br /><br />";
	$query = "ALTER TABLE `" . BWB_TABLE_PREFIX . "site_css` DROP `width_prefix`";
	$cnt = $bwb_dbh->exec($query);
	$query = "ALTER TABLE `" . BWB_TABLE_PREFIX . "site_css` DROP `height_prefix`";
	$cnt = $bwb_dbh->exec($query);
}

echo "Update done.";
?>