function export_google_calendar() { ajx_current("empty"); require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Gdata'); Zend_Loader::loadClass('Zend_Gdata_AuthSub'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); Zend_Loader::loadClass('Zend_Gdata_Calendar'); $users = ExternalCalendarUsers::findByContactId(); if($users){ if($users->getSync() == 1){ $sql = "SELECT ec.* FROM `".TABLE_PREFIX."external_calendars` ec,`".TABLE_PREFIX."external_calendar_users` ecu WHERE ec.calendar_feng = 1 AND ecu.contact_id = ".logged_user()->getId(); $calendar_feng = DB::executeOne($sql); $events = ProjectEvents::findNoSync(); $user = $users->getAuthUser(); $pass = $users->getAuthPass(); $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; try { $client = Zend_Gdata_ClientLogin::getHttpClient($user,$pass,$service); $gdataCal = new Zend_Gdata_Calendar($client); if ($calendar_feng){ foreach ($events as $event){ $calendarUrl = 'http://www.google.com/calendar/feeds/'.$calendar_feng['calendar_user'].'/private/full'; $newEvent = $gdataCal->newEventEntry(); $newEvent->title = $gdataCal->newTitle($event->getObjectName()); $newEvent->content = $gdataCal->newContent($event->getDescription()); $star_time = explode(" ",$event->getStart()->format("Y-m-d H:i:s")); $end_time = explode(" ",$event->getDuration()->format("Y-m-d H:i:s")); if($event->getTypeId() == 2){ $when = $gdataCal->newWhen(); $when->startTime = $star_time[0]; $when->endTime = $end_time[0]; $newEvent->when = array($when); }else{ $when = $gdataCal->newWhen(); $when->startTime = $star_time[0]."T".$star_time[1].".000-00:00"; $when->endTime = $end_time[0]."T".$end_time[1].".000-00:00"; $newEvent->when = array($when); } // insert event $createdEvent = $gdataCal->insertEvent($newEvent, $calendarUrl); $event_id = explode("/",$createdEvent->id->text); $special_id = end($event_id); $event->setSpecialID($special_id); $event->setUpdateSync(ProjectEvents::date_google_to_sql($createdEvent->updated)); $event->setExtCalId($calendar_feng['id']); $event->save(); } }else{ $appCalUrl = ''; $calFeed = $gdataCal->getCalendarListFeed(); foreach ($calFeed as $calF){ $instalation = explode("/", ROOT_URL); $instalation_name = end($instalation); if($calF->title->text == lang('feng calendar',$instalation_name)){ $appCalUrl = $calF->content->src; $t_calendario = $calF->title->text; } } if($appCalUrl != ""){ $title_cal = $t_calendario; }else{ $instalation = explode("/", ROOT_URL); $instalation_name = end($instalation); $appCal = $gdataCal -> newListEntry(); $appCal -> title = $gdataCal-> newTitle(lang('feng calendar',$instalation_name)); $own_cal = "http://www.google.com/calendar/feeds/default/owncalendars/full"; $new_cal = $gdataCal->insertEvent($appCal, $own_cal); $title_cal = $new_cal->title->text; $appCalUrl = $new_cal->content->src; } $cal_src = explode("/",$appCalUrl); array_pop($cal_src); $calendar_visibility = end($cal_src); array_pop($cal_src); $calendar_user = end($cal_src); $calendar = new ExternalCalendar(); $calendar->setCalendarUser($calendar_user); $calendar->setCalendarVisibility($calendar_visibility); $calendar->setCalendarName($title_cal); $calendar->setExtCalUserId($users->getId()); $calendar->setCalendarFeng(1); $calendar->save(); foreach ($events as $event){ $calendarUrl = 'http://www.google.com/calendar/feeds/'.$calendar->getCalendarUser().'/private/full'; $newEvent = $gdataCal->newEventEntry(); $newEvent->title = $gdataCal->newTitle($event->getObjectName()); $newEvent->content = $gdataCal->newContent($event->getDescription()); $star_time = explode(" ",$event->getStart()->format("Y-m-d H:i:s")); $end_time = explode(" ",$event->getDuration()->format("Y-m-d H:i:s")); if($event->getTypeId() == 2){ $when = $gdataCal->newWhen(); $when->startTime = $star_time[0]; $when->endTime = $end_time[0]; $newEvent->when = array($when); }else{ $when = $gdataCal->newWhen(); $when->startTime = $star_time[0]."T".$star_time[1].".000-00:00"; $when->endTime = $end_time[0]."T".$end_time[1].".000-00:00"; $newEvent->when = array($when); } // insert event $createdEvent = $gdataCal->insertEvent($newEvent, $calendarUrl); $event_id = explode("/",$createdEvent->id->text); $special_id = end($event_id); $event->setSpecialID($special_id); $event->setUpdateSync(ProjectEvents::date_google_to_sql($createdEvent->updated)); $event->setExtCalId($calendar->getId()); $event->save(); } } flash_success(lang('success add sync')); ajx_current("reload"); } catch(Exception $e) { Logger::log($e->getMessage()); flash_error(lang('could not connect to calendar')); ajx_current("empty"); } } } }
function export_google_calendar_for_user($user) { $service = $this->connect_with_google_calendar($user); if ($user->getSync() == 1) { $calendar_feng = ExternalCalendars::findFengCalendarByExtCalUserIdValue($user->getId()); //get events starting from past 2 weeks $previous_week = strtotime("-2 week"); $time_min = date(DATE_MYSQL, $previous_week); $events = ProjectEvents::findNoSync($user->getContactId(), $time_min, 100); $events_inv = ProjectEvents::findNoSyncInvitations($user->getContactId(), $time_min, 100); try { if ($calendar_feng instanceof ExternalCalendar) { $events_and_inv = array_merge($events, $events_inv); foreach ($events_and_inv as $event) { $this->update_event_on_google_calendar($event, $calendar_feng, $user, $service); } //we ask for events in this calendar in order to prevent checking the uploaded events on the import try { $now = strtotime("now"); $time_min = date(DATE_RFC3339, $now); $optParams['timeMin'] = $time_min; $events = $service->events->listEvents($calendar_feng->getOriginalCalendarId(), $optParams); } catch (Exception $e) { Logger::log("Fail to get events from feng external calendar: " . $calendar->getId()); Logger::log($e->getMessage()); } //update the calendar token $nextSyncToken = $events->getNextSyncToken(); if ($nextSyncToken) { $calendar_feng->setExternalCalendarPropertyValue("syncToken", $nextSyncToken); } } else { //create feng calendar on google if not exists and save it on feng $instalation = explode("/", ROOT_URL); $instalation_name = end($instalation); $calendar_name = lang('feng calendar', $instalation_name); $calendar_exists = false; //check if calendar exists try { $calendarList = $service->calendarList->listCalendarList(); while (true) { foreach ($calendarList->getItems() as $calendarListEntry) { if ($calendarListEntry->getSummary() == $calendar_name) { $calendar_exists = true; $external_calendar = array('original_calendar_id' => $calendarListEntry->getId(), 'title' => $calendarListEntry->getSummary(), 'calendar_status' => 1); break; } } $pageToken = $calendarList->getNextPageToken(); if ($pageToken) { $optParams = array('pageToken' => $pageToken); $calendarList = $service->calendarList->listCalendarList($optParams); } else { break; } } } catch (Exception $e) { Logger::log("Fail to get calendars list from google: " . $user->getContactId()); throw $e; } if (!$calendar_exists) { $new_calendar = new Google_Service_Calendar_Calendar(); $new_calendar->setSummary($calendar_name); //$calendar->setTimeZone('America/Los_Angeles'); $createdCalendar = $service->calendars->insert($new_calendar); $external_calendar = array('original_calendar_id' => $createdCalendar->getId(), 'title' => $createdCalendar->getSummary(), 'calendar_status' => 1); } $calendar = new ExternalCalendar(); $calendar->setOriginalCalendarId($external_calendar['original_calendar_id']); //$calendar->setCalendarVisibility($calendar_visibility); $calendar->setCalendarName($external_calendar['title']); $calendar->setExtCalUserId($user->getId()); $calendar->setCalendarFeng(1); $calendar->setSync(1); $calendar->save(); } flash_success(lang('success add sync')); ajx_current("reload"); } catch (Exception $e) { Logger::log($e->getMessage()); } } }