<?php
//
//
//
function opt_forms_create_campaign($prefix='') {
	
	global $wpdb;
	global $dwpc;	
	
		
	//Create campaign get campaign id
	if(isset($_POST['campaign_name']) && strlen($_POST['campaign_name']) > 5) { 
		
		$no_mailer = 0;
		if(isset($_POST['no_mailer'])&&$_POST['no_mailer']!=''){
			$no_mailer = 1;
		}
		
		$data = array(
						'id' => NULL,
						'time_created' => current_time('mysql'),
						'name' => $_POST['campaign_name'],
						'internal_description' => $_POST['campaign_description'],
						'no_mailer' => $no_mailer,
						'sendreach_facebook' => $_POST['sendreach_facebook']
					);
		
		$dwpc->insert(OPT_DB_PREFIX.'opt_campaigns',$data);		
		$campaign_id = $wpdb->insert_id;	
		
	} else { return; }
	
	
	//Loop the post array here
	foreach($_POST as $key => $value) {
				
		//Create new secure links
		if(strpos($key,$prefix.'new_secure_link_name')!==false) {
			
			$new_id = end(explode('_',$key));
			
			unset($data);
			$data['name'] = $_POST[$prefix.'new_secure_link_name_'.$new_id];
			$data['internal_description'] = $_POST[$prefix.'new_secure_link_description_'.$new_id];
			$data['url'] = $_POST[$prefix.'new_secure_link_url_'.$new_id];			
			
			 opt_forms_create_secured_page($data);
			 $new_secure_links[$new_id]	= $wpdb->insert_id;
			
		}		
		
		//Assign Mailing Lists		
		elseif(strpos($key,'mailer_list_')!==false) {
												
			unset($data);
			$data['opt_campaign_id'] = $campaign_id;
			$data['opt_mailer_id'] = $value;
			
			$dwpc->insert(OPT_DB_PREFIX.'opt_mailers_to_campaigns',$data);	
		
		}			
			
		//Assign secured links
		elseif(strpos($key,$prefix.'link_')!==false) {
									
			unset($data);
			$data['opt_campaign_id'] = $campaign_id;
			$data['opt_secured_page_id'] = $value;
			
			$dwpc->insert(OPT_DB_PREFIX.'opt_pages_to_campaigns',$data);	
		
		}	
				
		//Edit existing secure links
		if(strpos($key,$prefix.'edit_secure_link_name')!==false) {
			
			$edit_id = end(explode('_',$key));
			
			unset($data);
			$data['name'] = $_POST[$prefix.'edit_secure_link_name_'.$edit_id];
			$data['internal_description'] = $_POST[$prefix.'edit_secure_link_description_'.$edit_id];
			$data['url'] = $_POST[$prefix.'edit_secure_link_url_'.$edit_id];	
			
			
			opt_forms_edit_secured_page($data,$edit_id);
			
			
		}
		
	}
	
	//Associate newly created secure links to this campaign	
	if(is_array($new_secure_links)) {
		foreach($new_secure_links as $key => $value) {
										
				unset($data);
				$data['opt_campaign_id'] = $campaign_id;
				$data['opt_secured_page_id'] = $value;
				
				$dwpc->insert(OPT_DB_PREFIX.'opt_pages_to_campaigns',$data);	
			
		}	
	}
	
	
	//Loop through again to creat optin links with new secure pages properly
	foreach($_POST as $key => $value) {
	
		
		//Create Optin Links From The Form
		if($key == $prefix.'optin_link_name' && $value!='') {
							
								
			unset($data);
			$data['name'] = $_POST[$prefix.'optin_link_name'];
						
			if(strpos($_POST[$prefix.'optin_link_secure_link'],'new_')!==false) { 		
				
				$secure_id = end(explode('_',$_POST[$prefix.'optin_link_secure_link']));								
				$data['opt_secured_page_id'] = $new_secure_links[$secure_id];
			
			} else {
				
				$data['opt_secured_page_id'] = $_POST[$prefix.'optin_link_secure_link'];
				
			}
							
			$data['opt_form_id'] = $_POST[$prefix.'optin_link_form'];
			$data['link_type'] = $_POST[$prefix.'optin_link_type'];
			$data['optin_not_required'] = $_POST[$prefix.'optin_link_not_required'];
			$data['optin_no_redirect'] = $_POST[$prefix.'optin_no_redirect'];
			$data['optin_open_in_new_tab'] = $_POST[$prefix.'optin_open_in_new_tab'];
			$data['getresponse_double_optin'] = $_POST[$prefix.'getresponse_optin_link_double_optin'];
			$data['getresponse_unsubscribe'] = $_POST[$prefix.'getresponse_optin_link_unsubscribe'];
			
			opt_forms_create_link($data,$campaign_id);
		
		}	
		
		//Create New Optin Links From The List
		elseif(strpos($key,$prefix.'new_optin_link_name_')!==false) {
							
			$new_id = end(explode('_',$key));
								
			unset($data);
			$data['name'] = $_POST[$prefix.'new_optin_link_name_'.$new_id];			
				
						
			if(strpos($_POST[$prefix.'new_optin_link_secure_link_'.$new_id],'new_')!==false) { 		
				
				$secure_id = end(explode('_',$_POST[$prefix.'new_optin_link_secure_link_'.$new_id]));								
				$data['opt_secured_page_id'] = $new_secure_links[$secure_id];
				
			
			} else {
				
				$data['opt_secured_page_id'] = $_POST[$prefix.'new_optin_link_secure_link_'.$new_id];
				
			}
			
			$data['opt_form_id'] = $_POST[$prefix.'new_optin_link_optin_form_'.$new_id];
			
			if($_POST[$prefix.'new_secure_link_type_'.$new_id]==1) { $data['link_type'] = 'Popup'; }
			else { $data['link_type'] = 'Direct'; }						
			
			$data['optin_not_required'] = $_POST[$prefix.'new_optin_link_not_required_'.$new_id];
			$data['optin_no_redirect'] = $_POST[$prefix.'new_optin_no_redirect_'.$new_id];
			$data['optin_open_in_new_tab'] = $_POST[$prefix.'new_optin_open_in_new_tab_'.$new_id];
			$data['getresponse_double_optin'] = $_POST[$prefix.'new_getresponse_optin_link_double_optin_'.$new_id];
			$data['getresponse_unsubscribe'] = $_POST[$prefix.'new_getresponse_optin_link_unsubscribe_'.$new_id];
			opt_forms_create_link($data,$campaign_id);
		
		}		
		
		
	}
	

		

	//unset($_POST);
	
	return 1;
	
}


