PHP Code:
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
define("SOAP_CLIENT_BASEDIR", "includes/soapclient");
require_once (SOAP_CLIENT_BASEDIR.'/SforcePartnerClient.php');
$USERNAME = "YOURLOGIN";
$PASSWORD = "YOURPASSWORD";
$client = new SforcePartnerClient();
$client->createConnection(SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');
$myloginresult = $client->login($USERNAME, $PASSWORD);
$conn = mysql_connect("URL", "LOGIN", "PASS") or die(mysql_error());
mysql_select_db('SFmirror') or die(mysql_error());
mysql_set_charset('utf8', $conn);
//Start log file;
if(!$logfile = fopen("log.txt",'a+'))
{
echo "Cannot open file ($filename)";
exit;
}
//Get lastdatemodified from "mirror.info"
if(!$file = fopen("mirror.info",'r+'))
{
echo "Cannot open file ($filename)";
exit;
}
fwrite($logfile,"Starting Sync...\n");
$lastdatemodified = fgets($file);
$lastdatemodified = trim($lastdatemodified);
echo $lastdatemodified;
if (isset($client))
{
// Retrieve all the objects in the organization
$result = $client->describeGlobal();
// Cycle through each object
foreach ($result->types as $objectType)
{
echo "()";
echo $objectType;
echo "()";
$q = "Select ";
$resultObject = $client->describeSObject($objectType);
// Cycle through each field
foreach ($resultObject->fields as $field)
{
if ($field->name == "Binary")
{
$field->name = "Binary__c";
}
$q = $q . $field->name . ",";
}
$q = substr($q, 0, strlen($q) - 1);
$q = $q . " FROM ";
$q = $q . $objectType;
$q = $q . " where LastModifiedDate > ". $lastdatemodified;
//ObjecType exceptions to Ignore.
if(strcmp($objectType,"AccountHistory") == 0
|| strcmp($objectType,"AccountShare")== 0
|| strcmp($objectType,"ActivityHistory") == 0
|| strcmp($objectType,"AsyncApexJob")== 0
|| strcmp($objectType,"Case__c")== 0
|| strcmp($objectType,"CaseHistory")== 0
|| strcmp($objectType,"CaseSolution")== 0
|| strcmp($objectType,"ContactHistory")== 0
|| strcmp($objectType,"ContractHistory")== 0
|| strcmp($objectType,"DocumentAttachmentMap")== 0
|| strcmp($objectType,"EmailStatus")== 0
|| strcmp($objectType,"EmailTemplate")== 0
|| strcmp($objectType,"Event")== 0
|| strcmp($objectType,"FiscalYearSettings")== 0
|| strcmp($objectType,"GroupMember")== 0
|| strcmp($objectType,"LeadHistory")== 0
|| strcmp($objectType,"LeadHistory")== 0
|| strcmp($objectType,"Name")== 0
|| strcmp($objectType,"NoteAndAttachment")== 0
|| strcmp($objectType,"OpenActivity")== 0
|| strcmp($objectType,"OpportunityFieldHistory")== 0
|| strcmp($objectType,"OpportunityHistory")== 0
|| strcmp($objectType,"Period")== 0
|| strcmp($objectType,"ProcessInstanceHistory")== 0
|| strcmp($objectType,"ProcessInstanceStep")== 0
|| strcmp($objectType,"ProcessInstanceWorkitem")== 0
|| strcmp($objectType,"QueueSobject")== 0
|| strcmp($objectType,"Scontrol")== 0
|| strcmp($objectType,"SolutionHistory")== 0
|| strcmp($objectType,"UserLicense")== 0
|| strcmp($objectType,"UserPreference")== 0
|| strcmp($objectType,"UserRole")== 0
|| strcmp($objectType,"Vote")== 0
)
{
}
else
{
$field_list = array();
//build an array of field names which correspond to the current objectType;
$field_list = buildflist($field_list,$client,$objectType);
$queryOptions = new QueryOptions(500);
$response = $client->query(($q), $queryOptions);
if ($response->size > 0)
{
mysql__update($response,$objectType,$field_list,$conn);
while (!$response->done)
{
echo "<pre>";
echo "batch";
echo "</pre>";
set_time_limit(100);
$response = $client->queryMore($response->queryLocator);
mysql__update($response,$objectType,$field_list,$conn);
}
}
}
}
}
else
{
echo "Error establishing mysql connection on attempted table synch. Exiting...";
exit;
}
echo "test2";
include 'include/closedb.php';
update_timestamp($file,$logfile);
fwrite($logfile,"done");
fclose($logfile);
fclose($file);
function mysql__update($response,$objectType,$field_list,$conn)
{
if ($objectType == 'Case')
{
$objectType = 'Case__c';
}
elseif ($objectType == 'Group')
{
$objectType = 'Group__c';
}
else
{
$objectType = $objectType;
}
foreach($response->records as $record)
{
$push_list = array();
$query = "SELECT Count(*) FROM " . $objectType . " WHERE ID = \"" . $record->Id . "\"";
$cur_id = $record->Id;
array_push($push_list,$cur_id);
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$match = mysql_fetch_assoc($result);
foreach($record->fields as $field)
{
array_push($push_list,$field);
//$push_list = $push_list . $field . ",";
}
//Remove the last ',' from push_list
//If for some reason $push_list and $field_list do not have the same amount of elements, then
//something is amiss. Time to exit...
if(count($push_list) != count($field_list))
{
echo "Push_list != Field_List Exiting......";
exit;
}
//If match is 1, then the record exists and we are going to do an update
if($match['Count(*)'] == 1)
{
$updateq = "UPDATE " . $objectType . " SET ";
for($i=0;$i<count($push_list);$i++)
{
if($push_list[$i] != "")
{
$updateq = $updateq . $field_list[$i] . ' = "' . addslashes($push_list[$i]) . '",';
}
}
$updateq = substr($updateq, 0,-1);
$updateq = $updateq . ' WHERE Id = "' . $cur_id . '"';
//echo "<br />";
//echo $updateq;
$result = mysql_query($updateq);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
}
//If match is 0, then the record does not exist and we are going to do an insert
else
{
$insertq = "INSERT INTO " . $objectType . "(";
for($i=0;$i<count($push_list);$i++)
{
//Add field lists
if($push_list[$i] != "")
{
$insertq = $insertq . $field_list[$i] . ",";
}
}
$insertq = substr($insertq, 0, -1);
$insertq = $insertq . ") VALUES (";
for($i=0;$i<count($push_list);$i++)
{
//Add field lists
if($push_list[$i] != "")
{
$insertq = $insertq . '"' . addslashes($push_list[$i]) . '",';
}
}
$insertq = substr($insertq, 0, -1);
$insertq = $insertq . ")";
$result = mysql_query($insertq);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
//echo $insertq;
}
//echo "<br />";
//echo "<br />";
}
echo $objectType;
echo "<br />";
echo "done.";
}
function buildflist($field_list,$client,$objectType)
{
// Retrieve the object definition
$resultObject = $client->describeSObject($objectType);
// Cycle through each field
foreach ($resultObject->fields as $field)
{
$temp = $field->name;
array_push($field_list,$temp);
}
return $field_list;
}
function update_timestamp($file,$logfile)
{
echo "updating timestamp....";
$time = getdate();
$month;
$year;
$day;
$hour;
$min;
$second;
if(strlen($time['mon'])==1)
{
$month = '0' . $time['mon'];
}
else
{
$month = $time['mon'];
}
if(strlen($time['mday']) == 1)
{
$day = '0' . $time['mday'];
}
else
{
$day = $time['mday'];
}
if(strlen($time['hours']) == 1)
{
$hour = '0' . ($time['hours']-1);
}
else
{
$hour = $time['hours'];
}
if(strlen($time['minutes']) == 1)
{
$min = '0' . $time['minutes'];
}
else
{
$min = $time['minutes'];
}
if(strlen($time['seconds']) == 1)
{
$second = '0' . $time['seconds'];
}
else
{
$second = $time['seconds'];
}
$new_timestamp = $time['year']."-".$month."-".$day."T".$hour.":".$min.":".$second."Z";
rewind($file);
fwrite($file,$new_timestamp);
fwrite($logfile,$new_timestamp);
}
?>
Bookmarks