| 1 |
<?php |
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* @file |
| 6 |
|
* |
| 7 |
|
*/ |
| 8 |
|
|
| 9 |
/** |
/** |
| 10 |
* Implementation of hook_form_alter(). |
* Implementation of hook_form_alter(). |
| 11 |
*/ |
*/ |
| 12 |
function inline_registration_form_alter($form_id, &$form) { |
function inline_registration_form_alter(&$form, $form_state, $form_id) { |
| 13 |
if ($form['type']['#value'] . '_node_form' == $form_id && arg(1) == 'add') { // This is a node/add form |
global $user; |
|
global $user; |
|
|
if ($user->uid == 0) { // User is not logged in |
|
| 14 |
|
|
| 15 |
|
if ($user->uid == 0) { // User is not logged in |
| 16 |
|
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && arg(1) == 'add') { |
| 17 |
// Add the user registration form to the node/add/page |
// Add the user registration form to the node/add/page |
| 18 |
$form['register'] = array( |
$form['register'] = array( |
| 19 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 20 |
'#title' => t('Login or Register as a New User'), |
'#title' => t('Login or Register as a New User'), |
| 21 |
'#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']))), |
'#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', array('destination='. $_GET['q'])))), |
| 22 |
'#weight' => -99, |
'#weight' => -99, |
| 23 |
); |
); |
| 24 |
$form['register']['form'] = user_register(); |
$form['register']['form'] = user_register(); |
| 30 |
$form['register']['form']['name']['#title'] = t('Choose a Username'); |
$form['register']['form']['name']['#title'] = t('Choose a Username'); |
| 31 |
|
|
| 32 |
// Add our own validation and submit function to the node_form |
// Add our own validation and submit function to the node_form |
| 33 |
$form['#validate']['inline_registration_validate'] = array(); |
$form['#validate'][] = 'inline_registration_validate'; |
| 34 |
$form['#submit']['inline_registration_submit'] = array(); |
$form['#submit'][] = 'inline_registration_submit'; |
| 35 |
|
|
| 36 |
// And ensure our submit function is called first (so the node is authored by the newly created user) |
// And ensure our submit function is called first (so the node is authored by the newly created user) |
| 37 |
$form['#submit'] = array_reverse($form['#submit']); |
$form['#submit'] = array_reverse($form['#submit']); |
| 38 |
} |
} |
| 42 |
/** |
/** |
| 43 |
* Validation routine for inline registration form. |
* Validation routine for inline registration form. |
| 44 |
*/ |
*/ |
| 45 |
function inline_registration_validate($form_id, $form_values) { |
function inline_registration_validate($form, &$form_state) { |
| 46 |
// Validate using user module's validation routine |
// Validate using user module's validation routine |
| 47 |
user_module_invoke('validate', $form_values, $form_values, 'account'); |
unset($form_state['uid']); |
| 48 |
|
user_module_invoke('validate', $form_state['values'], $form_state['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']))); |
|
|
} |
|
| 49 |
} |
} |
| 50 |
|
|
| 51 |
/** |
/** |
| 52 |
* Submit routine for inline registration form. |
* Submit routine for inline registration form. |
| 53 |
*/ |
*/ |
| 54 |
function inline_registration_submit($form_id, &$form_values) { |
function inline_registration_submit($form, &$form_state) { |
|
// 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']); |
|
| 55 |
|
|
| 56 |
$merge_data = array('pass' => $pass, 'init' => $mail, 'roles' => $roles); |
unset($form_state['values']['uid']); |
| 57 |
|
unset($form_state['values']['status']); |
| 58 |
|
|
| 59 |
|
user_register_submit($form, $form_state); |
| 60 |
|
|
| 61 |
$account = user_save('', array_merge($form_values, $merge_data)); |
$form_state['values']['name'] = $form_state['user']->name; |
| 62 |
watchdog('user', t('New user: %name %email.', array('%name' => $name, '%email' => '<'. $mail .'>')), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit')); |
$form_state['values']['uid'] = $form_state['user']->uid; |
|
|
|
|
// 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 ''; |
|
|
} |
|
| 63 |
} |
} |