function opt_forms_edit_campaign($prefix='') {
	
	global $wpdb;
	global $dwpc;		
			
	//Edit campaign get campaign id
	if(isset($_POST['campaign_name']) && strlen($_POST['campaign_name']) > 5) { 
		
		$campaign_id = $_POST['edit_campaign_'.str_replace('_','',$prefix)];
		
		$no_mailer = 0;
		if(isset($_POST['no_mailer']) &&$_POST['no_mailer']!=''){
			$no_mailer = 1;	
		}
		
		$data = array(			
						'time_created' => current_time('mysql'),
						'name' => $_POST['campaign_name'],
						'internal_description' => $_POST['campaign_description'],
						'no_mailer' => $no_mailer,
						'sendreach_facebook' => $_POST['sendreach_facebook']
					);
		
		$dwpc->update(OPT_DB_PREFIX.'opt_campaigns',$data,array('id'=>$campaign_id));			
		
	} else { return; }
	
	//Delete all existing associations here
	$wpdb->query('DELETE FROM '.OPT_DB_PREFIX.'opt_mailers_to_campaigns WHERE opt_campaign_id = '.$campaign_id);
	$wpdb->query('DELETE FROM '.OPT_DB_PREFIX.'opt_pages_to_campaigns WHERE opt_campaign_id = '.$campaign_id);	
	
	
	//Loop the post array here
	foreach($_POST as $key => $value) {
				
		//Create new secure links
		if(strpos($key,$prefix.'new_secure_link_name')!==false) {
			
			$new_id = end(explode('_',$key));
			
			unset($data);
			$data['name'] = $_POST[$prefix.'new_secure_link_name_'.$new_id];
			$data['internal_description'] = $_POST[$prefix.'new_secure_link_description_'.$new_id];
			$data['url'] = $_POST[$prefix.'new_secure_link_url_'.$new_id];			
			
			 opt_forms_create_secured_page($data);
			 $new_secure_links[$new_id]	= $wpdb->insert_id;
			
		}		
		
		//Assign Mailing Lists		
		elseif(strpos($key,'mailer_list_')!==false) {
												
			unset($data);
			$data['opt_campaign_id'] = $campaign_id;
			$data['opt_mailer_id'] = $value;
			
			$dwpc->insert(OPT_DB_PREFIX.'opt_mailers_to_campaigns',$data);	
		
		}			
			
		//Assign secured links
		elseif(strpos($key,$prefix.'link_')!==false) {
									
			unset($data);
			$data['opt_campaign_id'] = $campaign_id;
			$data['opt_secured_page_id'] = $value;
			
			$dwpc->insert(OPT_DB_PREFIX.'opt_pages_to_campaigns',$data);	
		
		}	
		
				
		//Edit existing secure links
		if(strpos($key,$prefix.'edit_secure_link_name')!==false) {
			
			$edit_id = end(explode('_',$key));
			
			unset($data);
			$data['name'] = $_POST[$prefix.'edit_secure_link_name_'.$edit_id];
			$data['internal_description'] = $_POST[$prefix.'edit_secure_link_description_'.$edit_id];
			$data['url'] = $_POST[$prefix.'edit_secure_link_url_'.$edit_id];	
			
			opt_forms_edit_secured_page($data,$edit_id);
			
			
		}
		
	}
	

	//Associate newly created secure links to this campaign	
	if(is_array($new_secure_links)) {
		foreach($new_secure_links as $key => $value) {
										
				unset($data);
				$data['opt_campaign_id'] = $campaign_id;
				$data['opt_secured_page_id'] = $value;
				
				$dwpc->insert(OPT_DB_PREFIX.'opt_pages_to_campaigns',$data);	
			
		}	
	}
		
	//Loop through again so that we can add optin links with new url's	
	foreach($_POST as $key => $value) {
	
		
		//Create New Optin Links From The List
		if(strpos($key,$prefix.'new_optin_link_name_')!==false) {
							
			$new_id = end(explode('_',$key));
								
			unset($data);
			$data['name'] = $_POST[$prefix.'new_optin_link_name_'.$new_id];
			
			
			if(strpos($_POST[$prefix.'optin_link_secure_link_'.$new_id],'new_')!==false) { 		
				
				$secure_id = end(explode('_',$_POST[$prefix.'new_optin_link_secure_link_'.$new_id]));								
				$data['opt_secured_page_id'] = $new_secure_links[$secure_id];
			
			} else {
				
				$data['opt_secured_page_id'] = $_POST[$prefix.'new_optin_link_secure_link_'.$new_id];
				
			}
		
						
			if(strpos($_POST[$prefix.'new_optin_link_secure_link'],'new_')!==false) { 		
				
				$secure_id = end(explode('_',$_POST[$prefix.'new_optin_link_secure_link']));								
				$data['opt_secured_page_id'] = $secure_id;
			
			} else {
				
				$data['opt_secured_page_id'] = $_POST[$prefix.'new_optin_link_secure_link'];
				
			}
			
			$data['opt_form_id'] = $_POST[$prefix.'new_optin_link_optin_form_'.$new_id];
			
			if($_POST[$prefix.'new_secure_link_type_'.$new_id]==1) { $data['link_type'] = 'Popup'; }
			else { $data['link_type'] = 'Direct'; }						
			
			$data['optin_not_required'] = $_POST[$prefix.'new_optin_link_not_required_'.$new_id];
			$data['optin_no_redirect'] = $_POST[$prefix.'new_optin_no_redirect_'.$new_id];
			$data['optin_open_in_new_tab'] = $_POST[$prefix.'new_optin_open_in_new_tab_'.$new_id];
			$data['getresponse_double_optin'] = $_POST[$prefix.'new_getresponse_optin_link_double_optin_'.$new_id];
			$data['getresponse_unsubscribe'] = $_POST[$prefix.'new_getresponse_optin_link_unsubscribe_'.$new_id];
			opt_forms_create_link($data,$campaign_id);
		
		}	
		
		//Edit Existing Optin Links From The List
		elseif(strpos($key,$prefix.'existing_optin_link_name_')!==false) {
							
			$existing_id = end(explode('_',$key));
								
			unset($data);
			$data['name'] = $_POST[$prefix.'existing_optin_link_name_'.$existing_id];	
			
		
			if(strpos($_POST[$prefix.'existing_optin_link_secure_link_'.$existing_id],'new_')!==false) { 		
				
				$secure_id = end(explode('_',$_POST[$prefix.'existing_optin_link_secure_link_'.$existing_id]));								
				$data['opt_secured_page_id'] = $new_secure_links[$secure_id];
			
			} else {
				
				$data['opt_secured_page_id'] = $_POST[$prefix.'existing_optin_link_secure_link_'.$existing_id];
				
			}
			
			$data['opt_form_id'] = $_POST[$prefix.'existing_optin_link_optin_form_'.$existing_id];
			
			if($_POST[$prefix.'existing_secure_link_type_'.$existing_id]==1) { $data['link_type'] = 'Popup'; }
			elseif($_POST[$prefix.'existing_secure_link_type_'.$existing_id]==2) { $data['link_type'] = 'Direct'; }
			else {$data['link_type'] = $_POST[$prefix.'existing_secure_link_type_'.$existing_id]; }				
			
			$data['optin_not_required'] = $_POST[$prefix.'existing_optin_link_not_required_'.$existing_id];
			$data['optin_no_redirect'] = $_POST[$prefix.'existing_optin_no_redirect_'.$existing_id];
			$data['optin_open_in_new_tab'] = $_POST[$prefix.'existing_optin_open_in_new_tab_'.$existing_id];
			$data['getresponse_double_optin'] = $_POST[$prefix.'existing_getresponse_optin_link_double_optin_'.$existing_id];
			$data['getresponse_unsubscribe'] = $_POST[$prefix.'existing_getresponse_optin_link_unsubscribe_'.$existing_id];
			
			opt_forms_edit_link($existing_id,$data,$campaign_id);
		
		}	
		
		//Create Optin Links From The Form
		elseif($key == $prefix.'optin_link_name' && $value!='') {
							
								
			unset($data);
			$data['name'] = $_POST[$prefix.'optin_link_name'];
						
			if(strpos($_POST[$prefix.'optin_link_secure_link'],'new_')!==false) { 		
				
				$secure_id = end(explode('_',$_POST[$prefix.'optin_link_secure_link']));								
				$data['opt_secured_page_id'] = $new_secure_links[$secure_id];
			
			} else {
				
				$data['opt_secured_page_id'] = $_POST[$prefix.'optin_link_secure_link'];
				
			}
							
			$data['opt_form_id'] = $_POST[$prefix.'optin_link_form'];
			$data['link_type'] = $_POST[$prefix.'optin_link_type'];
			$data['optin_not_required'] = $_POST[$prefix.'optin_link_not_required'];
			$data['optin_no_redirect'] = $_POST[$prefix.'optin_no_redirect'];
			$data['optin_open_in_new_tab'] = $_POST[$prefix.'optin_open_in_new_tab'];
			$data['getresponse_double_optin'] = $_POST[$prefix.'getresponse_optin_link_double_optin'];
			$data['getresponse_unsubscribe'] = $_POST[$prefix.'getresponse_optin_link_unsubscribe'];
			
			opt_forms_create_link($data,$campaign_id);
		
		}		
		
	}

	//unset($_POST);
	
	return 1;



}


