diff --git a/CHANGELOG.MD b/CHANGELOG.MD index b0cea5ed..aedc1463 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [1.54.2] - 2023-11-22 + +- TMS-974: Fix event formatting if only manual events are shown + ## [1.54.1] - 2023-11-22 - TMS-974: Fix fatal error in EventzFormatter diff --git a/lib/Formatters/EventzFormatter.php b/lib/Formatters/EventzFormatter.php index 9c23d88f..9156e589 100644 --- a/lib/Formatters/EventzFormatter.php +++ b/lib/Formatters/EventzFormatter.php @@ -56,11 +56,11 @@ public function format( array $layout ) : array { $events = $this->get_events( $query_params ); if ( empty( $events ) ) { - $events['events'] = []; + $events = []; } // Create recurring events - $event_data['events'] = $events ?? null; + $event_data['events'] = $events ?? []; if ( ! empty( $event_data['events'] ) ) { $events = self::create_recurring_events( $event_data ); } @@ -70,7 +70,7 @@ public function format( array $layout ) : array { $manual_events = self::get_manual_events( $layout['manual_event_categories'] ); } - $events = array_merge( $events['events'], $manual_events ); + $events = array_merge( $events['events'] ?? [], $manual_events ); if ( empty( $events ) ) { return $layout; @@ -108,60 +108,62 @@ public function format( array $layout ) : array { public static function create_recurring_events( $events ) { $recurring_events = []; - foreach ( $events['events'] as $event ) { - if ( count( $event['dates'] ) > 1 ) { - foreach ( $event['dates'] as $date ) { - $clone = $event; - - // Split the dates and times into parts - list( $startPart, $endPart ) = explode( ' - ', $date['date'], 2 ); - list( $startDate, $startTime ) = explode( ' ', $startPart, 2 ); - - // Check if endPart includes date & time - if ( strpos($endPart, ' ') ) { - list( $endDate, $endTime ) = explode( ' ', $endPart, 2 ); + if( ! empty( $events['events'] ) ) { + foreach ( $events['events'] as $event ) { + if ( count( $event['dates'] ) > 1 ) { + foreach ( $event['dates'] as $date ) { + $clone = $event; + + // Split the dates and times into parts + list( $startPart, $endPart ) = explode( ' - ', $date['date'], 2 ); + list( $startDate, $startTime ) = explode( ' ', $startPart, 2 ); + + // Check if endPart includes date & time + if ( strpos($endPart, ' ') ) { + list( $endDate, $endTime ) = explode( ' ', $endPart, 2 ); + } + else { + $endTime = $endPart; + } + + // Parse the dates + $newStartDate = \DateTime::createFromFormat( 'd.m.Y', $startDate ); + $newEndDate = isset( $endDate ) ? \DateTime::createFromFormat( 'd.m.Y', $endDate ) : null; + + // Parse the start and end times + $startDateTime = \DateTime::createFromFormat( 'H.i', $startTime ); + $startDateTime->setDate( $newStartDate->format( 'Y' ), $newStartDate->format( 'm' ), $newStartDate->format( 'd' ) ); + if ( $newEndDate ) { + $endDateTime = \DateTime::createFromFormat( 'H.i', $endTime ); + $endDateTime->setDate( $newEndDate->format( 'Y' ), $newEndDate->format( 'm' ), $newEndDate->format( 'd' ) ); + } + + // Create time & date-ranges + if ( $endTime ) { + $timeRange = $startTime . ' - ' . $endTime; + } + else { + $timeRange = $startTime; + } + + if ( $newEndDate ) { + $dateRange = $newStartDate->format( 'd.m.Y' ) . ' - ' . $newEndDate->format( 'd.m.Y' ); + } + else { + $dateRange = $newStartDate->format( 'd.m.Y' ); + } + + $clone['date'] = $dateRange; + $clone['time'] = $timeRange; + $clone['start_date_raw'] = $startDateTime; + $clone['end_date_raw'] = $endDateTime ?? ''; + $clone['url'] = $event['url'] . '&date=' . urlencode( $dateRange ) . '&time=' . urlencode( $timeRange ); + + $recurring_events[] = $clone; } - else { - $endTime = $endPart; - } - - // Parse the dates - $newStartDate = \DateTime::createFromFormat( 'd.m.Y', $startDate ); - $newEndDate = isset( $endDate ) ? \DateTime::createFromFormat( 'd.m.Y', $endDate ) : null; - - // Parse the start and end times - $startDateTime = \DateTime::createFromFormat( 'H.i', $startTime ); - $startDateTime->setDate( $newStartDate->format( 'Y' ), $newStartDate->format( 'm' ), $newStartDate->format( 'd' ) ); - if ( $newEndDate ) { - $endDateTime = \DateTime::createFromFormat( 'H.i', $endTime ); - $endDateTime->setDate( $newEndDate->format( 'Y' ), $newEndDate->format( 'm' ), $newEndDate->format( 'd' ) ); - } - - // Create time & date-ranges - if ( $endTime ) { - $timeRange = $startTime . ' - ' . $endTime; - } - else { - $timeRange = $startTime; - } - - if ( $newEndDate ) { - $dateRange = $newStartDate->format( 'd.m.Y' ) . ' - ' . $newEndDate->format( 'd.m.Y' ); - } - else { - $dateRange = $newStartDate->format( 'd.m.Y' ); - } - - $clone['date'] = $dateRange; - $clone['time'] = $timeRange; - $clone['start_date_raw'] = $startDateTime; - $clone['end_date_raw'] = $endDateTime ?? ''; - $clone['url'] = $event['url'] . '&date=' . urlencode( $dateRange ) . '&time=' . urlencode( $timeRange ); - - $recurring_events[] = $clone; + } else { + $recurring_events[] = $event; } - } else { - $recurring_events[] = $event; } }