<?php
define('OPT_GOTOWEBINAR_KEY','0vfP0lA0gfaUxTFYQC78xBrR3oVKGoIJ');
class opt_api_gotowebinar implements opt_mailer_api {

	private $token;
	private $org_key;
	private $acc_key;
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }
		String on failure	
	*/
	public function aGetLists(){
		$webinars = $this->_request('upcomingWebinars','GET',array('Accept: application/json','Content-Type: application/json'));
		if($webinars === false){
			return array();	
		}
		$webinars['body'] = json_decode($webinars['body'],true);
		$ret = array();
		
		foreach($webinars['body'] as $key=>$value){
			
			$registrants = $this->_request('webinars/'.$value['webinarKey'].'/registrants','GET',array('Accept: application/json','Content-Type: application/json'));
			if($registrants === false){
				$contacts = 0;
			}else{
				$registrants['body'] = json_decode($registrants['body'],true);
				$contacts = count($registrants['body']);	
			}
			
			
			
			$ret[$value['webinarKey']] = array(
					'name'=>$value['subject'],
					'description'=>$value['description'],
					'contacts'=>$contacts
				);	
		}
		
		return $ret;
	}
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }*/                                                                                                                                                                                      
	public function aGetListById($id){
		$webinar = $this->_request('webinars/'.$id,'GET',array('Accept: application/json','Content-Type: application/json'));
		if($webinars === false){
			return array();	
		}
		$webinars['body'] = json_decode($webinars['body'],true);
		$ret = array();
		
		foreach($webinars['body'] as $key=>$value){
			
			$registrants = $this->_request('webinars/'.$value['webinarKey'].'/registrants','GET',array('Accept: application/json','Content-Type: application/json'));
			if($registrants === false){
				$contacts = 0;
			}else{
				$registrants['body'] = json_decode($registrants['body'],true);
				$contacts = count($registrants['body']);	
			}
			
			
			
			$ret[$value['webinarKey']] = array(
					'name'=>$value['subject'],
					'description'=>$value['description'],
					'contacts'=>$contacts
				);	
		}
		
		return $ret;
	}
	
	//campaign is the campaign id
	public function CreateContact($name,$email,$campaign,$phone,$extra){
		$name = opt_parse_name($name);
		$contact = array('firstName'=>$name['first'],'lastName'=>$name['last'],'email'=>$email);
		
		return $this->_request('webinars/'.$campaign.'/registrants','POST',array('Accept: application/json','Content-Type: application/json'),$contact);
	}
	
	//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(){}
	
	// n/a with gtw 
	public function CheckOptinStatus($leads_id = 0){
		return true;	
	}
	/*
		Array
	(
		[access_token] => GL2hCIv0LRpSPQ7UgNE0TP8iWQ0N
		[expires_in] => 30758399
		[refresh_token] => ND0URZSeAFpbgLoabDOC0L7vdCaWjdCe
		[organizer_key] => 1383895171297983749
		[account_key] => 9100056668797418757
		[account_type] => 
		[firstName] => na
		[lastName] => na
		[email] => dylan@qualityinternetservicesllc.com
		[platform] => GLOBAL
	)
	*/
	
	
	function __construct($data = ''){
		$this->token = $data['access_token'];
		$this->org_key = $data['organizer_key'];
		$this->acc_key = $data['account_key'];	
	}
	
	private function _request($res,$method='GET',$headers=array(),$data = array()){
		$url = 'https://api.citrixonline.com/G2W/rest/organizers/'.$this->org_key.'/'.$res;
		
		$headers = array_merge($headers,array('Authorization: OAuth oauth_token='.$this->token));
		
		$handle = curl_init($url);
		
		if($method == 'POST'){
			curl_setopt($handle, CURLOPT_POST, true);
			
			ksort($data);
			
			$postData = json_encode($data);
        	curl_setopt($handle, CURLOPT_POSTFIELDS, $postData);	
		}
		
		curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($handle, CURLOPT_HEADER, true);
		curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
		
		curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($handle, CURLOPT_VERBOSE, FALSE);
		curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($handle, CURLOPT_TIMEOUT, 90);
		$resp = curl_exec($handle);
		if($resp){
			
			curl_close($handle);
			return opt_curl_response($resp);	
		}else{
			return false;
		}
	}
	
	
}
?>