Example #1
0
 /**
  * Create shared calendar
  */
 public function actionCreate()
 {
     $model = new X2Calendar();
     if (isset($_POST['X2Calendar'])) {
         // copy $_POST data into Calendar model
         //            $this->render('test', array('model'=>$_POST));
         foreach (array_keys($model->attributes) as $field) {
             if (isset($_POST['X2Calendar'][$field])) {
                 $model->{$field} = $_POST['X2Calendar'][$field];
                 $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Calendar', 'fieldName' => $field));
                 if (isset($fieldData) && $fieldData->type == 'assignment' && $fieldData->linkType == 'multiple') {
                     $model->{$field} = Fields::parseUsers($model->{$field});
                 } elseif (isset($fieldData) && $fieldData->type == 'date') {
                     $model->{$field} = strtotime($model->{$field});
                 }
             }
         }
         if ($model->googleCalendar && isset($_SESSION['token'])) {
             $token = json_decode($_SESSION['token'], true);
             $model->googleRefreshToken = $token['refresh_token'];
             // used for accessing this google calendar at a later time
             $model->googleAccessToken = $_SESSION['token'];
         }
         $model->createdBy = Yii::app()->user->name;
         $model->updatedBy = Yii::app()->user->name;
         $model->createDate = time();
         $model->lastUpdated = time();
         $model->save();
         $this->redirect(array('index'));
     }
     $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) {
         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";
         $client = new Google_Client();
         $client->setApplicationName("Google Calendar Integration");
         // Visit https://code.google.com/apis/console?api=calendar to generate your
         // client id, client secret, and to register your redirect uri.
         $client->setClientId($admin->googleClientId);
         $client->setClientSecret($admin->googleClientSecret);
         $client->setRedirectUri((@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $this->createUrl(''));
         //$client->setDeveloperKey($admin->googleAPIKey);
         $client->setAccessType('offline');
         $googleCalendar = new Google_CalendarService($client);
         if (isset($_GET['unlinkGoogleCalendar'])) {
             // user changed thier mind about linking their google calendar
             unset($_SESSION['token']);
         }
         if (isset($_GET['code'])) {
             // returning from google with access token
             $client->authenticate();
             $_SESSION['token'] = $client->getAccessToken();
             header('Location: ' . (@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
         }
         if (isset($_SESSION['token'])) {
             $client->setAccessToken($_SESSION['token']);
             $calList = $googleCalendar->calendarList->listCalendarList();
             $googleCalendarList = array();
             foreach ($calList['items'] as $cal) {
                 $googleCalendarList[$cal['id']] = $cal['summary'];
             }
         } else {
             $googleCalendarList = null;
         }
     } else {
         $client = null;
         $googleCalendarList = null;
     }
     $this->render('create', array('model' => $model, 'googleIntegration' => $googleIntegration, 'client' => $client, 'googleCalendarList' => $googleCalendarList));
 }