public function actionSaveGoogleEvent($calendarId) { if (isset($_POST['EventId'])) { $eventId = $_POST['EventId']; $calendar = X2Calendar::model()->findByPk($calendarId); $googleCalendar = $calendar->getGoogleCalendar(); $googleEvent = $googleCalendar->events->get($calendar->googleCalendarId, $eventId); $model = new Actions(); foreach ($model->attributes as $field => $value) { if (isset($_POST['Actions'][$field])) { $model->{$field} = $_POST['Actions'][$field]; } } if ($model->allDay) { $googleEvent['start']['date'] = date('Y-m-d', Formatter::parseDateTime($model->dueDate)); if ($model->completeDate) { $googleEvent['end']['date'] = date('Y-m-d', Formatter::parseDateTime($model->completeDate) + 86400); } if (isset($googleEvent['start']['dateTime'])) { unset($googleEvent['start']['dateTime']); } if (isset($googleEvent['end']['dateTime'])) { unset($googleEvent['end']['dateTime']); } } else { $googleEvent['start']['dateTime'] = date('c', Formatter::parseDateTime($model->dueDate)); if ($model->completeDate) { $googleEvent['end']['dateTime'] = date('c', Formatter::parseDateTime($model->completeDate)); } if (isset($googleEvent['start']['date'])) { unset($googleEvent['start']['date']); } if (isset($googleEvent['end']['date'])) { unset($googleEvent['end']['date']); } } if ($model->color && $model->color != '#3366CC') { $colorTable = array(10 => 'Green', 11 => 'Red', 6 => 'Orange', 8 => 'Black'); if (($key = array_search($model->color, $colorTable)) != false) { $googleEvent['colorId'] = $key; } } $googleEvent = new Event($googleEvent); // we send back a proper Event object to google $googleEvent->setSummary($_POST['Actions']['actionDescription']); $googleCalendar->events->update($calendar->googleCalendarId, $eventId, $googleEvent); // $googleCalendar->events->delete($calendar->googleCalendarId, $eventId); } // $this->render('test', array('model'=>$_POST)); }
if (isset($_SESSION['logout'])) { unset($_SESSION['token']); } if (isset($_GET['code'])) { $client->authenticate(); $_SESSION['token'] = $client->getAccessToken(); header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } if ($client->getAccessToken()) { $start_date_reference = array("Monday" => 13, "Tuesday" => 14, "Wednesday" => 15, "Thursday" => 16, "Friday" => 17, "Saturday" => 18, "Sunday" => 19); foreach ($_SESSION['modules'] as $module) { $event = new Event(); $event->setSummary($module['module_code'] . " " . $module['ltype']); $event->setLocation($module['venue']); $start = new EventDateTime(); $start_string = '2012-08-' . $start_date_reference[$module['day_text']] . 'T' . $module['startTime'][0] . $module['startTime'][1] . ':' . $module['startTime'][2] . $module['startTime'][3] . ':00.000+08:00'; $start->setDateTime($start_string); $start->setTimeZone('Asia/Singapore'); $event->setStart($start); $end = new EventDateTime(); $end_string = '2012-08-' . $start_date_reference[$module['day_text']] . 'T' . $module['endTime'][0] . $module['endTime'][1] . ':' . $module['endTime'][2] . $module['endTime'][3] . ':00.000+08:00'; $end->setDateTime($end_string); $end->setTimeZone('Asia/Singapore'); $event->setEnd($end); $event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20121117T000000Z;')); $createdEvent = $cal->events->insert('primary', $event); } unset($_SESSION['module']);
public function createGoogleEvent($action) { // Google Calendar Libraries $timezone = date_default_timezone_get(); require_once 'protected/integration/Google/google-api-php-client/src/Google/autoload.php'; date_default_timezone_set($timezone); $googleCalendar = $this->getGoogleCalendar(); $event = new Event(); $event->setSummary($action->actionDescription); if ($action->allDay) { $start = new EventDateTime(); $start->setDate(date('Y-m-d', $action->dueDate)); $event->setStart($start); if (!$action->completeDate) { $action->completeDate = $action->dueDate + 86400; } $end = new EventDateTime(); $end->setDate(date('Y-m-d', $action->completeDate)); $event->setEnd($end); } else { $start = new EventDateTime(); $start->setDateTime(date('c', $action->dueDate)); $event->setStart($start); if (!$action->completeDate) { $action->completeDate = $action->dueDate + 3600; } // if no end time specified, make event 1 hour long $end = new EventDateTime(); $end->setDateTime(date('c', $action->completeDate)); $event->setEnd($end); } if ($action->color && $action->color != '#3366CC') { $colorTable = array(10 => 'Green', 11 => 'Red', 6 => 'Orange', 8 => 'Black'); if (($key = array_search($action->color, $colorTable)) != false) { $event->setColorId($key); } } $googleCalendar->events->insert($this->googleCalendarId, $event); }
public function syncActionToGoogleCalendar($action) { try { // catch google exceptions so the whole app doesn't crash if google has a problem syncing $admin = Yii::app()->params->admin; if ($admin->googleIntegration) { if (isset($this->syncGoogleCalendarId) && $this->syncGoogleCalendarId) { // Google Calendar Libraries $timezone = date_default_timezone_get(); require_once "protected/extensions/google-api-php-client/src/apiClient.php"; require_once "protected/extensions/google-api-php-client/src/contrib/apiCalendarService.php"; date_default_timezone_set($timezone); $client = new apiClient(); $client->setClientId($admin->googleClientId); $client->setClientSecret($admin->googleClientSecret); $client->setDeveloperKey($admin->googleAPIKey); $client->setAccessToken($this->syncGoogleCalendarAccessToken); $googleCalendar = new apiCalendarService($client); // check if the access token needs to be refreshed // note that the google library automatically refreshes the access token if we need a new one, // we just need to check if this happend by calling a google api function that requires authorization, // and, if the access token has changed, save this new access token $testCal = $googleCalendar->calendars->get($this->syncGoogleCalendarId); if ($this->syncGoogleCalendarAccessToken != $client->getAccessToken()) { $this->syncGoogleCalendarAccessToken = $client->getAccessToken(); $this->update(); } $summary = $action->actionDescription; if ($action->associationType == 'contacts' || $action->associationType == 'contact') { $summary = $action->associationName . ' - ' . $action->actionDescription; } $event = new Event(); $event->setSummary($summary); if ($action->allDay) { $start = new EventDateTime(); $start->setDate(date('Y-m-d', $action->dueDate)); $event->setStart($start); if (!$action->completeDate) { $action->completeDate = $action->dueDate; } $end = new EventDateTime(); $end->setDate(date('Y-m-d', $action->completeDate + 86400)); $event->setEnd($end); } else { $start = new EventDateTime(); $start->setDateTime(date('c', $action->dueDate)); $event->setStart($start); if (!$action->completeDate) { $action->completeDate = $action->dueDate; } // if no end time specified, make event 1 hour long $end = new EventDateTime(); $end->setDateTime(date('c', $action->completeDate)); $event->setEnd($end); } if ($action->color && $action->color != '#3366CC') { $colorTable = array(10 => 'Green', 11 => 'Red', 6 => 'Orange', 8 => 'Black'); if (($key = array_search($action->color, $colorTable)) != false) { $event->setColorId($key); } } $newEvent = $googleCalendar->events->insert($this->syncGoogleCalendarId, $event); $action->syncGoogleCalendarEventId = $newEvent['id']; } } } catch (Exception $e) { } }