<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 10/11/2012	ELL
// 
//
	include ("check_session.php");

	$plugin_id = $_REQUEST['plugin_id'];
	
	// Find CSS tables and so forth to remove.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "plugins` WHERE `id`=$plugin_id";
	$result = mysql_query($query, $bwb_link);
	$row = mysql_fetch_array($result);
	$style_sheet = $row["style_sheet"];
	$name = $row["name"];
	
	// Remove style sheet from list.
	if (isset($style_sheet) && ($style_sheet != ""))
	{
		// Check for other plugins using the same style sheet and deployed.	
		$skip_remove = false;
		$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "plugins` WHERE `style_sheet`='" . $style_sheet . "' AND `id`<>" . $plugin_id;
		$result = mysql_query($query, $bwb_link);
		while ($row = mysql_fetch_array($result))
		{
			if ($row['deployed'] == 1)
				$skip_remove = true;
		}
		
		if (!$skip_remove)
		{
			$query = "DELETE FROM " . BWB_TABLE_PREFIX . "style_sheets WHERE `name`='" . $style_sheet . "'";
			$result = mysql_query($query, $bwb_link);
		
			// Drop class and CSS tables.
			$table = $style_sheet . "_classes";
			$query = "DROP TABLE IF EXISTS " . BWB_TABLE_PREFIX . $table;
			$result = mysql_query($query, $bwb_link);
			
			$table = $style_sheet . "_css";
			$query = "DROP TABLE IF EXISTS " . BWB_TABLE_PREFIX . $table;
			$result = mysql_query($query, $bwb_link);
		}
	}
	
	// Mark plugin as not being deployed.
	$query = "UPDATE " . BWB_TABLE_PREFIX . "plugins SET `deployed`=0 WHERE `id`=$plugin_id";
	$result = mysql_query($query, $bwb_link);
	
	header("Location: main.php?mode=admin");
	
?>
