You are here

function _webform_civicrm_participant_validate in Webform CiviCRM Integration 7.2

Same name and namespace in other branches
  1. 6.2 webform_civicrm_forms.inc \_webform_civicrm_participant_validate()

Validation callback for event registrations.

1 call to _webform_civicrm_participant_validate()
webform_civicrm_form_validate in ./webform_civicrm.module
Validation callback for webform submissions.

File

./webform_civicrm_forms.inc, line 1135

Code

function _webform_civicrm_participant_validate($form, &$form_state, $submitted) {
  $data = $form['#node']->webform_civicrm['data'];
  $cids = webform_civicrm_aval($form_state, 'storage:civicrm:id:cid', array());

  // Check how many valid contacts we have
  $contacts = array();
  foreach (array_keys($data['contact']) as $c) {
    if (!empty($cids[$c]) || !empty($submitted["civicrm_{$c}_contact_1_contact_first_name"]) || !empty($submitted["civicrm_{$c}_contact_1_contact_last_name"]) || !empty($submitted["civicrm_{$c}_contact_1_contact_organization_name"]) || !empty($submitted["civicrm_{$c}_contact_1_contact_household_name"]) || !empty($submitted["civicrm_{$c}_contact_1_email_email"])) {
      $contacts[] = $c;
    }
  }
  $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']) && in_array($c, $contacts)) {
      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'] = webform_civicrm_aval($events, "{$eid}:count", 0) + $add;
          }
        }
      }
    }
  }

  // Add events selected by user
  foreach ($submitted as $field => $value) {
    if (strpos($field, 'participant_event_id')) {
      list($lobo, $c, $ent, $n, $table, $key) = webform_civicrm_explode_key($field);
      if (in_array($c, $contacts) || $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'] = webform_civicrm_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]);
        }
      }
    }
  }
  webform_civicrm_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'],
        )));
      }
    }
  }
}