| 1 |
<?php
|
| 2 |
// $Id: signup_form.inc,v 1.1 2008/11/15 02:03:50 dww Exp $
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Code for the form when users sign up.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Build the user signup form.
|
| 12 |
*
|
| 13 |
* @param $node
|
| 14 |
* The fully loaded node object.
|
| 15 |
* @param $signup_type
|
| 16 |
* Determines what kind of signup to generate a form for. Possible values:
|
| 17 |
* 'auth' -- regular authenticated user signup form
|
| 18 |
* 'anon' -- anonymous user signup form (includes required email field).
|
| 19 |
* 'admin' -- admin form to signup another user (includes user selector).
|
| 20 |
* @param $fieldset
|
| 21 |
* Boolean that indicates if the signup form should be in a fieldset.
|
| 22 |
*/
|
| 23 |
function signup_form(&$form_state, $node, $signup_type = 'auth', $fieldset = TRUE) {
|
| 24 |
global $user;
|
| 25 |
|
| 26 |
$form = array();
|
| 27 |
$form['nid'] = array('#type' => 'value', '#value' => $node->nid);
|
| 28 |
$form['uid'] = array('#type' => 'value', '#value' => $user->uid);
|
| 29 |
|
| 30 |
if ($fieldset) {
|
| 31 |
$form['collapse'] = array(
|
| 32 |
'#type' => 'fieldset',
|
| 33 |
'#collapsible' => TRUE,
|
| 34 |
'#collapsed' => variable_get('signup_fieldset_collapsed', 1),
|
| 35 |
);
|
| 36 |
if ($signup_type == 'admin') {
|
| 37 |
$form['collapse']['#title'] = t('Sign up another user');
|
| 38 |
// We always want this fieldset expanded on the node/N/signups tab.
|
| 39 |
$form['collapse']['#collapsed'] = FALSE;
|
| 40 |
}
|
| 41 |
else {
|
| 42 |
$form['collapse']['#title'] = t('Sign up for @title', array('@title' => $node->title));
|
| 43 |
}
|
| 44 |
}
|
| 45 |
else {
|
| 46 |
$form['collapse'] = array();
|
| 47 |
}
|
| 48 |
|
| 49 |
$signup_form = array();
|
| 50 |
if ($signup_type == 'anon') {
|
| 51 |
$anon_form = array();
|
| 52 |
$anon_form['signup_anon_mail'] = array(
|
| 53 |
'#type' => 'textfield',
|
| 54 |
'#title' => t('Email'),
|
| 55 |
'#description' => t('An e-mail address is required for users who are not registered at this site. If you are a registered user at this site, please !login to sign up for this %node_type.', array('!login' => l(t('login'), 'user/login', array('query' => drupal_get_destination())), '%node_type' => node_get_types('name', $node->type))),
|
| 56 |
'#size' => 40,
|
| 57 |
'#maxlength' => 255,
|
| 58 |
'#required' => TRUE,
|
| 59 |
);
|
| 60 |
$validate_handler = 'signup_form_validate_anon';
|
| 61 |
$signup_form += $anon_form;
|
| 62 |
}
|
| 63 |
elseif ($signup_type == 'admin') {
|
| 64 |
$admin_form = array();
|
| 65 |
$admin_form['signup_username'] = array(
|
| 66 |
'#title' => t('Username'),
|
| 67 |
'#type' => 'textfield',
|
| 68 |
'#autocomplete_path' => 'user/autocomplete',
|
| 69 |
'#maxlength' => USERNAME_MAX_LENGTH,
|
| 70 |
'#size' => 40,
|
| 71 |
'#weight' => -1,
|
| 72 |
'#required' => TRUE,
|
| 73 |
);
|
| 74 |
$validate_handler = 'signup_form_validate_username';
|
| 75 |
$signup_form += $admin_form;
|
| 76 |
}
|
| 77 |
|
| 78 |
// Build the themed signup form for this site and include that.
|
| 79 |
$signup_themed_form = theme('signup_user_form', $node);
|
| 80 |
|
| 81 |
if ($signup_type == 'admin') {
|
| 82 |
// Special case hack for the default signup form, where the current
|
| 83 |
// username is being filled in as the default for the 'Name' field.
|
| 84 |
if (!empty($signup_themed_form['signup_form_data']['Name']['#default_value'])) {
|
| 85 |
unset($signup_themed_form['signup_form_data']['Name']['#default_value']);
|
| 86 |
}
|
| 87 |
}
|
| 88 |
$signup_form += $signup_themed_form;
|
| 89 |
|
| 90 |
$form['collapse']['signup_user_form'] = $signup_form;
|
| 91 |
$form['collapse']['submit'] = array(
|
| 92 |
'#type' => 'submit',
|
| 93 |
'#value' => t('Sign up'),
|
| 94 |
);
|
| 95 |
if (!empty($validate_handler)) {
|
| 96 |
$form['#validate'][] = $validate_handler;
|
| 97 |
}
|
| 98 |
return $form;
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Submit handler for the user signup form.
|
| 103 |
*
|
| 104 |
* @param $form
|
| 105 |
* The form being submitted.
|
| 106 |
* @param $form_values
|
| 107 |
* The state of the form, including the submitted values.
|
| 108 |
*/
|
| 109 |
function signup_form_submit($form, &$form_state) {
|
| 110 |
if (isset($form_state['values']['signup_username'])) {
|
| 111 |
$account = user_load(array('name' => $form_state['values']['signup_username']));
|
| 112 |
$form_state['values']['uid'] = $account->uid;
|
| 113 |
}
|
| 114 |
signup_sign_up_user($form_state['values']);
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
* Validate handler for the email address on the anonymous user signup form.
|
| 119 |
*
|
| 120 |
* @param $form
|
| 121 |
* Form array for the anonymous user email field.
|
| 122 |
* @param $form_state
|
| 123 |
* State of the form, including the submitted values to validate.
|
| 124 |
*/
|
| 125 |
function signup_form_validate_anon($form, $form_state) {
|
| 126 |
$nid = $form_state['values']['nid'];
|
| 127 |
$anon_mail = $form_state['values']['signup_anon_mail'];
|
| 128 |
signup_validate_anon_email($nid, $anon_mail, 'signup_anon_mail');
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
* Validate the email address for an anonymous signup.
|
| 133 |
*
|
| 134 |
* @param $nid
|
| 135 |
* The node the user is signing up for.
|
| 136 |
* @param $anon_mail
|
| 137 |
* The anonymous email address to validate.
|
| 138 |
* @param $name
|
| 139 |
* The form element being validated (optional).
|
| 140 |
*
|
| 141 |
* @return Boolean.
|
| 142 |
* TRUE if the address validates, FALSE otherwise.
|
| 143 |
*/
|
| 144 |
function signup_validate_anon_email($nid, $anon_mail, $name = FALSE) {
|
| 145 |
if (!valid_email_address($anon_mail)) {
|
| 146 |
$message = t('Invalid email address entered for signup.');
|
| 147 |
}
|
| 148 |
elseif (db_result(db_query("SELECT COUNT(*) FROM {users} WHERE mail = '%s'", $anon_mail))) {
|
| 149 |
$message = t('The email address entered belongs to a registered user.');
|
| 150 |
}
|
| 151 |
elseif (db_result(db_query("SELECT COUNT(*) FROM {signup_log} WHERE anon_mail = '%s' AND nid = %d", $anon_mail, $nid))) {
|
| 152 |
$node = node_load($nid);
|
| 153 |
$message = t('The email address entered has already been used to sign up for this %node_type.', array('%node_type' => node_get_types('name', $node->type)));
|
| 154 |
}
|
| 155 |
|
| 156 |
// If there's no message, it's a valid email, so return success.
|
| 157 |
if (!isset($message)) {
|
| 158 |
return TRUE;
|
| 159 |
}
|
| 160 |
|
| 161 |
// Depending on how we were called, propagate the error accordinly.
|
| 162 |
if ($name) {
|
| 163 |
form_set_error($name, $message);
|
| 164 |
}
|
| 165 |
else {
|
| 166 |
drupal_set_message($message, 'error');
|
| 167 |
}
|
| 168 |
return FALSE;
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Validates the username on the admin form to signup another user.
|
| 173 |
*
|
| 174 |
* @param $form
|
| 175 |
* Form array for the username field.
|
| 176 |
* @param $nid
|
| 177 |
* Node id of the node the user is being signed up for.
|
| 178 |
*/
|
| 179 |
function signup_form_validate_username($form, $form_state) {
|
| 180 |
$nid = $form_state['values']['nid'];
|
| 181 |
$username = $form_state['values']['signup_username'];
|
| 182 |
$account = user_load(array('name' => $username));
|
| 183 |
if (empty($account)) {
|
| 184 |
form_set_error('signup_username', t('User %user_name does not exist.', array('%user_name' => $username)));
|
| 185 |
}
|
| 186 |
elseif (!user_access('sign up for content', $account)) {
|
| 187 |
form_set_error('signup_username', t('User !user does not have permission to sign up.', array('!user' => theme('username', $account))));
|
| 188 |
}
|
| 189 |
elseif (db_result(db_query("SELECT COUNT(*) FROM {signup_log} WHERE uid = %d AND nid = %d", $account->uid, $nid)) > 0) {
|
| 190 |
$node = node_load($nid);
|
| 191 |
form_set_error('signup_username', t('User !user is already signed up for %title', array('!user' => theme('username', $account), '%title' => $node->title)));
|
| 192 |
}
|
| 193 |
}
|
| 194 |
|
| 195 |
function signup_node_admin_add_user_page($node) {
|
| 196 |
$output = '';
|
| 197 |
if ($node->signup_status) {
|
| 198 |
$output = drupal_get_form('signup_form', $node, 'admin', FALSE);
|
| 199 |
}
|
| 200 |
return $output;
|
| 201 |
}
|
| 202 |
|