You are here

function fbconnect_login_authorize_page in Facebook Connect 7.2

Same name and namespace in other branches
  1. 8.2 fbconnect_login/fbconnect_login.pages.inc \fbconnect_login_authorize_page()

Menu callback. Called when user perform Facebook registration

1 string reference to 'fbconnect_login_authorize_page'
fbconnect_login_menu in fbconnect_login/fbconnect_login.module
Implements hook_menu().

File

fbconnect_login/fbconnect_login.pages.inc, line 83
User pages callbacks for the fbconnect module.

Code

function fbconnect_login_authorize_page($form, $form_state = array()) {
  $conf = fbconnect_get_config();
  $data = fbconnect_get_user_info(array(
    'name',
    'email',
  ));

  // Check if user has already registered with thier Facebook account
  $uid = _is_fbconnect_user($data['id']);
  if ($uid) {

    // User already registered Facebook account to site, log them in
    $form_state['uid'] = $uid;
    user_login_submit($form, $form_state);
    if (!empty($_GET['fb-destination'])) {
      drupal_goto($_GET['fb-destination']);
    }
    else {
      drupal_goto('<front>');
    }
  }
  elseif (!empty($data['email']) && _email_already_exist($data['email']) && user_is_anonymous()) {

    // The user was not found in the fbconnect_users table, but the
    // email from Facebook might already have an account.
    // Redirect user to the login page with a message..
    drupal_set_message(t('This email address is already registered to an account. Please log in to this account with the username and password provided during registration'));
    $options = array(
      'query' => array(
        'fbconnect' => 'true',
      ),
    );
    drupal_goto('user/login', $options);
    return;
  }
  $form = drupal_retrieve_form('user_register_form', $form_state);
  $site = $conf['invite_name'];

  // #758918 : prevent users registration if admin has blocked free registration
  $user_reg_mode = variable_get('user_register', 1);
  if ($user_reg_mode == 0) {
    drupal_access_denied();
    return;
  }
  $form['account']['name']['#value'] = empty($form_state['input']['name']) ? $data['name'] : $form_state['input']['name'];
  $form['account']['name']['#parents'] = array(
    'name',
  );
  if (!empty($data['email'])) {
    $form['account']['mail']['#value'] = $data['email'];
    $form['account']['mail']['#parents'] = array(
      'mail',
    );
  }
  else {
    $form['account']['mail']['#value'] = $data['id'] . '@facebook.com';
    $form['account']['mail']['#parents'] = array(
      'mail',
    );
  }
  $form['fbconnect'] = array(
    '#type' => 'fieldset',
    '#title' => t('Facebook Connect'),
  );
  $form['fbconnect']['fb_visible'] = array(
    '#type' => 'checkbox',
    '#title' => t('Let my Facebook friends see me on @sitename', array(
      '@sitename' => $site,
    )),
    '#default_value' => 1,
  );
  if (variable_get('user_pictures', 0) && $conf['user_pictures'] == 'allow') {
    $form['fbconnect']['fb_avatar'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use my Facebook picture as user picture'),
      '#description' => t('Your picture will be loaded from Facebook.'),
      '#default_value' => 1,
    );
  }
  $form_state['redirect'] = isset($_GET['destination']) ? $_GET['destination'] : '';
  $form['#validate'][] = 'fbconnect_login_register_form_validate';
  $form['#submit'][] = 'fbconnect_login_register_form_submit';

  // Fast registration mode, we by pass std drupal reg form.
  if ($conf['fast_reg_mode']) {
    $newname = $conf['fast_reg_autoname'] ? _fbconnect_get_username($data['name']) : $data['name'];

    //drupal_set_message('new name : ' . $newname);
    $form_state['values']['name'] = $newname;
    $form_state['values']['mail'] = $data['email'];
    $form_state['values']['pass'] = user_password();
    $form_state['values']['status'] = 1;
    $form_state['values']['fb_visible'] = 1;
    $form_state['values']['fb_avatar'] = 1;

    //to make validation work have to unset some form fields and settings Taxonomy fields cause errors

    //TODO:: check this code sure it will need to check for required profile fields and fill them with data
    foreach ($form as $key => $val) {

      //unset all cusom fields
      if (strstr($key, 'field_')) {

        //unset this custom field
        unset($form[$key]);
      }
    }
    $form_state['complete form'] = $form;

    //taxonomy linked fields causes issues with this validate form function
    drupal_validate_form('user_register', $form, $form_state);
    $success = !form_get_errors();
    if (!$success) {
      $dest = array(
        'query' => 'destination=fbconnect/link',
      );
      drupal_set_message(t('Facebook connect registration failed for the reasons listed. You may register now, or if you already have an account you can <a href="@login">log in</a> now and link your account', array(
        '@login' => url('user/login', $dest),
      )), 'warning');

      // Append form validation errors below the above warning.
      $messages = drupal_get_messages('error');
      foreach ($messages['error'] as $message) {
        drupal_set_message($message, 'error');
      }
      drupal_goto('user/register', $dest['query']);
    }
    fbconnect_login_register_form_submit($form, $form_state);
  }
  return $form;
}