Exemplo n.º 1
0
 public function actionSyncActionsToGoogleCalendar()
 {
     $errors = array();
     $model = Yii::app()->params->profile;
     $client = null;
     if (isset($_POST['Profile'])) {
         foreach (array_keys($model->attributes) as $field) {
             if (isset($_POST['Profile'][$field])) {
                 $model->{$field} = $_POST['Profile'][$field];
             }
         }
         if ($model->syncGoogleCalendarId && isset($_SESSION['token'])) {
             $token = json_decode($_SESSION['token'], true);
             // used for accessing this google calendar at a later time
             //$model->syncGoogleCalendarRefreshToken = $token['refresh_token'];
             $model->syncGoogleCalendarAccessToken = $_SESSION['token'];
         }
         $model->update();
     }
     if (isset($_SESSION['calendarForceRefresh']) && $_SESSION['calendarForceRefresh']) {
         unset($_SESSION['calendarForceRefresh']);
         Yii::app()->user->setFlash('error', 'Your Refresh Token was invalid and needed to be refreshed. The last action you ' . 'attempted to Sync with Google did not successfully synchronize.');
     }
     $admin = Yii::app()->settings;
     $googleIntegration = $admin->googleIntegration;
     /* if google integration is activated let user choose if they want to link this calendar 
        to a google calendar */
     if ($googleIntegration) {
         //            $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"; // for google calendar sync
         //            require_once 'protected/extensions/google-api-php-client/src/contrib/Google_Oauth2Service.php'; // for google oauth login
         //            date_default_timezone_set($timezone);
         $auth = new GoogleAuthenticator();
         /* name of the Google Calendar that current user's actions are being synced to if it 
            has been set */
         $syncGoogleCalendarName = null;
         try {
             if (isset($_GET['unlinkGoogleCalendar'])) {
                 // user changed their mind about linking their google calendar
                 unset($_SESSION['token']);
                 $model->syncGoogleCalendarId = null;
                 // used for accessing this google calendar at a later time
                 //$model->syncGoogleCalendarRefreshToken = null;
                 $model->syncGoogleCalendarAccessToken = null;
                 $model->update();
                 $googleCalendarList = null;
                 if ($auth->getAccessToken()) {
                     $googleCalendar = $auth->getCalendarService();
                     try {
                         $calList = $googleCalendar->calendarList->listCalendarList();
                         $googleCalendarList = array();
                         foreach ($calList['items'] as $cal) {
                             $googleCalendarList[$cal['id']] = $cal['summary'];
                         }
                     } catch (Google_ServiceException $e) {
                         if ($e->getCode() == '403') {
                             $errors[] = $e->getMessage();
                             Yii::app()->user->setFlash('error', $e->getMessage());
                             $googleCalendarList = null;
                             //$auth->flushCredentials();
                         } elseif ($e->getCode() == '401') {
                             $errors[] = 'Invalid user credentials provided. Please try again.';
                             Yii::app()->user->setFlash('error', 'Invalid user credentials. Please ensure your account is ' . 'able to use this service or delete the access permissions ' . 'and try again.');
                             $googleCalendarList = null;
                             $auth->flushCredentials();
                         }
                     }
                 } else {
                     $googleCalendarList = null;
                 }
             } else {
                 if ($auth->getAccessToken()) {
                     $googleCalendar = $auth->getCalendarService();
                     try {
                         $calList = $googleCalendar->calendarList->listCalendarList();
                         $googleCalendarList = array();
                         foreach ($calList['items'] as $cal) {
                             $googleCalendarList[$cal['id']] = $cal['summary'];
                         }
                     } catch (Google_ServiceException $e) {
                         if ($e->getCode() == '403') {
                             $errors[] = 'Google Calendar API access has not been configured.';
                             Yii::app()->user->setFlash('error', 'Google Calendar API access has not been configured.');
                             $googleCalendarList = null;
                             //$auth->flushCredentials();
                         } elseif ($e->getCode() == '401') {
                             $errors[] = 'Invalid user credentials provided. Please try again.';
                             Yii::app()->user->setFlash('error', 'Invalid user credentials. Please ensure your account is ' . 'able to use this service or delete the access permissions ' . 'and try again.');
                             $googleCalendarList = null;
                             $auth->flushCredentials();
                         }
                     }
                 } else {
                     $googleCalendarList = null;
                 }
             }
         } catch (Google_AuthException $e) {
             $auth->flushCredentials();
             $auth->setErrors($e->getMessage());
             $client = null;
             $googleCalendarList = null;
             $syncGoogleCalendarName = null;
         }
     } else {
         $client = null;
         $googleCalendarList = null;
         $syncGoogleCalendarName = null;
     }
     $syncGoogleCalendarId = Yii::app()->params->profile->syncGoogleCalendarId;
     $this->render('syncActionsToGoogleCalendar', array('errors' => $errors, 'auth' => isset($auth) ? $auth : null, 'model' => $model, 'googleIntegration' => $googleIntegration, 'client' => $client, 'googleCalendarList' => $googleCalendarList, 'syncGoogleCalendarName' => $syncGoogleCalendarId));
 }
Exemplo n.º 2
0
 public function updateGoogleCalendarEvent($action)
 {
     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);
                 //                    $client->setUseObjects(true); // return objects instead of arrays
                 //                    $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 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(array('syncGoogleCalendarAccessToken'));
                 //                    }
                 $summary = $action->actionDescription;
                 if ($action->associationType == 'contacts' || $action->associationType == 'contact') {
                     $summary = $action->associationName . ' - ' . $action->actionDescription;
                 }
                 $event = $googleCalendar->events->get($this->syncGoogleCalendarId, $action->syncGoogleCalendarEventId);
                 if (is_array($event)) {
                     $event = new Google_Event($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->update($this->syncGoogleCalendarId, $action->syncGoogleCalendarEventId, $event);
             }
         }
     } catch (Exception $e) {
     }
 }