Beispiel #1
0
 function sync_highrise()
 {
     $db =& JFactory::getDBO();
     $params =& JComponentHelper::getParams('com_joomailermailchimpintegration');
     $highrise_url = $params->get('params.highrise_url');
     $highrise_api_token = $params->get('params.highrise_api_token');
     $config = $this->getModel('sync')->getConfig('highrise');
     if ($config == NULL) {
         jimport('joomla.application.component.helper');
         $cHelper = JComponentHelper::getComponent('com_comprofiler', false);
         $cbInstalled = $cHelper->enabled;
         $config = new stdClass();
         $config->{'first-name'} = $cbInstalled ? 'CB' : 'core';
         $config->email_work = 'default';
     }
     $validator = new EmailAddressValidator();
     $elements = JRequest::getVar('elements', '', 'request', 'string');
     $elements = json_decode($elements);
     if ($elements->done == 0) {
         $_SESSION['abortAJAX'] = 0;
         unset($_SESSION['addedUsers']);
     }
     $failed = $elements->errors;
     $errorMsg = $elements->errorMsg;
     $step = $elements->step;
     if ($_SESSION['abortAJAX'] != 1) {
         if (isset($_SESSION['addedUsers'])) {
             $exclude = $_SESSION['addedUsers'];
         } else {
             $exclude = array();
         }
         $addedUsers = $exclude;
         if (isset($exclude[0])) {
             $exclude = implode('","', $exclude);
             $exclude = '"' . $exclude . '"';
             $excludeCond = 'AND id NOT IN (' . $exclude . ') ';
         } else {
             $excludeCond = '';
         }
         if ($elements->range == 'all') {
             $query = 'SELECT * FROM #__users ' . 'WHERE block = 0 ' . $excludeCond . 'ORDER BY id ' . 'LIMIT ' . $step;
         } else {
             $idList = implode(" OR id = ", $elements->cid);
             $query = 'SELECT * FROM #__users ' . 'WHERE block = 0 ' . $excludeCond . 'AND (id = ' . $idList . ') ' . 'ORDER BY id ';
         }
         $db->setQuery($query);
         $users = $db->loadObjectList();
         $queryJS = false;
         $queryCB = false;
         $JSand = array();
         foreach ($config as $k => $v) {
             if ($k != 'first-name' && $k != 'last-name') {
                 $vEx = explode(';', $v);
                 if ($vEx[0] == 'js') {
                     $queryJS = true;
                     $JSand[] = $vEx[1];
                 } else {
                     if ($vEx[0] == 'CB') {
                         $queryCB = true;
                     }
                 }
             }
         }
         $JSand = implode("','", array_unique($JSand));
         require_once JPATH_ADMINISTRATOR . '/components/com_joomailermailchimpintegration/libraries/push2Highrise.php';
         $highrise = new Push_Highrise($highrise_url, $highrise_api_token);
         $data = array();
         $emails = array();
         $x = 0;
         $new = $elements->new;
         $updated = $elements->updated;
         $userIDs = array();
         foreach ($users as $user) {
             if ($validator->check_email_address($user->email)) {
                 $request = array();
                 $userCB = false;
                 $names = explode(' ', $user->name);
                 $firstname = $names[0];
                 $lastname = '';
                 if (isset($names[1])) {
                     for ($i = 1; $i < count($names); $i++) {
                         $lastname .= $names[$i] . ' ';
                     }
                 }
                 $lastname = trim($lastname);
                 if ($config->{'first-name'} != 'core') {
                     $query = "SELECT * FROM #__comprofiler WHERE user_id = '{$user->id}'";
                     $db->setQuery($query);
                     $userCB = $db->loadObjectList();
                     $firstname = $userCB[0]->firstname ? $userCB[0]->firstname : $firstname;
                     $lastname = $userCB[0]->lastname ? $userCB[0]->lastname : $lastname;
                     if ($userCB[0]->middlename != '') {
                         $lastname = $userCB[0]->middlename . ' ' . $lastname;
                     }
                 }
                 $highriseUser = $highrise->person_in_highrise(array('first-name' => $firstname, 'last-name' => $lastname));
                 $request['id'] = $highriseUser->id;
                 //	    var_dump($highriseUser);die;
                 if ($queryJS) {
                     $query = "SELECT field_id, value FROM #__community_fields_values " . "WHERE user_id = '{$user->id}' " . "AND field_id IN ('{$JSand}')";
                     $db->setQuery($query);
                     $JSfields = $db->loadObjectList();
                     $JSfieldsArray = array();
                     foreach ($JSfields as $jsf) {
                         $JSfieldsArray[$jsf->field_id] = $jsf->value;
                     }
                 }
                 if ($queryCB) {
                     if (!$userCB) {
                         $query = "SELECT * FROM #__comprofiler WHERE user_id = '{$user->id}'";
                         $db->setQuery($query);
                         $userCB = $db->loadObjectList();
                     }
                 }
                 $xml = "<person>\n";
                 if ((int) $highriseUser->id > 0) {
                     $xml .= '<id>' . $highriseUser->id . "</id>\n";
                 }
                 $xml .= "<first-name>" . htmlspecialchars($firstname) . "</first-name>\n" . "<last-name>" . htmlspecialchars($lastname) . "</last-name>";
                 if (isset($config->title) && $config->title != '') {
                     $conf = explode(';', $config->title);
                     $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                     $xml .= "\n<title>" . htmlspecialchars($value) . "</title>";
                 }
                 if (isset($config->background) && $config->background != '') {
                     $conf = explode(';', $config->background);
                     $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                     $xml .= "\n<background>" . htmlspecialchars($value) . "</background>";
                 }
                 if (isset($config->company) && $config->company != '') {
                     $conf = explode(';', $config->company);
                     $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                     $xml .= "\n<company-name>" . htmlspecialchars($value) . '</company-name>';
                 }
                 $xml .= "\n<contact-data>";
                 $xml .= "\n<email-addresses>";
                 $emailTypes = array('work', 'home', 'other');
                 foreach ($emailTypes as $et) {
                     if (isset($config->{'email_' . $et}) && $config->{'email_' . $et} != '') {
                         if ($config->{'email_' . $et} == 'default') {
                             $value = $user->email;
                         } else {
                             $conf = explode(';', $config->{'email_' . $et});
                             $value = $conf[0] == 'js' ? $JSfieldsArray[$conf[1]] : $userCB[0]->{$conf[1]};
                         }
                         $fieldId = '';
                         if (isset($highriseUser->{'contact-data'}->{'email-addresses'}->{'email-address'})) {
                             foreach ($highriseUser->{'contact-data'}->{'email-addresses'} as $hu) {
                                 foreach ($hu->{'email-address'} as $ea) {
                                     if ($ea->location == ucfirst($et)) {
                                         $fieldId = '<id type="integer">' . $ea->id[0] . "</id>\n";
                                         break;
                                     }
                                 }
                             }
                         }
                         $xml .= "\n<email-address>\n" . $fieldId . "<address>" . htmlspecialchars($value) . "</address>\n" . "<location>" . ucfirst($et) . "</location>\n" . "</email-address>";
                     }
                 }
                 $xml .= "\n</email-addresses>\n";
                 $xml .= "\n<phone-numbers>\n";
                 $phoneTypes = array('work', 'mobile', 'fax', 'pager', 'home', 'skype', 'other');
                 foreach ($phoneTypes as $pt) {
                     if ($config->{'phone_' . $pt} != NULL && $config->{'phone_' . $pt} != '') {
                         $conf = explode(';', $config->{'phone_' . $pt});
                         $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                         $fieldId = '';
                         if (isset($highriseUser->{'contact-data'}->{'phone-numbers'}->{'phone-number'})) {
                             foreach ($highriseUser->{'contact-data'}->{'phone-numbers'} as $hu) {
                                 foreach ($hu->{'phone-number'} as $pn) {
                                     if ($pn->location == ucfirst($pt)) {
                                         $fieldId = '<id type="integer">' . $pn->id[0] . "</id>\n";
                                         break;
                                     }
                                 }
                             }
                         }
                         $xml .= "<phone-number>\n" . $fieldId . "<number>" . htmlspecialchars($value) . "</number>\n" . "<location>" . ucfirst($pt) . "</location>\n" . "</phone-number>";
                     }
                 }
                 $xml .= "\n</phone-numbers>\n";
                 $xml .= "\n<instant-messengers>\n";
                 $imTypes = array('AIM', 'MSN', 'ICQ', 'Jabber', 'Yahoo', 'Skype', 'QQ', 'Sametime', 'Gadu-Gadu', 'Google Talk', 'Other');
                 foreach ($imTypes as $im) {
                     if (isset($config->{$im}) && $config->{$im} != '') {
                         $value = false;
                         if ($config->{$im} == 'default') {
                             $value = $user->email;
                         } else {
                             if ($config->{$im} != '') {
                                 $conf = explode(';', $config->{$im});
                                 $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                             }
                         }
                         if ($value) {
                             $fieldId = '';
                             if (isset($highriseUser->{'contact-data'}->{'instant-messengers'}->{'instant-messenger'})) {
                                 foreach ($highriseUser->{'contact-data'}->{'instant-messengers'} as $imx) {
                                     foreach ($imx->{'instant-messenger'} as $ia) {
                                         if ($ia->protocol == $im) {
                                             $fieldId = '<id type="integer">' . $ia->id[0] . "</id>\n";
                                             break;
                                         }
                                     }
                                 }
                             }
                             $xml .= "<instant-messenger>\n" . $fieldId . "<address>" . htmlspecialchars($value) . "</address>\n" . "<location>Work</location>\n" . "<protocol>" . $im . "</protocol>\n" . "</instant-messenger>";
                         }
                     }
                 }
                 $xml .= "\n</instant-messengers>\n";
                 if (isset($config->website) && $config->website != '') {
                     $xml .= "\n<web-addresses>\n";
                     $conf = explode(';', $config->website);
                     $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                     $fieldId = '';
                     if (isset($highriseUser->{'contact-data'}->{'web-addresses'}->{'web-address'})) {
                         foreach ($highriseUser->{'contact-data'}->{'web-addresses'} as $ws) {
                             foreach ($ws->{'web-address'} as $wa) {
                                 if ($wa->location == 'Work') {
                                     $fieldId = '<id type="integer">' . $wa->id[0] . "</id>\n";
                                     break;
                                 }
                             }
                         }
                     }
                     $xml .= "<web-address>\n" . $fieldId . "<url>" . htmlspecialchars($value) . "</url>\n" . "<location>Work</location>\n" . "</web-address>";
                     $xml .= "\n</web-addresses>\n";
                 }
                 if (isset($config->twitter) && $config->twitter != '') {
                     $xml .= "\n<twitter-accounts>\n";
                     $conf = explode(';', $config->twitter);
                     $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                     $value = removeSpecialCharacters($value);
                     $fieldId = '';
                     if (isset($highriseUser->{'contact-data'}->{'twitter-accounts'}->{'twitter-account'})) {
                         foreach ($highriseUser->{'contact-data'}->{'twitter-accounts'} as $tac) {
                             foreach ($tac->{'twitter-account'} as $ta) {
                                 if ($ta->location == 'Personal') {
                                     $fieldId = '<id type="integer">' . $ta->id[0] . "</id>\n";
                                     break;
                                 }
                             }
                         }
                     }
                     $xml .= "<twitter-account>\n" . $fieldId . "<username>" . htmlspecialchars(str_replace(' ', '', $value)) . "</username>\n" . "<location>Personal</location>\n" . "</twitter-account>";
                     $xml .= "\n</twitter-accounts>\n";
                 }
                 if (isset($config->street) && $config->street != '' || isset($config->city) && $config->city != '' || isset($config->zip) && $config->zip != '' || isset($config->state) && $config->state != '' || isset($config->country) && $config->country != '') {
                     $xml .= "\n<addresses>\n";
                     $xml .= "<address>\n";
                     $fieldId = '';
                     if (isset($highriseUser->{'contact-data'}->addresses->address)) {
                         foreach ($highriseUser->{'contact-data'}->addresses as $ads) {
                             foreach ($ads->address as $ad) {
                                 if ($ad->location == 'Work') {
                                     $fieldId = '<id type="integer">' . $ad->id[0] . "</id>\n";
                                     break;
                                 }
                             }
                         }
                     }
                     $xml .= $fieldId;
                     if (isset($config->street) && $config->street != '') {
                         $conf = explode(';', $config->street);
                         $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                         $xml .= "<street>" . htmlspecialchars($value) . "</street>\n";
                     }
                     if (isset($config->city) && $config->city != '') {
                         $conf = explode(';', $config->city);
                         $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                         $xml .= "<city>" . htmlspecialchars($value) . "</city>\n";
                     }
                     if (isset($config->zip) && $config->zip != '') {
                         $conf = explode(';', $config->zip);
                         $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                         $xml .= "<zip>" . htmlspecialchars($value) . "</zip>\n";
                     }
                     if (isset($config->state) && $config->state != '') {
                         $conf = explode(';', $config->state);
                         $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                         $xml .= "<state>" . htmlspecialchars($value) . "</state>\n";
                     }
                     if (isset($config->country) && $config->country != '') {
                         $conf = explode(';', $config->country);
                         $value = $conf[0] == 'js' ? isset($JSfieldsArray[$conf[1]]) ? $JSfieldsArray[$conf[1]] : '' : (isset($userCB[0]->{$conf[1]}) ? $userCB[0]->{$conf[1]} : '');
                         $xml .= "<country>" . htmlspecialchars($value) . "</country>\n";
                     }
                     $xml .= "<location>Work</location>\n";
                     $xml .= "</address>\n";
                     $xml .= "</addresses>\n";
                 }
                 $xml .= "\n</contact-data>";
                 $xml .= "\n</person>";
                 $request['xml'] = $xml;
                 $apiResult = $highrise->pushContact($request);
                 if ($apiResult['status'] != 200 && $apiResult['status'] != 201) {
                     // error
                     $failed++;
                     $errorMsg .= '"Server returned error code ' . $apiResult['status'] . ' for user ' . $user->name . ' (ID ' . $user->id . ')", ';
                     $apiResult['newContacts'] = 0;
                     $apiResult['updated'] = 0;
                 } else {
                     // success
                     $query = "INSERT INTO #__joomailermailchimpintegration_crm_users " . "(crm, user_id) VALUES " . "('highrise', '{$user->id}.')";
                     $db->setQuery($query);
                     $db->execute();
                     $addedUsers[] = $user->id;
                 }
             } else {
                 $failed++;
                 $errorMsg .= '"Invalid email => ' . $user->email . ' (' . $user->name . ' - ID ' . $user->id . ')", ';
                 $apiResult['newContacts'] = 0;
                 $apiResult['updated'] = 0;
             }
         }
     } else {
         unset($_SESSION['addedUsers']);
         $response['finished'] = 1;
         $response['addedUsers'] = '';
         $response['abortAJAX'] = $_SESSION['abortAJAX'];
         echo json_encode($response);
     }
     if (!count($users)) {
         $done = $elements->total;
         unset($_SESSION['addedUsers']);
         $percent = 100;
     } else {
         $done = count($addedUsers);
         $_SESSION['addedUsers'] = $addedUsers;
         $percent = $done / $elements->total * 100;
     }
     $response['msg'] = '<div id="bg"></div>' . '<div id="progressBarContainer">' . '<div id="progressBarTitle">' . JText::_('JM_ADDING_USERS') . ' (' . $done . '/' . $total . ' ' . JText::_('JM_DONE') . ')</div>' . '<div id="progressBarBg">' . '<div id="progressBarCompleted" style="width: ' . round($percent) . '%;"></div>' . '<div id="progressBarNumber">' . round($percent) . ' %</div>' . '</div>' . '<a id="sbox-btn-close" href="javascript:joomlamailerJS.sync.abortAJAX();">abort</a>' . '</div>';
     $response['done'] = $done;
     $response['newContacts'] = $new + $apiResult['new'];
     $response['updated'] = $updated + $apiResult['updated'];
     $response['errors'] = $failed;
     $response['errorMsg'] = $errorMsg;
     if ($done + $failed >= $elements->total) {
         unset($_SESSION['addedUsers']);
         $response['finished'] = 1;
         if ($errorMsg) {
             $errorMsg = substr($errorMsg, 0, -2);
             $msgErrors = ' ; ' . $failed . ' ' . JText::_('JM_ERRORS') . ': ' . $errorMsg . ' ';
         }
         $msg = $done + $failed . ' ' . JText::_('JM_USERS_PROCESSED');
         $msg .= ' (' . $response['newContacts'] . ' ' . JText::_('JM_NEW') . ' ; ' . $response['updated'] . ' ' . JText::_('JM_UPDATED') . ' ';
         if (isset($msgErrors) && $msgErrors) {
             $msg .= $msgErrors;
         }
         $msg .= ')';
         $response['finalMessage'] = $msg;
     } else {
         $response['finished'] = 0;
         $response['finalMessage'] = '';
     }
     $response['abortAJAX'] = $_SESSION['abortAJAX'];
     echo json_encode($response);
 }
	function addToHighrise( $uid ){

	    $user =& JFactory::getUser( $uid );
	    $db	=& JFactory::getDBO();
	    $params =& JComponentHelper::getParams( 'com_joomailermailchimpintegration' );
	    $paramsPrefix = (version_compare(JVERSION,'1.6.0','ge')) ? 'params.' : '';
	    $highrise_url = $params->get( $paramsPrefix.'highrise_url' );
	    $highrise_api_token = $params->get( $paramsPrefix.'highrise_api_token' );

	    $config = $this->getCrmConfig( 'highrise' );
	    if( $config == NULL ){
		jimport('joomla.filesystem.file');
		if ( JFile::exists( JPATH_ADMINISTRATOR.'/components/com_comprofiler/admin.comprofiler.php') ) {
		    jimport( 'joomla.application.component.helper' );
		    $cHelper = JComponentHelper::getComponent( 'com_comprofiler', true );
		} else {
		     $cHelper->enabled = false;
		}

		$config = new stdClass();
		$config->{'first-name'} = ($cHelper->enabled) ? 'CB' : 'core';
		$config->email_work = 'default';
	    }

	    require_once( JPATH_ADMINISTRATOR.'/components/com_joomailermailchimpintegration/helpers'.DS.'common.php' );

	    $queryJS = false;
	    $queryCB = false;
	    $JSand = array();
	    foreach( $config as $k => $v ){
		if( $k != 'first-name' && $k != 'last-name' ){
		    $vEx = explode(';', $v);
		    if($vEx[0] == 'js' ) {
			$queryJS = true;
			$JSand[] = $vEx[1];
		    } else {
			$queryCB = true;
		    }
		}
	    }
	    $JSand = implode("','", array_unique($JSand) );

	    require_once(JPATH_ADMINISTRATOR.'/components/com_joomailermailchimpintegration/libraries/push2Highrise.php');
	    $highrise = new Push_Highrise($highrise_url, $highrise_api_token);

	    $userCB = false;

	    if( $config->{'first-name'} == 'core' ){
		$names = explode(' ', $user->name);
		$firstname = $names[0];
		$lastname = '';
		if(isset($names[1])){
		    for($i=1;$i<count($names);$i++){
			$lastname .= $names[$i].' ';
		    }
		}
		$lastname = trim($lastname);
	    } else {
		$query = "SELECT * FROM #__comprofiler WHERE user_id = '$user->id'";
		$db->setQuery($query);
		$userCB = $db->loadObjectList();

		$firstname = $userCB[0]->firstname;
		$lastname  = $userCB[0]->lastname;
		if( $userCB[0]->middlename != '' ){
		    $lastname = $userCB[0]->middlename.' '.$lastname;
		}
	    }

	    $highriseUser = $highrise->person_in_highrise( array( 'first-name' => $firstname, 'last-name' => $lastname) );
	    $request['id'] = $highriseUser->id;

	    if( $queryJS ){
		$query = "SELECT field_id, value FROM #__community_fields_values ".
			 "WHERE user_id = '$user->id' ".
			 "AND field_id IN ('$JSand')";
		$db->setQuery($query);
		$JSfields = $db->loadObjectList();
		$JSfieldsArray = array();
		foreach($JSfields as $jsf){
		    $JSfieldsArray[$jsf->field_id] = $jsf->value;
		}
	    }

	    if( $queryCB ){
		if( !$userCB ){
		    $query = "SELECT * FROM #__comprofiler WHERE user_id = '$user->id'";
		    $db->setQuery($query);
		    $userCB = $db->loadObjectList();
		}
	    }

	    $xml =  "<person>\n";

	    if( (int)$highriseUser->id > 0){
		$xml .= '<id>'.$highriseUser->id."</id>\n";
	    }

	    $xml .=  "<first-name>".htmlspecialchars($firstname)."</first-name>\n"
		    ."<last-name>".htmlspecialchars($lastname)."</last-name>";



	    if( isset($config->title) && $config->title != '' ){
		$conf = explode(';', $config->title);
		$value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		$xml .= "\n<title>".htmlspecialchars($value)."</title>";
	    }
	    if( isset($config->background) && $config->background != '' ){
		$conf = explode(';', $config->background);
		$value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		$xml .= "\n<background>".htmlspecialchars($value)."</background>";
	    }
	    if( isset($config->company) && $config->company != '' ){
		$conf = explode(';', $config->company);
		$value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		$xml .= "\n<company-name>".htmlspecialchars($value).'</company-name>';
	    }


	    $xml .= "\n<contact-data>";
	    $xml .= "\n<email-addresses>";

	    $emailTypes = array( 'work', 'home', 'other' );
	    foreach ($emailTypes as $et){

		if( isset($config->{'email_'.$et}) && $config->{'email_'.$et} != '' ){
		if($config->{'email_'.$et} == 'default'){
		    $value = $user->email;
		} else {
		    $conf = explode(';', $config->{'email_'.$et});
		    $value = ( $conf[0] == 'js' ) ?  $JSfieldsArray[$conf[1]] : $userCB[0]->{$conf[1]};
		}

		$fieldId = '';
		if( isset($highriseUser->{'contact-data'}->{'email-addresses'}->{'email-address'}) ){
		foreach( $highriseUser->{'contact-data'}->{'email-addresses'} as $hu){
		    foreach( $hu->{'email-address'} as $ea){
			if( $ea->location == ucfirst($et) ){
			    $fieldId = '<id type="integer">'.$ea->id[0]."</id>\n";
			    break;
			}
		    }
		}
		}
		$xml .= "\n<email-address>\n"
			    .$fieldId
			    ."<address>".htmlspecialchars($value)."</address>\n"
			    ."<location>".ucfirst($et)."</location>\n"
			."</email-address>";
		}


	    }

	    $xml .= "\n</email-addresses>\n";

	    $xml .= "\n<phone-numbers>\n";
	    $phoneTypes = array('work','mobile','fax','pager','home','skype','other');
	    foreach($phoneTypes as $pt){
		if( isset($config->{'phone_'.$pt}) && $config->{'phone_'.$pt} != '' ){
		    $conf = explode(';', $config->{'phone_'.$pt});
		    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		}

		$fieldId = '';
		if( isset($highriseUser->{'contact-data'}->{'phone-numbers'}->{'phone-number'}) ){
		foreach( $highriseUser->{'contact-data'}->{'phone-numbers'} as $hu){
		    foreach( $hu->{'phone-number'} as $pn){
			if( $pn->location == ucfirst($pt) ){
			    $fieldId = '<id type="integer">'.$pn->id[0]."</id>\n";
			    break;
			}
		    }
		}
		}
		$xml .= "<phone-number>\n"
			    .$fieldId
			    ."<number>".htmlspecialchars($value)."</number>\n"
			    ."<location>".ucfirst($pt)."</location>\n"
			."</phone-number>";
	    }
	    $xml .= "\n</phone-numbers>\n";

	    $xml .= "\n<instant-messengers>\n";
	    $imTypes = array('AIM','MSN','ICQ','Jabber','Yahoo','Skype','QQ','Sametime','Gadu-Gadu','Google Talk','Other');
	    foreach($imTypes as $im){
		if( isset($config->{$im}) && $config->{$im} != '' ){
		    $value = false;
		    if( $config->{$im} == 'default' ){
			$value = $user->email;
		    } else if( $config->{$im} != '' ){
			$conf = explode(';', $config->{$im});
			$value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		    }
		    if( $value ){
			$fieldId = '';
			if( isset($highriseUser->{'contact-data'}->{'instant-messengers'}->{'instant-messenger'}) ){
			foreach( $highriseUser->{'contact-data'}->{'instant-messengers'} as $imx){
			    foreach( $imx->{'instant-messenger'} as $ia){
				if( $ia->protocol == $im ){
				    $fieldId = '<id type="integer">'.$ia->id[0]."</id>\n";
				    break;
				}
			    }
			}
			}
			$xml .= "<instant-messenger>\n"
				    .$fieldId
				    ."<address>".htmlspecialchars($value)."</address>\n"
				    ."<location>Work</location>\n"
				    ."<protocol>".$im."</protocol>\n"
				."</instant-messenger>";
		    }
		}
	    }
	    $xml .= "\n</instant-messengers>\n";

	    if( isset($config->website) && $config->website != '' ){
	    $xml .= "\n<web-addresses>\n";
	    $conf = explode(';', $config->website);
	    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');

	    $fieldId = '';
	    if( isset($highriseUser->{'contact-data'}->{'web-addresses'}->{'web-address'}) ){
	    foreach( $highriseUser->{'contact-data'}->{'web-addresses'} as $ws){
		foreach( $ws->{'web-address'} as $wa){
		    if( $wa->location == 'Work' ){
			$fieldId = '<id type="integer">'.$wa->id[0]."</id>\n";
			break;
		    }
		}
	    }
	    }
	    $xml .= "<web-address>\n"
			.$fieldId
			."<url>".htmlspecialchars($value)."</url>\n"
			."<location>Work</location>\n"
		    ."</web-address>";
	    $xml .= "\n</web-addresses>\n";
	    }

	    if( isset($config->twitter) && $config->twitter != '' ){
	    $xml .= "\n<twitter-accounts>\n";
	    $conf = explode(';', $config->twitter);
	    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
	    $value = removeSpecialCharacters( $value );
	    $fieldId = '';
	    if( isset($highriseUser->{'contact-data'}->{'twitter-accounts'}->{'twitter-account'}) ){
	    foreach( $highriseUser->{'contact-data'}->{'twitter-accounts'} as $tac){
		foreach( $tac->{'twitter-account'} as $ta){
		    if( $ta->location == 'Personal' ){
			$fieldId = '<id type="integer">'.$ta->id[0]."</id>\n";
			break;
		    }
		}
	    }
	    }
	    $xml .= "<twitter-account>\n"
			.$fieldId
			."<username>".htmlspecialchars( str_replace(' ','',$value) )."</username>\n"
			."<location>Personal</location>\n"
		    ."</twitter-account>";
	    $xml .= "\n</twitter-accounts>\n";
	    }

	    if(    ( isset($config->street) && $config->street != '' )
		|| ( isset($config->city)   && $config->city != ''   )
		|| ( isset($config->zip)    && $config->zip != ''    )
		|| ( isset($config->state)  && $config->state != ''  )
		|| ( isset($config->country)&& $config->country != '')
	      ){
		$xml .= "\n<addresses>\n";
		$xml .= "<address>\n";

		$fieldId = '';
		if( isset($highriseUser->{'contact-data'}->addresses->address) ){
		foreach( $highriseUser->{'contact-data'}->addresses as $ads){
		    foreach( $ads->address as $ad){
			if( $ad->location == 'Work' ){
			    $fieldId = '<id type="integer">'.$ad->id[0]."</id>\n";
			    break;
			}
		    }
		}
		}
		$xml .= $fieldId;

		if( isset($config->street) && $config->street != '' ) {
		    $conf = explode(';', $config->street);
		    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		    $xml .= "<street>".htmlspecialchars($value)."</street>\n";
		}
		if( isset($config->city)   && $config->city != '' ) {
		    $conf = explode(';', $config->city);
		    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		    $xml .= "<city>".htmlspecialchars($value)."</city>\n";
		}
		if( isset($config->zip)    && $config->zip != '' ) {
		    $conf = explode(';', $config->zip);
		    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		    $xml .= "<zip>".htmlspecialchars($value)."</zip>\n";
		}
		if( isset($config->state)  && $config->state != '' ) {
		    $conf = explode(';', $config->state);
		    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		    $xml .= "<state>".htmlspecialchars($value)."</state>\n";
		}
		if( isset($config->country) && $config->country != '' ) {
		    $conf = explode(';', $config->country);
		    $value = ( $conf[0] == 'js' ) ?  ( (isset($JSfieldsArray[$conf[1]]))?$JSfieldsArray[$conf[1]]:'') : ((isset($userCB[0]->{$conf[1]}))?$userCB[0]->{$conf[1]}:'');
		    $xml .= "<country>".htmlspecialchars($value)."</country>\n";
		}

		$xml .= "<location>Work</location>\n";
		$xml .= "</address>\n";
		$xml .= "</addresses>\n";
	    }

	    $xml .= "\n</contact-data>";
	    $xml .= "\n</person>";

	    $request['xml'] = $xml;

	    $highrise->pushContact($request);

	    return;
	}
