From ebb668221efab2b3e705282c9e09730965009d11 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 7 Sep 2021 08:36:48 +0200 Subject: [PATCH] Support canceled attribute during import of destination data Destination data provides an attribute "DETAILS_ABGESAGT" to mark an event as canceled. We now fetch that info and mark all dates of that event as canceled. This also contains refactoring of creation of dates within import. All dates had the same info, and are now created at a single place. Relates: #9280 --- Classes/Domain/Model/Date.php | 18 +++++ .../Service/DestinationDataImportService.php | 74 ++++++++++++++----- 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/Classes/Domain/Model/Date.php b/Classes/Domain/Model/Date.php index 8b76967..8479a92 100644 --- a/Classes/Domain/Model/Date.php +++ b/Classes/Domain/Model/Date.php @@ -158,4 +158,22 @@ class Date extends AbstractEntity return ''; } + + public static function createFromDestinationData( + \DateTime $start, + \DateTime $end, + bool $canceled + ): self { + $date = new Date(); + $date->setLanguageUid(-1); + + $date->setStart($start); + $date->setEnd($end); + + if ($canceled) { + $date->setCanceled('canceled'); + } + + return $date; + } } diff --git a/Classes/Service/DestinationDataImportService.php b/Classes/Service/DestinationDataImportService.php index 2b358b2..98b2390 100644 --- a/Classes/Service/DestinationDataImportService.php +++ b/Classes/Service/DestinationDataImportService.php @@ -312,7 +312,10 @@ class DestinationDataImportService // Set Dates if ($event['timeIntervals']) { - $this->setDates($event['timeIntervals']); + $this->setDates( + $event['timeIntervals'], + (bool) $this->getAttributeValue($event, 'DETAILS_ABGESAGT') + ); } // Set Assets @@ -357,8 +360,10 @@ class DestinationDataImportService * @param array $timeIntervals * @TODO: split into functions */ - protected function setDates(array $timeIntervals) - { + protected function setDates( + array $timeIntervals, + bool $canceled + ) { // @TODO: does not seem to work --> //$currentEventDates = $this->tmpCurrentEvent->getDates(); @@ -381,16 +386,15 @@ class DestinationDataImportService if (empty($date['interval'])) { if (strtotime($date['start']) > $today) { $this->logger->info('Setup single date'); - $dateObj = $this->objectManager->get(Date::class); $start = new \DateTime($date['start'], new \DateTimeZone($date['tz'])); $end = new \DateTime($date['end'], new \DateTimeZone($date['tz'])); $this->logger->info('Start transformed ' . $start->format('Y-m-d H:i')); $this->logger->info('End transformed ' . $end->format('Y-m-d H:i')); - // Set language UID - $dateObj->setLanguageUid(-1); - $dateObj->setStart($start); - $dateObj->setEnd($end); - $this->tmpCurrentEvent->addDate($dateObj); + $this->tmpCurrentEvent->addDate(Date::createFromDestinationData( + $start, + $end, + $canceled + )); } } else { if ($date['freq'] == 'Daily' && empty($date['weekdays']) && !empty($date['repeatUntil'])) { @@ -408,11 +412,11 @@ class DestinationDataImportService $eventEnd = new \DateTime(); $eventEnd->setTimestamp($i); $eventEnd->setTime($until->format('H'), $until->format('i')); - $dateObj = $this->objectManager->get(Date::class); - $dateObj->setLanguageUid(-1); - $dateObj->setStart($eventStart); - $dateObj->setEnd($eventEnd); - $this->tmpCurrentEvent->addDate($dateObj); + $this->tmpCurrentEvent->addDate(Date::createFromDestinationData( + $eventStart, + $eventEnd, + $canceled + )); } } } elseif ($date['freq'] == 'Weekly' && !empty($date['weekdays']) && !empty($date['repeatUntil'])) { @@ -431,11 +435,11 @@ class DestinationDataImportService $eventEnd = new \DateTime(); $eventEnd->setTimestamp($i); $eventEnd->setTime($until->format('H'), $until->format('i')); - $dateObj = $this->objectManager->get(Date::class); - $dateObj->setLanguageUid(-1); - $dateObj->setStart($eventStart); - $dateObj->setEnd($eventEnd); - $this->tmpCurrentEvent->addDate($dateObj); + $this->tmpCurrentEvent->addDate(Date::createFromDestinationData( + $eventStart, + $eventEnd, + $canceled + )); } } } @@ -786,4 +790,36 @@ class DestinationDataImportService return true; } + + /** + * Fetch the value for requested attribute. + * + * Returns first if multiple attributes with same key exist. + * Casts "true" and "false" to true and false. + * Return null in case no attribute value could exists. + */ + private function getAttributeValue( + array $event, + string $attributeKey + ) { + $attributes = array_filter($event['attributes'] ?? [], function (array $attribute) use ($attributeKey) { + $currentKey = $attribute['key'] ?? ''; + return $currentKey === $attributeKey; + }); + + if ($attributes === []) { + return null; + } + + $value = $attributes[0]['value'] ?? null; + + if ($value === 'true') { + return true; + } + if ($value === 'false') { + return false; + } + + return $value; + } }