<?php
class opt_api_ontraport implements opt_mailer_api {

	private $appid;
	private $_key;
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }
		String on failure	
	*/
	public function aGetLists(){
		
		$data = '<field name="Full">true</field>';
		
		
		$result = $this->_request('fetch_sequences','&return_id=2');
		$ret = array();
		foreach($result->sequence as $sequence){
		
			$ret[(string)$sequence->attributes()->id] = array(
				'name'=>(string)$sequence
			);	
		}
		
		return $ret;
	
	}
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }*/                                                                                                                                                                                      
	public function aGetListById($id){
		$data = '<field name="Full">true</field>';
		
		
		$result = $this->_request('fetch_sequences','&return_id=2');
		$ret = array();
		foreach($result->sequence as $sequence){
		
			$ret[(string)$sequence->attributes()->id] = array(
				'name'=>(string)$sequence
			);	
		}
		
		return $ret;
	}
	
	//campaign is the campaign id
	public function CreateContact($name,$email,$campaign,$phone,$extra){
		
		$name = opt_parse_name($name);
		
		$data = '<contact>';
		$data .= '<Group_Tag name="Contact Information">';
		$data .= '<field name="First Name">'.$name['first'].'</field>';
		$data .= '<field name="Last Name">'.$name['last'].'</field>';
		$data .= '<field name="Email">'.$email.'</field>';
		$data .= '</Group_Tag><Group_Tag name="Sequences and Tags">';
		$data .= '<field name="Sequences">*/*'.$campaign.'*/*</field>';
		$data .= '</Group_Tag>';
		$data .= '</contact>';
		
		$data = '&data='.rawurlencode($data);
		
		return $this->_request('add',$data);

	}
	
	//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($appid='',$key=''){
		$this->appid = $appid;
		$this->_key = $key;
	}
	
	
	protected function _request($type,$data = ''){
		$postArgs = 'appid='.$this->appid.'&key='.$this->_key.'&reqType='.$type;
		$request = 	"https://api.ontraport.com/cdata.php";
		
		if($data != ''){
			$postArgs .=$data;	
		}
		
		$session = curl_init($request);
		curl_setopt ($session, CURLOPT_POST, true);
		curl_setopt ($session, CURLOPT_POSTFIELDS, $postArgs);
		curl_setopt($session, CURLOPT_HEADER, false);
		curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($session);
		opt_pr(htmlentities($response));
		
		curl_close($session);
		return simplexml_load_string($response);
	}
}
?>