/** * * @param Account $account * @param <type> $settings * @return <type> */ public function performSync() { echo "-----synching contacts<br>\n"; $fnContactManager = new FunambolContactManager(); $wmContactManager = new ContactManager(); if (!$fnContactManager->InitManager() || !$wmContactManager->InitManager()) { echo "managers not inited<br>\n"; return false; } $user_id = $this->_account->IdUser; // in minutes. with dayLightSavingTime adjustment // WARNING // this is different from calendars $accountOffset = $this->_settings->AllowUsersChangeTimeZone ? $this->_account->GetDefaultTimeOffset() : $this->_account->GetDefaultTimeOffset($this->_settings->DefaultTimeZone); echo "making wmContactManager<br>\n"; echo "about to call wmContactManager->InitAccount<br>\n"; $wmContactManager->InitAccount($this->_account); echo "about to call wmContactManager->GetFullContactsList<br>\n"; $wmContactContainers = $wmContactManager->GetFullContactsList(true); echo "making fnContactManager<br>\n"; $fnContactManager->InitAccount($this->_account->Email); $fnContactContainers = $fnContactManager->GetFullContactsList(); echo "fnContactContainers LOOP STARTED<br>\n"; foreach ($fnContactContainers as $fnContactContainer) { // we are interested in the following set of fields from Funambol PIM record // 1. id - unique ID of the Funambol PIM record, INT // 2. last_update INT which is UNIX_TIMESTAMP*1000 + MSECS // 3. status 'N', 'U' and 'D' respectively // first_name // middle_name // last_name $funambolId = $fnContactContainer->GetValue('id'); $funambolStatus = $fnContactContainer->GetValue('status'); echo "---funambolId={$funambolId}, funambolStatus={$funambolStatus}<br/>\n"; $wmContactContainer = $this->GetContactContainerByFunambolId($wmContactContainers, $funambolId); if ($wmContactContainer == null) { echo "no in WM<br/>\n"; // this contact is presented in Funambol DB but is NOT presented in WM //if($funambolStatus === FUNAMBOL_STATUS_DELETED) //{ //echo "does not need sync<br/>\n"; // this case does not need sync // account is alredy deleted prior to get known to us //} //else //{ echo "adding from FN to WM<br/>\n"; // new account, need to insert in into WM $wmContactContainer = $this->ConvertFunambolToWMContactContainer($fnContactContainer, $user_id, TRUE); $wmContactManager->CreateContact($wmContactContainer); //} } else { echo "old known contact, sync it<br/>\n"; echo "wmId=" . $wmContactContainer->GetValue('IdAddress') . "<br/>\n"; // this contact is known for both Funambol and WM // 1. compare modification time // 2. update more old one // GMT modification date as 2000-12-31 23:59:59 $wmDateModified = $wmContactContainer->GetValue('DateModified'); // seconds from Epoch for this date $wmTimestampModified = strtotime($wmDateModified); // local server's timestamp - offset to get GMT as seconds from Epoch $fnTimestampModified = $this->ConvertFNtoWMTimestamp($fnContactContainer->GetValue('last_update'), TRUE); echo " wmtime=" . $wmDateModified . " " . $wmTimestampModified . "<br/>\n" . " fntime=" . date('Y-m-d H:i:s', $fnTimestampModified) . " " . $fnTimestampModified . " " . $fnContactContainer->GetValue('last_update') . "<br/>\n"; if ($wmTimestampModified == $fnTimestampModified) { echo "already synced<br/>\n"; } else { if ($wmTimestampModified > $fnTimestampModified) { echo "WM is newer, updating FN<br/>\n"; $fnContactContainer = $this->ConvertWMToFunambolContactContainer($wmContactContainer, TRUE); $fnContactManager->ReplaceContact($fnContactContainer); $fnContactManager->ReplaceContactAddressInfo($fnContactContainer); $fnContactManager->ReplaceContactOtherInfo($fnContactContainer); } else { echo "FN is newer, updating WM<br/>\n"; $wmCC = $this->ConvertFunambolToWMContactContainer($fnContactContainer, $user_id, TRUE); $wmCC->SetValue('IdAddress', $wmContactContainer->GetValue('IdAddress'), 'int'); $wmCC->SetValue('IdUser', $wmContactContainer->GetValue('IdUser'), 'int'); $wmCC->SetValue('DateCreated', $wmContactContainer->GetValue('DateCreated')); if ($wmContactManager->UpdateContact($wmCC)) { echo "updated<br/>\n"; } else { echo "NOT updated<br/>\n"; } } } // unset contact from $wmContactContainers // usable for old contacts only $this->RemoveWMContactContainer($wmContactContainers, $wmContactContainer); } // if( this contact is a new one or an old one ) } // foreach ($fnContactContainers as $fnContactContainer) echo "fnContactContainers LOOP ENDED<br>\n"; // here $wmContactContainers contains only newly created contacts on WM -side // we need to propagate them to Funambol echo "wmContactContainers LOOP STARTED<br>\n"; foreach ($wmContactContainers as $wmContactContainer) { echo "adding from WM to FN<br/>\n"; $fnContactContainer = $this->ConvertWMToFunambolContactContainer($wmContactContainer, TRUE); $fnContactManager->ReplaceContact($fnContactContainer); $fnContactManager->ReplaceContactAddressInfo($fnContactContainer); $fnContactManager->ReplaceContactOtherInfo($fnContactContainer); // get newly create id in Funambol DB and update WM table with it $wmContactContainer->SetValue('FunambolContactId', $fnContactContainer->GetValue('id')); $wmContactManager->UpdateFunambolContactId($wmContactContainer); } // foreach ($wmContactContainers as $wmContactContainer) echo "wmContactContainers LOOP ENDED<br>\n"; return true; }