<?php
require_once("common/classes/Db_Connect.class");
require_once("common/classes/DbConnectTxEmail.class");
require_once("common/classes/DbConnectTxEmailActivity.class");
require_once("common/classes/DbConnectTxClientMgt.class");

require_once("common/classes/SF.class");
/*******************************************************************************
Page Name            :  sf_activity_mgnr.class
Author               :
Date                 :  08apr2008
Description          :

Variable Name & Description :

--------------------------------------------------------------------------------
Modification History :

      Date      Description

1>    8apr2008    Original File

2>

To Do :
 *******************************************************************************/
#[\AllowDynamicProperties]
class sf_activity_mgnr
{

	var $db_conn_obj;
	var $dbconn_read;
	var $dbconn_write;
	var $dbconn_read_tx_email;
	var $dbconn_read_tx_clientmgt;
	var $sf;
	var $sf_act_mgr_session_id;
	var $sf_act_mgr_location;
	var $srm_prefix;

	function __construct()
	{
		$this->db_conn_obj = new Db_Connect($this->dbconn_read, $this->dbconn_write);
		$this->dbconn_read_tx_email = DbConnectTxEmail::connect("read");
		$this->dbconn_write_tx_email_activity = DbConnectTxEmailActivity::connect("write");
		$this->dbconn_read_tx_clientmgt = DbConnectTxClientMgt::connect("read");
	} // End of functions

	function insert_activity($id, $bcast_id, $activity, $url)
	{
		if (empty($id) or empty($bcast_id) or empty($activity)) {
			echo "Please check the arguments";
			exit;
		}
		//get the client id and campaign id
		$sql_str = "select client_id,campaign_id from broadcast_queue where broadcast_id = \"$bcast_id\"";
		$row = $this->dbconn_read_tx_email->query($sql_str);
		if (DB::iserror($row)) {
			print_r($row);
			exit;
		}

		$row->fetchinto($result);

		$client_id = $result->client_id;
		$camp_id = $result->campaign_id;

		$sql_str = "SELECT COUNT(*) AS count_config
			FROM config_activity_sync
			WHERE client_id = $client_id
			AND active = true
			AND upload_frequency_hours > 0";
		$row = $this->dbconn_read_tx_email->query($sql_str);
		if (DB::iserror($row)) {
			print_r($row);
			exit;
		}

		$row->fetchinto($result);

		if ($result->count_config > 0) {
			$status_sync_ebm = "'New'";
		} else {
			$status_sync_ebm = 'null';
		}

		// insert the activity into table activity_log_<client_id>
		$sql_str = "INSERT INTO activity_log_$client_id
			(broadcast_id, recipient_id, campaign_id, activity, interact_url, status_sync_ebm)
			VALUES
			('$bcast_id', '$id', '$camp_id', '$activity', '$url', $status_sync_ebm)";

		$row = $this->dbconn_write_tx_email_activity->query($sql_str);
		if (DB::iserror($row)) {
			print_r($row);
			exit;
		}

	}

	function sf_login($orgID, $bcast_id)
	{

		//echo $orgID."--".$bcast_id."here"; exit;

		if (strlen($bcast_id) > 0 and strlen($orgID) < 1) {
			// get the session id from
			$sql_str = "select orgId from clientmgt.clients c
				INNER JOIN broadcast_queue b USING(client_id)
				where b.broadcast_id = \"$bcast_id\" ";

			$row = $this->dbconn_read_tx_email->query($sql_str);
			if (DB::iserror($row)) {
				print_r($row);
				exit;
			}
			$row->fetchinto($result);
			$orgID = $result->orgId;
		}

		// Get the session ID

		$orgID = substr($orgID, 0, 15);
		$sql_str = "select sfSessionId,username,password_encrypt,sfEndPointURL,sandbox_flag,sf_srm_prefix from clients  where orgId = \"$orgID\"";
		//echo $sql_str;
		$row = $this->dbconn_read_tx_clientmgt->query($sql_str);
		if (DB::iserror($row)) {
			print_r($row);
			exit;
		}
		$row->fetchinto($result);
		$session_id  = $result->sfSessionId;
		$user_name   = $result->username;
		$password    = $result->password_encrypt;
		$sf_location = $result->sfEndPointURL;
		$this->srm_prefix = $result->sf_srm_prefix;
		if ($result->sandbox_flag == "Y") {
			$wsdl_path   = WSDL_SANDBOX_PATH;
		} else {
			$wsdl_path   = WSDL_PATH;
		}

		$this->sf = new SF();

		if ($this->sf->SF_login_session($session_id, $sf_location)) {

			$this->sf_act_mgr_session_id  = $session_id;
			$this->sf_act_mgr_location    = $sf_location;
		} else {
			$this->sf->SF_login($wsdl_path, $user_name, $password);
		}
	}

