You are here

function _linkedin_get_profile_fields in LinkedIn Integration 7

@todo Please document this function.

See also

http://drupal.org/node/1354

1 call to _linkedin_get_profile_fields()
linkedin_get_profile_fields in ./linkedin.module
@todo Please document this function.

File

./linkedin.inc, line 154

Code

function _linkedin_get_profile_fields($uid, $fields = array()) {

  //Get sure library is loaded before doing anything.
  linkedin_init();
  $base_url = "https://api.linkedin.com/v1/people/";
  $row = db_query("SELECT * FROM {linkedin_token} WHERE uid = :uid AND type = :type", array(
    ':uid' => $uid,
    ':type' => 'access',
  ))
    ->fetchAssoc();
  if (!$row || empty($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_query("SELECT * FROM {linkedin_token} WHERE uid = :uid AND type = :type", array(
      ':uid' => $user->uid,
      ':type' => 'access',
    ))
      ->fetchAssoc();
    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_query("SELECT authname FROM {authmap} WHERE uid = :uid AND module = :module", array(
        ':uid' => $uid,
        ':module' => 'linkedin',
      ))
        ->fetchField();
      $append = 'id=' . $authname;
      $type = 'auth';
    }
  }
  $tokens = (array) $tokens;
  $append .= _linkedin_build_fields_request($fields, $type);
  $url = $base_url . $append;
  $response = linkedin_get_fields($url, $tokens);
  if (isset($response['error']['status']) && ($response['error']['status'] == 401 || $response['error']['status'] == 403)) {

    // No relationships between users, switch back to public profile and retry
    $tokens = (array) $row;
    $append = '~:public';
    $append .= _linkedin_build_fields_request($fields, 'public');
    $url = $base_url . $append;
    $response = linkedin_get_fields($url, $tokens);
  }
  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;
}