<?php

/*

This file handles all initial setup of the plugin via 

*/

include_once dirname( __FILE__ ).'/dwpc_tables.php';
		
global $opt_db_version;
$opt_db_version = "0.92";

function opt_install($upgrade='') {
	
   global $wpdb;
   global $opt_db_version;

	dwpc_wpopt_install($upgrade);
	
	if(OPT_DB_PREFIX=='get_the_table_prefix') {$table_prefix = $wpdb->prefix; } else {$table_prefix = OPT_DB_PREFIX;}
  
	  
	  $sql = "CREATE TABLE ".$table_prefix."opt_settings (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			name VARCHAR(64) DEFAULT '' NOT NULL,
			value TEXT DEFAULT '' NOT NULL,
			wp_user_id mediumint(9) NOT NULL, 
			UNIQUE KEY id (id),
			KEY name (wp_user_id,name),
			KEY name2 (name)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_campaigns (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			deleted tinyint(1) NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			name VARCHAR(64) DEFAULT '' NOT NULL,
			internal_description TEXT DEFAULT '' NOT NULL,
			no_mailer TINYINT(1) DEFAULT 0 NOT NULL,
			sendreach_facebook TINYINT(1) DEFAULT 0 NOT NULL,
			email VARCHAR(64) DEFAULT '' NOT NULL, 
			UNIQUE KEY id (id),
			KEY name (name)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_leads (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			deleted tinyint(1) NOT NULL,
			name VARCHAR(128) DEFAULT '' NOT NULL,
			cookie VARCHAR(32) DEFAULT '' NOT NULL,
			campaign_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_optin_link_tracking_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_optin_link_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_optin_link_form_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			dwpc_visitor_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			dwpc_impression_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			subscription_status ENUM('Pending','Subscribed','Unsubscribed'),
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			time_subscribed datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			time_unsubscribed datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			state VARCHAR(48) DEFAULT '' NOT NULL,
			zip VARCHAR(48) DEFAULT '' NOT NULL,
			country VARCHAR(16) DEFAULT '' NOT NULL,
			ip_address VARCHAR(48) DEFAULT '' NOT NULL,
			email_text_id VARCHAR(64) DEFAULT '' NOT NULL,
			api_data TEXT DEFAULT '' NOT NULL,
			custom_info TEXT DEFAULT '' NOT NULL,
			UNIQUE KEY id (id),
			UNIQUE KEY cookie (cookie)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_mailers (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			deleted tinyint(1) NOT NULL,
			parent_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, 
			mailer_type ENUM('AWeber','GetResponse','iContact','ConstantContact','MailChimp','SendReach','RapidMailer','InfusionSoft','OntraPort','GotoWebinar','Custom'),
			name VARCHAR(64) DEFAULT '' NOT NULL,
			list_name VARCHAR(64) DEFAULT '' NOT NULL,
			data TEXT DEFAULT '' NOT NULL,
			subscriptions MEDIUMINT(9) DEFAULT 0 NOT NULL,
			error_flag TINYINT(1) DEFAULT 0 NOT NULL,
			custom_form_html TEXT DEFAULT '' NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_secured_pages (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			opt_campaign_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_form_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			deleted tinyint(1) NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, 
			name VARCHAR(64) DEFAULT '' NOT NULL,
			internal_description VARCHAR(255) DEFAULT '' NOT NULL,
			url VARCHAR(255) DEFAULT '' NOT NULL,
			page_type ENUM('Page','Link') DEFAULT 'Page' NOT NULL,
			wildcard TINYINT(1) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id),
			KEY name (name)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_optin_links (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			text_id VARCHAR(6) DEFAULT '' NOT NULL,
			deleted tinyint(1) NOT NULL,
			name VARCHAR(64) DEFAULT '' NOT NULL,
			opt_campaign_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_form_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_secured_page_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, 
			link_type ENUM('Popup','Direct'),
			optin_not_required tinyint(1) NOT NULL,
			optin_no_redirect tinyint(1) NOT NULL,
			optin_open_in_new_tab tinyint(1) NOT NULL,
			getresponse_double_optin tinyint(1) NOT NULL,
			getresponse_unsubscribe tinyint(1) NOT NULL,
			opt_split_test_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id),
			UNIQUE KEY text_id (text_id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_optin_link_tracking (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			filtered tinyint(1) NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			tracking_type ENUM('Load','Click','View'),
			dwpc_visitor_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			dwpc_impression_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_optin_link_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id),
			KEY filtered (filtered),
			KEY time_created (time_created),
			KEY time_created_by_filter (filtered,time_created)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_forms (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			parent_id mediumint(9) NOT NULL, 
			deleted tinyint(1) NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			name VARCHAR(64) DEFAULT '' NOT NULL,
			file_url VARCHAR(255) DEFAULT '' NOT NULL,
			data TEXT DEFAULT '' NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			CREATE TABLE ".$table_prefix."opt_forms_tracking (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			filtered tinyint(1) NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			tracking_type ENUM('Load','Close','Hover','Click'),
			dwpc_visitor_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			dwpc_impression_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_optin_link_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_form_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_split_test_item_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id),
			KEY filtered (filtered),
			KEY time_created (time_created),
			KEY time_created_by_filter (filtered,time_created)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
			  
			  
			CREATE TABLE ".$table_prefix."opt_leads_to_mailers (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			opt_lead_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_mailer_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id),
			KEY time_created (time_created)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;		
			  
			CREATE TABLE ".$table_prefix."opt_mailers_to_campaigns (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			opt_campaign_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_mailer_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;			  
			  
			CREATE TABLE ".$table_prefix."opt_pages_to_campaigns (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			opt_campaign_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_secured_page_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
						
			 CREATE TABLE ".$table_prefix."opt_split_tests (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			name VARCHAR(255) DEFAULT '' NOT NULL,
			description TEXT DEFAULT '' NOT NULL,
			deleted tinyint(1) NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;	
			  
			 CREATE TABLE ".$table_prefix."opt_split_test_items (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			opt_split_test_id mediumint(9) NOT NULL,
			opt_form_id mediumint(9) NOT NULL,
			item_weight mediumint(4) NOT NULL, 
			deleted tinyint(1) NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;				  
			  
			CREATE TABLE ".$table_prefix."opt_clients (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			deleted tinyint(1) NOT NULL,
			time_created datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			name VARCHAR(64) DEFAULT '' NOT NULL,
			internal_description TEXT DEFAULT '' NOT NULL,
			email VARCHAR(64) DEFAULT '' NOT NULL,
			email_text TEXT DEFAULT '' NOT NULL,
			UNIQUE KEY id (id),
			KEY name (name)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;			  
			  
			 CREATE TABLE ".$table_prefix."opt_client_urls (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			opt_client_id mediumint(9) NOT NULL,
			client_url TEXT DEFAULT '' NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;		  
			  
			CREATE TABLE ".$table_prefix."opt_clients_to_campaigns (
			id mediumint(9) NOT NULL AUTO_INCREMENT, 
			opt_client_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			opt_campaign_id MEDIUMINT(9) DEFAULT 0 NOT NULL,
			UNIQUE KEY id (id)
			  )CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
					
			  
			";




   require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
   dbDelta($sql);
	
	$wpdb->query('ALTER TABLE '.$table_prefix.'opt_leads
		ADD FOREIGN KEY (dwpc_visitor_id) REFERENCES '.$table_prefix.'dwpc_visitors(id),
		ADD FOREIGN KEY (dwpc_impression_id) REFERENCES '.$table_prefix.'dwpc_impressions(id),
		ADD FOREIGN KEY (opt_optin_link_tracking_id) REFERENCES '.$table_prefix.'opt_optin_link_tracking(id),
		ADD FOREIGN KEY (opt_optin_link_id) REFERENCES '.$table_prefix.'opt_optin_links(id),
		ADD FOREIGN KEY (opt_optin_form_tracking_id) REFERENCES '.$table_prefix.'opt_optin_form_tracking(id),
		ADD FOREIGN KEY (campaign_id) REFERENCES '.$table_prefix.'opt_campaigns(id);');
	
	$wpdb->query('ALTER TABLE '.$table_prefix.'opt_optin_links
		ADD FOREIGN KEY (dwpc_visitor_id) REFERENCES '.$table_prefix.'dwpc_visitors(id),
		ADD FOREIGN KEY (opt_split_test_id) REFERENCES '.$table_prefix.'opt_split_tests(id),
		ADD FOREIGN KEY (opt_campaign_id) REFERENCES '.$table_prefix.'opt_campaigns(id);');
	
	$wpdb->query('ALTER TABLE '.$table_prefix.'opt_optin_link_tracking 
		ADD FOREIGN KEY (dwpc_visitor_id) REFERENCES '.$table_prefix.'dwpc_visitors(id),
		ADD FOREIGN KEY (dwpc_impression_id) REFERENCES '.$table_prefix.'dwpc_impressions(id),
		ADD FOREIGN KEY (opt_optin_link_id) REFERENCES '.$table_prefix.'opt_optin_links(id);');

	$wpdb->query('ALTER TABLE '.$table_prefix.'opt_forms_tracking
		ADD FOREIGN KEY (dwpc_visitor_id) REFERENCES '.$table_prefix.'dwpc_visitors(id),
		ADD FOREIGN KEY (dwpc_impression_id) REFERENCES '.$table_prefix.'dwpc_impressions(id),
		ADD FOREIGN KEY (opt_optin_link_id) REFERENCES '.$table_prefix.'opt_optin_links(id),
		ADD FOREIGN KEY (opt_optin_link_tracking_id) REFERENCES '.$table_prefix.'opt_optin_link_tracking(id),
		ADD FOREIGN KEY (forms_id) REFERENCES '.$table_prefix.'opt_forms(id);');
	
	$wpdb->query('ALTER TABLE '.$table_prefix.'opt_leads_to_mailers
		ADD FOREIGN KEY (opt_lead_id) REFERENCES '.$table_prefix.'opt_leads(id),
		ADD FOREIGN KEY (opt_mailer_id) REFERENCES '.$table_prefix.'opt_mailers(id);');
		
	$wpdb->query('ALTER TABLE '.$table_prefix.'opt_mailers_to_campaigns
		ADD FOREIGN KEY (opt_campaign_id) REFERENCES '.$table_prefix.'opt_campaigns(id),
		ADD FOREIGN KEY (opt_mailer_id) REFERENCES '.$table_prefix.'opt_mailers(id);');
		
	$wpdb->query('ALTER TABLE '.$table_prefix.'opt_pages_to_campaigns
		ADD FOREIGN KEY (opt_campaign_id) REFERENCES '.$table_prefix.'opt_campaigns(id),
		ADD FOREIGN KEY (opt_secured_page_id) REFERENCES '.$table_prefix.'opt_secured_pages(id);');
	

   update_option("opt_db_version", $opt_db_version); 

}


function opt_uninstall() {
	
   global $wpdb;
   global $opt_db_version;
  
  dwpc_wpopt_uninstall();
  
  //Delete opt Tables - FULL UNINSTALL REMOVE COMMENT TAGS BELOW AND THEN DEACTIVE PLUGIN
  /*   
  $wpdb->query("DROP TABLE ".OPT_DB_PREFIX."opt_load_times");
  $wpdb->query("DROP TABLE ".OPT_DB_PREFIX."opt_verbose_log");
  $wpdb->query("DROP TABLE ".OPT_DB_PREFIX."opt_settings");
  */
  
}



        ?>