Skip to content

Commit

Permalink
Merge pull request #484 from devgeniem/TMS-974
Browse files Browse the repository at this point in the history
TMS-974: Add recurring events to event-component
  • Loading branch information
eebbi authored Nov 21, 2023
2 parents 2b2daf5 + 9444cc7 commit cfd5bf8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 78 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/Eventz.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) : '';
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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' );
}

Expand Down
81 changes: 80 additions & 1 deletion lib/Formatters/EventzFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down
74 changes: 1 addition & 73 deletions models/page-events-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );

Expand Down Expand Up @@ -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;

}
}

0 comments on commit cfd5bf8

Please sign in to comment.