+
+ /**
+ * Delete event
+ *
+ * @param string $calendar The calendar mail
+ * @param Event $event The google event instance
+ * @return void
+ */
+ function delete(string $calendar, Event $event): int {
+ //Get cache events
+ $cacheEvents = $this->item->get();
+
+ //Get event id
+ $eid = $event->getId();
+
+ //Delete the event
+ $this->service->events->delete($calendar, $eid);
+
+ //Set sid
+ $sid = intval(substr($event->getId(), strlen($this->prefix)));
+
+ //Remove from events and cache events
+ unset($cacheEvents[$sid]);
+
+ //Set cache events
+ $this->item->set($cacheEvents);
+
+ //Save cache item
+ $this->cache->saveDeferred($this->item);
+
+ //Return session id
+ return $sid;
+ }
+
+ /**
+ * Fill event
+ *
+ * TODO: add domain based/calendar mail specific templates ?
+ *
+ * @param array $session The session instance
+ * @param ?Event $event The event instance
+ * @return Event The filled event
+ */
+ function fill(array $session, ?Event $event = null): Event {
+ //Init private properties
+ $private = [
+ 'id' => $session['id'],
+ 'domain' => $this->domain,
+ 'updated' => $session['modified']->format(\DateTime::ISO8601)
+ ];
+
+ //Init shared properties
+ //TODO: validate for constraints here ??? https://developers.google.com/calendar/api/guides/extended-properties
+ //TODO: drop shared as unused ???
+ $shared = [
+ 'gps' => $session['l_latitude'].','.$session['l_longitude']
+ ];
+
+ //Init source
+ $source = new EventSource(
+ [
+ 'title' => $this->translator->trans('%dance% %id% by %pseudonym%', ['%id%' => $session['id'], '%dance%' => $this->translator->trans($session['ad_name'].' '.lcfirst($session['ad_type'])), '%pseudonym%' => $session['au_pseudonym']]).' '.$this->translator->trans('at '.$session['l_title']),
+ 'url' => $this->router->generate('rapsysair_session_view', ['id' => $session['id'], 'location' => $this->slugger->slug($this->translator->trans($session['l_title'])), 'dance' => $this->slugger->slug($this->translator->trans($session['ad_name'].' '.lcfirst($session['ad_type']))), 'user' => $this->slugger->slug($session['au_pseudonym'])], UrlGeneratorInterface::ABSOLUTE_URL)
+ ]
+ );
+
+ //Init location
+ $description = 'Emplacement :'."\n".$this->translator->trans($session['l_description']);
+ $shared['location'] = strip_tags($this->translator->trans($session['l_description']));
+
+ //Add description when available
+ if(!empty($session['p_description'])) {
+ $description .= "\n\n".'Description :'."\n".strip_tags(preg_replace('!<a href="([^"]+)"(?: title="[^"]+")?'.'>([^<]+)</a>!', '\1', $this->markdown->convert(strip_tags($session['p_description']))));
+ $shared['description'] = $this->markdown->convert(strip_tags($session['p_description']));
+ }
+
+ //Add class when available
+ if (!empty($session['p_class'])) {
+ $description .= "\n\n".'Classe :'."\n".$session['p_class'];
+ $shared['class'] = $session['p_class'];
+ }
+
+ //Add contact when available
+ if (!empty($session['p_contact'])) {
+ $description .= "\n\n".'Contact :'."\n".$session['p_contact'];
+ $shared['contact'] = $session['p_contact'];
+ }
+
+ //Add donate when available
+ if (!empty($session['p_donate'])) {
+ $description .= "\n\n".'Contribuer :'."\n".$session['p_donate'];
+ $shared['donate'] = $session['p_donate'];
+ }
+
+ //Add link when available
+ if (!empty($session['p_link'])) {
+ $description .= "\n\n".'Site :'."\n".$session['p_link'];
+ $shared['link'] = $session['p_link'];
+ }
+
+ //Add profile when available
+ if (!empty($session['p_profile'])) {
+ $description .= "\n\n".'Réseau social :'."\n".$session['p_profile'];
+ $shared['profile'] = $session['p_profile'];
+ }
+
+ //Set properties
+ $properties = new EventExtendedProperties(
+ [
+ //Set private property
+ 'private' => $private,
+ //Set shared property
+ 'shared' => $shared
+ ]
+ );
+
+ //Without event
+ if ($event === null) {
+ //Init event
+ $event = new Event(
+ [
+ //Id must match /^[a-v0-9]{5,}$/
+ //XXX: see https://developers.google.com/calendar/api/v3/reference/events/insert#id
+ 'id' => $this->prefix.$session['id'],
+ 'summary' => $source->getTitle(),
+ 'description' => $description,
+ 'status' => empty($session['a_canceled'])?'confirmed':'cancelled',
+ 'location' => implode(' ', [$session['l_address'], $session['l_zipcode'], $session['l_city']]),
+ 'source' => $source,
+ 'extendedProperties' => $properties,
+ //TODO: colorId ?
+ //TODO: attendees[] ?
+ 'start' => [
+ 'dateTime' => $session['start']->format(\DateTime::ISO8601)
+ ],
+ 'end' => [
+ 'dateTime' => $session['stop']->format(\DateTime::ISO8601)
+ ]
+ ]
+ );
+ //With event
+ } else {
+ //Set summary
+ $event->setSummary($source->getTitle());
+
+ //Set description
+ $event->setDescription($description);
+
+ //Set status
+ $event->setStatus(empty($session['a_canceled'])?'confirmed':'cancelled');
+
+ //Set location
+ $event->setLocation(implode(' ', [$session['l_address'], $session['l_zipcode'], $session['l_city']]));
+
+ //Get source
+ #$eventSource = $event->getSource();
+
+ //Update source title
+ #$eventSource->setTitle($source->getTitle());
+
+ //Update source url
+ #$eventSource->setUrl($source->getUrl());
+
+ //Set source
+ $event->setSource($source);
+
+ //Get extended properties
+ #$extendedProperties = $event->getExtendedProperties();
+
+ //Update private
+ #$extendedProperties->setPrivate($properties->getPrivate());
+
+ //Update shared
+ #$extendedProperties->setShared($properties->getShared());
+
+ //Set properties
+ $event->setExtendedProperties($properties);
+
+ //TODO: colorId ?
+ //TODO: attendees[] ?
+
+ //Set start
+ $start = $event->getStart();
+
+ //Update start datetime
+ $start->setDateTime($session['start']->format(\DateTime::ISO8601));
+
+ //Set end
+ $end = $event->getEnd();
+
+ //Update stop datetime
+ $end->setDateTime($session['stop']->format(\DateTime::ISO8601));
+ }
+
+ //Return event
+ return $event;
+ }
+
+ /**
+ * Insert event
+ *
+ * @param string $calendar The calendar mail
+ * @param array $session The session instance
+ * @return void
+ */
+ function insert(string $calendar, array $session): void {
+ //Get event
+ $event = $this->fill($session);
+
+ //Get cache events
+ $cacheEvents = $this->item->get();
+
+ //Insert in cache event
+ $cacheEvents[$session['id']] = $this->service->events->insert($calendar, $event);
+
+ //Set cache events
+ $this->item->set($cacheEvents);
+
+ //Save cache item
+ $this->cache->saveDeferred($this->item);
+ }
+
+ /**
+ * Update event
+ *
+ * @param string $calendar The calendar mail
+ * @param Event $event The google event instance
+ * @param array $session The session instance
+ * @return int The session id
+ */
+ function update(string $calendar, Event $event, array $session): int {
+ //Get event
+ $event = $this->fill($session, $event);
+
+ //Get cache events
+ $cacheEvents = $this->item->get();
+
+ //Update in cache events
+ $cacheEvents[$session['id']] = $this->service->events->update($calendar, $event->getId(), $event);
+
+ //Set cache events
+ $this->item->set($cacheEvents);
+
+ //Save cache item
+ $this->cache->saveDeferred($this->item);
+
+ //Return session id
+ return $session['id'];
+ }