You are here

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

1 call to EditRepeatingRuleConfirmationModalForm::addEvents()
EditRepeatingRuleConfirmationModalForm::submitForm in modules/bat_event_series/src/Form/EditRepeatingRuleConfirmationModalForm.php
Form submission handler.

File

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

Class

EditRepeatingRuleConfirmationModalForm

Namespace

Drupal\bat_event_series\Form

Code

private function addEvents($events) {
  $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;
  foreach ($events as $dates) {
    $event = bat_event_create([
      'type' => $event_type
        ->id(),
    ]);
    list($start_date, $end_date) = explode(' - ', $dates);
    $start_date = new \DateTime($start_date);
    $end_date = new \DateTime($end_date);
    if ($event_granularity == 'bat_daily') {
      $start_date
        ->setTime(0, 0);
      $end_date
        ->setTime(0, 0);
      $event_dates = [
        'value' => $start_date
          ->format('Y-m-d\\T00:00:00'),
        'end_value' => $end_date
          ->format('Y-m-d\\T00:00:00'),
      ];
    }
    else {
      $event_dates = [
        'value' => $start_date
          ->format('Y-m-d\\TH:i:00'),
        'end_value' => $end_date
          ->format('Y-m-d\\TH:i:00'),
      ];
    }
    $event
      ->set('event_dates', $event_dates);
    $event
      ->set('event_state_reference', $this->event_series
      ->get('event_state_reference')->entity
      ->id());
    $event
      ->set($field_name, $unit
      ->id());
    $event
      ->set('event_series', $this->event_series
      ->id());
    $event
      ->save();
  }
}