<?php


class opt_api_aweber implements opt_mailer_api{ 

	public $consumerKey,$consumerSecret,$accesskey,$accessSecret;
	
	public $api, $account;
	
	//returns 'active', 'unconfirmed', 'unsubbed'
	public function CheckOptinStatus($leads_id = 0){
		$contact = opt_get_lead($leads_id);
		try{
			if($contact['subscription_status'] == 'Subscribed'){return true;}
				
			$lists = opt_get_related_mailers_list_name($contact['campaign_id'],'AWeber');
			
			
			$account = $this->api->loadFromUrl('/accounts/'.$this->account->id);
			//echo '<pre>'.print_r($account,true).'</pre>';
			$subscribers = $account->findSubscribers(array('email'=>$contact['email_text_id']));
			
			global $wpdb;
			
			$retVal = '';
			foreach($subscribers as $key=>$value){
				if($value->data['status'] == 'subscribed'){
					$text_id = $contact['email_text_id'];
					$wpdb->update(OPT_DB_PREFIX.'opt_leads',
						array('email_text_id'=>$text_id,'subscription_status'=>'Subscribed'),
						array('id'=>$contact['id']));
					return true;	
				}		
			}
		}catch(Exception $e){
			return $e;	
		}
		return false;
	}
	public function aGetLists(){
		try{
			$listsUrl = '/accounts/'.$this->account->id.'/lists/';
			
			$response = $this->api->loadFromUrl($listsUrl);
			
			//($response);
			
			$return_array = array();
			
			foreach($response as $key=>$value)
			{
				$return_array[$value->data['id']] = array(
						'name' => $value->data['name'],
						'description' => '',
						'contacts' => $value->data['total_subscribers']
					);
			}
			
			return $return_array;
		}catch(Exception $e)
		{
			return $e->getMessage();
		}
	}
	public function aGetListById($id){
		try{
			$listsUrl = '/accounts/'.$this->account->id.'/lists/'.$id;	
			
			$value = $this->api->loadFromUrl($listsUrl);
			
			$return_array = array();
			
			
			$return_array[$value->data['id']] = array(
					'name' => $value->data['name'],
					'description' => '',
					'contacts' => $value->data['total_subscribers']
				);
			
			
			return $return_array;
		}catch(Exception $e)
		{
			return $e->getMessage();
		}
	}
	
	
	public function CreateContact($name,$email,$campaign,$phone,$extra){
		try{
			$listsUrl = '/accounts/'.$this->account->id.'/lists/'.$campaign;
			$list = $this->api->loadFromUrl($listsUrl);
			
			$newSubscriber = array(
				'email' => $email,
				'name' => $name,
				'ip_address'=>$_SERVER['REMOTE_ADDR']
			);	
			if(!is_array($extra)){
				$extra = array();	
			}
			/*if($phone != ''){
				$extra['phone'] = $phone;	
			}*/
			/*$newSubscriber['custom_fields'] = $extra;*/
			
			if(isset($extra['zip'])){
				$newSubscriber['postal_code'] = $extra['zip'];	
			}
			
			if(isset($extra['city'])){
				$newSubscriber['city'] = $extra['city'];	
			}
			
			if(isset($extra['country'])){
				$newSubscriber['country'] = $extra['country'];	
			}
			
			$result = $list->subscribers->create($newSubscriber);
			return $result;
		}catch(Exception $e)
		{
			return $e->getMessage();
		}
	}
	
	
	public function aGetContacts(){
		
	}
	
	
	function __construct($consumerKey = '',$consumerSercret = '', $accessKey = '',$accessSecret = ''){
		$this->consumerKey = $consumerKey;
		$this->consumerSecret = $consumerSercret;
		$this->accesskey = $accessKey;
		$this->accessSecret = $accessSecret;
	
		
		
		
		$this->api = new AWeberAPI($this->consumerKey,$this->consumerSecret);
		$this->account = $this->api->getAccount($this->accesskey, $this->accessSecret);	
	}
}
?>