You are here

function _wf_crm_participant_validate in Webform CiviCRM Integration 7.3

Validation callback for event registrations.

Parameters

$form: FAPI form array

$form_state: FAPI form_state array

$submitted: Webform submission array

1 call to _wf_crm_participant_validate()
wf_crm_validate in ./webform_civicrm.module
Validation callback for webform submissions.

File

./webform_civicrm_forms.inc, line 1323

Code

function _wf_crm_participant_validate($form, &$form_state, $submitted) {
  $data = $form['#node']->webform_civicrm['data'];
  $contacts = $cids = array();

  // Check how many valid contacts we have
  foreach ($data['contact'] as $c => $contact) {

    // Check if we have a contact_id
    if (is_numeric(wf_crm_aval($submitted, "civicrm_{$c}_contact_1_contact_existing"))) {
      $cids[] = $contacts[$c] = $submitted["civicrm_{$c}_contact_1_contact_existing"];
    }
    elseif (wf_crm_name_field_exists($submitted, $c, $contact['contact'][1]['contact_type'])) {
      $contacts[$c] = 0;
    }
  }
  $events = array();
  $add = $data['participant_reg_type'] == 'all' ? count($contacts) : 1;

  // Fetch events set in back-end
  foreach ($data['participant'] as $c => $par) {
    if (!empty($par['participant']) && isset($contacts[$c])) {
      foreach ($par['participant'] as $p) {
        if (!empty($p['event_id']) && $p['event_id'] != 'create_civicrm_webform_element') {
          list($eid) = explode('-', $p['event_id']);
          if (is_numeric($eid)) {
            $events[$eid]['ended'] = TRUE;
            $events[$eid]['title'] = t('this event');
            $events[$eid]['count'] = wf_crm_aval($events, "{$eid}:count", 0) + $add;
          }
        }
      }
    }
  }

  // Add events selected by user
  foreach ($submitted as $field => $value) {
    if (strpos($field, 'participant_event_id')) {
      list(, $c) = wf_crm_explode_key($field);
      if (isset($contacts[$c]) || $c == 1 && $data['participant_reg_type'] == 'all') {
        $value = is_array($value) ? $value : array(
          $value,
        );
        foreach ($value as $val) {
          $eid = explode('-', $val);
          if (count($eid) == 2 && is_numeric($eid[0]) && $eid[0]) {
            $eid = $eid[0];
            $events[$eid]['ended'] = TRUE;
            $events[$eid]['title'] = t('this event');
            $events[$eid]['count'] = wf_crm_aval($events, "{$eid}:count", 0) + $add;
          }
        }
      }
    }
  }

  // Subtract events already registered for - this only works with known contacts
  if ($events && $cids) {
    $dao =& CRM_Core_DAO::executeQuery("SELECT event_id\n      FROM civicrm_participant p, civicrm_participant_status_type s\n      WHERE s.id = p.status_id AND s.is_counted = 1\n      AND event_id IN (" . implode(',', array_keys($events)) . ")\n      AND contact_id IN (" . implode(',', $cids) . ")\n      AND is_test = 0");
    while ($dao
      ->fetch()) {
      if (isset($events[$dao->event_id])) {
        if (--$events[$dao->event_id]['count'] === 0) {
          unset($events[$dao->event_id]);
        }
      }
    }
    $dao
      ->free();
  }
  wf_crm_event_info($events);
  foreach ($events as $eid => $event) {
    if ($event['ended']) {
      form_set_error($eid, t('Sorry, you can no longer register for %event.', array(
        '%event' => $event['title'],
      )));
    }
    elseif ($event['max_participants'] && $event['count'] > $event['remaining']) {
      if (!empty($event['full'])) {
        form_set_error($eid, '<em>' . $event['title'] . '</em>: ' . $event['full_message']);
      }
      else {
        form_set_error($eid, format_plural($event['remaining'], 'Sorry, you tried to register !count people for %event but there is only 1 space remaining.', 'Sorry, you tried to register !count people for %event but there are only @count spaces remaining.', array(
          '%event' => $event['title'],
          '!count' => $event['count'],
        )));
      }
    }
  }
}