Exemple #1
0
 public function syncActionToGoogleCalendar($action, $ajax = false)
 {
     try {
         // catch google exceptions so the whole app doesn't crash if google has a problem syncing
         $admin = Yii::app()->settings;
         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/Google_Client.php";
                 //                    require_once "protected/extensions/google-api-php-client/src/contrib/Google_CalendarService.php";
                 //                    date_default_timezone_set($timezone);
                 //
                 //                    $client = new Google_Client();
                 //                    $client->setClientId($admin->googleClientId);
                 //                    $client->setClientSecret($admin->googleClientSecret);
                 //                    //$client->setDeveloperKey($admin->googleAPIKey);
                 //                    $client->setAccessToken($this->syncGoogleCalendarAccessToken);
                 //                    $googleCalendar = new Google_CalendarService($client);
                 $auth = new GoogleAuthenticator();
                 $googleCalendar = $auth->getCalendarService();
                 // 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 happened by calling a google api function that
                 // requires authorization,
                 // and, if the access token has changed, save this new access token
                 if (!$googleCalendar) {
                     $redirectUrl = $auth->getAuthorizationUrl('calendar');
                     if ($ajax) {
                         echo CJSON::encode(array('redirect' => $redirectUrl));
                         Yii::app()->end();
                     } else {
                         Yii::app()->controller->redirect($redirectUrl);
                     }
                 }
                 //                    if($this->syncGoogleCalendarAccessToken != $client->getAccessToken()){
                 //                        $this->syncGoogleCalendarAccessToken = $client->getAccessToken();
                 //                        $this->update(array('syncGoogleCalendarAccessToken'));
                 //                    }
                 $summary = $action->actionDescription;
                 if ($action->associationType == 'contacts' || $action->associationType == 'contact') {
                     $summary = $action->associationName . ' - ' . $action->actionDescription;
                 }
                 $event = new Google_Event();
                 $event->setSummary($summary);
                 if (empty($action->dueDate)) {
                     $action->dueDate = time();
                 }
                 if ($action->allDay) {
                     $start = new Google_EventDateTime();
                     $start->setDate(date('Y-m-d', $action->dueDate));
                     $event->setStart($start);
                     if (!$action->completeDate) {
                         $action->completeDate = $action->dueDate;
                     }
                     $end = new Google_EventDateTime();
                     $end->setDate(date('Y-m-d', $action->completeDate + 86400));
                     $event->setEnd($end);
                 } else {
                     $start = new Google_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 Google_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'];
                 $action->save();
             }
         }
     } catch (Exception $e) {
         if (isset($auth)) {
             $auth->flushCredentials();
         }
     }
 }