You are here

private function WebformCivicrmPostProcess::processParticipants in Webform CiviCRM Integration 8.5

Process event participation for a contact

id

Parameters

int $c:

1 call to WebformCivicrmPostProcess::processParticipants()
WebformCivicrmPostProcess::preSave in src/WebformCivicrmPostProcess.php
Process webform submission when it is about to be saved. Called by the following hook:

File

src/WebformCivicrmPostProcess.php, line 1136
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function processParticipants($c, $cid) {
  $utils = \Drupal::service('webform_civicrm.utils');
  static $registered_by_id = [];
  $n = $this->data['participant_reg_type'] == 'separate' ? $c : 1;
  if ($p = wf_crm_aval($this->data, "participant:{$n}:participant")) {

    // Fetch existing participant records
    $existing = $utils
      ->wf_crm_apivalues('Participant', 'get', [
      'return' => [
        "event_id",
      ],
      'contact_id' => $cid,
      'is_test' => 0,
    ]);
    $existing = array_column($existing, 'id', 'event_id');
    foreach ($p as $e => $params) {
      $remove = [];
      $fid = "civicrm_{$c}_participant_{$e}_participant_event_id";

      // Automatic status - de-selected events will be cancelled if 'disable_unregister' is not selected
      if (empty($this->data['reg_options']['disable_unregister'])) {
        if (empty($params['status_id'])) {
          foreach ($this
            ->getExposedOptions($fid) as $eid => $title) {
            list($eid) = explode('-', $eid);
            if (isset($existing[$eid])) {
              $remove[$eid] = $title;
            }
          }
        }
      }
      if (!empty($params['event_id'])) {
        $params['contact_id'] = $cid;
        if (empty($params['campaign_id']) || empty($this->all_fields['participant_campaign_id'])) {
          unset($params['campaign_id']);
        }

        // Loop through event ids to support multi-valued form elements
        $this->events = (array) $params['event_id'];
        foreach ($this->events as $i => $id_and_type) {
          if (!empty($id_and_type)) {
            list($eid) = explode('-', $id_and_type);
            $params['event_id'] = $eid;
            unset($remove[$eid], $params['registered_by_id'], $params['id'], $params['source']);

            // Is existing participant?
            if (!empty($existing[$eid])) {
              $params['id'] = $existing[$params['event_id']];
            }
            else {
              if (isset($this->data['contact'][$c]['contact'][1]['source'])) {
                $params['source'] = $this->data['contact'][$c]['contact'][1]['source'];
              }
              else {
                $params['source'] = $this->settings['new_contact_source'];
              }
              if ($c > 1 && !empty($registered_by_id[$e][$i])) {
                $params['registered_by_id'] = $registered_by_id[$e][$i];
              }
            }

            // Automatic status
            if (empty($params['status_id']) && empty($params['id'])) {
              $params['status_id'] = 'Registered';

              // Pending payment status
              if ($this->contributionIsIncomplete && !empty($params['fee_amount'])) {
                $params['status_id'] = $this->contributionIsPayLater ? 'Pending from pay later' : 'Pending from incomplete transaction';
              }
            }

            // Do not update status of existing participant in "Automatic" mode
            if (empty($params['status_id'])) {
              unset($params['status_id']);
            }
            $result = $utils
              ->wf_civicrm_api('participant', 'create', $params);
            $this->ent['participant'][$n]['id'] = $result['id'];

            // Update line-item
            foreach ($this->line_items as &$item) {
              if ($item['element'] == "civicrm_{$n}_participant_{$e}_participant_{$id_and_type}") {
                if (empty($item['participant_id'])) {
                  $item['participant_id'] = $item['entity_id'] = $result['id'];
                }
                $item['participant_count'] = wf_crm_aval($item, 'participant_count', 0) + 1;
                break;
              }
            }

            // When registering contact 1, store id to apply to other contacts
            if ($c == 1) {
              $registered_by_id[$e][$i] = $result['id'];
            }
          }
        }
      }
      foreach ($remove as $eid => $title) {
        $utils
          ->wf_civicrm_api('participant', 'create', [
          'status_id' => "Cancelled",
          'id' => $existing[$eid],
        ]);
        \Drupal::messenger()
          ->addStatus(t('Registration cancelled for @event', [
          '@event' => $title,
        ]));
      }
    }
  }
}