<?php

/*

___Canvas Installation Scripts__________________________________

Creates the necessary SQL tables for a clean Canvas install.

________________________________________________________________

*/


function canvas_clean_install() {
    global $wpdb, $canvas;
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->main."");
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->variables."");
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->options."");
	canvas_install();
	return true;
}

function canvas_clean_deactivate() {
    	global $wpdb, $canvas;
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->main."");
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->duplicates."");
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->variables."");
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->options."");
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->zone_options."");
	$wpdb->query("DROP TABLE IF EXISTS ".$canvas->ink."");

	return true;
}



function canvas_create_db() {
	global $wpdb, $canvas;

	${$canvas->main} = "CREATE TABLE ".$canvas->main." (
	        block_id INT NOT NULL AUTO_INCREMENT,
	        PRIMARY KEY(block_id),
   			module_id INT NOT NULL,
			type tinytext NOT NULL,
   			zone tinytext NOT NULL,
   			position int(6) NOT NULL,
   			author tinytext NOT NULL,
   			description text NOT NULL,
   			block_group tinytext NOT NULL,
   			uri text NOT NULL,
   			path text NOT NULL,
   			theme tinytext NOT NULL,
   			ubi text NOT NULL,
			template_name text NOT NULL,
			template_size text NOT NULL,
			duplicate_id INT NOT NULL,
			page text NOT NULL
			);";

	${$canvas->duplicates} = "CREATE TABLE ".$canvas->duplicates." (
	        duplicate_id INT NOT NULL AUTO_INCREMENT,
	        PRIMARY KEY(duplicate_id),
   			module_id INT NOT NULL,
			duplicate_name text NOT NULL
			);";


	${$canvas->variables} = "CREATE TABLE ".$canvas->variables." (
   			variable_id INT NOT NULL AUTO_INCREMENT,
   			PRIMARY KEY(variable_id),
   			variable_name text NOT NULL,
   			parent INT NOT NULL,
   			type text NOT NULL,
   			value text NOT NULL,
   			default_value text NOT NULL,
   			description text NOT NULL
			);";

	${$canvas->options} = "CREATE TABLE ".$canvas->options." (
   			option_id INT NOT NULL AUTO_INCREMENT,
   			PRIMARY KEY(option_id),
   			var_id INT NOT NULL,
   			option_text text NOT NULL,
   			option_value text NOT NULL,
			option_params text NOT NULL
			);";

	${$canvas->zone_options} = "CREATE TABLE ".$canvas->zone_options." (
   			option_id INT NOT NULL AUTO_INCREMENT,
   			PRIMARY KEY(option_id),
   			zone text NOT NULL,
   			option_name text NOT NULL,
   			value text NOT NULL,
   			theme text NOT NULL
			);";

	${$canvas->ink} = "CREATE TABLE ".$canvas->ink." (
	        element_id INT NOT NULL AUTO_INCREMENT,
	        PRIMARY KEY(element_id),
   			element text NOT NULL,
   			theme text NOT NULL,
   			color text NOT NULL,
   			background text NOT NULL,
   			border text NOT NULL,
   			font_family text NOT NULL,
   			font_size text NOT NULL,
   			font_style text NOT NULL,
   			other text NOT NULL
			);";

	// try to get around
	// these includes like http://trac.mu.wordpress.org/ticket/384 
	// and http://www.quirm.net/punbb/viewtopic.php?pid=832#p832

	if (file_exists(ABSPATH . 'wp-includes/pluggable.php')) {
		require_once(ABSPATH . 'wp-includes/pluggable.php');
	} else {
		require_once(ABSPATH . 'wp-includes/pluggable-functions.php');
	}
	require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
	
	foreach($canvas as $table)
		dbDelta(${$table});
	
	return;
}

function canvas_install($DBChanged=false) {
    	global $wpdb, $canvas; //, $installed, $canvas_external_plugins;
	
     	if(!$wpdb->get_var("SHOW TABLES LIKE '".$canvas->main."'") == $canvas->main ||
			$DBChanged) {
		canvas_create_db();
		update_option('RC_CWP_BLOG_DB_VERSION', RC_CWP_DB_VERSION);
		ink_clean_install();
		//canvas_check_theme();
		
		
		update_option('canvas_auto_publish','false'); // for future releases

		/*if(file_exists(get_template_directory().'/canvas.php') && !$wpdb->get_results("SELECT block_id FROM ".$canvas->main." WHERE theme = '".get_option('template')."'")) {
			$position = 1;
			$canvas_dir = CANVASPATH.'/modules/';
			list($plugins, $position) = canvas_import_plugins($position, $canvas_dir);
		} elseif(file_exists(get_template_directory().'/canvas.php')) {
			canvas_reload();
		}*/

		

	}
	
	return true;
}


function canvas_reload() {
	global $wpdb, $canvas, $canvas_external_plugins;
	$current_blocks = $wpdb->get_results("SELECT block_id, path FROM ".$canvas->main." WHERE theme = '".get_option('template')."' AND type = 'block'");
	$current_plugins = $wpdb->get_results("SELECT block_id, path FROM ".$canvas->main." WHERE theme = '".get_option('template')."' AND type = 'plugin'");
	$positions = $wpdb->get_var("SELECT MAX(position) FROM ".$canvas->main." WHERE theme = '".get_option('template')."' AND zone = 'shelf'");
	if($positions) $position = $positions->position;
		else $position = 1;
	$block_count = 0; $plugin_count = 0;
	foreach($current_blocks as $block) {
		$current_block_paths[] = $block->path;
	}
	foreach($current_plugins as $plugin) {
		$current_plugin_paths[] = $plugin->path;
	}

	// Search for modules
	$dir = CANVASPATH.'/modules/';
	canvas_import_plugins($position, $dir);
		

	if($block_count > 0 || $plugin_count > 0) $message = '<p>Installed '.$block_count.' blocks and '.$plugin_count.' plugins.</p>';
		else $message = '<p>No new blocks or plugins found.</p>';

	return $message;
}

?>