//
//
//
function opt_forms_create_link($data='',$campaign_id='') {
	
	
	global $wpdb;
	global $dwpc;
	
	$data['time_created'] = current_time('mysql');
	$data['opt_campaign_id'] = $campaign_id;
	$data['text_id'] = opt_random_string(6,6);
	
	if(empty($data['name'])) { $data['name'] = $_POST['optin_link_name']; }
			
	if(empty($data['opt_secured_page_id'])) { $data['opt_secured_page_id'] = $_POST['optin_link_secure_link']; }
	if(empty($data['opt_form_id'])) { $data['opt_form_id'] = $_POST['optin_link_form']; }
	if(empty($data['link_type'])) { $data['link_type'] = $_POST['optin_link_type']; }	
	if(empty($data['optin_no_redirect']	)) { $data['optin_no_redirect'] = $_POST['optin_no_redirect']; }
	if(empty($data['optin_open_in_new_tab'])) { $data['optin_open_in_new_tab'] = $_POST['optin_open_in_new_tab']; }
	if(empty($data['optin_not_required'])) { $data['optin_not_required'] = $_POST['optin_link_not_required']; }
	if(empty($data['getresponse_double_optin'])) { $data['getresponse_double_optin'] = $_POST['getresponse_optin_link_double_optin']; }
	if(empty($data['getresponse_unsubscribe'])) { $data['getresponse_unsubscribe'] = $_POST['getresponse_optin_link_unsubscribe']; }	
	
	if(strpos($data['opt_form_id'],'split_test')!==false) {
		$data['opt_split_test_id'] = end(explode('-',$data['opt_form_id']));
		unset($data['opt_form_id']);		
	}
		
	//krumo($data);	
			
	$dwpc->insert(OPT_DB_PREFIX.'opt_optin_links',$data);
		
	return $wpdb->insert_id;

}


