You are here

function logintoboggan_user_register_submit in LoginToboggan 7

Same name and namespace in other branches
  1. 5 logintoboggan.module \logintoboggan_user_register_submit()
  2. 6 logintoboggan.module \logintoboggan_user_register_submit()

Custom submit function for user registration form

1 string reference to 'logintoboggan_user_register_submit'
logintoboggan_form_user_register_form_alter in ./logintoboggan.module
Implement hook_form_user_register_form_alter().

File

./logintoboggan.module, line 430
LoginToboggan module

Code

function logintoboggan_user_register_submit($form, &$form_state) {
  $reg_pass_set = !variable_get('user_email_verification', TRUE);

  // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  // handle things a bit differently.
  $pre_auth = logintoboggan_validating_id() != DRUPAL_AUTHENTICATED_RID;

  // If we are allowing user selected passwords then skip the auto-generate function
  // The new user's status will be 1 (visitors can create own accounts) if reg_pass_set == 1
  // Immediate login, we are going to assign a pre-auth role, until email validation completed
  if ($reg_pass_set) {
    $pass = $form_state['values']['pass'];

    // Let's leave the user submitted value.
    $status = $form_state['values']['status'];
  }
  else {
    $pass = user_password();
    $status = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS;
  }

  // The unset below is needed to prevent these form values from being saved as
  // user data.
  form_state_values_clean($form_state);

  // Set the roles for the new user -- add the pre-auth role if they can pick their own password,
  // and the pre-auth role isn't anon or auth user.
  $validating_id = logintoboggan_validating_id();
  $roles = isset($form_state['values']['roles']) ? array_filter($form_state['values']['roles']) : array();
  if ($reg_pass_set && $validating_id > DRUPAL_AUTHENTICATED_RID) {
    $roles[$validating_id] = 1;
  }
  $form_state['values']['pass'] = $pass;
  $form_state['values']['init'] = $form_state['values']['mail'];
  $form_state['values']['roles'] = $roles;
  $form_state['values']['status'] = $status;
  $account = $form['#user'];
  entity_form_submit_build_entity('user', $account, $form, $form_state);

  // Populate $edit with the properties of $account, which have been edited on
  // this form by taking over all values, which appear in the form values too.
  $edit = array_intersect_key((array) $account, $form_state['values']);
  $account = user_save($account, $edit);

  // Terminate if an error occurred during user_save().
  if (!$account) {
    drupal_set_message(t("Error saving user account."), 'error');
    $form_state['redirect'] = '';
    return;
  }
  $form_state['user'] = $account;
  $form_state['values']['uid'] = $account->uid;
  watchdog('user', 'New user: %name (%email).', array(
    '%name' => $form_state['values']['name'],
    '%email' => $form_state['values']['mail'],
  ), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));

  // Add plain text password into user account to generate mail tokens.
  $account->password = $pass;

  // Compose the appropriate user message. Validation emails are only sent if:
  //   1. Users can set their own password.
  //   2. The pre-auth role isn't the auth user.
  //   3. Visitors can create their own accounts.
  $message = t('Further instructions have been sent to your e-mail address.');
  if ($reg_pass_set && $pre_auth && variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
    $message = t('A validation e-mail has been sent to your e-mail address. You will need to follow the instructions in that message in order to gain full access to the site.');
  }
  if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {

    // Create new user account, no administrator approval required.
    $mailkey = 'register_no_approval_required';
  }
  elseif (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {

    // Create new user account, administrator approval required.
    $mailkey = 'register_pending_approval';
    $message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />Once it has been approved, you will receive an e-mail containing further instructions.');
  }

  // Mail the user.
  _user_mail_notify($mailkey, $account);
  drupal_set_message($message);

  // where do we need to redirect after registration?
  $redirect = _logintoboggan_process_redirect(variable_get('logintoboggan_redirect_on_register', ''), $account);

  // Log the user in if they created the account and immediate login is enabled.
  if ($reg_pass_set && variable_get('logintoboggan_immediate_login_on_register', TRUE)) {
    $form_state['redirect'] = logintoboggan_process_login($account, $form_state['values'], $redirect);
  }
  else {

    // Redirect to the appropriate page.
    $form_state['redirect'] = $redirect;
  }
}