user = $user; $this->targetConnector = $this->getTargetConnector(); $this->sourceConnector = $this->getSourceConnector(); $this->db = PearDatabase::getInstance(); } function getSourceConnector() { $connector = new WSAPP_VtigerConnector(); $connector->setSynchronizeController($this); $targetName = $this->targetConnector->getName(); if(empty ($targetName)){ throw new Exception('Target Name cannot be empty'); } return $connector->setName('Vtiger_'.$targetName); } function getTargetRecordModel($data) { return new WSAPP_TargetModel($data); } function getSourceRecordModel($data) { return new WSAPP_VtigerModel($data); } function getSyncStateModel($connector) { return $connector->getSyncState($this->getSourceType())->setType($this->getSourceType()); } function updateSyncStateModel($connector,WSAPP_SyncStateModel $syncStateModel){ return $connector->updateSyncState($syncStateModel); } public function synchronizePull() { $synchronizedRecords = array(); $sourceType = $this->getSourceType(); $this->sourceConnector->preEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PULL_EVENT); $this->targetConnector->preEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PUSH_EVENT); $syncStateModel = $this->getSyncStateModel($this->sourceConnector); $sourceRecords = $this->sourceConnector->pull($syncStateModel, $this->user); foreach($sourceRecords as $record){ $record->setSyncIdentificationKey(uniqid()); } $transformedRecords = $this->targetConnector->transformToTargetRecord($sourceRecords, $this->user); $targetRecords = $this->targetConnector->push($transformedRecords, $this->user); $targetSyncStateModel = $this->getSyncStateModel($this->targetConnector); foreach($sourceRecords as $sourceRecord){ $sourceId = $sourceRecord->getId(); foreach($targetRecords as $targetRecord){ if($sourceRecord->getSyncIdentificationKey() == $targetRecord->getSyncIdentificationKey()){ $sychronizeRecord = array(); $sychronizeRecord['source'] = $sourceRecord; $sychronizeRecord['target'] = $targetRecord; $synchronizedRecords[] = $sychronizeRecord; break; } } } $this->sourceConnector->postEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PULL_EVENT, $synchronizedRecords, $syncStateModel); $this->targetConnector->postEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PUSH_EVENT, $synchronizedRecords, $targetSyncStateModel); return $synchronizedRecords; } function synchronizePush(){ $synchronizedRecords = array(); $sourceType = $this->getSourceType(); $this->sourceConnector->preEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PUSH_EVENT); $this->targetConnector->preEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PULL_EVENT); $syncStateModel = $this->getSyncStateModel($this->targetConnector); $targetRecords = $this->targetConnector->pull($syncStateModel, $this->user); foreach($targetRecords as $record){ $record->setSyncIdentificationKey(uniqid()); } $transformedRecords = $this->targetConnector->transformToSourceRecord($targetRecords, $this->user); $sourceSyncStateModel = $this->getSyncStateModel($this->sourceConnector); $sourceRecords = $this->sourceConnector->push($transformedRecords, $sourceSyncStateModel); foreach ($targetRecords as $targetRecord) { $targetId = $targetRecord->getId(); foreach ($sourceRecords as $sourceRecord) { if ($sourceRecord->getSyncIdentificationKey() == $targetRecord->getSyncIdentificationKey()) { $sychronizeRecord = array(); $sychronizeRecord['source'] = $sourceRecord; $sychronizeRecord['target'] = $targetRecord; $synchronizedRecords[] = $sychronizeRecord; break; } } } $this->targetConnector->postEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PULL_EVENT, $synchronizedRecords, $syncStateModel); $this->sourceConnector->postEvent(self::WSAPP_SYNCHRONIZECONTROLLER_PUSH_EVENT, $synchronizedRecords, $sourceSyncStateModel); $this->updateSyncStateModel($this->sourceConnector, $sourceSyncStateModel); return $synchronizedRecords; } public function synchronize($pullTargetFirst = true, $push = true, $pull = true) { $records = array(); $currentTime = date('y-m-d H:i:s'); $user = Users_Record_model::getCurrentUserModel(); $records['synctime'] = $currentTime; $records['Extension'] = explode('_',get_class($this)); $records['ExtensionModule'] = $this->getSourceType(); $records['user'] = $user->id; if ($pullTargetFirst) { if($push) $records['push'] = $this->synchronizePush(); if($pull) $records['pull'] = $this->synchronizePull(); } else { if($pull) $records['pull'] = $this->synchronizePull(); if($push) $records['push'] = $this->synchronizePush(); } //To Log sync information WSAPP_Logs::add($records); return $records; } } ?>