trans('app.dav_birthdays'), '{'.SabreServer::NS_SABREDAV.'}read-only' => true, '{'.CalDAVPlugin::NS_CALDAV.'}calendar-description' => trans('app.dav_birthdays_description', ['name' => $this->user->name]), '{'.CalDAVPlugin::NS_CALDAV.'}calendar-timezone' => $this->user->timezone, '{'.CalDAVPlugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VEVENT']), '{'.CalDAVPlugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp(ScheduleCalendarTransp::TRANSPARENT), ]; } /** * Extension for Calendar objects. * * @return string */ public function getExtension() { return '.ics'; } /** * Datas for this date. * * @param mixed $obj * @return array */ public function prepareData($obj) { $calendardata = null; if ($obj instanceof SpecialDate) { try { $calendardata = $this->refreshObject($obj); return [ 'id' => $obj->id, 'uri' => $this->encodeUri($obj), 'calendardata' => $calendardata, 'etag' => '"'.sha1($calendardata).'"', 'lastmodified' => $obj->updated_at->timestamp, ]; } catch (\Exception $e) { Log::error(__CLASS__.' '.__FUNCTION__.': '.$e->getMessage(), [ 'calendardata' => $calendardata, $e, ]); } } return []; } /** * Get the new exported version of the object. * * @param mixed $obj date * @return string */ protected function refreshObject($obj): string { $vcal = app(ExportVCalendar::class) ->execute([ 'account_id' => $this->user->account_id, 'special_date_id' => $obj->id, ]); return $vcal->serialize(); } private function hasBirthday($contact) { if (! $contact || ! $contact->birthdate) { return false; } $birthdayState = $contact->getBirthdayState(); if ($birthdayState != 'almost' && $birthdayState != 'exact') { return false; } return true; } /** * Returns the date for the specific uuid. * * @param string|null $collectionId * @param string $uuid * @return mixed */ public function getObjectUuid($collectionId, $uuid) { return SpecialDate::where([ 'account_id' => $this->user->account_id, 'uuid' => $uuid, ])->first(); } /** * Returns the collection of contact's birthdays. * * @return \Illuminate\Support\Collection */ public function getObjects($collectionId) { // We only return the birthday of default addressBook $contacts = $this->user->account->contacts() ->real() ->active() ->get(); return $contacts->filter(function ($contact) { return $this->hasBirthday($contact); }) ->map(function ($contact) { return $contact->birthdate; }); } /** * Returns the collection of deleted birthdays. * * @param string|null $collectionId * @return \Illuminate\Support\Collection */ public function getDeletedObjects($collectionId) { return collect(); } /** * @return string|null */ public function updateOrCreateCalendarObject($calendarId, $objectUri, $calendarData): ?string { return null; } public function deleteCalendarObject($objectUri) { // Not implemented } }