<?php


class opt_api_constantcontact implements opt_mailer_api{ 

	public $sToken;
	public $ccObj;
	
	//returns Subscribed', 'Pending', 'Unsubscribed'
	public function CheckOptinStatus($leads_id = 0){
		$contact = opt_get_lead($leads_id);
		
		if($contact['subscription_status']=='Subscribed'){
			return true;	
		}
		
		global $wpdb;
		
		$result = $this->ccObj->getContactByEmail($this->sToken,$contact['email_text_id']);
		//($result);
		
		foreach($result->results as $curRes){
			if($curRes->status == 'ACTIVE'){
				//($curRes);
				$wpdb->update(OPT_DB_PREFIX.'opt_leads',array('subscription_status'=>'Subscribed','email_text_id'=>$contact['email_text_id']) ,array('id'=>$contact['id']) );	
				return true;
			}
		}
		
		return false;
	    //$lists = opt_get_related_mailers_list_name($contact['campaign_id'], 'ConstantContact');
	}
	public function aGetLists(){
		try{
			$result = $this->ccObj->getLists($this->sToken);	
			
			$return = array();
			
			foreach($result as $value)
			{
				$return[$value->id] = array(
					'name' => $value->name,
					'contacts' => $value->contact_count
				);
			}
			
			return $return;
		}catch(Exception $e){
			
			$errors = $e->getErrors();
			return $errors[0]['error_message'];
		}
	}
	public function aGetListById($id){
		try{
			$result = $this->ccObj->getList($this->sToken,$id);
			
			$return = array($result->id => array('name'=>$result->name,'contacts'=>$result->contact_count));
			return $return;	
		}catch(Exception $e){
			return $e->getMessage();
		}
	}
	
	public function CreateContact($name,$email,$campaign,$phone,$extra){
		try{
			list($fname,$lname) = explode(' ',$name);
			
			
			$contact = array('first_name'=>$fname,
						'last_name'=>$lname,
						'email_addresses'=>array(array('email_address'=>$email,'confirm_status'=>'NO_CONFIRMATION_REQUIRED')),
						'opt_in_date' => current_time('mysql'),
						'lists'=>array(array('id'=>$campaign)));
			
			if($phone != ''){
			$contact['home_phone'] = $phone;	
			}
			
			if(is_array($extra)){
				$address = array();
				$customs = array();
				foreach($extra as $key=>$value){
					if($key == 'address1'){
						$address['street'] = $value;	
					}elseif($key == 'address2'){
						$address['street2'] = $value;	
					}elseif($key == 'city'){
						$address['city'] = $value;	
					}elseif($key == 'state'){
						$address['state'] = $value;
					}elseif($key == 'zip'){
						$address['postalCode'] = $value;		
					}else{
						$customs[] = array('name'=>$key,'value'=>$value);	
					}
				}
				if(!empty($customs)){
					$contact['custom_fields'] = $customs;	
				}
				if(!empty($address)){
					$contact['addresses'] = array($address);	
				}
			}
			
			$contact = \Ctct\Components\Contacts\Contact::create($contact);
			$result = $this->ccObj->addContact($this->sToken,$contact,true);
			return $result;
		}catch(Exception $e){
			return $e->getMessage();
		}
	}
	public function aGetContacts(){}
	
	
	function __construct($accessToken = ''){
				
		require_once dirname(__FILE__) . '/Ctct/autoload.php';	
		
		$this->sToken = $accessToken;
		
		$this->ccObj = new \Ctct\ConstantContact(OPT_CONSTANTCONTACT_KEY);	
	}
}
?>