123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- declare(strict_types=1);
- namespace OCA\DAV\CalDAV;
- use OCA\DAV\Db\PropertyMapper;
- use OCP\Calendar\ICalendar;
- use OCP\Calendar\IManager;
- use OCP\IConfig;
- use Sabre\VObject\Component\VCalendar;
- use Sabre\VObject\Component\VTimeZone;
- use Sabre\VObject\Reader;
- use function array_reduce;
- class TimezoneService {
- public function __construct(
- private IConfig $config,
- private PropertyMapper $propertyMapper,
- private IManager $calendarManager,
- ) {
- }
- public function getUserTimezone(string $userId): ?string {
- $fromConfig = $this->config->getUserValue(
- $userId,
- 'core',
- 'timezone',
- );
- if ($fromConfig !== '') {
- return $fromConfig;
- }
- $availabilityPropPath = 'calendars/' . $userId . '/inbox';
- $availabilityProp = '{' . Plugin::NS_CALDAV . '}calendar-availability';
- $availabilities = $this->propertyMapper->findPropertyByPathAndName($userId, $availabilityPropPath, $availabilityProp);
- if (!empty($availabilities)) {
- $availability = $availabilities[0]->getPropertyvalue();
-
- $vCalendar = Reader::read($availability);
-
- $vTimezone = $vCalendar->VTIMEZONE;
-
- return $vTimezone->getTimeZone()->getName();
- }
- $principal = 'principals/users/' . $userId;
- $uri = $this->config->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI);
- $calendars = $this->calendarManager->getCalendarsForPrincipal($principal);
-
- $personalCalendarTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) use ($uri) {
- if ($acc !== null) {
- return $acc;
- }
- if ($calendar->getUri() === $uri && !$calendar->isDeleted() && $calendar instanceof CalendarImpl) {
- return $calendar->getSchedulingTimezone();
- }
- return null;
- });
- if ($personalCalendarTimezone !== null) {
- return $personalCalendarTimezone->getTimeZone()->getName();
- }
-
-
-
- $firstTimezone = array_reduce($calendars, function (?VTimeZone $acc, ICalendar $calendar) {
- if ($acc !== null) {
- return $acc;
- }
- if (!$calendar->isDeleted() && $calendar instanceof CalendarImpl) {
- return $calendar->getSchedulingTimezone();
- }
- return null;
- });
- if ($firstTimezone !== null) {
- return $firstTimezone->getTimeZone()->getName();
- }
- return null;
- }
- public function getDefaultTimezone(): string {
- return $this->config->getSystemValueString('default_timezone', 'UTC');
- }
- }
|