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"); } } } }
if ($result === false) { apologize("Your subscription to this club could not be added to the database."); } $gdataCal = new Zend_Gdata_Calendar($_SESSION["client"]); //Get list of existing calendars $calFeed = $gdataCal->getCalendarListFeed(); $levels = query("SELECT * FROM privacy"); $num = count($levels); for ($i = 1; $i <= $num; $i++) { // ensure that when POST variables are extracted during loops, the abbr is not incorrect $abbreviation = strtoupper($abbreviation); if (isset($_POST["abbreviation"]) && $abbreviation == "") { $abbreviation = strtoupper($_POST[name]); } // I actually had to guess this method based on Google API's "magic" factory $appCal = $gdataCal->newListEntry(); // I only set the title, other options like color are available. $appCal->title = $gdataCal->newTitle($abbreviation . $i); //This is the right URL to post to for new calendars... //Notice that the user's info is nowhere in there $own_cal = "http://www.google.com/calendar/feeds/default/owncalendars/full"; //And here's the payoff. //Use the insertEvent method, set the second optional var to the right URL $gdataCal->insertEvent($appCal, $own_cal); $calFeed = $gdataCal->getCalendarListFeed(); foreach ($calFeed as $calendar) { if ($calendar->title->text == $abbreviation . $i) { //This is the money, you need to use '->content-src' //Anything else and you have to manipulate it to get it right. $appCalUrl = $calendar->content->src; }