You are here

function registration_form_submit in Entity Registration 7.2

Same name and namespace in other branches
  1. 8.2 includes/registration.forms.inc \registration_form_submit()
  2. 8 includes/registration.forms.inc \registration_form_submit()
  3. 7 includes/registration.forms.inc \registration_form_submit()

Submit callback for registration_form().

File

includes/registration.forms.inc, line 260
Form definitions and callbacks for Registration.

Code

function registration_form_submit($form, &$form_state) {
  global $user;
  $registration = $form_state['registration'];
  $registration_type = registration_get_types($registration->type);
  $settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
  $registrant_entity_type = $registration_type->registrant_entity_type;
  $registrant_bundle = $registration_type->registrant_bundle;
  $registrant_entity_info = entity_get_info($registrant_entity_type);

  // Set the registration's author uid:
  if (!isset($registration->registration_id)) {
    $registration->author_uid = $user->uid;
  }
  $registration->count = $form_state['values']['count'];
  if (!empty($form_state['values']['state'])) {
    $registration->state = $form_state['values']['state'];
  }
  $registrant_mail = NULL;
  switch ($form_state['values']['who_is_registering']) {
    case REGISTRATION_REGISTRANT_TYPE_SELF:
      $registrant_mail = $user->mail;
      break;
    case REGISTRATION_REGISTRANT_TYPE_OTHER:
      $registrant_mail = $form_state['values']['registrant_mail'];
      break;
  }

  // Look up entity by $registrant_mail.
  $registrant_wrapper = registration_registrant_load_by_mail($registration_type, $registrant_mail);
  if (!empty($settings['settings']['upsert_registrants'])) {
    if (!$registrant_wrapper) {
      $registrant_wrapper = registration_registrant_create($registration_type, $registrant_mail);
    }
    $registrant = $registrant_wrapper
      ->value();
    $values_excluding_fields = $form_state['values'];
    if ($registrant_entity_info['fieldable']) {
      $values_excluding_fields = array_diff_key($form_state['values'], field_info_instances($registrant_entity_type, $registrant_bundle));
    }
    $values_in_sub_form = array_intersect_key($values_excluding_fields, $form['registrant_fields']);
    foreach ($values_in_sub_form as $key => $value) {
      $registrant->{$key} = $value;
    }
    if ($registrant_entity_info['fieldable']) {
      field_attach_submit($registrant_entity_type, $registrant, $form, $form_state);
    }
    $registrant_wrapper = entity_metadata_wrapper($registrant_entity_type, $registrant);
    $registrant_wrapper
      ->save();
  }

  // Set registrant_id if possible. Otherwise, store just the email.
  if ($registrant_wrapper) {
    $registration->registrant_id = $registrant_wrapper
      ->getIdentifier();
  }
  else {
    $registration->anon_mail = $registrant_mail;
  }

  // Notify field widgets.
  field_attach_submit('registration', $registration, $form, $form_state);

  // Save the registration and redirect.
  if (registration_save($registration)) {
    $reg_config = registration_entity_settings($registration->entity_type, $registration->entity_id);
    if (!empty($reg_config['settings']['confirmation']) && strlen($reg_config['settings']['confirmation'])) {
      drupal_set_message(t($reg_config['settings']['confirmation']));
    }
    $wrapper = entity_metadata_wrapper('registration', $registration);
    $host = $wrapper->entity
      ->value();

    // Set redirect to configured value, if there is one.
    if (!empty($reg_config['settings']['confirmation_redirect']) && strlen($reg_config['settings']['confirmation_redirect'])) {
      if (module_exists('token')) {
        $form_state['redirect'] = token_replace($reg_config['settings']['confirmation_redirect'], array(
          $host->type => $host,
          'registration' => $registration,
        ));
      }
      else {
        $form_state['redirect'] = $reg_config['settings']['confirmation_redirect'];
      }
    }
    else {

      // Redirect to registration:
      if (entity_access('view', 'registration', $registration)) {
        $uri = entity_uri('registration', $registration);
        $form_state['redirect'] = $uri['path'];
      }
      else {
        if (entity_access('view', $registration->entity_type, $host)) {
          $uri = entity_uri($registration->entity_type, $host);
          $form_state['redirect'] = $uri['path'];
        }
      }
    }

    // Make sure to Start a Session and save the registration info so we
    // can check it for anonymous user access.
    drupal_session_start();
    $_SESSION['registration_ids'][$registration
      ->identifier()] = $wrapper->anon_access_hash
      ->value();
  }
  else {
    drupal_set_message(t('There was a problem submitting your registration.'));
  }
}