function opt_forms_edit_link($id,$data='',$campaign_id='') {
	
	global $wpdb;
	global $dwpc;
	
	if($campaign_id=='') {
		$data['opt_campaign_id'] = $_POST['opt_campaign_id'];	
	} else {
		$data['opt_campaign_id'] = $campaign_id;
	}
		
	if(empty($data['name'])) { $data['name'] = $_POST['optin_link_name']; }
			
	if(empty($data['opt_secured_page_id'])) { $data['opt_secured_page_id'] = $_POST['optin_link_secure_link']; }
	if(empty($data['opt_form_id'])) { $data['opt_form_id'] = $_POST['optin_link_form']; }
	if(empty($data['link_type'])) { $data['link_type'] = $_POST['optin_link_type']; }	
	if(empty($data['optin_no_redirect']	)) { $data['optin_no_redirect'] = $_POST['optin_no_redirect']; }
	if(empty($data['optin_open_in_new_tab'])) { $data['optin_open_in_new_tab'] = $_POST['optin_open_in_new_tab']; }
	if(empty($data['optin_not_required'])) { $data['optin_not_required'] = $_POST['optin_link_not_required']; }
	if(empty($data['getresponse_double_optin'])) { $data['getresponse_double_optin'] = $_POST['getresponse_optin_link_double_optin']; }
	if(empty($data['getresponse_unsubscribe'])) { $data['getresponse_unsubscribe'] = $_POST['getresponse_optin_link_unsubscribe']; }	
	
	if(strpos($data['opt_form_id'],'split_test')!==false) {
		$data['opt_split_test_id'] = end(explode('-',$data['opt_form_id']));
		$data['opt_form_id']=0;		
	}
			
	$dwpc->update(OPT_DB_PREFIX.'opt_optin_links',$data,array('id'=>$id));


}



