<?php
namespace App\Library;
use DateTime;
use DateInterval;
use App\Library\GraphGeneric;
use Microsoft\Graph\Model\User;
use Microsoft\Graph\Model\Event;
use Microsoft\Graph\Model\BodyType;
use Microsoft\Graph\Model\Calendar;
use Microsoft\Graph\Model\ItemBody;
use Microsoft\Graph\Model\Location;
use Microsoft\Graph\Model\DateTimeTimeZone;
use Microsoft\Graph\Model\Drive;
require_once 'GraphGeneric.php';
class GraphUser extends GraphGeneric
{
public $test = 123;
public function getUsers()
{
return $this->appClient->createCollectionRequest('GET', '/users')
->setReturnType(User::class)
->execute();
}
public function listUserDrives(string $id)
{
return $this->appClient->createRequest('GET', '/users/' . $id . '/drives')
->setReturnType(Drive::class)
->execute();
}
public function getDefaultCalendar(string $id)
{
return $this->appClient->createRequest('GET', '/users/' . $id . '/calendar')
->setReturnType(Calendar::class)
->execute();
}
public function getEventsDefaultCalendar(string $id, ?int $numWeek = null)
{
$calendar = $this->getDefaultCalendar($id);
$startDate = new DateTime();
$endDate = new DateTime();
if ($startDate->format('N') > 1)
$startDate->sub(new DateInterval('P' . ($startDate->format('N') - 1) . 'D'));
$endDate->setTimestamp($startDate->getTimestamp());
$endDate->add(new DateInterval('P6D'));
if (!empty($numWeek)) {
$dto = new DateTime();
$dto->setISODate((int)$dto->format('Y'), $numWeek);
$startDate->setTimestamp($dto->getTimestamp());
$dto->modify('+6 days');
$endDate->setTimestamp($dto->getTimestamp());
}
return $this->appClient->createCollectionRequest('GET', '/users/' . $id . '/calendars/' . $calendar->getId() . '/events?$filter=start/dateTime ge \'' . $startDate->format('Y-m-d') . 'T00:00:00Z\' and end/dateTime le \'' . $endDate->format('Y-m-d') . 'T23:59:59\'&$orderby=start/dateTime')
->setReturnType(Event::class)
->execute();
}
public function getCalendarEvent(string $uid, string $eventId){
return $this->appClient->createRequest('GET', '/users/' . $uid . '/calendar/events/'.$eventId)
->execute();
}
public function addEventUserDefaultCalendar(string $uid, string $startDate, string $endDate, string $subject, string $bodyContent = "", string $adresse = "", float $alertHoursBefore)
{
if (!isset($uid) || !isset($startDate) || !isset($endDate) || !isset($subject)) return null;
$event = new Event();
$event->setSubject($subject);
$body = new ItemBody();
$body->setContentType(new BodyType('html'));
$body->setContent($bodyContent);
$event->setBody($body);
$start = new DateTimeTimeZone();
$start->setDateTime($startDate);
$start->setTimeZone('Europe/Paris');
$event->setStart($start);
$end = new DateTimeTimeZone();
$end->setDateTime($endDate);
$end->setTimeZone('Europe/Paris');
$event->setEnd($end);
$location = new Location();
$location->setDisplayName($adresse);
$event->setLocation($location);
// ** Gestion des alertes
if (isset($alertHoursBefore) && $alertHoursBefore > 0) {
$minute = (int) round($alertHoursBefore * 60);
$event->setIsReminderOn(true);
$event->setReminderMinutesBeforeStart($minute);
} else
$event->setIsReminderOn(false);
return $this->appClient->createRequest('POST', '/users/' . $uid . '/calendar/events')
->attachBody($event)
->execute();
}
public function deleteEventCalendar(string $uid, string $eventId){
return $this->appClient->createRequest('DELETE', '/users/' . $uid . '/calendar/events/'.$eventId)
->execute();
}
public function sendMail(string $content) {
$body = [
"message" => [
"subject" => '=?iso-8859-1?B?'.base64_encode("Erreur dans le décompte").'?=',
"body" => [
"contentType" => "HTML",
"content" => $content
],
"toRecipients" => [
[
"emailAddress" => [
"address" => "cedric@cco-info.fr"
]
]
],
"ccRecipients" => [
[
"emailAddress" => [
"address" => "celine@cco-info.fr"
]
]
]
],
"saveToSentItems" => "false"
];
return $this->appClient->createRequest('POST', '/users/david@cco-info.fr/sendMail')
->attachBody($body)
->execute();
}
}