/**
* function to get specific countries from a region
* @param: $region e.g. africa, south america etc
*/
function loadCountriesFromRegion($region,$companyID) {
	if($region) {
		global $trailSessionUser;
	
		$theUserID=0;
		//ensure $trailSessionUser is not a visitor
		if(substr($trailSessionUser,0,7)!="visitor") {
			$theUserID=getUserID($trailSessionUser);
		}
	
		//get the towns first
		$query=0;
		$query=mysqlquery("select distinct countryID,country from vl_countries where region='$region' order by country");
		if(mysqlnumrows($query)) {
			$return=0;
			$return="
				<table width=\"100%\" border=\"0\">
				  <tr>
					<td colspan=\"2\">Select the markets covered:</td>
				  </tr>";
			$q=array();
			while($q=mysqlfetcharray($query)) {
				$return.="
						<tr>
							<td width=\"1%\"><input type=\"checkbox\" name=\"marketscoveredUnique[]\" value=\"$q[countryID]\" ".(checkMarketAgainstProvider($theUserID,$q["countryID"],$companyID)?"checked":"")."></td>
							<td width=\"99%\">".removeSpecialCharacters($q[country])."</td>
						</tr>";
			}
			$return.="</table>";
	
			return $return;
		} else {
			$return=0;
			//$return="No countries found in database!";
			$return="";
	
			return $return;
		}
	}
}