Ejemplo n.º 1
0
        function add_calendar_user() {
                ajx_current("empty");
                if($_POST){                    
                    if (!array_var($_POST, 'auth_user')) {
                            flash_error(lang('must enter a account gmail'));
                            ajx_current("empty");
                            return;
                    }
                    $user_email = ExternalCalendarUsers::findByEmail(array_var($_POST, 'auth_user'));
                    if($user_email) {
                            flash_error(lang('account has already'));
                            ajx_current("empty");
                            return;
                    } 
                    if (!array_var($_POST, 'auth_pass')) {
                            flash_error(lang('must enter the password gmail'));
                            ajx_current("empty");
                            return;
                    }
                    
                    $sync = 0;
                    if(array_var($_POST, 'sync')){
                        $sync = 1;
                    }
                    
                    $member_ids = json_decode(array_var($_POST, 'related_to'));
                    $members = "";
                    foreach($member_ids as $member_id){
                        $members .= $member_id.",";
                    }
                    $members = rtrim($members, ",");
                    
                    $user_cal = ExternalCalendarUsers::findById(get_id('cal_user_id'));
                    if($user_cal){
                        $user_cal->setAuthUser(array_var($_POST, 'auth_user'));
                        $user_cal->setAuthPass(array_var($_POST, 'auth_pass'));
                        $user_cal->setRelatedTo($members);
                        $user_cal->setSync($sync);
                        $user_cal->save();
                        
                        flash_success(lang('success edit account gmail'));
                        
                    }else{
                        $user_cal = new ExternalCalendarUser();
                        $user_cal->setAuthUser(array_var($_POST, 'auth_user'));
                        $user_cal->setAuthPass(array_var($_POST, 'auth_pass'));
                        $user_cal->setContactId(logged_user()->getId());
                        $user_cal->setRelatedTo($members);
                        $user_cal->setType("google");
                        $user_cal->setSync($sync);
                        $user_cal->save();
                        
                        flash_success(lang('success add account gmail'));
                    }                   
                    ajx_current("reload");
                }
                
                $this->update_sync_cron_events();
	}
 function calendar_sinchronization()
 {
     $user = ExternalCalendarUsers::findByContactId();
     $user_data = array();
     $client = new Google_Client();
     // Step 2: The user accepted your access now you need to exchange it.
     $google_code = array_var($_GET, 'google_code', false);
     if ($google_code) {
         try {
             $credentials = $client->authenticate(urldecode(array_var($_GET, 'google_code')));
             $google_acces_token = $client->getAccessToken();
             $service = new Google_Service_Oauth2($client);
             $user_info = $service->userinfo->get();
             $user_email = ExternalCalendarUsers::findByEmail($user_info['email']);
             if (!$user_email) {
                 $user_email = new ExternalCalendarUser();
                 $user_email->setAuthUser($user_info['email']);
                 $user_email->setAuthPass($credentials);
                 $user_email->setContactId(logged_user()->getId());
                 $user_email->setType("google");
                 $user_email->save();
                 $this->update_sync_cron_events();
             } else {
                 $user_email->setAuthPass($credentials);
                 $user_email->save();
             }
         } catch (Exception $e) {
             Logger::log("ERROR Google Sync Step 2");
             Logger::log($e->getMessage());
         }
     }
     $service_is_working = true;
     if ($user) {
         //calendars actions
         $calendars_actions = array();
         //1 = start sync
         $calendars_actions[1] = array();
         $calendars_actions[1]['text'] = lang('start sync');
         $calendars_actions[1]['function'] = 'og.google_calendar_start_sync_calendar';
         //2 = stop sync
         $calendars_actions[2] = array();
         $calendars_actions[2]['text'] = lang('stop sync');
         $calendars_actions[2]['function'] = 'og.google_calendar_stop_sync_calendar';
         //3 = restart sync
         $calendars_actions[3] = array();
         $calendars_actions[3]['text'] = lang('restart sync');
         $calendars_actions[3]['function'] = 'og.google_calendar_start_sync_calendar';
         //4 = delete calendar
         $calendars_actions[4] = array();
         $calendars_actions[4]['text'] = lang('delete calendar');
         $calendars_actions[4]['function'] = 'og.google_calendar_delete_calendar';
         //5 = delete calendar
         $calendars_actions[5] = array();
         $calendars_actions[5]['text'] = lang('classify');
         $calendars_actions[5]['function'] = 'og.google_calendar_classify';
         tpl_assign('calendars_actions', $calendars_actions);
         //calendars status
         //1 = never sync
         //2 = in sync
         //3 = not in sync
         //4 = deleted from google
         $calendars_status = array();
         //1 = never sync
         $calendars_status[1] = array();
         $calendars_status[1]['text'] = lang('never sync');
         $calendars_status[1]['actions'] = array(1);
         //2 = in sync
         $calendars_status[2] = array();
         $calendars_status[2]['text'] = lang('in sync');
         $calendars_status[2]['actions'] = array(2, 4, 5);
         //3 = not in sync
         $calendars_status[3] = array();
         $calendars_status[3]['text'] = lang('not in sync');
         $calendars_status[3]['actions'] = array(3, 4);
         //4 = deleted from google
         $calendars_status[4] = array();
         $calendars_status[4]['text'] = lang('deleted from google');
         $calendars_status[4]['actions'] = array(4);
         //5 = delete calendar
         $calendars_actions[5] = array();
         $calendars_actions[5]['text'] = lang('classify');
         $calendars_actions[5]['function'] = 'og.google_calendar_classify';
         tpl_assign('calendars_status', $calendars_status);
         $user_data['id'] = $user->getId();
         $user_data['auth_user'] = $user->getAuthUser();
         $user_data['sync'] = $user->getSync();
         tpl_assign('user', $user_data);
         // Step 3: We have access we can now create our service
         try {
             $service = $this->connect_with_google_calendar($user);
             $calendarList = $service->calendarList->listCalendarList();
             $instalation = explode("/", ROOT_URL);
             $instalation_name = end($instalation);
             $feng_calendar_name = lang('feng calendar', $instalation_name);
             while (true) {
                 foreach ($calendarList->getItems() as $calendarListEntry) {
                     //is feng calendar
                     if ($calendarListEntry->getSummary() == $feng_calendar_name) {
                         continue;
                     }
                     $external_calendars[$calendarListEntry->getId()] = array('original_calendar_id' => $calendarListEntry->getId(), 'title' => $calendarListEntry->getSummary(), 'calendar_status' => 1);
                 }
                 $pageToken = $calendarList->getNextPageToken();
                 if ($pageToken) {
                     $optParams = array('pageToken' => $pageToken);
                     $calendarList = $service->calendarList->listCalendarList($optParams);
                 } else {
                     break;
                 }
             }
         } catch (Exception $e) {
             $service_is_working = false;
         }
         //Calendars status
         $view_calendars = array();
         $calendars = ExternalCalendars::findByExtCalUserId($user->getId(), true);
         foreach ($calendars as $ext_calendar) {
             $view_calendar = array();
             $view_calendar['original_calendar_id'] = $ext_calendar->getOriginalCalendarId();
             $view_calendar['title'] = $ext_calendar->getCalendarName();
             $members = array();
             $member_ids = explode(",", $ext_calendar->getRelatedTo());
             foreach ($member_ids as $member_id) {
                 $members[$member_id] = $member_id;
             }
             if (count($members)) {
                 $view_calendar['members'] = json_encode(array(1 => $members));
             }
             $view_calendar['calendar_id'] = $ext_calendar->getId();
             //deleted on google
             $view_calendar['calendar_status'] = 4;
             if (array_key_exists($ext_calendar->getOriginalCalendarId(), $external_calendars)) {
                 //not in sync
                 $view_calendar['calendar_status'] = 3;
                 if ($ext_calendar->getSync()) {
                     //in sync
                     $view_calendar['calendar_status'] = 2;
                 }
                 unset($external_calendars[$ext_calendar->getOriginalCalendarId()]);
             }
             $view_calendars[$ext_calendar->getOriginalCalendarId()] = $view_calendar;
         }
         $all_calendars = array_merge($external_calendars, $view_calendars);
         ksort($all_calendars);
         tpl_assign('external_calendars', $all_calendars);
         $sync_from_feng = false;
         if ($user->getSync() == 1) {
             $calendar_feng = ExternalCalendars::findFengCalendarByExtCalUserIdValue($user->getId());
             if ($calendar_feng instanceof ExternalCalendar && $calendar_feng->getSync() == 0) {
                 $sync_from_feng = false;
             } else {
                 $sync_from_feng = true;
             }
         }
         if ($sync_from_feng) {
             $sync_from_feng_action = "og.google_calendar_stop_sync_feng_calendar";
             $sync_from_feng_color = "2";
             $sync_from_feng_text = lang('stop sync');
         } else {
             $sync_from_feng_action = "og.google_calendar_start_sync_feng_calendar";
             $sync_from_feng_color = "3";
             $sync_from_feng_text = lang('start sync');
         }
         tpl_assign('sync_from_feng_action', $sync_from_feng_action);
         tpl_assign('sync_from_feng_color', $sync_from_feng_color);
         tpl_assign('sync_from_feng_text', $sync_from_feng_text);
     }
     if (!$user || !$service_is_working) {
         // Step 1:  The user has not authenticated we give them a link to login
         if (!$google_code) {
             $client->setScopes(array('https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
             $client->setState(ROOT_URL . '/index.php?c=external_calendar&a=calendar_sinchronization');
             $authUrl = $client->createAuthUrl();
             tpl_assign('auth_url', $authUrl);
         }
     }
 }