Пример #1
0
 /**
  * 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;
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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;
 }
Пример #4
0
 /**
  * Pull the contacts from google
  * @param <object> $SyncState
  * @return <array> google Records
  */
 public function getContacts($SyncState)
 {
     if (!$user) {
         $user = Users_Record_Model::getCurrentUserModel();
     }
     $query = array('max-results' => $this->maxResults, 'start-index' => 1, 'orderby' => 'lastmodified', 'sortorder' => 'ascending');
     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'])) {
             $query['group'] = $this->selectedGroup;
         } else {
             return array();
         }
     }
     if (Google_Utils_Helper::getSyncTime('Contacts', $user)) {
         $query['updated-min'] = $this->googleFormat(Google_Utils_Helper::getSyncTime('Contacts', $user));
         $query['showdeleted'] = 'true';
     }
     $feed = $this->getContactListFeed($query);
     $this->totalRecords = $feed['openSearch$totalResults']['$t'];
     $contactRecords = array();
     if (count($feed['entry']) > 0) {
         $lastEntry = end($feed['entry']);
         $maxModifiedTime = date('Y-m-d H:i:s', strtotime(Google_Contacts_Model::vtigerFormat($lastEntry['updated']['$t'])) + 1);
         if ($this->totalRecords > $this->maxResults) {
             if (!Google_Utils_Helper::getSyncTime('Contacts', $user)) {
                 $query['updated-min'] = $this->googleFormat(date('Y-m-d H:i:s', strtotime(Google_Contacts_Model::vtigerFormat($lastEntry['updated']['$t']))));
                 $query['start-index'] = $this->maxResults;
             }
             if ($this->selectedGroup != '' && $this->selectedGroup != 'all') {
                 $query['group'] = $this->selectedGroup;
             }
             $query['max-results'] = 5000;
             $query['updated-max'] = $this->googleFormat($maxModifiedTime);
             $extendedFeed = $this->getContactListFeed($query);
             if (is_array($extendedFeed['entry'])) {
                 $contactRecords = array_merge($feed['entry'], $extendedFeed['entry']);
             } else {
                 $contactRecords = $feed['entry'];
             }
         } else {
             $contactRecords = $feed['entry'];
         }
     }
     $googleRecords = array();
     foreach ($contactRecords as $i => $contact) {
         $recordModel = Google_Contacts_Model::getInstanceFromValues(array('entity' => $contact));
         $deleted = false;
         if (array_key_exists('gd$deleted', $contact)) {
             $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']['$t']] = $recordModel;
     }
     $this->createdRecords = count($googleRecords);
     if (isset($maxModifiedTime)) {
         Google_Utils_Helper::updateSyncTime('Contacts', $maxModifiedTime);
     } else {
         Google_Utils_Helper::updateSyncTime('Contacts');
     }
     return $googleRecords;
 }