<?php
class opt_api_infusionsoft implements opt_mailer_api {
	
	public $client;
	
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }
		String on failure	
	*/
	public function aGetLists(){
		$ret = array();
		$groups = $this->client->dsQuery('ContactGroup',1000,0,array('GroupName'=>'%'),array('GroupName','Id','GroupDescription'));	
		foreach($groups as $c){
			
			$name = $c['GroupName'];
			$desc = '';
			if(array_key_exists('GroupDescription',$c)){
				$desc = $c['GroupDescription'];
			}
			$contacts = $this->client->dsCount('ContactGroupAssign',array('GroupId'=>$c['id']));
			$ret[$c['Id']] = array(
				'name'=>$name,
				'description'=>$description,
				'contacts'=>$contacts
			);	
		}
		return $ret;
	}
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }*/                                                                                                                                                                                      
	public function aGetListById($id){
		$group = $this->client->dsQuery('ContactGroup',1000,0,array('Id'=>$id),array('GroupName','Id','GroupDescription'));	
		$ret = array();
		foreach($group as $c){
			
			$name = $c['GroupName'];
			$desc = '';
			if(array_key_exists('GroupDescription',$c)){
				$desc = $c['GroupDescription'];
			}
			$contacts = $this->client->dsCount('ContactGroupAssign',array('GroupId'=>$c['id']));
			$ret[$c['id']] = array(
				'name'=>$name,
				'description'=>$description,
				'contacts'=>$contacts
			);	
		}
		return $ret;
	}
	 
	//campaign is the campaign id
	public function CreateContact($name,$email,$campaign,$phone,$extra){
		$contact = array('FirstName'=>$name,
						'Email'=>$email);
		
		if($phone != ''){
			$contact['Phone1'] = $phone;	
		}
		
		if(is_array($extra)){
			foreach($extra as $key=>$value){
				if($key == 'dob'){
					$contact['Birthday'] = $value;	
				}elseif($key == 'address1'){
					$contact['StreetAddress1'] = $value;	
				}elseif($key == 'address2'){
					$contact['StreetAddress2'] = $value;	
				}elseif($key == 'city'){
					$contact['City'] = $value;	
				}elseif($key == 'state'){
					$contact['State'] = $value;
				}elseif($key == 'zip'){
					$contact['PostalCode'] = $value;	
				}elseif($key == 'country'){
					$contact['Country'] = $value;	
				}else{
					$contact[$key] = $value;	
				}
			}
		}
		
		$con_id = '';
		$contact_inf = $this->client->findByEmail($email,array('Id'));
		if(isset($contact_inf[0]) && isset($contact_inf[0]['Id'])){
			$con_id = $contact_inf[0]['Id'];
				
		}else{
			$con_id = $this->client->addCon($contact,'OptinLinks OptIn');	
		}
		$this->client->grpAssign($con_id,$campaign);
	}
	
	//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($api_key='',$subdomain=''){
		
		try{
		if(!class_exists('iSDK')){
			require_once dirname(__FILE__).'/infusionsoft/isdk.php';
		}
		
		$this->client = new iSDK($subdomain,$api_key,'throw');
		
			$this->client->cfgCon($subdomain,$api_key);
		
		}catch(Exception $e){
			
			print_r($e);
			//do something for errors	
		}
	}

}
?>