realname_registration.admin.inc 16 KB
Newer Older
1 2 3
<?php
/**
 * @file
Steve Gerbino's avatar
Steve Gerbino committed
4
 * Admin page callbacks for the realname_registration module.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 */

/**
 * Form builder;
 *
 * @ingroup forms
 * @see system_settings_form()
 */
function realname_registration_settings_form() {
  $form = array();

17
  if (!module_exists('profile2')) {
18 19 20 21 22 23
    variable_del('realname_registration_use_profile2_firstname_field');
    variable_del('realname_registration_profile2_firstname_field_profile_name');
    variable_del('realname_registration_use_profile2_middlename_field');
    variable_del('realname_registration_profile2_middlename_field_profile_name');
    variable_del('realname_registration_use_profile2_lastname_field');
    variable_del('realname_registration_profile2_lastname_field_profile_name');
24 25
  }

26
  $field_req = '
Steve Gerbino's avatar
Steve Gerbino committed
27
<ul>
28
  <li>' . t('The field name is correct and represents an existing field') . '</li>
29
  <li>' . t('The field is associated with the') . ' <em>' . t('user or profile2') . '</em> ' . t('entity') . '</li>
30
  <li>' . t('The field is required and displays on the registration form') . '</li>
Steve Gerbino's avatar
Steve Gerbino committed
31
</ul>
32
';
Steve Gerbino's avatar
Steve Gerbino committed
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  $form['fields'] = array(
    '#type' => 'fieldset',
    '#title' => t('Realname registration fields'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['fields']['firstname'] = array(
    '#type' => 'fieldset',
    '#title' => t('First name field'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['fields']['firstname']['realname_registration_firstname_field'] = array(
Steve Gerbino's avatar
Steve Gerbino committed
49 50 51
    '#type' => 'textfield',
    '#title' => t('First name field'),
    '#default_value' => variable_get('realname_registration_firstname_field'),
52
    '#description' => t('The name of your first name field. Ensure that:') . $field_req,
53 54 55
    '#required' => TRUE,
  );

56
  if (module_exists('profile2')) {
57 58 59 60 61 62 63 64
    $form['fields']['firstname']['profile2_firstname'] = array(
      '#type' => 'fieldset',
      '#title' => t('Profile2 first name'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );

    $form['fields']['firstname']['profile2_firstname']['realname_registration_use_profile2_firstname_field'] = array(
65 66 67 68 69
        '#type' => 'checkbox',
        '#title' => t('Use the profile2 module to store your first names.'),
        '#default_value' => variable_get('realname_registration_use_profile2_firstname_field'),
        '#description' => t('When this checkbox is checked, Realname registration will use profile2 fields to store first names.'),
    );
70
    $form['fields']['firstname']['profile2_firstname']['realname_registration_profile2_firstname_field_profile_name'] = array(
71 72 73
      '#type' => 'textfield',
      '#title' => t('Profile2 type'),
      '#default_value' => variable_get('realname_registration_profile2_firstname_field_profile_name'),
74
      '#description' => t('Provide the profile2 machine name that will be used during registration.'),
75 76 77
    );
  }

78 79 80 81 82 83 84 85
  $form['fields']['middlename'] = array(
    '#type' => 'fieldset',
    '#title' => t('Middle name field'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['fields']['middlename']['realname_registration_middlename_field'] = array(
86 87 88 89
    '#type' => 'textfield',
    '#title' => t('Middle name field'),
    '#default_value' => variable_get('realname_registration_middlename_field'),
    '#description' => 
90
      t('The name of your middle name field. Ensure that:')
91 92 93 94
      . '<ul>'
      . '<li>' . t('The field name is correct and represents an existing field') . '</li>'
      . '<li>' . t('The field is associated with the') . ' <em>' . t('user') . '</em> ' . t('entity') . '</li>'
      . '</ul>',
Steve Gerbino's avatar
Steve Gerbino committed
95 96
  );

97
  if (module_exists('profile2')) {
98 99 100 101 102 103 104
    $form['fields']['middlename']['profile2_middlename'] = array(
      '#type' => 'fieldset',
      '#title' => t('Profile2 middle name'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['fields']['middlename']['profile2_middlename']['realname_registration_use_profile2_middlename_field'] = array(
105 106 107 108 109
        '#type' => 'checkbox',
        '#title' => t('Use the profile2 module to store your middle names.'),
        '#default_value' => variable_get('realname_registration_use_profile2_middlename_field'),
        '#description' => t('When this checkbox is checked, Realname registration will use profile2 fields to store middle names.'),
    );
110
    $form['fields']['middlename']['profile2_middlename']['realname_registration_profile2_middlename_field_profile_name'] = array(
111 112 113
      '#type' => 'textfield',
      '#title' => t('Profile2 type'),
      '#default_value' => variable_get('realname_registration_profile2_middlename_field_profile_name'),
114
      '#description' => t('Provide the profile2 machine name that will be used during registration.'),
115 116 117
    );
  }

118 119
  $form['fields']['lastname'] = array(
    '#type' => 'fieldset',
120
    '#title' => t('Last name field'),
121 122 123 124 125
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['fields']['lastname']['realname_registration_lastname_field'] = array(
Steve Gerbino's avatar
Steve Gerbino committed
126 127 128
    '#type' => 'textfield',
    '#title' => t('Last name field'),
    '#default_value' => variable_get('realname_registration_lastname_field'),
129
    '#description' => t('The name of your last name field. Ensure that:') . $field_req,
130
    '#required' => TRUE,
Steve Gerbino's avatar
Steve Gerbino committed
131 132
  );

133
  if (module_exists('profile2')) {
134 135 136 137 138 139 140 141
    $form['fields']['lastname']['profile2_lastname'] = array(
      '#type' => 'fieldset',
      '#title' => t('Profile2 last name'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );

    $form['fields']['lastname']['profile2_lastname']['realname_registration_use_profile2_lastname_field'] = array(
142 143 144 145 146
        '#type' => 'checkbox',
        '#title' => t('Use the profile2 module to store your last names.'),
        '#default_value' => variable_get('realname_registration_use_profile2_lastname_field'),
        '#description' => t('When this checkbox is checked, Realname registration will use profile2 fields to store last names.'),
    );
147
    $form['fields']['lastname']['profile2_lastname']['realname_registration_profile2_lastname_field_profile_name'] = array(
148 149 150
      '#type' => 'textfield',
      '#title' => t('Profile2 type'),
      '#default_value' => variable_get('realname_registration_profile2_lastname_field_profile_name'),
151
      '#description' => t('Provide the profile2 machine name that will be used during registration.'),
152 153 154
    );
  }

155 156
  $form['fields']['realname_registration_ucfirst'] = array(
    '#type' => 'checkbox',
157
    '#title' => t('Force the first letters of the first, middle, and last names to uppercase'),
158 159 160 161 162 163 164 165 166 167 168 169
    '#default_value' => variable_get('realname_registration_ucfirst', TRUE),
    '#description' => t('Ensures that the first letter of the users first name, middle name, and last name are capitalized.'),
  );

  $form['username_format'] = array(
    '#type' => 'fieldset',
    '#title' => t('Realname registration username creation'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['username_format']['realname_registration_format'] = array(
170 171 172
    '#type' => 'radios',
    '#title' => t('Username format'),
    '#description' => t('Select the format in which Realname registration will create new usernames.'),
Steve Gerbino's avatar
Steve Gerbino committed
173
    '#default_value' => variable_get('realname_registration_format', 0),
174
    '#options' => array(
175
      t('First name and last name (e.g., John Smith)'),
176 177
      t('First initial and last name (e.g., JSmith)'),
      t('First name, middle name, and last name (e.g., John Jacob Smith)'),
178
      t('First name, middle initial, and last name (e.g., John J Smith)'),
179 180 181 182
      t('First initial, middle initial, and last name (e.g., JJSmith)'),
      t('First name and last name without spaces (e.g., JohnSmith)'),
      t('First name, middle name, and last name without spaces (e.g., JohnJacobSmith)'),
      t('First name, middle initial, and last name without spaces (e.g., JohnJSmith)')
183
    ),
184 185 186
    '#required' => TRUE,
  );

187
  $form['username_format']['realname_registration_tolower'] = array(
188 189
    '#type' => 'checkbox',
    '#title' => t('Force lowercase'),
190
    '#default_value' => variable_get('realname_registration_tolower', TRUE),
191
    '#description' => t('Usernames will be created using only lowercase characters.'),
192 193
  );

194
  $form['username_format']['realname_registration_use_validation'] = array(
Steve Gerbino's avatar
Steve Gerbino committed
195 196
    '#type' => 'checkbox',
    '#title' => t('Use Realname registration validation'),
197
    '#default_value' => variable_get('realname_registration_use_validation', TRUE),
198
    '#description' => t('Use regex to validate real names.'),
Steve Gerbino's avatar
Steve Gerbino committed
199 200 201
  );

  $form['#validate'][] = 'realname_registration_settings_validate';
202 203
  return system_settings_form($form);
}
Steve Gerbino's avatar
Steve Gerbino committed
204 205

function realname_registration_settings_validate($form, &$form_state) {
206 207 208
  $err_pretext      = t('The field name') . ', <em>';
  $err_req          = '</em>, ' . t("you've provided must be required.");
  $err_unknown      = '</em>, ' . t("you've provided either does not exist or is not associated with the corresponding entity.");
209 210 211
  $err_textfield    = '</em>, ' . t("must use textfield widget.");
  $err_register     = '</em>, ' . t("must be displayed on the registration form.") . '<em>';
  $err_profile2_req = t("You must specify a profile2 machine name for the field,") . ' <em>';
212 213

  // If we are told to use profile2 fields to store values, we need to know which profile2 type.
214 215 216 217 218 219 220 221
  if (module_exists('profile2')) {
    if ($form_state['values']['realname_registration_use_profile2_firstname_field'] && empty($form_state['values']['realname_registration_profile2_firstname_field_profile_name'])) {
      form_set_error('realname_registration_profile2_firstname_field_profile_name', $err_profile2_req . $form_state['values']['realname_registration_firstname_field'] . '</em>');
    }
    if ($form_state['values']['realname_registration_use_profile2_middlename_field'] && empty($form_state['values']['realname_registration_profile2_middlename_field_profile_name'])) {
      form_set_error('realname_registration_profile2_middlename_field_profile_name', $err_profile2_req . $form_state['values']['realname_registration_middlename_field'] . '</em>');
    }
    if ($form_state['values']['realname_registration_use_profile2_lastname_field'] && empty($form_state['values']['realname_registration_profile2_lastname_field_profile_name'])) {
222
      form_set_error('realname_registration_profile2_lastname_field_profile_name', $err_profile2_req . $form_state['values']['realname_registration_firstname_field'] . '</em>');
223
    }
224
  }
225

226
  // Validate the first name field.
227
  if (module_exists('profile2') && $form_state['values']['realname_registration_use_profile2_firstname_field']) {
228 229 230
    $firstname_field = field_info_field($form_state['values']['realname_registration_firstname_field']);
    $match = FALSE;
    foreach (profile2_get_types() as $type_name => $profile_type) {
231
      if ($profile_type->data['registration'] && is_array($firstname_field['bundles']['profile2'])) {
232 233 234 235 236 237 238 239 240 241 242 243
        foreach ($firstname_field['bundles']['profile2'] as $key => $value) {
          if ($value == $profile_type->type) {
            $match = TRUE;
          }
        }
      }
    }
    if (!$match) {
      form_set_error('realname_registration_firstname_field', $err_pretext . $form_state['values']['realname_registration_firstname_field'] . $err_register );
    }
  }

244
  if (module_exists('profile2') && $form_state['values']['realname_registration_use_profile2_firstname_field']) {
245
    $firstname_instance = field_info_instance('profile2', $form_state['values']['realname_registration_firstname_field'], $form_state['values']['realname_registration_profile2_firstname_field_profile_name']);
246 247 248 249
  }
  else {
    $firstname_instance = field_info_instance('user', $form_state['values']['realname_registration_firstname_field'], 'user');
  }
250 251
  if (!isset($firstname_instance)) {
    form_set_error('realname_registration_firstname_field', $err_pretext . $form_state['values']['realname_registration_firstname_field'] . $err_unknown);
Steve Gerbino's avatar
Steve Gerbino committed
252
  }
253 254
  if ($firstname_instance['widget']['type'] != 'text_textfield') {
    form_set_error('realname_registration_firstname_field', $err_pretext . $form_state['values']['realname_registration_firstname_field'] . $err_textfield);
Steve Gerbino's avatar
Steve Gerbino committed
255
  }
256 257
  if ($firstname_instance['required'] != 1) {
    form_set_error('realname_registration_firstname_field', $err_pretext . $form_state['values']['realname_registration_firstname_field'] . $err_req);
258
  }
259 260

  // Validate the middle name field if it is used.
261
  if (module_exists('profile2') && $form_state['values']['realname_registration_use_profile2_middlename_field']) {
262 263 264
    $middlename_field = field_info_field($form_state['values']['realname_registration_middlename_field']);
    $match = FALSE;
    foreach (profile2_get_types() as $type_name => $profile_type) {
265
      if ($profile_type->data['registration'] && is_array($middlename_field['bundles']['profile2'])) {
266
        foreach ($middlename_field['bundles']['profile2'] as $key => $value) {
267 268 269 270 271 272 273 274 275 276 277
          if ($value == $profile_type->type) {
            $match = TRUE;
          }
        }
      }
    }
    if (!$match) {
      form_set_error('realname_registration_middlename_field', $err_pretext . $form_state['values']['realname_registration_middlename_field'] . $err_register);
    }
  }

278
  if (!empty($form_state['values']['realname_registration_middlename_field'])) {
279
    if (module_exists('profile2') && $form_state['values']['realname_registration_use_profile2_middlename_field']) {
280
      $middlename_instance = field_info_instance('profile2', $form_state['values']['realname_registration_middlename_field'], $form_state['values']['realname_registration_profile2_middlename_field_profile_name']);
281 282 283 284
    }
    else {
      $middlename_instance = field_info_instance('user', $form_state['values']['realname_registration_middlename_field'], 'user');
    }
285 286
    if (!isset($middlename_instance)) {
      form_set_error('realname_registration_middlename_field', $err_pretext . $form_state['values']['realname_registration_middlename_field'] . $err_unknown);
287
    }
288 289
    if ($middlename_instance['widget']['type'] != 'text_textfield') {
      form_set_error('realname_registration_middlename_field', $err_pretext . $form_state['values']['realname_registration_middlename_field'] . $err_textfield);
290 291 292 293
    }
  }
  else {
    // If there is no middle name field provided, we can't base usernames off the middle initial.
294
    if ($form_state['values']['realname_registration_format'] == 2 || $form_state['values']['realname_registration_format'] == 3) {
295 296
      form_set_error('realname_registration_format', t('A valid middle name field is required for this username format.'));
    }
Steve Gerbino's avatar
Steve Gerbino committed
297 298
  }

299
  // Validate the last name field.
300
  if (module_exists('profile2') && $form_state['values']['realname_registration_use_profile2_lastname_field']) {
301 302 303
    $lastname_field = field_info_field($form_state['values']['realname_registration_lastname_field']);
    $match = FALSE;
    foreach (profile2_get_types() as $type_name => $profile_type) {
304
      if ($profile_type->data['registration'] && is_array($lastname_field['bundles']['profile2'])) {
305
        foreach ($lastname_field['bundles']['profile2'] as $key => $value) {
306 307 308 309 310 311 312 313 314 315 316
          if ($value == $profile_type->type) {
            $match = TRUE;
          }
        }
      }
    }
    if (!$match) {
      form_set_error('realname_registration_lastname_field', $err_pretext . $form_state['values']['realname_registration_lastname_field'] . $err_register);
    }
  }

317
  if (module_exists('profile2') && $form_state['values']['realname_registration_use_profile2_lastname_field']) {
318
    $lastname_instance = field_info_instance('profile2', $form_state['values']['realname_registration_lastname_field'], $form_state['values']['realname_registration_profile2_lastname_field_profile_name']);
319 320 321 322
  }
  else {
    $lastname_instance = field_info_instance('user', $form_state['values']['realname_registration_lastname_field'], 'user');
  }
323 324
  if (!isset($lastname_instance)) {
    form_set_error('realname_registration_lastname_field', $err_pretext . $form_state['values']['realname_registration_lastname_field'] . $err_unknown);
325
  }
326 327
  if ($lastname_instance['widget']['type'] != 'text_textfield') {
    form_set_error('realname_registration_lastname_field', $err_pretext . $form_state['values']['realname_registration_lastname_field'] . $err_textfield);
Steve Gerbino's avatar
Steve Gerbino committed
328
  }
329 330
  if ($lastname_instance['required'] != 1) {
    form_set_error('realname_registration_lastname_field', $err_pretext . $form_state['values']['realname_registration_lastname_field'] . $err_req);
Steve Gerbino's avatar
Steve Gerbino committed
331 332
  }
}