You are here

inline_registration.module in Inline Registration 5

File

inline_registration.module
View source
<?php

/**
 * @file
 *
 */

/**
 * Implementation of hook_form_alter().
 */
function inline_registration_form_alter($form_id, &$form) {
  if ($form['type']['#value'] . '_node_form' == $form_id && variable_get('inline_registration_' . $form['#node']->type, 0) && arg(1) == 'add') {

    // This is a node/add form
    global $user;
    if ($user->uid == 0) {

      // User is not logged in
      // Add the user registration form to the node/add/page
      $form['register'] = array(
        '#type' => 'fieldset',
        '#title' => t('Login or Register as a New User'),
        '#description' => t('You are not currently logged in. In order to post this item please !login or provide the following details to register.', array(
          '!login' => l(t('login now'), 'user/login', NULL, 'destination=' . $_GET['q']),
        )),
        '#weight' => variable_get('inline_registration_weight_' . $form['#node']->type, 0),
      );
      $user_register_form = user_register();
      foreach (module_implements('form_alter') as $module) {
        $function = $module . '_form_alter';
        $function('user_register', $user_register_form);
      }
      $form['register']['form'] = $user_register_form;

      // Remove the user_register submit button in favor of the node submit button
      $form['register']['form']['submit'] = NULL;

      // Rename the user field to remind the user that this is the registration form and not a login field
      $form['register']['form']['name']['#title'] = t('Choose a Username');

      // Add our own validation and submit function to the node_form
      $form['#validate']['inline_registration_validate'] = array();
      $form['#submit']['inline_registration_submit'] = array();

      // And ensure our submit function is called first (so the node is authored by the newly created user)
      $form['#submit'] = array_reverse($form['#submit']);
    }
  }
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $form['inline_registration'] = array(
      '#type' => 'fieldset',
      '#title' => t('Registration inline'),
      '#description' => t('Setting for publishing this content from anonymous user, and automatically create account for this.'),
      '#weight' => 20,
      '#collapsible' => TRUE,
      '#collapsed' => variable_get('inline_registration_' . $form['#node_type']->type, 0) ? FALSE : TRUE,
    );
    $form['inline_registration']['inline_registration'] = array(
      '#type' => 'checkbox',
      '#title' => t('Registration inline'),
      '#default_value' => variable_get('inline_registration_' . $form['#node_type']->type, 0),
      '#description' => t('Enable user creation from this content.'),
    );
    $form['inline_registration']['inline_registration_weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight of field'),
      '#default_value' => variable_get('inline_registration_weight_' . $form['#node_type']->type, -10),
      '#description' => t("Select weight for this field into content creation form."),
    );
  }
}

/**
 * Validation routine for inline registration form.
 */
function inline_registration_validate($form_id, $form_values) {

  // Validate using user module's validation routine
  user_module_invoke('validate', $form_values, $form_values, 'account');

  // Why doesn't user_module_invoke('validate') check if the username is taken? We'll do that now.
  if (db_fetch_object(db_query("SELECT uid FROM {users} WHERE name = '%s'", $form_values['name']))) {
    form_set_error('name', t('The name %name is already taken.', array(
      '%name' => $form_values['name'],
    )));
  }
}

/**
 * Submit routine for inline registration form.
 */
function inline_registration_submit($form_id, &$form_values) {

  // Most of this was ripped from user_register_submit().
  // Not sure why we couldn't just pass the $form_values into user_register_submit(),
  // but it didn't work well for me so I ripped off the code. Suggestions welcome.
  global $base_url;
  $mail = $form_values['mail'];
  $name = $form_values['name'];
  if (!variable_get('user_email_verification', TRUE)) {
    $pass = $form_values['pass'];
  }
  else {
    $pass = user_password();
  }
  $notify = FALSE;
  $from = variable_get('site_mail', ini_get('sendmail_from'));
  if (isset($form_values['roles'])) {
    $roles = array_filter($form_values['roles']);

    // Remove unset roles
  }

  //the unset below is needed to prevent these form values from being saved as user data
  unset($form_values['form_token'], $form_values['submit'], $form_values['op'], $form_values['notify'], $form_values['form_id'], $form_values['affiliates'], $form_values['destination']);
  $merge_data = array(
    'pass' => $pass,
    'init' => $mail,
    'roles' => $roles,
  );
  $account = user_save('', array_merge($form_values, $merge_data));
  watchdog('user', t('New user: %name %email.', array(
    '%name' => $name,
    '%email' => '<' . $mail . '>',
  )), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));

  // Set the node uid and name to the newly created user so node will be properly attributed to the new user when saved
  $form_values['name'] = $account->name;
  $form_values['uid'] = $account->uid;
  $variables = array(
    '!username' => $name,
    '!site' => variable_get('site_name', 'Drupal'),
    '!password' => $pass,
    '!uri' => $base_url,
    '!uri_brief' => substr($base_url, strlen('http://')),
    '!mailto' => $mail,
    '!date' => format_date(time()),
    '!login_uri' => url('user', NULL, NULL, TRUE),
    '!edit_uri' => url('user/' . $account->uid . '/edit', NULL, NULL, TRUE),
    '!login_url' => user_pass_reset_url($account),
  );
  if (!variable_get('user_email_verification', TRUE) && $account->status) {

    // No e-mail verification is required, create new user account, and login user immediately.
    $subject = _user_mail_text('welcome_subject', $variables);
    $body = _user_mail_text('welcome_body', $variables);
    drupal_mail('user-register-welcome', $mail, $subject, $body, $from);
    user_authenticate($account->name, trim($pass));
    $edit = array();
    user_module_invoke('login', $edit, $account);
    return '';
  }
  else {
    if ($account->status || $notify) {

      // Create new user account, no administrator approval required.
      $subject = $notify ? _user_mail_text('admin_subject', $variables) : _user_mail_text('welcome_subject', $variables);
      $body = $notify ? _user_mail_text('admin_body', $variables) : _user_mail_text('welcome_body', $variables);
      drupal_mail($notify ? 'user-register-notify' : 'user-register-welcome', $mail, $subject, $body, $from);
      if ($notify) {
        drupal_set_message(t('Password and further instructions have been e-mailed to the new user %user.', array(
          '%user' => $name,
        )));
      }
      else {
        drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
        return '';
      }
    }
    else {

      // Create new user account, administrator approval required.
      $subject = _user_mail_text('approval_subject', $variables);
      $body = _user_mail_text('approval_body', $variables);
      drupal_mail('user-register-approval-user', $mail, $subject, $body, $from);
      drupal_mail('user-register-approval-admin', $from, $subject, t("!username has applied for an account.\n\n!edit_uri", $variables), $from);
      drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, your password and further instructions have been sent to your e-mail address.'));
      return '';
    }
  }
}

Functions

Namesort descending Description
inline_registration_form_alter Implementation of hook_form_alter().
inline_registration_submit Submit routine for inline registration form.
inline_registration_validate Validation routine for inline registration form.