View source
<?php
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') {
global $user;
if ($user->uid == 0) {
$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;
$form['register']['form']['submit'] = NULL;
$form['register']['form']['name']['#title'] = t('Choose a Username');
$form['#validate']['inline_registration_validate'] = array();
$form['#submit']['inline_registration_submit'] = array();
$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."),
);
}
}
function inline_registration_validate($form_id, $form_values) {
user_module_invoke('validate', $form_values, $form_values, 'account');
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'],
)));
}
}
function inline_registration_submit($form_id, &$form_values) {
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']);
}
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'));
$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) {
$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) {
$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 {
$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 '';
}
}
}