<?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'];

switch ($version)	{
	case "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);
		break;
	case "6.5.60":
		echo "Copying and converting records from schedule table to events table...<br /><br />";
		
		$sql = "CREATE TABLE IF NOT EXISTS `" . BWB_TABLE_PREFIX . "events` (  `event_id` bigint(20) NOT NULL,  `event_owner` varchar(255) DEFAULT NULL,  `event_status` tinyint(4) DEFAULT NULL,  `event_name` varchar(255) DEFAULT NULL,  `event_description` longtext NOT NULL,  `event_start_date` date NOT NULL,  `event_end_date` date DEFAULT NULL,  `event_group_start_date` date DEFAULT NULL,  `event_start_time` time DEFAULT NULL,  `event_end_time` time DEFAULT NULL,  `event_all_day` tinyint(4) NOT NULL DEFAULT '0',  `recurrence` tinyint(4) NOT NULL DEFAULT '0',  `recurrence_freq` tinytext,  `recurrence_byday` tinytext,  `recurrence_byweekno` smallint(6) DEFAULT NULL,  `recurrence_days` smallint(6) DEFAULT NULL,  `group_id` bigint(20) DEFAULT NULL,  `event_private` tinyint(4) NOT NULL DEFAULT '0',  `event_categories` text,  `HebrewDate` varchar(50) DEFAULT NULL,  `URL` varchar(255) DEFAULT NULL,  `target` varchar(32) DEFAULT NULL,  `event_active` tinyint(4) NOT NULL DEFAULT '1',  `user_table` varchar(64) DEFAULT NULL,  `UserID` varchar(64) DEFAULT NULL,  `AccessLevel` varchar(64) DEFAULT NULL,  `location_id` bigint(20) DEFAULT '-1',  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  `modified` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
		$cnt = $bwb_dbh->exec($sql);
		
		$sql = "ALTER TABLE `" . BWB_TABLE_PREFIX . "events`  ADD PRIMARY KEY (`event_id`),  ADD KEY `group_id` (`group_id`),  ADD KEY `event_status` (`event_status`);";
		$cnt = $bwb_dbh->exec($sql);
	
		$sql = "ALTER TABLE `" . BWB_TABLE_PREFIX . "events`  MODIFY `event_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;";
		$cnt = $bwb_dbh->exec($sql);
		
		$sql = "COMMIT;";
		$cnt = $bwb_dbh->exec($sql);
		
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "schedule";
		$result = $bwb_dbh->query($query);
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$event_id = $row['ID'];
			$event_description = $row['Description'];
			$event_description = str_replace("'", "&#39;", $event_description);
			$EventDate = $row['EventDate'];
			$EventDate_parts = explode("/", $EventDate);
			if (($EventDate_parts[0] != "") && ($EventDate_parts[1] != "") && ($EventDate_parts[2] != ""))
				$event_start_date = $EventDate_parts[2] . "-" . $EventDate_parts[0] . "-" . $EventDate_parts[1];
			else
				continue;
				
			$am_pm = array("am", "pm", "AM", "PM", " ");
			$StartTime = $row['StartTime'];
			$pm = false;
			if (stripos($StartTime, "p") !== false)
				$pm = true;
			$StartTime = str_replace($am_pm, "", $StartTime);
			if ($pm)
				$StartTime = $StartTime + 12;
			elseif (strpos($StartTime, ":") < 2)
				$StartTime = "0" . $StartTime;
			if (strpos($StartTime, ":") === false)
				$event_start_time = $StartTime . ":00:00";
			else
				$event_start_time = $StartTime . ":00";
				
			$EndTime = $row['EndTime'];
			$pm = false;
			if (stripos($EndTime, "p") !== false)
				$pm = true;
			$EndTime = str_replace($am_pm, "", $EndTime);
			if ($pm)
				$EndTime = $EndTime + 12;
			elseif (strpos($EndTime, ":") < 2)
				$EndTime = "0" . $EndTime;
			if (strpos($EndTime, ":") === false)
				$event_end_time = $EndTime . ":00:00";
			else
				$event_end_time = $EndTime . ":00";
				
			$HebrewDate = $row['HebrewDate'];
			$URL = $row['URL'];
			$target = $row['target'];
			$event_active = $row['Active'];
			$user_table = $row['user_table'];
			$UserID = $row['UserID'];
			$AccessLevel = $row['AccessLevel'];
		
			$query = "INSERT INTO " . BWB_TABLE_PREFIX . "events (`event_description`, `event_start_date`, `event_start_time`, `event_end_time`, `HebrewDate`, `URL`, `target`, `event_active`, `user_table`, `UserID`, `AccessLevel`) VALUES ('$event_description', '$event_start_date', '$event_start_time', '$event_end_time', '$HebrewDate', '$URL', '$target', '$event_active', '$user_table', '$UserID', '$AccessLevel')";
			$cnt = $bwb_dbh->exec($query);
		}
		break;
	case "6.5.64":
		echo "Updating plugins_deployed table...<br /><br />";
		$query = "SELECT id, name FROM " . BWB_TABLE_PREFIX . "plugins";
		$result = $bwb_dbh->query($query);
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$plugin_id = $row['id'];
			$name = $row['name'];
			$sql = "UPDATE " . BWB_TABLE_PREFIX . "plugins_deployed SET `name` = '$name' WHERE plugin_id = $plugin_id";
			$cnt = $bwb_dbh->exec($sql);
		}
		$sql = "DELETE FROM " . BWB_TABLE_PREFIX . "plugins_deployed WHERE (`name` = '') OR (`name` IS NULL)";
		$cnt = $bwb_dbh->exec($sql);
		
		$sql = "ALTER TABLE `" . BWB_TABLE_PREFIX . "plugins_deployed` ADD UNIQUE(`name`)";
		$cnt = $bwb_dbh->exec($sql);
}	// end switch ($version)

echo "Update done.";
?>