diff --git a/CHANGELOG.MD b/CHANGELOG.MD index fa020bd4..d4859b60 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -11,7 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added -- TMS-974: Add recurring events to lists as single item. +- TMS-974: + - Add recurring events to lists as single item. + - Add recurring events to events-component - TMS-994: Add missing list-item block to use ### Fixed diff --git a/lib/Eventz.php b/lib/Eventz.php index 1052ca38..fb84fc17 100644 --- a/lib/Eventz.php +++ b/lib/Eventz.php @@ -174,7 +174,7 @@ public static function get_event_date( $event ) { // If date-parameter exists in url if ( ! empty( $_GET['date'] ) ) { - list( $start_date, $end_date ) = explode( ' - ', $_GET['date'] ); + list( $start_date, $end_date ) = array_merge( explode( ' - ', $_GET['date'] ), array( true ) ); $start_datetime = static::get_as_datetime( $start_date ); $end_datetime = ! is_null($end_date) ? static::get_as_datetime( $end_date ) : ''; @@ -215,7 +215,7 @@ public static function get_event_time( $event ) { // If time-parameter exists in url if ( ! empty( $_GET['time'] ) ) { - list( $start_time, $end_time) = explode( ' - ', urldecode($_GET['time'] ) ); + list( $start_time, $end_time) = array_merge( explode( ' - ', urldecode( $_GET['time'] ) ), array( false ) ); if ( $start_time && $end_time ) { return sprintf( @@ -314,7 +314,7 @@ public static function get_event_price_info( $event ) : ?array { * @return string|null */ public static function format_price( $price ) : ?string { - if ( $price->isFree ) { + if ( property_exists( $price, 'isFree' ) ) { return __( 'Free', 'tms-theme-base' ); } diff --git a/lib/Formatters/EventzFormatter.php b/lib/Formatters/EventzFormatter.php index 007a3617..639ce613 100644 --- a/lib/Formatters/EventzFormatter.php +++ b/lib/Formatters/EventzFormatter.php @@ -59,12 +59,18 @@ public function format( array $layout ) : array { $events = []; } + // Create recurring events + $event_data['events'] = $events ?? null; + if ( ! empty( $event_data['events'] ) ) { + $events = self::create_recurring_events( $event_data ); + } + $manual_events = []; if ( ! empty( $layout['manual_event_categories'] ) ) { $manual_events = self::get_manual_events( $layout['manual_event_categories'] ); } - $events = array_merge( $events, $manual_events ); + $events = array_merge( $events['events'], $manual_events ); if ( empty( $events ) ) { return $layout; @@ -91,6 +97,79 @@ public function format( array $layout ) : array { return $layout; } + + /** + * Create recurring events as single item. + * + * @param array $events Events. + * + * @return void + */ + 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 ); + } + 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; + } + } + + $events['events'] = $recurring_events; + + return $events; + } + /** * Format events * diff --git a/models/page-events-search.php b/models/page-events-search.php index fc5a177f..b11a8bd4 100644 --- a/models/page-events-search.php +++ b/models/page-events-search.php @@ -238,7 +238,7 @@ protected function do_get_events( array $params ) : array { if ( ! empty( $event_data['events'] ) ) { - $event_data = $this->create_recurring_events( $event_data ); + $event_data = EventzFormatter::create_recurring_events( $event_data ); $event_data['events'] = ( new EventzFormatter() )->format_events( $event_data['events'] ); @@ -303,76 +303,4 @@ protected function set_pagination_data( int $event_count ) : void { $this->pagination->items = $event_count; $this->pagination->max_page = (int) ceil( $event_count / $per_page ); } - - /** - * Create recurring events as single item. - * - * @param array $events Events. - * - * @return void - */ - protected 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 ); - } - else { - $endTime = $endPart; - } - - // Parse the dates - $newStartDate = DateTime::createFromFormat( 'd.m.Y', $startDate ); - $newEndDate = $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['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; - } - } - - $events['events'] = $recurring_events; - - return $events; - - } }