realname_registration.admin.inc 4.7 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();

Steve Gerbino's avatar
Steve Gerbino committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  $field_req = "
<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>
  <li>".t('The field is required and displays on the registration form')."</li>
</ul>
";

  $form['realname_registration_firstname_field'] = array(
    '#type' => 'textfield',
    '#title' => t('First name field'),
    '#default_value' => variable_get('realname_registration_firstname_field'),
    '#description' => t("The name of your first name field. Ensure that:").$field_req,
  );

  $form['realname_registration_lastname_field'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name field'),
    '#default_value' => variable_get('realname_registration_lastname_field'),
    '#description' => t("The name of your last name field. Ensure that:").$field_req,
  );

39 40 41 42
  $form['realname_registration_format'] = array(
    '#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
43 44
    '#default_value' => variable_get('realname_registration_format', 0),
    '#options' => array(t('First name and last name separated by a space (e.g., John Smith)'), t('First initial and last name (e.g., JSmith)')),
45 46 47
    '#required' => TRUE,
  );

Steve Gerbino's avatar
Steve Gerbino committed
48 49 50 51 52 53 54
  $form['realname_registration_ucfirst'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force the first letters of the first and last names to uppercase'),
    '#default_value' => variable_get('realname_registration_ucfirst', 1),
    '#description' => t("Ensures that the first letter of the users first name and first letter of the users last name are capitalized."),
  );

55 56 57 58 59 60 61
  $form['realname_registration_tolower'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force lowercase'),
    '#default_value' => variable_get('realname_registration_tolower'),
    '#description' => t("Usernames will be created using only lowercase characters."),
  );

Steve Gerbino's avatar
Steve Gerbino committed
62 63 64 65 66 67 68 69
  $form['realname_registration_use_validation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Realname registration validation'),
    '#default_value' => variable_get('realname_registration_use_validation', 1),
    '#description' => t("Use regex to validate real names (/^[A-ZÀ-ÖØ-öø-ÿ]+$/i)"),
  );

  $form['#validate'][] = 'realname_registration_settings_validate';
70 71
  return system_settings_form($form);
}
Steve Gerbino's avatar
Steve Gerbino committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

function realname_registration_settings_validate($form, &$form_state) {
  // Retrieve the user instance and bundle using the field names provided.
  $firstname_instance = field_info_instance('user', $form_state['values']['realname_registration_firstname_field'], 'user');
  $lastname_instance = field_info_instance('user', $form_state['values']['realname_registration_lastname_field'], 'user');

  $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 user entity and bundle.");
  $err_textfield = '</em>, '.t('must use textfield widget.');
  
  // We must check to see if the provided fields return a valid instance.
  if(!isset($firstname_instance)) {
    form_set_error('realname_registration_firstname_field', $err_pretext.$form_state['values']['realname_registration_firstname_field'].$err_unknown);
  }
  if(!isset($lastname_instance)) {
    form_set_error('realname_registration_lastname_field', $err_pretext.$form_state['values']['realname_registration_lastname_field'].$err_unknown);
  }
  
  // The widget associated with the field the user has provided should be a text field.
  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);
  }
  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);
  }

  // Because the field is used in creating usernames, it should be required.
  if($firstname_instance['required'] != 1) {
    form_set_error('realname_registration_firstname_field', $err_pretext.$form_state['values']['realname_registration_firstname_field'].$err_req);
  }
  if($lastname_instance['required'] != 1) {
    form_set_error('realname_registration_lastname_field', $err_pretext.$form_state['values']['realname_registration_lastname_field'].$err_req);
  }
}