You are here

private function WebformCivicrmPreProcess::populateEvents in Webform CiviCRM Integration 8.5

Check if events are open to registration and take appropriate action

1 call to WebformCivicrmPreProcess::populateEvents()
WebformCivicrmPreProcess::alterForm in src/WebformCivicrmPreProcess.php
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

src/WebformCivicrmPreProcess.php, line 257
Front-end form pre-processor.

Class

WebformCivicrmPreProcess

Namespace

Drupal\webform_civicrm

Code

private function populateEvents() {
  $reg = NestedArray::getValue($this->data, [
    'reg_options',
  ]) ?: [];

  // Fetch events set in back-end
  $this->data += [
    'participant' => [],
  ];
  foreach ($this->data['participant'] as $e => $par) {
    if (empty($par['participant'])) {
      continue;
    }
    foreach ($par['participant'] as $n => $p) {
      if (empty($p['event_id'])) {
        continue;
      }

      // Handle multi-valued event selection
      foreach ((array) $p['event_id'] as $eid) {
        if ($eid = (int) $eid) {
          $this->events[$eid]['ended'] = TRUE;
          $this->events[$eid]['title'] = t('this event');
          $this->events[$eid]['count'] = (NestedArray::getValue($this->events, [
            $eid,
            'count',
          ]) ?: 0) + 1;
          $status_fid = "civicrm_{$e}_participant_{$n}_participant_status_id";
          $this->events[$eid]['form'][] = [
            'contact' => $e,
            'num' => $n,
            'eid' => NULL,
            'status_id' => (array) $this
              ->getData($status_fid, array_keys($this
              ->getExposedOptions($status_fid))),
          ];
        }
      }
    }
  }

  // Add events exposed to the form
  foreach ($this->enabled as $field => $fid) {
    if (strpos($field, 'participant_event_id')) {
      foreach ($this
        ->getExposedOptions($field) as $p => $label) {
        list($eid) = explode('-', $p);
        $this->events[$eid]['ended'] = TRUE;
        $this->events[$eid]['title'] = $label;
        list(, $e, , $n) = explode('_', $field);
        $status_fid = "civicrm_{$e}_participant_{$n}_participant_status_id";
        $this->events[$eid]['form'][] = [
          'contact' => $e,
          'num' => $n,
          'eid' => $p,
          'status_id' => (array) $this
            ->getData($status_fid, array_keys($this
            ->getExposedOptions($status_fid))),
        ];
      }
    }
  }
  if ($this->events && (!empty($reg['show_remaining']) || !empty($reg['block_form']))) {
    $this
      ->loadEvents();
    foreach ($this->events as $eid => $event) {
      if ($event['ended']) {
        if (!empty($reg['show_remaining'])) {
          $this
            ->setMessage(t('Sorry, %event has ended.', [
            '%event' => $event['title'],
          ]), 'warning');
        }
      }
      elseif (!empty($event['is_full'])) {
        if (!empty($reg['show_remaining'])) {
          $this
            ->setMessage('<em>' . Html::escape($event['title']) . '</em>: ' . Html::escape($event['event_full_text']), 'warning');
        }
      }
      else {
        $reg['block_form'] = FALSE;
        if (!empty($event['max_participants']) && ($reg['show_remaining'] == 'always' || (int) $reg['show_remaining'] >= $event['available_places'])) {
          $this
            ->setMessage(\Drupal::translation()
            ->formatPlural($event['available_places'], '%event has 1 remaining space.', '%event has @count remaining spaces.', [
            '%event' => $event['title'],
          ]));
        }
      }
    }
    if ($reg['block_form']) {
      $this->form['submitted']['#access'] = $this->form['actions']['#access'] = FALSE;
      return;
    }
  }
}