You are here

function node_registration_form_submit in Node registration 7

Submit handler for node_registration_form().

1 string reference to 'node_registration_form_submit'
node_registration_form in includes/node_registration.forms.inc
The registration form. It takes a (mandatory) existing or empty registration object.

File

includes/node_registration.forms.inc, line 1008
New registration forms. Public and admin.

Code

function node_registration_form_submit($form, &$form_state) {
  global $user;
  $account = $form['#account'];
  $registration = $form['#registration'];
  $node = $form['#node'];
  $settings = $node->registration;
  $can_register_others = node_registration_node_access($node, 'register others');
  $values =& $form_state['values'];

  // Change user context for admins.
  if ($can_register_others && isset($form['account'])) {
    $account = '' == $values['account'] ? drupal_anonymous_user() : user_load_by_name($values['account']);
  }

  // E-mail not editable.
  if (isset($form['#fixed_email'])) {
    $values['email'] = $form['#fixed_email'];
  }

  // Used values / flags.
  $email = $values['email'];
  $is_new = (bool) $registration->is_new;

  // {node_registration} values.
  $registration->email = $email;
  $registration->slots = (int) $values['slots'];
  isset($values['attended']) && ($registration->attended = (int) $values['attended']);

  // Only set for new registration.
  if ($is_new) {
    $registration->uid = $account->uid;
    $registration->author_uid = $user->uid;
    $registration->secret = _node_registration_secret();

    // Auto verify for authenticated users and admins.
    if ($account->uid || $can_register_others) {
      $registration->verified = 1;
    }
  }

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

  // Alter registration object.
  $context = array(
    'form' => $form,
    'form_state' => $form_state,
    'node' => $node,
    'user' => $account,
  );
  $errors = array();
  drupal_alter('node_registration_submit', $registration, $context, $errors);

  // Very last pre-flight check to make sure this is a unique e-mail address.
  if ($is_new) {
    $exists = db_query('SELECT registration_id FROM {node_registration} WHERE nid = :nid AND cancelled = :cancelled AND email = :email', array(
      ':nid' => $node->nid,
      ':cancelled' => 0,
      ':email' => $email,
    ))
      ->fetchField();
    if ($exists) {
      $form_state['rebuild'] = TRUE;
      return form_set_error('email', t('%email is already registered for this event.', array(
        '%email' => $email,
      )));
    }
  }

  // Save the registration.
  if (node_registration_save($registration)) {
    $uri = node_registration_uri($registration);
    if ($is_new) {
      drupal_set_message(t($settings->registration_success_message, array(
        '@registration_url' => url($uri['path']),
      )));

      // Token param.
      $registration->node = $node;

      // Waitinglist? then re-fetch object.
      $waitinglist = FALSE;
      if (_node_registration_reset_waitinglist($node)) {
        $registration = node_registration_load($registration->registration_id, TRUE);
        $waitinglist = TRUE;
      }
      $token_data = array(
        'node' => $node,
        'node-registration' => $registration,
      );

      // Send e-mail to new registree.
      if ($settings->send_mail_to_registree) {
        $subject = $waitinglist && $settings->change_mail_for_waiting_list_registree && $settings->mail_for_waiting_list_registree_subject ? $settings->mail_for_waiting_list_registree_subject : $settings->mail_to_registree_subject;
        $message = $waitinglist && $settings->change_mail_for_waiting_list_registree && $settings->mail_for_waiting_list_registree_body ? $settings->mail_for_waiting_list_registree_body : $settings->mail_to_registree_body;
        $options = array(
          'bcc' => token_replace($settings->mail_to_registree_bcc, $token_data, array(
            'clear' => TRUE,
          )),
          'alter' => 'new_registration',
        );
        node_registration_send_broadcast($node, $subject, $message, array(
          $registration,
        ), $options);
      }

      // Send notification e-mail to admin.
      if ($settings->send_mail_to_admin) {
        $subject = token_replace($settings->mail_to_admin_subject, $token_data);
        $message = token_replace($settings->mail_to_admin_body, $token_data);
        $recipients = token_replace($settings->mail_to_admin_recipients, $token_data);
        _node_registration_send_email($recipients, $subject, $message, $token_data);
      }
    }
    else {

      // Trigger: verify / unverify.
      $op = $registration->verified ? 'verify' : 'unverify';
      module_invoke_all('node_registration_' . $op, $registration);
      drupal_set_message(t('Your registration has been updated.'));
    }

    // Redirect to event node.
    $form_state['redirect'] = $is_new ? 'node/' . $registration->nid : $uri['path'];
  }
  else {
    drupal_set_message(t('Sorry, there has been a problem submitting your registration.'));
  }
}