You are here

function linkedin_get_profile_fields in LinkedIn Integration 6

Same name and namespace in other branches
  1. 7 linkedin.module \linkedin_get_profile_fields()
5 calls to linkedin_get_profile_fields()
linkedin_access_token in ./linkedin.inc
linkedin_profile_display_access in linkedin_profile/linkedin_profile.module
linkedin_profile_user in linkedin_profile/linkedin_profile.module
linkedin_status_update_form in linkedin_status/linkedin_status.pages.inc
linkedin_user_settings in ./linkedin.pages.inc

File

./linkedin.inc, line 139

Code

function linkedin_get_profile_fields($uid, $fields = array()) {
  $base_url = "https://api.linkedin.com/v1/people/";
  $row = db_fetch_array(db_query("SELECT * FROM {linkedin_token} WHERE uid = %d AND type = 'access'", $uid));
  if (!$row) {

    // This account does not have any LinkedIn account associated with.
    $response = array(
      'status' => '401',
      'error-code' => 'custom',
      'message' => 'No LinkedIn account is associated with this user',
    );
    if (variable_get('linkedin_debug_mode', 0) == 1) {
      drupal_set_message(t('Linkedin debug : @status : @message', array(
        '@status' => $response['status'],
        '@message' => $response['message'],
      )));
    }
    return $response;
  }
  global $user;
  if ($user->uid == $uid) {

    //User is requesting his own profile.
    $tokens = $row;
    $append = '~';
    $type = 'auth';
  }
  else {
    $tokens = db_fetch_array(db_query("SELECT * FROM {linkedin_token} WHERE uid = %d AND type = 'access'", $user->uid));
    if (!$tokens) {

      //We don't have any LinkedIn account associated with the user viewing the profile.

      //Make the request on the behalf on viewed user and switch to public profile.
      $tokens = $row;
      $append = '~:public';
      $type = 'public';
    }
    else {

      //Fetch profile. Fields returned will depend on the relationships between viewing/viewed users
      $authname = db_result(db_query("SELECT authname FROM {authmap} WHERE uid = %d AND module = 'linkedin'", $uid));
      $append = 'id=' . $authname;
      $type = 'auth';
    }
  }
  $append .= _linkedin_build_fields_request($fields, $type);
  $url = $base_url . $append;
  $response = linkedin_get_fields($url, $tokens);
  if ($response['error']['status'] == 401 || $response['error']['status'] == 403) {

    // No relationships between users, switch back to public profile and retry
    $tokens = $row;
    $append = '~:public';
    $append .= _linkedin_build_fields_request($fields, 'public');
    $url = $base_url . $append;
    $response = linkedin_get_fields($url, $tokens, $flat);
  }
  if (isset($response['person'])) {
    $response = $response['person'];
  }
  else {
    $response = $response['error'];
  }
  if (variable_get('linkedin_debug_mode', 0) == 1) {
    if (isset($response['error-code'])) {
      drupal_set_message(t('Linkedin debug : LinkedIn.com answered "@status : @message', array(
        '@status' => $response['status'],
        '@message' => $response['message'],
      )));
    }
  }
  return $response;
}