You are here

function fboauth_profile_create_user in Facebook OAuth (FBOAuth) 7

Same name and namespace in other branches
  1. 6 includes/fboauth.profile.inc \fboauth_profile_create_user()
  2. 7.2 includes/fboauth.profile.inc \fboauth_profile_create_user()

Add profile info to a Drupal user array (before account creation).

1 call to fboauth_profile_create_user()
fboauth_create_user in includes/fboauth.fboauth.inc
Given a Facebook user object, associate or save a Drupal user account.

File

includes/fboauth.profile.inc, line 80
Functions to assist with handling with Profile module data.

Code

function fboauth_profile_create_user(&$edit, $fbuser) {
  $profile_map = variable_get('fboauth_user_profile', array());
  $profile_fields = fboauth_profile_fields();
  foreach ($profile_map as $profile_field_name => $facebook_property_name) {
    if (isset($profile_fields[$profile_field_name])) {
      $profile_field = $profile_fields[$profile_field_name];
      switch ($profile_field['type']) {
        case 'date':
          $date = array();
          $fbdate = explode('/', $fbuser->{$facebook_property_name});
          if (count($fbdate) == 3) {
            foreach (array(
              'month',
              'day',
              'year',
            ) as $count => $part) {
              $date[$part] = (int) ltrim($fbdate[$count], '0');
            }
            $edit[$profile_field_name] = $date;
          }
          break;
        case 'selection':

          // We can't import anything other than strings into selects.
          if (!is_string($fbuser->{$facebook_property_name})) {
            break;
          }

          // Mapping options is tricky business. We loop through all available
          // options and choose the closest one to match the incoming value.
          $options = explode("\n", $profile_field['options']);
          $best_match = 0.0;
          $best_option = NULL;
          $fb_option = is_string($fbuser->{$facebook_property_name}) ? $fbuser->{$facebook_property_name} : '';
          $match_fb = strtolower($fbuser->{$facebook_property_name});
          foreach ($options as $option) {
            $option = trim($option);
            $match_option = strtolower($option);
            $this_match = 0;
            similar_text($match_option, $match_fb, $this_match);
            if ($this_match > $best_match) {
              $best_match = $this_match;
              $best_option = $option;
            }
          }
          if (isset($best_option)) {
            $edit[$profile_field_name] = $best_option;
          }
          break;
        case 'textfield':
        case 'textarea':
        case 'list':
        case 'url':
        default:

          // We can't import anything other than strings into textfields.
          if (is_string($fbuser->{$facebook_property_name})) {
            $edit[$profile_field_name] = $fbuser->{$facebook_property_name};
          }
          elseif (is_object($fbuser->{$facebook_property_name})) {
            $object = $fbuser->{$facebook_property_name};

            // Locations, Signficant Other, Hometown, and other properties use "name"
            // as the human-readable value in the object.
            if (isset($object->name)) {
              $edit[$profile_field_name] = $object->name;
            }
          }
          break;
      }
    }
  }
}