Skip to content

Commit

Permalink
TMS-974: Add recurring events.
Browse files Browse the repository at this point in the history
  • Loading branch information
aerkkilae committed Oct 10, 2023
1 parent ccf1dd8 commit 848fd89
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- TMS-974: Add recurring events to lists as single item.

## [1.49.0] - 2023-09-15

- TMS-962: Add Lunch Menus plugin caps to roles.
Expand Down
18 changes: 14 additions & 4 deletions models/page-events-calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ protected function get_events() : array {
'targets' => get_field( 'target' ),
'tags' => get_field( 'tag' ),
'sort' => 'startDate',
'size' => get_option( 'posts_per_page' ),
'skip' => $skip,
'show_images' => get_field( 'show_images' ),
];

Expand Down Expand Up @@ -135,8 +133,20 @@ protected function get_events() : array {
}
}

if ( ! empty( $response['meta'] ) ) {
$this->set_pagination_data( $response['meta']->total );
if ( ! empty ( $response ) ) {
$response = $this->create_recurring_events( $response );
}

if ( ! empty( $response['events'] ) ) {

// Sort events.
usort( $response['events'], function( $a, $b ) {
return $a['start_date_raw'] <=> $b['start_date_raw'];
} );

$this->set_pagination_data( count( $response['events'] ) );

$response['events'] = array_slice( $response['events'], $skip, get_option( 'posts_per_page' ) );
}

return $response;
Expand Down
74 changes: 65 additions & 9 deletions models/page-events-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ protected function get_events() : array {

$paged = get_query_var( 'paged', 1 );
$skip = 0;
$count = 0;

if ( $paged > 1 ) {
$skip = ( $paged - 1 ) * get_option( 'posts_per_page' );
Expand All @@ -152,8 +153,6 @@ protected function get_events() : array {
'end' => $end_date,
'sort' => 'startDate',
'category_id' => get_field( 'category' ) ?? [],
'size' => get_option( 'posts_per_page' ),
'skip' => $skip,
];

$formatter = new EventzFormatter();
Expand All @@ -176,12 +175,22 @@ protected function get_events() : array {
}
}

if ( ! empty( $response['meta'] ) ) {
$this->set_pagination_data( $response['meta']->total );
if ( ! empty( $response['events'] ) ) {

// Sort and paginate events.
usort( $response['events'], function( $a, $b ) {
return $a['start_date_raw'] <=> $b['start_date_raw'];
} );

$this->set_pagination_data( count( $response['events'] ) );

$count = count( $response['events'] );

$response['events'] = array_slice( $response['events'], $skip, get_option( 'posts_per_page' ) );
}

return [
'summary' => $this->get_results_text( $response['meta']->total ?? 0 ),
'summary' => $this->get_results_text( $count ),
'posts' => $response['events'],
];
}
Expand Down Expand Up @@ -227,12 +236,12 @@ protected function get_results_text( $event_count ) : ?string {
protected function do_get_events( array $params ) : array {
$event_data = $this->do_api_call( $params );

if ( ! empty( $event_data['meta'] ) ) {
$this->set_pagination_data( $event_data['meta']->total );
}

if ( ! empty( $event_data['events'] ) ) {

$event_data = $this->create_recurring_events( $event_data );

$event_data['events'] = ( new EventzFormatter() )->format_events( $event_data['events'] );

$event_data['events'] = array_map( function ( $item ) {
$item['short_description'] = wp_trim_words( $item['short_description'], 30 );
$item['location_icon'] = $item['is_virtual_event']
Expand Down Expand Up @@ -294,4 +303,51 @@ 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 string into date and time range
list($datePart, $timeRange) = explode(' ', $date['date'], 2);

// Parse the date
$newDate = DateTime::createFromFormat('d.m.Y', $datePart);

// Split the time range into start and end times
list($startTime, $endTime) = explode(' - ', $timeRange);

// Parse the start and end times
$startDateTime = DateTime::createFromFormat('H.i', $startTime);
$startDateTime->setDate( $newDate->format('Y'), $newDate->format('m'), $newDate->format('d') );
$endDateTime = DateTime::createFromFormat('H.i', $endTime);
$endDateTime->setDate( $newDate->format('Y'), $newDate->format('m'), $newDate->format('d') );

$clone['date'] = $newDate->format('d.m.Y');
$clone['start_date_raw'] = $startDateTime;
$clone['end_date_raw'] = $endDateTime;
$clone['url'] = $event['url'].'&date='.urlencode( $date['date'] );

$recurring_events[] = $clone;
}
} else {
$recurring_events[] = $event;
}
}

$events['events'] = $recurring_events;

return $events;

}
}

0 comments on commit 848fd89

Please sign in to comment.