diff options
author | Georg Ehrke <developer@georgehrke.com> | 2019-08-21 10:35:17 +0200 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2019-08-26 10:36:49 +0200 |
commit | befe38e1d71079eeaf31421bee08b0dcf79fd000 (patch) | |
tree | f108a983efc095968d24d4c342d38a47318fd118 /apps/dav/lib/CalDAV | |
parent | af8abab4b34cc5e2a86ef6159ce2c359f127fc93 (diff) | |
download | nextcloud-server-befe38e1d71079eeaf31421bee08b0dcf79fd000.tar.gz nextcloud-server-befe38e1d71079eeaf31421bee08b0dcf79fd000.zip |
replace coffin with textual description "Death of ..."
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
Diffstat (limited to 'apps/dav/lib/CalDAV')
-rw-r--r-- | apps/dav/lib/CalDAV/BirthdayService.php | 181 |
1 files changed, 132 insertions, 49 deletions
diff --git a/apps/dav/lib/CalDAV/BirthdayService.php b/apps/dav/lib/CalDAV/BirthdayService.php index 0e8926e775a..278373733a4 100644 --- a/apps/dav/lib/CalDAV/BirthdayService.php +++ b/apps/dav/lib/CalDAV/BirthdayService.php @@ -1,7 +1,8 @@ <?php +declare(strict_types=1); /** * @copyright Copyright (c) 2016, ownCloud, Inc. - * @copyright Copyright (c) 2016, Georg Ehrke + * @copyright Copyright (c) 2019, Georg Ehrke * * @author Achim Königs <garfonso@tratschtante.de> * @author Georg Ehrke <oc.list@georgehrke.com> @@ -33,6 +34,7 @@ use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\DAV\GroupPrincipalBackend; use OCP\IConfig; use OCP\IDBConnection; +use OCP\IL10N; use Sabre\VObject\Component\VCalendar; use Sabre\VObject\Component\VCard; use Sabre\VObject\DateTimeParser; @@ -41,6 +43,11 @@ use Sabre\VObject\InvalidDataException; use Sabre\VObject\Property\VCard\DateAndOrTime; use Sabre\VObject\Reader; +/** + * Class BirthdayService + * + * @package OCA\DAV\CalDAV + */ class BirthdayService { const BIRTHDAY_CALENDAR_URI = 'contact_birthdays'; @@ -60,20 +67,31 @@ class BirthdayService { /** @var IDBConnection */ private $dbConnection; + /** @var IL10N */ + private $l10n; + /** * BirthdayService constructor. * * @param CalDavBackend $calDavBackEnd * @param CardDavBackend $cardDavBackEnd * @param GroupPrincipalBackend $principalBackend - * @param IConfig $config; + * @param IConfig $config + * @param IDBConnection $dbConnection + * @param IL10N $l10n */ - public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config, IDBConnection $dbConnection) { + public function __construct(CalDavBackend $calDavBackEnd, + CardDavBackend $cardDavBackEnd, + GroupPrincipalBackend $principalBackend, + IConfig $config, + IDBConnection $dbConnection, + IL10N $l10n) { $this->calDavBackEnd = $calDavBackEnd; $this->cardDavBackEnd = $cardDavBackEnd; $this->principalBackend = $principalBackend; $this->config = $config; $this->dbConnection = $dbConnection; + $this->l10n = $l10n; } /** @@ -81,7 +99,9 @@ class BirthdayService { * @param string $cardUri * @param string $cardData */ - public function onCardChanged($addressBookId, $cardUri, $cardData) { + public function onCardChanged(int $addressBookId, + string $cardUri, + string $cardData) { if (!$this->isGloballyEnabled()) { return; } @@ -90,10 +110,11 @@ class BirthdayService { $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); $targetPrincipals[] = $book['principaluri']; $datesToSync = [ - ['postfix' => '', 'field' => 'BDAY', 'symbol' => '*', 'utfSymbol' => '🎂'], - ['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†", 'utfSymbol' => '⚰️'], - ['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭", 'utfSymbol' => '💍'], + ['postfix' => '', 'field' => 'BDAY'], + ['postfix' => '-death', 'field' => 'DEATHDATE'], + ['postfix' => '-anniversary', 'field' => 'ANNIVERSARY'], ]; + foreach ($targetPrincipals as $principalUri) { if (!$this->isUserEnabled($principalUri)) { continue; @@ -101,7 +122,7 @@ class BirthdayService { $calendar = $this->ensureCalendarExists($principalUri); foreach ($datesToSync as $type) { - $this->updateCalendar($cardUri, $cardData, $book, $calendar['id'], $type); + $this->updateCalendar($cardUri, $cardData, $book, (int) $calendar['id'], $type); } } } @@ -110,7 +131,8 @@ class BirthdayService { * @param int $addressBookId * @param string $cardUri */ - public function onCardDeleted($addressBookId, $cardUri) { + public function onCardDeleted(int $addressBookId, + string $cardUri) { if (!$this->isGloballyEnabled()) { return; } @@ -136,7 +158,7 @@ class BirthdayService { * @return array|null * @throws \Sabre\DAV\Exception\BadRequest */ - public function ensureCalendarExists($principal) { + public function ensureCalendarExists(string $principal):?array { $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); if (!is_null($calendar)) { return $calendar; @@ -151,14 +173,15 @@ class BirthdayService { } /** - * @param string $cardData - * @param string $dateField - * @param string $postfix - * @param string $summarySymbol - * @param string $utfSummarySymbol - * @return null|VCalendar + * @param $cardData + * @param $dateField + * @param $postfix + * @return VCalendar|null + * @throws InvalidDataException */ - public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol, $utfSummarySymbol) { + public function buildDateFromContact(string $cardData, + string $dateField, + string $postfix):?VCalendar { if (empty($cardData)) { return null; } @@ -224,19 +247,8 @@ class BirthdayService { } catch (Exception $e) { return null; } - if ($this->dbConnection->supports4ByteText()) { - if ($unknownYear) { - $summary = $utfSummarySymbol . ' ' . $doc->FN->getValue(); - } else { - $summary = $utfSummarySymbol . ' ' . $doc->FN->getValue() . " ($originalYear)"; - } - } else { - if ($unknownYear) { - $summary = $doc->FN->getValue() . ' ' . $summarySymbol; - } else { - $summary = $doc->FN->getValue() . " ($summarySymbol$originalYear)"; - } - } + + $summary = $this->formatTitle($dateField, $doc->FN->getValue(), $originalYear, $this->dbConnection->supports4ByteText()); $vCal = new VCalendar(); $vCal->VERSION = '2.0'; @@ -273,7 +285,7 @@ class BirthdayService { /** * @param string $user */ - public function resetForUser($user) { + public function resetForUser(string $user):void { $principal = 'principals/users/'.$user; $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); $calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR); @@ -285,8 +297,9 @@ class BirthdayService { /** * @param string $user + * @throws \Sabre\DAV\Exception\BadRequest */ - public function syncUser($user) { + public function syncUser(string $user):void { $principal = 'principals/users/'.$user; $this->ensureCalendarExists($principal); $books = $this->cardDavBackEnd->getAddressBooksForUser($principal); @@ -303,25 +316,25 @@ class BirthdayService { * @param VCalendar $newCalendarData * @return bool */ - public function birthdayEvenChanged($existingCalendarData, $newCalendarData) { + public function birthdayEvenChanged(string $existingCalendarData, + VCalendar $newCalendarData):bool { try { $existingBirthday = Reader::read($existingCalendarData); } catch (Exception $ex) { return true; } - if ($newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() || + + return ( + $newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() || $newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue() - ) { - return true; - } - return false; + ); } /** * @param integer $addressBookId * @return mixed */ - protected function getAllAffectedPrincipals($addressBookId) { + protected function getAllAffectedPrincipals(int $addressBookId) { $targetPrincipals = []; $shares = $this->cardDavBackEnd->getShares($addressBookId); foreach ($shares as $share) { @@ -339,14 +352,20 @@ class BirthdayService { /** * @param string $cardUri - * @param string $cardData + * @param string $cardData * @param array $book * @param int $calendarId - * @param string[] $type + * @param array $type + * @throws InvalidDataException + * @throws \Sabre\DAV\Exception\BadRequest */ - private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) { + private function updateCalendar(string $cardUri, + string $cardData, + array $book, + int $calendarId, + array $type):void { $objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics'; - $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol'], $type['utfSymbol']); + $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix']); $existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri); if (is_null($calendarData)) { if (!is_null($existing)) { @@ -368,18 +387,17 @@ class BirthdayService { * * @return bool */ - private function isGloballyEnabled() { - $isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); - return $isGloballyEnabled === 'yes'; + private function isGloballyEnabled():bool { + return $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes') === 'yes'; } /** - * checks if the user opted-out of birthday calendars + * Checks if the user opted-out of birthday calendars * - * @param $userPrincipal + * @param string $userPrincipal The user principal to check for * @return bool */ - private function isUserEnabled($userPrincipal) { + private function isUserEnabled(string $userPrincipal):bool { if (strpos($userPrincipal, 'principals/users/') === 0) { $userId = substr($userPrincipal, 17); $isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes'); @@ -390,4 +408,69 @@ class BirthdayService { return true; } + /** + * Formats title of Birthday event + * + * @param string $field Field name like BDAY, ANNIVERSARY, ... + * @param string $name Name of contact + * @param int|null $year Year of birth, anniversary, ... + * @param bool $supports4Byte Whether or not the database supports 4 byte chars + * @return string The formatted title + */ + private function formatTitle(string $field, + string $name, + int $year=null, + bool $supports4Byte=true):string { + if ($supports4Byte) { + switch ($field) { + case 'BDAY': + return implode('', [ + '🎂 ', + $name, + $year ? (' (' . $year . ')') : '', + ]); + + case 'DEATHDATE': + return implode('', [ + $this->l10n->t('Death of %s', [$name]), + $year ? (' (' . $year . ')') : '', + ]); + + case 'ANNIVERSARY': + return implode('', [ + '💍 ', + $name, + $year ? (' (' . $year . ')') : '', + ]); + + default: + return ''; + } + } else { + switch($field) { + case 'BDAY': + return implode('', [ + $name, + ' ', + $year ? ('(*' . $year . ')') : '*', + ]); + + case 'DEATHDATE': + return implode('', [ + $this->l10n->t('Death of %s', [$name]), + $year ? (' (' . $year . ')') : '', + ]); + + case 'ANNIVERSARY': + return implode('', [ + $name, + ' ', + $year ? ('(⚭' . $year . ')') : '⚭', + ]); + + default: + return ''; + } + } + } } |