From f5bb809a75555539e049aa0f9de713108b53ec57 Mon Sep 17 00:00:00 2001 From: Andrew Sauder Date: Thu, 16 Nov 2023 17:18:13 -0500 Subject: [PATCH] add calendar service --- src/calendar.php | 128 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 src/calendar.php diff --git a/src/calendar.php b/src/calendar.php new file mode 100644 index 0000000..6ec4936 --- /dev/null +++ b/src/calendar.php @@ -0,0 +1,128 @@ +getMicrosoftAccessToken(); + + $graph = new \Microsoft\Graph\Graph(); + $graph->setAccessToken( $accessToken ); + + try { + + $iterator = $graph->createCollectionRequest( 'GET', '/users/' . $emailAddress . '/calendars' ) + ->setReturnType( \Microsoft\Graph\Model\Calendar::class ); + $calendars = $iterator->getPage(); + while( !$iterator->isEnd() ) { + $calendars = array_merge( $calendars, $iterator->getPage() ); + } + return $calendars; + } + catch( GraphException $e ) { + error_log( $e ); + throw new serviceException( 'Failed to get calendars: ' . $e->getMessage(), 500, $e ); + } + catch( GuzzleException $e ) { + error_log( $e ); + throw new serviceException( 'Failed to get calendars: ' . $e->getMessage(), $e->getCode(), $e ); + } + } + + + /** + * @param string $calendarOwner User id or username of a user whose calendars you want to fetch. Access token must have permission to access this account. + * @param string $calendarId Calendar id to get events from + * + * @return \Microsoft\Graph\Model\Event[] + * @throws \andrewsauder\microsoftServices\exceptions\serviceException + */ + public function getCalendarEvents( string $calendarOwner, string $calendarId, \DateTimeImmutable $startDateTime, \DateTimeImmutable $endDateTime ): array { + //get application access token + $accessToken = $this->getMicrosoftAccessToken(); + + $graph = new \Microsoft\Graph\Graph(); + $graph->setAccessToken( $accessToken ); + + try { + + $iterator = $graph->createCollectionRequest( 'GET', '/users/' . $calendarOwner . '/calendars/'. $calendarId .'/calendarview?startdatetime='. $startDateTime->format('c').'&enddatetime='. $endDateTime->format('c') .'&$orderby=start/dateTime' ) + ->setReturnType( \Microsoft\Graph\Model\Event::class ); + $events = $iterator->getPage(); + while( !$iterator->isEnd() ) { + $events = array_merge( $events, $iterator->getPage() ); + } + return $events; + } + catch( GraphException $e ) { + error_log( $e ); + throw new serviceException( 'Failed to get calendar events: ' . $e->getMessage(), 500, $e ); + } + catch( GuzzleException $e ) { + error_log( $e ); + throw new serviceException( 'Failed to get calendar events: ' . $e->getMessage(), $e->getCode(), $e ); + } + } + + + /** + * @param string $calendarOwner User id or username of a user whose calendars you want to fetch. Access token must have permission to access this account. + * @param string $calendarId Calendar id to get events from + * + * @return \Microsoft\Graph\Model\Event[] + * @throws \andrewsauder\microsoftServices\exceptions\serviceException + */ + public function getNCalendarEventsOccurringDuringOrAfter( string $calendarOwner, string $calendarId, \DateTimeImmutable $startDateTime, int $numberOfEventsToGet ): array { + //get application access token + $accessToken = $this->getMicrosoftAccessToken(); + + $graph = new \Microsoft\Graph\Graph(); + $graph->setAccessToken( $accessToken ); + + try { + + $iterator = $graph->createCollectionRequest( 'GET', '/users/' . $calendarOwner . '/calendars/'. $calendarId .'/events?$filter=start/dateTime ge \''.$startDateTime->format('c').'\' or end/dateTime ge \''.$startDateTime->format('c').'\'&$top='.$numberOfEventsToGet.'&$orderby=start/dateTime' ) + ->setReturnType( \Microsoft\Graph\Model\Event::class ); + $events = $iterator->getPage(); + while( !$iterator->isEnd() ) { + $events = array_merge( $events, $iterator->getPage() ); + } + return $events; + } + catch( GraphException $e ) { + error_log( $e ); + throw new serviceException( 'Failed to get calendar events: ' . $e->getMessage(), 500, $e ); + } + catch( GuzzleException $e ) { + error_log( $e ); + throw new serviceException( 'Failed to get calendar events: ' . $e->getMessage(), $e->getCode(), $e ); + } + } + + +}