| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_form_alter().
|
| 5 |
*/
|
| 6 |
function inline_registration_form_alter($form_id, &$form) {
|
| 7 |
if ($form['type']['#value'] . '_node_form' == $form_id && arg(1) == 'add') { // This is a node/add form
|
| 8 |
global $user;
|
| 9 |
if ($user->uid == 0) { // User is not logged in
|
| 10 |
|
| 11 |
// Add the user registration form to the node/add/page
|
| 12 |
$form['register'] = array(
|
| 13 |
'#type' => 'fieldset',
|
| 14 |
'#title' => t('Login or Register as a New User'),
|
| 15 |
'#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']))),
|
| 16 |
'#weight' => -99,
|
| 17 |
);
|
| 18 |
$form['register']['form'] = user_register();
|
| 19 |
|
| 20 |
// Remove the user_register submit button in favor of the node submit button
|
| 21 |
$form['register']['form']['submit'] = NULL;
|
| 22 |
|
| 23 |
// Rename the user field to remind the user that this is the registration form and not a login field
|
| 24 |
$form['register']['form']['name']['#title'] = t('Choose a Username');
|
| 25 |
|
| 26 |
// Add our own validation and submit function to the node_form
|
| 27 |
$form['#validate']['inline_registration_validate'] = array();
|
| 28 |
$form['#submit']['inline_registration_submit'] = array();
|
| 29 |
|
| 30 |
// And ensure our submit function is called first (so the node is authored by the newly created user)
|
| 31 |
$form['#submit'] = array_reverse($form['#submit']);
|
| 32 |
}
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
/**
|
| 37 |
* Validation routine for inline registration form.
|
| 38 |
*/
|
| 39 |
function inline_registration_validate($form_id, $form_values) {
|
| 40 |
// Validate using user module's validation routine
|
| 41 |
user_module_invoke('validate', $form_values, $form_values, 'account');
|
| 42 |
|
| 43 |
// Why doesn't user_module_invoke('validate') check if the username is taken? We'll do that now.
|
| 44 |
if (db_fetch_object(db_query("SELECT uid FROM {users} WHERE name = '%s'", $form_values['name']))) {
|
| 45 |
form_set_error('name', t('The name %name is already taken.', array('%name' => $form_values['name'])));
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Submit routine for inline registration form.
|
| 51 |
*/
|
| 52 |
function inline_registration_submit($form_id, &$form_values) {
|
| 53 |
// Most of this was ripped from user_register_submit().
|
| 54 |
// Not sure why we couldn't just pass the $form_values into user_register_submit(),
|
| 55 |
// but it didn't work well for me so I ripped off the code. Suggestions welcome.
|
| 56 |
|
| 57 |
global $base_url;
|
| 58 |
|
| 59 |
$mail = $form_values['mail'];
|
| 60 |
$name = $form_values['name'];
|
| 61 |
if (!variable_get('user_email_verification', TRUE)) {
|
| 62 |
$pass = $form_values['pass'];
|
| 63 |
}
|
| 64 |
else {
|
| 65 |
$pass = user_password();
|
| 66 |
};
|
| 67 |
$notify = FALSE;
|
| 68 |
|
| 69 |
$from = variable_get('site_mail', ini_get('sendmail_from'));
|
| 70 |
if (isset($form_values['roles'])) {
|
| 71 |
$roles = array_filter($form_values['roles']); // Remove unset roles
|
| 72 |
}
|
| 73 |
|
| 74 |
//the unset below is needed to prevent these form values from being saved as user data
|
| 75 |
unset($form_values['form_token'], $form_values['submit'], $form_values['op'], $form_values['notify'], $form_values['form_id'], $form_values['affiliates'], $form_values['destination']);
|
| 76 |
|
| 77 |
$merge_data = array('pass' => $pass, 'init' => $mail, 'roles' => $roles);
|
| 78 |
|
| 79 |
$account = user_save('', array_merge($form_values, $merge_data));
|
| 80 |
watchdog('user', t('New user: %name %email.', array('%name' => $name, '%email' => '<'. $mail .'>')), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit'));
|
| 81 |
|
| 82 |
// Set the node uid and name to the newly created user so node will be properly attributed to the new user when saved
|
| 83 |
$form_values['name'] = $account->name;
|
| 84 |
$form_values['uid'] = $account->uid;
|
| 85 |
|
| 86 |
$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));
|
| 87 |
|
| 88 |
if (!variable_get('user_email_verification', TRUE) && $account->status) {
|
| 89 |
// No e-mail verification is required, create new user account, and login user immediately.
|
| 90 |
$subject = _user_mail_text('welcome_subject', $variables);
|
| 91 |
$body = _user_mail_text('welcome_body', $variables);
|
| 92 |
drupal_mail('user-register-welcome', $mail, $subject, $body, $from);
|
| 93 |
user_authenticate($account->name, trim($pass));
|
| 94 |
$edit = array();
|
| 95 |
user_module_invoke('login', $edit, $account);
|
| 96 |
return '';
|
| 97 |
}
|
| 98 |
else if ($account->status || $notify) {
|
| 99 |
// Create new user account, no administrator approval required.
|
| 100 |
$subject = $notify ? _user_mail_text('admin_subject', $variables) : _user_mail_text('welcome_subject', $variables);
|
| 101 |
$body = $notify ? _user_mail_text('admin_body', $variables) : _user_mail_text('welcome_body', $variables);
|
| 102 |
|
| 103 |
drupal_mail(($notify ? 'user-register-notify' : 'user-register-welcome'), $mail, $subject, $body, $from);
|
| 104 |
|
| 105 |
if ($notify) {
|
| 106 |
drupal_set_message(t('Password and further instructions have been e-mailed to the new user %user.', array('%user' => $name)));
|
| 107 |
}
|
| 108 |
else {
|
| 109 |
drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
|
| 110 |
return '';
|
| 111 |
}
|
| 112 |
}
|
| 113 |
else {
|
| 114 |
// Create new user account, administrator approval required.
|
| 115 |
$subject = _user_mail_text('approval_subject', $variables);
|
| 116 |
$body = _user_mail_text('approval_body', $variables);
|
| 117 |
|
| 118 |
drupal_mail('user-register-approval-user', $mail, $subject, $body, $from);
|
| 119 |
drupal_mail('user-register-approval-admin', $from, $subject, t("!username has applied for an account.\n\n!edit_uri", $variables), $from);
|
| 120 |
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.'));
|
| 121 |
return '';
|
| 122 |
}
|
| 123 |
}
|