function createCalendar($summary) { $calendar = new Google_Service_Calendar_Calendar(); $calendar->setSummary($summary); $calendar->setTimeZone('America/Bogota'); $createdCalendar = $this->service->calendars->insert($calendar); $id = $createdCalendar->getId(); $this->addAcl($id); return $id; }
/** * Creates a new calendar * * @param string calendar name * @return string calendar ID (or false on failure) */ public function createCalendar($calendarName) { if ($this->authorised()) { // set up the calendar and save $calendar = new Google_Service_Calendar_Calendar(); $calendar->setSummary($calendarName); $calendar->setTimeZone('Europe/London'); $calendar->setLocation('United Kingdom'); try { $calendar = $this->__service->calendars->insert($calendar); } catch (Exception $e) { return false; } // make it public, delete if this fails try { $acl = new Google_Service_Calendar_AclRule(); $acl->setRole('reader'); $scope = new Google_Service_Calendar_AclRuleScope(); $scope->setType('default'); $acl->setScope($scope); $this->__service->acl->insert($calendar->getId(), $acl); } catch (Exception $e) { $this->__service->calendars->delete($calendar->getId()); return false; } return $calendar->getId(); } return false; }
public function calendar() { // require_once 'Google/Calendar.php'; // $entry = '<entry // xmlns="http://www.w3.org/2005/Atom" // xmlns:gd="http://schemas.google.com/g/2005"> // <category // scheme="http://schemas.google.com/g/2005#kind" // term="http://schemas.google.com/g/2005#event"> // </category> // <title type="text">일정제목</title> // <content type="text">내용</content> // <gd:where valueString="장소"> // </gd:where> // <gd:when // startTime="시작시간" // endTime="끝나는시간"> // <gd:reminder minutes="1" method="sms" /> // </gd:when> // </entry>' // $service = new Google_Calendar; // // if (!$service->requestClientLogin(본인의 구글아이디,비밀번호)) { // // exit("ClientLogin has failed\n".$service->getResponseBody()); // // } // // if (!$service->insert($entry)) { // // exit("Inserting an entry has failed\n".$service->getResponseBody()); // // } // if (!$service->insert($entry, "http://www.google.com/calendar/feeds/[등록하고싶은 캘린더의 ID]/private/full")) { // exit("Inserting an entry has failed\n".$service->getResponseBody()); // } // $client_id = '591777926178-kvdbu7llidjni1qvrpe330og3518hc9o.apps.googleusercontent.com'; // $client_secret = '0e1332d5822ba7ba2005fb7667a4ae67f1a2856d'; // $redirect_uri = 'http://localhost.me:8006/'; // $cTitle = "캘린더등곡"; // require_once('../tf_app/ext/google/Client.php'); // require_once('../tf_app/ext/google/Service/Calendar.php'); // $client = new Google_Client(); // $client->setClientId($client_id); // $client->setClientSecret($client_secret); // $client->setRedirectUri($redirect_uri); // $client->setAccessType('offline'); // $client->setApprovalPrompt('force'); // $client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/calendar')); // $cal = new Google_Service_Calendar($client); // $calendarId = 'ko.south_korea#holiday@group.v.calendar.google.com'; // // $targetDate = '2015-03-18'; // $tDate = new Google_Service_Calendar_EventDateTime(); // $tDate->setDate($targetDate); // $body = new Google_Service_Calendar_Event(); // $body->setSummary($cTitle); // 일정 제목 // $body->setStart($tDate); // 일정 시작 // $body->setEnd($tDate); // 일정 종료 // $cal->events->insert($calendarId, $body); // step2 // phpinfo(); // exit(1); // error_reporting(E_ALL); // // require_once('../tf_app/ext/google/Client.php'); // // require_once('../tf_app/ext/google/Service/Calendar.php'); // require_once '../tf_app/ext/google/autoload.php'; // $client_id = '591777926178-kvdbu7llidjni1qvrpe330og3518hc9o.apps.googleusercontent.com'; // $client_secret = '0e1332d5822ba7ba2005fb7667a4ae67f1a2856d'; // $redirect_uri = 'http://localhost.me:8006/'; // $calendarId = 'ko.south_korea#holiday@group.v.calendar.google.com'; // $client = new Google_Client(); // $client->setClientId($client_id); // $client->setClientSecret($client_secret); // $client->setRedirectUri($redirect_uri); // $client->setAccessType('offline'); // $client->setApprovalPrompt('force'); // $client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/calendar')); // $cal = new Google_Service_Calendar($client); // $event = new Google_Service_Calendar_Event(); // $event->setSummary('Appointment'); // $event->setLocation('Somewhere'); // $start = new Google_Service_Calendar_EventDateTime(); // $start->setDateTime('2015-03-19T10:00:00.000-07:00'); // $start->setTimeZone('America/Los_Angeles'); // $event->setStart($start); // $end = new Google_Service_Calendar_EventDateTime(); // $end->setDateTime('2011-06-03T10:25:00.000-07:00'); // $end->setTimeZone('America/Los_Angeles'); // $event->setEnd($end); // $event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20150319T170000Z')); // $attendee1 = new Google_Service_Calendar_EventAttendee(); // $attendee1->setEmail('attendeeEmail'); // // ... // $attendees = array($attendee1, // // ... // ); // $event->attendees = $attendees; // // $recurringEvent = $cal->events->insert('primary', $event); // $recurringEvent = $cal->events->insert($calendarId, $event); // echo $recurringEvent->getId(); // step3 // $this->tpl_name = ""; // // require_once('../tf_app/ext/google/Client.php'); // // require_once('../tf_app/ext/google/Service/Calendar.php'); // require_once '../tf_app/ext/google/autoload.php'; // // require_once 'CalendarHelper.php'; // session_start(); // $client = new Google_Client(); // $client->setApplicationName("calendar-api-test"); // // $client->setDeveloperKey("AIzaSyBBH88dIQPjcl5nIG-n1mmuQ12J7HThDBE"); // $client->setClientId('591777926178-kvdbu7llidjni1qvrpe330og3518hc9o.apps.googleusercontent.com'); // $client->setClientSecret('0e1332d5822ba7ba2005fb7667a4ae67f1a2856d'); // $client->setRedirectUri('http://localhost.me:8006/lounge/calendar'); // $client->setAccessType('offline'); // Gets us our refreshtoken // $client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly')); // //For loging out. // if (isset($_GET['logout'])) { // unset($_SESSION['token']); // } // // Step 2: The user accepted your access now you need to exchange it. // if (isset($_GET['code'])) { // $client->authenticate($_GET['code']); // $_SESSION['token'] = $client->getAccessToken(); // $redirect = 'http://localhost.me:8006/lounge/calendar'; // header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); // } // // Step 1: The user has not authenticated we give them a link to login // if (!isset($_SESSION['token'])) { // $authUrl = $client->createAuthUrl(); // print "<a class='login' href='$authUrl'>Connect Me!</a>"; // } // // Step 3: We have access we can now create our service // if (isset($_SESSION['token'])) { // $client->setAccessToken($_SESSION['token']); // print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>"; // $service = new Google_Service_Calendar($client); // $calendarList = $service->calendarList->listCalendarList();; // print_r($calendarList); // while(true) { // foreach ($calendarList->getItems() as $calendarListEntry) { // echo $calendarListEntry->getSummary()."<br>\n"; // } // $pageToken = $calendarList->getNextPageToken(); // if ($pageToken) { // $optParams = array('pageToken' => $pageToken); // $calendarList = $service->calendarList->listCalendarList($optParams); // } else { // break; // } // } // } // step4 // If you've used composer to include the library, remove the following line // and make sure to follow the standard composer autoloading. // https://getcomposer.org/doc/01-basic-usage.md#autoloading require_once '../tf_app/ext/google/autoload.php'; // $client = new Google_Client(); // // OAuth2 client ID and secret can be found in the Google Developers Console. // $client->setClientId('591777926178-kvdbu7llidjni1qvrpe330og3518hc9o.apps.googleusercontent.com'); // $client->setClientSecret('0e1332d5822ba7ba2005fb7667a4ae67f1a2856d'); // $client->setRedirectUri('http://localhost.me:8006/lounge/calendar'); // $client->addScope('https://www.googleapis.com/auth/calendar'); // $service = new Google_Service_Calendar($client); // $authUrl = $client->createAuthUrl(); // //Request authorization // print "Please visit:\n$authUrl\n\n"; // print "Please enter the auth code:\n"; // $authCode = trim(fgets(STDIN)); // // Exchange authorization code for access token // $accessToken = $client->authenticate($authCode); // $client->setAccessToken($accessToken); // step5 // Service Account info $client_id = '591777926178-kvdbu7llidjni1qvrpe330og3518hc9o.apps.googleusercontent.com'; $service_account_name = '*****@*****.**'; $key_file_location = '../tf_app/ext/calendar-api-test-826600ed61c3.p12'; // Calendar id $calName = '*****@*****.**'; $client = new Google_Client(); $client->setApplicationName("Calendar test"); $service = new Google_Service_Calendar($client); $key = file_get_contents($key_file_location); $cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/calendar.readonly'), $key); $client->setAssertionCredentials($cred); $cals = $service->calendarList->listCalendarList(); print_r($cals); //exit; // // Convert recurring events to single events // // Look for events in the next week - now to now+1week // $params = array( // 'singleEvents' => TRUE, // 'timeMin' => (new DateTime())->format(DateTime::RFC3339), // 'timeMax' => (new DateTime())->add(new DateInterval('P1W'))->format(DateTime::RFC3339), // ); // $events = $service->events->listEvents($calName, $params); // foreach ($events->getItems() as $event) { // echo "Summary: ", $event->getSummary(), "\n"; // echo "Location: ", $event->getLocation(), "\n"; // echo "Start: ", fmt_gdate($event->getStart()), "\n"; // echo "End: ", fmt_gdate($event->getEnd()), "\n"; // } // function fmt_gdate( $gdate ) { // if ($val = $gdate->getDateTime()) { // return (new DateTime($val))->format( 'd/m/Y H:i' ); // } else if ($val = $gdate->getDate()) { // return (new DateTime($val))->format( 'd/m/Y' ) . ' (all day)'; // } // } $calendar = new Google_Service_Calendar_Calendar(); $calendar->setSummary('calendarSummary'); $calendar->setTimeZone('America/Los_Angeles'); $createdCalendar = $service->calendars->insert($calendar); echo $createdCalendar->getId(); }
function export_google_calendar_for_user($user) { $service = $this->connect_with_google_calendar($user); if ($user->getSync() == 1) { $calendar_feng = ExternalCalendars::findFengCalendarByExtCalUserIdValue($user->getId()); //get events starting from past 2 weeks $previous_week = strtotime("-2 week"); $time_min = date(DATE_MYSQL, $previous_week); $events = ProjectEvents::findNoSync($user->getContactId(), $time_min, 100); $events_inv = ProjectEvents::findNoSyncInvitations($user->getContactId(), $time_min, 100); try { if ($calendar_feng instanceof ExternalCalendar) { $events_and_inv = array_merge($events, $events_inv); foreach ($events_and_inv as $event) { $this->update_event_on_google_calendar($event, $calendar_feng, $user, $service); } //we ask for events in this calendar in order to prevent checking the uploaded events on the import try { $now = strtotime("now"); $time_min = date(DATE_RFC3339, $now); $optParams['timeMin'] = $time_min; $events = $service->events->listEvents($calendar_feng->getOriginalCalendarId(), $optParams); } catch (Exception $e) { Logger::log("Fail to get events from feng external calendar: " . $calendar->getId()); Logger::log($e->getMessage()); } //update the calendar token $nextSyncToken = $events->getNextSyncToken(); if ($nextSyncToken) { $calendar_feng->setExternalCalendarPropertyValue("syncToken", $nextSyncToken); } } else { //create feng calendar on google if not exists and save it on feng $instalation = explode("/", ROOT_URL); $instalation_name = end($instalation); $calendar_name = lang('feng calendar', $instalation_name); $calendar_exists = false; //check if calendar exists try { $calendarList = $service->calendarList->listCalendarList(); while (true) { foreach ($calendarList->getItems() as $calendarListEntry) { if ($calendarListEntry->getSummary() == $calendar_name) { $calendar_exists = true; $external_calendar = array('original_calendar_id' => $calendarListEntry->getId(), 'title' => $calendarListEntry->getSummary(), 'calendar_status' => 1); break; } } $pageToken = $calendarList->getNextPageToken(); if ($pageToken) { $optParams = array('pageToken' => $pageToken); $calendarList = $service->calendarList->listCalendarList($optParams); } else { break; } } } catch (Exception $e) { Logger::log("Fail to get calendars list from google: " . $user->getContactId()); throw $e; } if (!$calendar_exists) { $new_calendar = new Google_Service_Calendar_Calendar(); $new_calendar->setSummary($calendar_name); //$calendar->setTimeZone('America/Los_Angeles'); $createdCalendar = $service->calendars->insert($new_calendar); $external_calendar = array('original_calendar_id' => $createdCalendar->getId(), 'title' => $createdCalendar->getSummary(), 'calendar_status' => 1); } $calendar = new ExternalCalendar(); $calendar->setOriginalCalendarId($external_calendar['original_calendar_id']); //$calendar->setCalendarVisibility($calendar_visibility); $calendar->setCalendarName($external_calendar['title']); $calendar->setExtCalUserId($user->getId()); $calendar->setCalendarFeng(1); $calendar->setSync(1); $calendar->save(); } flash_success(lang('success add sync')); ajx_current("reload"); } catch (Exception $e) { Logger::log($e->getMessage()); } } }