You are here

private function EditRepeatingRuleConfirmationModalForm::getEvents in Booking and Availability Management Tools for Drupal 8

Parameters

$start:

$end:

$repeat_frequency:

$repeat_until:

Return value

array

1 call to EditRepeatingRuleConfirmationModalForm::getEvents()
EditRepeatingRuleConfirmationModalForm::buildForm in modules/bat_event_series/src/Form/EditRepeatingRuleConfirmationModalForm.php
Form constructor.

File

modules/bat_event_series/src/Form/EditRepeatingRuleConfirmationModalForm.php, line 242
Contains \Drupal\bat_event_series\Form\EditRepeatingRuleConfirmationModalForm.

Class

EditRepeatingRuleConfirmationModalForm

Namespace

Drupal\bat_event_series\Form

Code

private function getEvents($start, $end, $repeat_frequency, $repeat_until) {
  $event_series_type = bat_event_series_type_load($this->event_series
    ->bundle());
  $event_granularity = $event_series_type
    ->getEventGranularity();
  $event_type = bat_event_type_load($event_series_type
    ->getTargetEventType());
  $field_name = 'event_' . $event_series_type
    ->getTargetEntityType() . '_reference';
  $unit = $this->event_series
    ->get($field_name)->entity;
  $query = $this->entityTypeManager
    ->getStorage('bat_event')
    ->getQuery()
    ->condition('event_series.target_id', $this->event_series
    ->id())
    ->condition('event_dates.value', date('Y-m-d\\TH:i:s'), '>');
  $events = $query
    ->execute();
  $current_event_dates = [];
  foreach ($events as $event_id) {
    $event = bat_event_load($event_id);
    $start_event_date = new \DateTime($event
      ->get('event_dates')->value);
    $end_event_date = new \DateTime($event
      ->get('event_dates')->end_value);
    if ($event_granularity == 'bat_daily') {
      $formatted_dates = $start_event_date
        ->format('Y-m-d') . ' - ' . $end_event_date
        ->format('Y-m-d');
    }
    else {
      $formatted_dates = $start_event_date
        ->format('Y-m-d H:i') . ' - ' . $end_event_date
        ->format('Y-m-d H:i');
    }
    $current_event_dates[$formatted_dates] = $event_id;
  }
  $rrule = new RRule([
    'FREQ' => strtoupper($repeat_frequency),
    'UNTIL' => $repeat_until . 'T235959Z',
    'DTSTART' => $start,
  ]);
  $not_available_events = [];
  $event_dates = [];
  $now = new \DateTime();
  foreach ($rrule as $occurrence) {
    if ($occurrence > $now) {
      $start_date = clone $occurrence;
      $end_date = clone $occurrence;
      if ($event_granularity == 'bat_daily') {
        $end_date
          ->add($start
          ->diff($end));
        $start_date
          ->setTime(0, 0);
        $end_date
          ->setTime(0, 0);
        if ($this
          ->checkAvailability($start_date, $end_date, $event_type, $unit, $events)) {
          $event_dates[] = $start_date
            ->format('Y-m-d') . ' - ' . $end_date
            ->format('Y-m-d');
        }
        else {
          $not_available_events[] = $start_date
            ->format('Y-m-d') . ' - ' . $end_date
            ->format('Y-m-d');
        }
      }
      else {
        $start_date
          ->setTime($start
          ->format('H'), $start
          ->format('i'));
        $end_date
          ->setTime($start
          ->format('H'), $start
          ->format('i'));
        $end_date
          ->add($start
          ->diff($end));
        if ($this
          ->checkAvailability($start_date, $end_date, $event_type, $unit, $events)) {
          $event_dates[] = $start_date
            ->format('Y-m-d H:i') . ' - ' . $end_date
            ->format('Y-m-d H:i');
        }
        else {
          $not_available_events[] = $start_date
            ->format('Y-m-d H:i') . ' - ' . $end_date
            ->format('Y-m-d H:i');
        }
      }
    }
  }
  $add_events = array_diff($event_dates, array_keys($current_event_dates));
  $delete_events = array_diff(array_keys($current_event_dates), $event_dates);
  $delete_events_ids = [];
  if (!empty($delete_events)) {
    $delete_events_ids = array_intersect_key($current_event_dates, array_flip($delete_events));
  }
  return [
    'add_events' => $add_events,
    'delete_events' => $delete_events,
    'delete_events_ids' => $delete_events_ids,
    'not_available_events' => $not_available_events,
  ];
}