//
//
//
function opt_forms_create_secured_page($data='') {
	
	global $wpdb;
	global $dwpc;
	
	if(empty($data['name'])) { $data['name'] = $_POST['name']; }
	if(empty($data['internal_description'])) { $data['internal_description'] = $_POST['description']; }
	if(empty($data['url'])) { $data['url'] = $_POST['url']; }
	if(isset($_POST['opt_campaign_id'])) { $data['opt_campaign_id'] = $_POST['opt_campaign_id']; }	
	if(isset($_POST['opt_form_id'])) { $data['opt_form_id'] = $_POST['opt_form_id']; }	
	$data['time_created'] = current_time('mysql');
	
	$dwpc->insert(OPT_DB_PREFIX.'opt_secured_pages',$data);	
		
	return $wpdb->insert_id;
	

}

function opt_forms_edit_secured_page($data='',$id) {
	
	global $wpdb;
	global $dwpc;
	
	if(empty($data['name'])) { $data['name'] = $_POST['name']; }
	if(empty($data['internal_description'])) { $data['internal_description'] = $_POST['description']; }
	if(empty($data['url'])) { $data['url'] = $_POST['url']; }
	if(isset($_POST['opt_campaign_id'])) { $data['opt_campaign_id'] = $_POST['opt_campaign_id']; }	
	if(isset($_POST['opt_form_id'])) { $data['opt_form_id'] = $_POST['opt_form_id']; }	
	
			
	$dwpc->update(OPT_DB_PREFIX.'opt_secured_pages',$data,array('id' => $id));	


}


//
//
//
function opt_forms_create_form() {
	
	global $dwpc;
	
	$data = array(
					'id' => NULL,				
					'time_created' => current_time('mysql'),
					'name' => $_POST['optin_form_name'],
					'form_type' => $_POST['optin_form_type'],
					'file_url' => $_POST['optin_form_url'],
					'data' => $_POST['optin_form_data']
				);
	
	$dwpc->insert(OPT_DB_PREFIX.'opt_forms',$data);	

}


function opt_forms_edit_form($id) {
	
	global $dwpc;
	
	$data = array(
					'id' => NULL,				
					'time_created' => current_time('mysql'),
					'name' => $_POST['optin_form_name'],
					'form_type' => $_POST['optin_form_type'],
					'file_url' => $_POST['optin_form_url'],
					'data' => $_POST['optin_form_data']
				);
	
	$dwpc->update(OPT_DB_PREFIX.'opt_forms',$data,array('id'=>$id));	
	

}



?>