	//  the email address from SF
	function get_email($id, $bcast_id)
	{

		$this->sf_login($dummy, $bcast_id);

		$orgID = substr($this->sf->user_info()->organizationId, 0, 15);

		//$this->get_prefixs($orgID,&$eml_prefix,&$srm_prefix,&$evnt_prefix,&$volunteer_prefix);
		$this->get_prefixs($orgID, $eml_prefix, $srm_prefix, $evnt_prefix, $volunteer_prefix);


		if (strpos($id, SF_CONTACT_ID_BGN) !== FALSE) {
			if ($this->isAltEmailBroadacst($bcast_id) == 1) {
				$altEmail = $this->getAltEmailField($eml_prefix, $orgID);
				$sql_str = "Select email,$altEmail From Contact  where Id = '" . $id . "'";
			} else {
				$sql_str = "Select email From Contact  where Id = '" . $id . "'";
			}


			$this->sf->sfSoqlQuery($sql_str);
			$email = $this->sf->results_array[0]['Contact'][0]['Email'];
			if ($this->sf->results_array[0]['Contact'][0][$altEmail] <> null) {
				$email = $this->sf->results_array[0]['Contact'][0][$altEmail];
			}
		} else if (strpos($id, SF_LEAD_ID_BGN) !== FALSE) {
			$sql_str = "Select email From Lead  where Id = '" . $id . "'";
			$this->sf->sfSoqlQuery($sql_str);
			$email = $this->sf->results_array[0]['Lead'][0]['Email'];
		} else {

			$sql_str = "select parent_email_flag from clients  where orgId = \"$orgID\"";
			$row = $this->dbconn_read_tx_clientmgt->query($sql_str);
			if (DB::iserror($row)) {
				print_r($row);
				exit;
			}
			$row->fetchinto($result);
			$parentEmail   = $result->parent_email_flag;
			$row->free();

			if ($parentEmail == "Y") {
				$sql_str = "Select " . $this->srm_prefix . "Email__c  From " . $this->srm_prefix . "Family_Relationship__c   where Id ='" . $id . "'";
				$this->sf->sfSoqlQuery($sql_str);
				$email = $this->sf->results_array[0][$this->srm_prefix . "Family_Relationship__c"][0][$this->srm_prefix . "Email__c"];
			}
		}



		return $email;
	}

	//  Get a field value  from SF
	function get_sf_val($bcast_id, $id, $obj, $field)
	{
		//select from the local DB

		$sql_str = "Select  `$obj.$field`  from express4sf_camp.$bcast_id where contact_id = '$id'";
		$row = $this->dbconn_read->query($sql_str);
		if (DB::iserror($row)) {


			$no_table = 1;
		}
		//echo "--".$sql_str;
		$no_table = 1;
		$row->fetchinto($result);


		$ff = "$obj.$field";
		$val = $result->$ff;

		//echo "***".$val."$$$";

		if ($no_table == 1) {
			$this->sf_login($dummy, $bcast_id);

			if ($obj == "Contact" or $obj == "Lead") {
				$id_ = "Id";
			} else {
				$id_ = "TargetXDev__Contact__c";
			}

			$sql_str = "Select $field From $obj  where $id_ = '" . $id . "'";
			//echo $sql_str;
			$this->sf->sfSoqlQuery($sql_str);
			$val = $this->sf->results_array[0][$obj][0][$field];
		}

		return $val;
	}

	function get_object($id)
	{
		$obj = "Lead";
		$con_bgn_str = SF_CONTACT_ID_BGN;
		if (substr($con_bgn_str, 0, 3) == $con_bgn_str) {
			$obj = "Contact";
		}

		return $obj;
	}

