You are here

function civicrm_entity_price_set_field_display_form_event_submit in CiviCRM Entity 7.2

Submit handler for event registration form 'Register' button

_state

Parameters

$form:

File

modules/civicrm_entity_price_set_field/includes/civicrm_entity_price_set_field.event_registration.inc, line 656
CiviCRM Entity Price Set Field, Form display formatter, event registration

Code

function civicrm_entity_price_set_field_display_form_event_submit($form, &$form_state) {
  switch ($form_state['step']) {
    case 1:

      // Registration form submit
      $form_state['storage']['registration_form'] = $form_state['values']['registration_form'];
      $form_state['storage']['registration_form']['transaction'] = _civicrm_entity_price_set_field_calculate_total($form_state['price_set_data'], 'civicrm_event', $form_state['event'], $form_state['storage']['registration_form']);
      $form_state['step']++;
      break;
    case 2:

      // confirmation page submit
      $event = $form_state['event'];
      $success = TRUE;

      //  Dedupe the participants, and make sure nobody has already registered, if so kick it back to the register form, messaging
      foreach ($form_state['contacts'] as $count => $contact) {
        if (empty($contact->id)) {

          // the de-dupe api way
          $params = array(
            'sequential' => 1,
            'check_permission' => FALSE,
          );
          $params['match']['contact_type'] = 'Individual';
          if (!empty($form_state['event']->dedupe_rule_group_id)) {
            $params['dedupe_rule_id'] = $form_state['event']->dedupe_rule_group_id;
          }

          // build up match param array with values for each contact profile field
          $first_pid = 0;
          foreach ($form_state['storage']['registration_form']['contacts'][$count]['contact_info'] as $profile_id => $profile) {
            $pids = explode('-', $profile_id);
            $pid = $pids[1];
            $profile_entity_type = _civicrm_entity_profile_determine_profile_entity($pid);
            if ($profile_entity_type == 'contact') {
              if (empty($first_pid)) {
                $first_pid = $pid;
                $first_profile = $profile;
              }

              // should we gather up all the contact profile fields and add those to the 'match' param array for the dedupe, instead of just the first?
              foreach ($profile['profile'] as $name => $value) {
                $params['match'][$name] = $value;
              }
            }
          }
          $dedupe_result = civicrm_api3('Contact', 'duplicatecheck', $params);
          if (!empty($dedupe_result['id'])) {

            // should we check here if the contact is already registered, and if so throw back a message?
            $contact_registrations_for_event = _civicrm_entity_price_set_field_get_participant_count_for_event($event->id, $dedupe_result['id']);
            if (empty($event->allow_same_participant_emails) && $contact_registrations_for_event) {
              drupal_set_message('Participant ' . ($count + 1) . ' has already registered for this event.');
              $form_state['step'] = 1;
              $form_state['rebuild'] = TRUE;
              return;
            }
            $form_state['contacts'][$count] = entity_load_single('civicrm_contact', $dedupe_result['id']);
          }

          // submit main registrant contact profile here, if there isn't a contact_id yet (its new)
          // we need the contact_id to run the transaction and create a contribution
          // if not, $success = false, and it displays an error and goes back to the registration form
          if ($count == 0) {
            if (empty($form_state['contacts'][$count]->id)) {
              $submitted_result = 0;
              if (!empty($first_pid) && !empty($first_profile)) {
                $submitted_result = _civicrm_entity_profile_process_profile_submitted_data($first_pid, $first_profile['profile']);
              }
              if (!empty($submitted_result)) {
                $form_state['contacts'][0] = $submitted_result;
                $form_state['first_profile_submitted'] = 1;
              }
              else {
                $success = FALSE;
              }
            }
          }
        }
      }
      if (!empty($form_state['storage']['registration_form']['transaction']['total']) && is_numeric($form_state['storage']['registration_form']['transaction']['total']) && $form_state['storage']['registration_form']['transaction']['total'] > 0) {
        if (empty($form_state['storage']['registration_form']['pay_later'])) {

          // if we have a main registrant contact id we continue to process the transaction
          if ($success) {
            $transact_result = _civicrm_entity_price_set_field_run_cc_transaction($form_state['display_settings'], $form_state['price_set_data'], 'civicrm_event', $event, $form_state['contacts'], $form_state['storage']['registration_form']);
            if ($transact_result !== FALSE && !empty($transact_result['contribution']) && is_object($transact_result['contribution'])) {
              $form_state['contribution'] = $transact_result['contribution'];
            }
            else {
              $success = FALSE;
            }
          }
        }
        else {

          // if is pay later, make a contribution with status Pending
          if ($success) {
            $contribution_params = array(
              'is_new' => TRUE,
              'financial_type_id' => $form_state['event']->financial_type_id,
              'total_amount' => $form_state['storage']['registration_form']['transaction']['total'],
              'contact_id' => $form_state['contacts'][0]->id,
              'invoice_id' => md5(uniqid(rand(), TRUE)),
              'source' => 'CiviCRM Entity Price Set Field -- Event Registration',
              'is_pay_later' => 1,
              'receive_date' => date('Y-m-d H:i:s'),
              'contribution_status_id' => "Pending",
              'skipLineItem' => 1,
            );
            $contribution = new CivicrmEntity($contribution_params, 'civicrm_contribution');
            $contribution_wrapper = entity_metadata_wrapper('civicrm_contribution', $contribution);
            $contribution_wrapper
              ->save();
            $form_state['contribution'] = $contribution_wrapper
              ->value();
          }
        }
      }
      if (!empty($success)) {
        _civicrm_entity_price_set_field_process_event_registration($form_state);
        $form_state['step']++;
      }
      else {
        drupal_set_message(t('Error processing registration. Contact Site Administrator.'));
        $form_state['step']--;
      }
      break;
    case 3:
      $form_state['step'] = 1;
      break;
  }
  $form_state['rebuild'] = TRUE;
}