public function emitContactsSyncSettingUI(Vtiger_Request $request) { $user = Users_Record_Model::getCurrentUserModel(); $connector = new Google_Contacts_Connector(FALSE); $fieldMappping = Google_Utils_Helper::getFieldMappingForUser(); $oauth2 = new Google_Oauth2_Connector($request->get('sourcemodule')); if ($oauth2->hasStoredToken()) { $controller = new Google_Contacts_Controller($user); $connector = $controller->getTargetConnector(); $groups = $connector->pullGroups(); } $targetFields = $connector->getFields(); $selectedGroup = Google_Utils_Helper::getSelectedContactGroupForUser(); $syncDirection = Google_Utils_Helper::getSyncDirectionForUser($user); $contactsModuleModel = Vtiger_Module_Model::getInstance($request->get('sourcemodule')); $mandatoryMapFields = array('salutationtype', 'firstname', 'lastname', 'title', 'account_id', 'birthday', 'email', 'secondaryemail', 'mobile', 'phone', 'homephone', 'mailingstreet', 'otherstreet', 'mailingpobox', 'otherpobox', 'mailingcity', 'othercity', 'mailingstate', 'otherstate', 'mailingzip', 'otherzip', 'mailingcountry', 'othercountry', 'otheraddress', 'description', 'mailingaddress', 'otheraddress'); $customFieldMapping = array(); $contactsFields = $contactsModuleModel->getFields(); foreach ($fieldMappping as $vtFieldName => $googleFieldDetails) { if (!in_array($vtFieldName, $mandatoryMapFields) && $contactsFields[$vtFieldName]->isViewable()) { $customFieldMapping[$vtFieldName] = $googleFieldDetails; } } $skipFields = array('reference', 'contact_id', 'leadsource', 'assigned_user_id', 'donotcall', 'notify_owner', 'emailoptout', 'createdtime', 'modifiedtime', 'contact_no', 'modifiedby', 'isconvertedfromlead', 'created_user_id', 'portal', 'support_start_date', 'support_end_date', 'imagename'); $emailFields = $phoneFields = $urlFields = $otherFields = array(); $disAllowedFieldTypes = array('reference', 'picklist', 'multipicklist'); foreach ($contactsFields as $contactFieldModel) { if ($contactFieldModel->isViewable() && !in_array($contactFieldModel->getFieldName(), array_merge($mandatoryMapFields, $skipFields))) { if ($contactFieldModel->getFieldDataType() == 'email') { $emailFields[$contactFieldModel->getFieldName()] = $contactFieldModel->get('label'); } else { if ($contactFieldModel->getFieldDataType() == 'phone') { $phoneFields[$contactFieldModel->getFieldName()] = $contactFieldModel->get('label'); } else { if ($contactFieldModel->getFieldDataType() == 'url') { $urlFields[$contactFieldModel->getFieldName()] = $contactFieldModel->get('label'); } else { if (!in_array($contactFieldModel->getFieldDataType(), $disAllowedFieldTypes)) { $otherFields[$contactFieldModel->getFieldName()] = $contactFieldModel->get('label'); } } } } } } $viewer = $this->getViewer($request); $viewer->assign('MODULENAME', $request->getModule()); $viewer->assign('SOURCE_MODULE', $request->get('sourcemodule')); $viewer->assign('SELECTED_GROUP', $selectedGroup); $viewer->assign('SYNC_DIRECTION', $syncDirection); $viewer->assign('GOOGLE_GROUPS', $groups); $viewer->assign('GOOGLE_FIELDS', $targetFields); $viewer->assign('FIELD_MAPPING', $fieldMappping); $viewer->assign('CUSTOM_FIELD_MAPPING', $customFieldMapping); $viewer->assign('VTIGER_EMAIL_FIELDS', $emailFields); $viewer->assign('VTIGER_PHONE_FIELDS', $phoneFields); $viewer->assign('VTIGER_URL_FIELDS', $urlFields); $viewer->assign('VTIGER_OTHER_FIELDS', $otherFields); echo $viewer->view('ContactsSyncSettings.tpl', $request->getModule(), true); }
function renderSyncUI(Vtiger_Request $request) { $sourceModule = $request->get('sourcemodule'); $viewer = $this->getViewer($request); $viewer->assign('SCRIPTS', $this->getHeaderScripts($request)); $oauth = new Google_Oauth_Connector(Google_Utils_Helper::getCallbackUrl(array('module' => 'Google', 'sourcemodule' => $sourceModule, array('operation' => 'sync')))); if ($request->has('oauth_verifier')) { try { $oauth->getHttpClient($sourceModule); } catch (Exception $e) { $viewer->assign('DENY', true); } $viewer->assign('MODULE_NAME', $request->getModule()); $viewer->assign('STATE', 'CLOSEWINDOW'); $viewer->view('Contents.tpl', $request->getModule()); } else { if (!empty($sourceModule)) { $records = $this->invokeExposedMethod($sourceModule); } $firstime = $oauth->hasStoredToken($sourceModule, false, true); $viewer->assign('MODULE_NAME', $request->getModule()); $viewer->assign('FIRSTTIME', $firstime); $viewer->assign('RECORDS', $records); $viewer->assign('NORECORDS', $this->noRecords); $viewer->assign('SYNCTIME', Google_Utils_Helper::getLastSyncTime($sourceModule)); $viewer->assign('STATE', $request->get('operation')); $viewer->assign('SOURCEMODULE', $request->get('sourcemodule')); if (!$firstime) { $viewer->view('Contents.tpl', $request->getModule()); } else { echo $viewer->view('ContentDetails.tpl', $request->getModule(), true); } } }
/** * Function to delete google synchronization completely. Deletes all mapping information stored. * @param <string> $module - Module Name * @param <integer> $user - User Id */ public function deleteSync($module, $user) { if ($module == 'Contacts' || $module == 'Calendar') { $name = 'Vtiger_Google' . $module; } else { return; } $db = PearDatabase::getInstance(); $db->pquery("DELETE FROM vtiger_google_oauth2 WHERE service = ? AND userid = ?", array('Google' . $module, $user)); $db->pquery("DELETE FROM vtiger_google_sync WHERE googlemodule = ? AND user = ?", array($module, $user)); $result = $db->pquery("SELECT stateencodedvalues FROM vtiger_wsapp_sync_state WHERE name = ? AND userid = ?", array($name, $user)); $stateValuesJson = $db->query_result($result, 0, 'stateencodedvalues'); $stateValues = Zend_Json::decode(decode_html($stateValuesJson)); $appKey = $stateValues['synctrackerid']; $result = $db->pquery("SELECT appid FROM vtiger_wsapp WHERE appkey = ?", array($appKey)); $appId = $db->query_result($result, 0, 'appid'); $db->pquery("DELETE FROM vtiger_wsapp_recordmapping WHERE appid = ?", array($appId)); $db->pquery("DELETE FROM vtiger_wsapp WHERE appid = ?", array($appId)); $db->pquery("DELETE FROM vtiger_wsapp_sync_state WHERE name = ? AND userid = ?", array($name, $user)); if ($module == 'Contacts') { $db->pquery("DELETE FROM vtiger_google_sync_settings WHERE user = ?", array($user)); $db->pquery("DELETE FROM vtiger_google_sync_fieldmapping WHERE user = ?", array($user)); } Google_Utils_Helper::errorLog(); return; }
/** * Returns the connector of the google contacts * @return Google_Contacts_Connector */ public function getTargetConnector() { $oauthConnector = new Google_Oauth_Connector(Google_Utils_Helper::getCallbackUrl(array('module' => 'Google', 'sourcemodule' => $this->getSourceType()), array('operation' => 'sync'))); $client = $oauthConnector->getHttpClient($this->getSourceType()); $connector = new Google_Contacts_Connector($client); $connector->setSynchronizeController($this); return $connector; }
public function process(Vtiger_Request $request) { $sourceModule = $request->get('sourcemodule'); $fieldMapping = $request->get('fieldmapping'); Google_Utils_Helper::saveSettings($request); Google_Utils_Helper::saveFieldMappings($sourceModule, $fieldMapping); $response = new Vtiger_Response(); $result = array('settingssaved' => true); $response->setResult($result); $response->emit(); }
/** * Sync Contacts Records * @return <array> Count of Contacts Records */ public function Contacts() { $user = Users_Record_Model::getCurrentUserModel(); $controller = new Google_Contacts_Controller($user); $syncDirection = Google_Utils_Helper::getSyncDirectionForUser($user); $records = $controller->synchronize(true, $syncDirection[0], $syncDirection[1]); $syncRecords = $this->getSyncRecordsCount($records); $syncRecords['vtiger']['more'] = $controller->targetConnector->moreRecordsExits(); $syncRecords['google']['more'] = $controller->sourceConnector->moreRecordsExits(); return $syncRecords; }
static function getSyncDirectionForUser($user = false) { if (!$user) { $user = Users_Record_Model::getCurrentUserModel(); } if (!Google_Utils_Helper::hasSettingsForUser($user->getId())) { return '11'; // defaults to bi-directional sync } else { $db = PearDatabase::getInstance(); $sql = 'SELECT direction FROM ' . self::settings_table_name . ' WHERE user = ?'; $result = $db->pquery($sql, array($user->getId())); return $db->query_result($result, 0, 'direction'); } }
/** * Pull the contacts from google * @param <object> $SyncState * @return <array> google Records */ public function getContacts($SyncState) { $contacts = new Zend_Gdata_Contacts($this->apiInstance); $query = $contacts->newQuery(); $query->setMaxResults($this->maxResults); $query->setStartIndex(1); $query->setOrderBy('lastmodified'); $query->setsortorder('ascending'); if (Google_Utils_Helper::getSyncTime('Contacts')) { $query->setUpdatedMin(Google_Utils_Helper::getSyncTime('Contacts')); $query->setShowDeleted("true"); } $feed = $contacts->getContactListFeed($query); $this->totalRecords = $feed->totalResults->text; $contactRecords = array(); if (count($feed->entry) > 0) { $maxModifiedTime = date('Y-m-d H:i:s', strtotime(Google_Contacts_Model::vtigerFormat(end($feed->entry)->updated->text)) + 1); if ($this->totalRecords > $this->maxResults) { if (!Google_Utils_Helper::getSyncTime('Contacts')) { $query->setUpdatedMin(date('Y-m-d H:i:s', strtotime(Google_Contacts_Model::vtigerFormat(end($feed->entry)->updated->text)))); $query->setStartIndex($this->maxResults); } $query->setMaxResults(5000); $query->setUpdatedMax($maxModifiedTime); $extendedFeed = $contacts->getContactListFeed($query); $contactRecords = array_merge($feed->entry, $extendedFeed->entry); } else { $contactRecords = $feed->entry; } } $googleRecords = array(); foreach ($contactRecords as $i => $contact) { $recordModel = Google_Contacts_Model::getInstanceFromValues(array('entity' => $contact)); $deleted = false; foreach ($contact->getextensionElements() as $extentionElement) { if ($extentionElement->rootElement == 'deleted') { $deleted = true; } } if (!$deleted) { $recordModel->setType($this->getSynchronizeController()->getSourceType())->setMode(WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE); } else { $recordModel->setType($this->getSynchronizeController()->getSourceType())->setMode(WSAPP_SyncRecordModel::WSAPP_DELETE_MODE); } $googleRecords[$contact->id->text] = $recordModel; } $this->createdRecords = count($googleRecords); if (isset($maxModifiedTime)) { Google_Utils_Helper::updateSyncTime('Contacts', $maxModifiedTime); } else { Google_Utils_Helper::updateSyncTime('Contacts'); } return $googleRecords; }
/** * Pull the events from google * @param <object> $SyncState * @return <array> google Records */ public function getCalendar($SyncState, $user = false) { if ($this->apiConnection->isTokenExpired()) { $this->apiConnection->refreshToken(); $this->client->setAccessToken($this->apiConnection->getAccessToken()); $this->service = new Google_Service_Calendar($this->client); } $query = array('maxResults' => $this->maxResults, 'orderBy' => 'updated', 'singleEvents' => true); if (Google_Utils_Helper::getSyncTime('Calendar', $user)) { $query['updatedMin'] = $this->googleFormat(Google_Utils_Helper::getSyncTime('Calendar', $user)); //shows deleted by default } try { $feed = $this->service->events->listEvents('primary', $query); } catch (Exception $e) { if ($e->getCode() == 410) { $query['showDeleted'] = false; $feed = $this->service->events->listEvents('primary', $query); } } $calendarRecords = array(); if ($feed) { $calendarRecords = $feed->getItems(); if ($feed->getNextPageToken()) { $this->totalRecords = $this->maxResults + 1; } } if (count($calendarRecords) > 0) { $maxModifiedTime = date('Y-m-d H:i:s', strtotime(Google_Contacts_Model::vtigerFormat(end($calendarRecords)->getUpdated())) + 1); } $googleRecords = array(); foreach ($calendarRecords as $i => $calendar) { $recordModel = Google_Calendar_Model::getInstanceFromValues(array('entity' => $calendar)); $deleted = false; if ($calendar->getStatus() == 'cancelled') { $deleted = true; } if (!$deleted) { $recordModel->setType($this->getSynchronizeController()->getSourceType())->setMode(WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE); } else { $recordModel->setType($this->getSynchronizeController()->getSourceType())->setMode(WSAPP_SyncRecordModel::WSAPP_DELETE_MODE); } $googleRecords[$calendar->getId()] = $recordModel; } $this->createdRecords = count($googleRecords); if (isset($maxModifiedTime)) { Google_Utils_Helper::updateSyncTime('Calendar', $maxModifiedTime, $user); } else { Google_Utils_Helper::updateSyncTime('Calendar', false, $user); } return $googleRecords; }
/** * Pull the events from google * @param <object> $SyncState * @return <array> google Records */ public function getCalendar($SyncState) { $calendars = new Zend_Gdata_Calendar($this->apiInstance); $query = $calendars->newEventQuery($query); $query->setVisibility('private'); $query->setMaxResults($this->maxResults); $query->setStartIndex(1); $query->setOrderBy('lastmodified'); $query->setsortorder('ascending'); if (Google_Utils_Helper::getSyncTime('Calendar')) { $query->setUpdatedMin(Google_Utils_Helper::getSyncTime('Calendar')); } $feed = $calendars->getCalendarEventFeed($query); $this->totalRecords = $feed->totalResults->text; $calendarRecords = array(); if (count($feed->entry) > 0) { $maxModifiedTime = date('Y-m-d H:i:s', strtotime(Google_Contacts_Model::vtigerFormat(end($feed->entry)->updated->text)) + 1); if ($this->totalRecords > $this->maxResults) { if (!Google_Utils_Helper::getSyncTime('Calendar')) { $query->setUpdatedMin(date('Y-m-d H:i:s', strtotime(Google_Contacts_Model::vtigerFormat(end($feed->entry)->updated->text)))); $query->setStartIndex($this->maxResults); } $query->setMaxResults(500); $query->setUpdatedMax($maxModifiedTime); $extendedFeed = $calendars->getCalendarEventFeed($query); $calendarRecords = array_merge($feed->entry, $extendedFeed->entry); } else { $calendarRecords = $feed->entry; } } $googleRecords = array(); foreach ($calendarRecords as $i => $calendar) { $recordModel = Google_Calendar_Model::getInstanceFromValues(array('entity' => $calendar)); $deleted = false; if (end(explode('.', $calendar->eventstatus->value)) == 'canceled') { $deleted = true; } if (!$deleted) { $recordModel->setType($this->getSynchronizeController()->getSourceType())->setMode(WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE); } else { $recordModel->setType($this->getSynchronizeController()->getSourceType())->setMode(WSAPP_SyncRecordModel::WSAPP_DELETE_MODE); } $googleRecords[$calendar->id->text] = $recordModel; } $this->createdRecords = count($googleRecords); if (isset($maxModifiedTime)) { Google_Utils_Helper::updateSyncTime('Calendar', $maxModifiedTime); } else { Google_Utils_Helper::updateSyncTime('Calendar'); } return $googleRecords; }
/** * Push the vtiger records to google * @param <array> $records vtiger records to be pushed to google * @return <array> pushed records */ public function push($records, $user = false) { if (!$user) { $user = Users_Record_Model::getCurrentUserModel(); } if (!isset($this->selectedGroup)) { $this->selectedGroup = Google_Utils_Helper::getSelectedContactGroupForUser($user); } if ($this->selectedGroup != '' && $this->selectedGroup != 'all') { if ($this->selectedGroup == 'none') { return array(); } if (!isset($this->groups)) { $this->groups = $this->pullGroups(TRUE); } if (!in_array($this->selectedGroup, $this->groups['entry'])) { return array(); } } $updateRecords = $deleteRecords = $addRecords = array(); foreach ($records as $record) { if ($record->getMode() == WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE) { $updateRecords[] = $record; } else { if ($record->getMode() == WSAPP_SyncRecordModel::WSAPP_DELETE_MODE) { $deleteRecords[] = $record; } else { $addRecords[] = $record; } } } if (count($deleteRecords)) { $deletedRecords = $this->batchPush($deleteRecords, $user); } if (count($updateRecords)) { $updatedRecords = $this->batchPush($updateRecords, $user); } if (count($addRecords)) { $addedRecords = $this->batchPush($addRecords, $user); } $i = $j = $k = 0; foreach ($records as $record) { if ($record->getMode() == WSAPP_SyncRecordModel::WSAPP_UPDATE_MODE) { $uprecord = $updatedRecords[$i++]; $newEntity = $uprecord->get('entity'); $record->set('entity', $newEntity); } else { if ($record->getMode() == WSAPP_SyncRecordModel::WSAPP_DELETE_MODE) { $delrecord = $deletedRecords[$j++]; $newEntity = $delrecord->get('entity'); $record->set('entity', $newEntity); } else { $adrecord = $addedRecords[$k++]; $newEntity = $adrecord->get('entity'); $record->set('entity', $newEntity); } } } return $records; }