	function get_ctrl_flag($org_id, $bcast_id)
	{

		//get the org id and campaign id

		if (!empty($bcast_id)) {
			$sql_str = "select A.ctrl_flag from clientmgt.clients as A, broadcast_queue as B
    	             where A.client_id = B.client_id and B.broadcast_id =  \"$bcast_id\"";
			$row = $this->dbconn_read_tx_email->query($sql_str);
			if (DB::iserror($row)) {
				print_r($row);
				exit;
			}

			$row->fetchinto($result);
			$ctrl_flag = $result->ctrl_flag;
			$row->free();


			return $ctrl_flag;
		} else if (!empty($org_id)) {
			$sql_str = "select A.ctrl_flag from clients as A
    	             where A.orgId =   \"$org_id\"";
			$row = $this->dbconn_read_tx_clientmgt->query($sql_str);


			if (DB::iserror($row)) {
				print_r($row);
				exit;
			}

			$row->fetchinto($result);
			$ctrl_flag = $result->ctrl_flag;
			$row->free();


			return $ctrl_flag;
		}
	}


	function get_org_name($bcast_id)
	{

		//get the org id and campaign id
		$sql_str = "select A.orgname from clientmgt.clients as A, broadcast_queue as B
	             where A.client_id = B.client_id and B.broadcast_id =  \"$bcast_id\"";
		// echo $sql_str;
		$row = $this->dbconn_read_tx_email->query($sql_str);
		if (DB::iserror($row)) {
			print_r($row);
			exit;
		}

		$row->fetchinto($result);
		$org_name = $result->orgname;
		$row->free();


		return $org_name;
	}


	function get_prefixs($org, &$eml_prefix, &$srm_prefix, &$evnt_prefix, &$volunteer_prefix)
	{
		$org = substr($org, 0, 15);
		$sql_str = "select sf_eml_prefix,sf_evnt_prefix,sf_srm_prefix,sf_volunteer_prefix from clients where  orgid =\"$org\" ";

		$row = $this->dbconn_read_tx_clientmgt->query($sql_str);
		$row->fetchinto($result);

		$eml_prefix = $result->sf_eml_prefix;
		$evnt_prefix = $result->sf_evnt_prefix;
		$srm_prefix = $result->sf_srm_prefix;
		$volunteer_prefix = $result->sf_volunteer_prefix;
		$row->free();
	}

	function isAltEmailBroadacst($bcast_id)
	{
		$sql_str = "select alt_email from broadcast_queue where broadcast_id =\"$bcast_id\" ";
		$row = $this->dbconn_read_tx_email->query($sql_str);
		$row->fetchinto($result);
		if ($result->alt_email == true or $result->alt_email == 1) {
			return 1;
		}
		return 0;
		$row->free();
	}

	function getAltEmailField($emailPrefix, $org_id)
	{
		if (substr($org_id, 0, 15) == '00DF0000000gbVO') {
			return  'AltEmail__c';
		} else {
			return  $emailPrefix . 'AltEmail__c';
		}
	}


	// optout email
	function optout_email($id)
	{
		$obj = $this->get_object($id);
		$finalUpdateArray['HasOptedOutOfEmail'] = 1;

		$sObjectArray[] = $this->sf->sf_create_sobjects($id, $finalUpdateArray, $fieldstonull, $obj_type);
		$ret_val = $this->sf->sfUpdateQuery($sObjectArray);
		return $val;
	}

	function get_redir_url($bcast_id, $flag)
	{

		$sql_str = "select A.sub_url, A.unsub_url from clientmgt.clients as A, broadcast_queue as B
	             where A.client_id = B.client_id and B.broadcast_id =  \"$bcast_id\"";
		$row = $this->dbconn_read_tx_email->query($sql_str);
		if (DB::iserror($row)) {
			print_r($row);
			exit;
		}

		$row->fetchinto($result);
		$sub_url   = $result->sub_url;
		$unsub_url = $result->unsub_url;
		$row->free();
		if ($flag == "sub_url") {
			return $sub_url;
		} else {
			return $unsub_url;
		}

		/*
	       $sql_str = "Select expEmail_unsub_url__c,expEmail_sub_url__c From expSuitepreferences__c";
	       $this->sf->sfSoqlQuery($sql_str);
		   $sub_url = $this->sf->results_array[0][expSuitepreferences__c][0][expEmail_sub_url__c];
		   $unsub_url = $this->sf->results_array[0][expSuitepreferences__c][0][expEmail_unsub_url__c];

		   if($flag == "sub_url")
		   {
		      return $sub_url;
		   }
		   else
		   {
		      return $unsub_url;
		   }*/
	}
}// End of class
