<?php
// OPTIONS:
//		newsrequireactivate - doubleoptin - Y/N
//

class opt_api_rapidmailer implements opt_mailer_api {

	
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }
		String on failure	
	*/
	public function aGetLists(){
		global $wpdb;
		
		$lists = $wpdb->get_results('SELECT * FROM '.OPT_DB_PREFIX.'newsmailinglists',ARRAY_A);
		$ret = array();
		foreach($lists as $l){
			$ret[$l['id']] = array(
				'name'=>$l['title'],
				'description'=>'',
				'contacts'=> $wpdb->query('SELECT rel_id FROM '.OPT_DB_PREFIX.'newssubscriberslists WHERE list_id='.$l['id'])
			);	
		}
		return $ret;
	}
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }*/                                                                                                                                                                                      
	public function aGetListById($id){
		global $wpdb;
		$l = $wpdb->get_row('SELECT * FROM '.OPT_DB_PREFIX.'newsmailinglists WHERE id='.$id,ARRAY_A);
		$ret = array();
		if(empty($l)){return $ret;}
		$ret[$l['id']] = array(
				'name'=>$l['title'],
				'description'=>'',
				'contacts'=> $wpdb->query('SELECT id FROM '.OPT_DB_PREFIX.'newssubscriberslists WHERE list_id='.$l['id'])
			);		
	}
	
	//campaign is the campaign id
	public function CreateContact($name,$email,$campaign,$phone,$extra){
		global $wpdb,$dwpc;
			
		
		$subscriber_id = $wpdb->get_var('SELECT id FROM '.OPT_DB_PREFIX.'newssubscribers WHERE email="'.mysql_real_escape_string($email).'"');
		if($chk == NULL){
			$data = array(
				'name'=>$name,
				'email'=>$email,
				'registered'=>'N',
				'ip_address'=>$_SERVER['REMOTE_ADDR'],
				'format'=>'html',
				'authinprog'=>'N',
				'created'=>current_time('mysql'),
				'modified'=>current_time('mysql')
			);	
			$dwpc->insert(OPT_DB_PREFIX.'newssubscribers',$data);
			$subscriber_id = $wpdb->insert_id;
		}
		
		$subscriber_to_list_id = $wpdb->get_var('SELECT rel_id FROM '.OPT_DB_PREFIX.'newssubscriberslists WHERE subscriber_id='.$subscriber_id.' AND list_id='.$campaign);
		if($subscriber_to_list_id == NULL){
			$data = array(
				'subscriber_id'=>$subscriber_id,
				'list_id'=>$campaign,
				'active'=>1,
				'paid'=>'N',
				'authinprog'=>'N',
				'ppsubscription'=>'N',
				'created'=>current_time('mysql'),
				'modified'=>current_time('mysql'),
				'paid_date'=>current_time('mysql')	
			);	
			$wpdb->insert(OPT_DB_PREFIX.'newssubscriberslists',$data);
			//$wpdb->update(OPT_DB_PREFIX.'newssubscriberslists',array('active'=>'Y'),array('rel_id'=>$wpdb->insert_id));
		}
	}
	
	//do we even actually need this one? I'll put off writing it for now until we decide. It would be a little strenuous on the server for aweber
	/*"CONTACT_ID" : {
            "campaign"      : "CAMPAIGN_ID",
            "name"          : "My Contact Name",
            "email"         : "my_contact_1@emailaddress.com",
        }*/
	public function aGetContacts(){}
	
	//returns bool for subscription status 
	public function CheckOptinStatus($leads_id = 0){return true;}
	function __construct(){}

}

?>