Skip to content
linkedin.module 6.19 KiB
Newer Older
<?php
// $Id

/*
 * @file : Main hooks implementation for Linkedin module
*/

/*
 * hook_init()
 * Make sure we load needed files.
*/

function linkedin_init() {
  module_load_include('lib.php', 'oauth');
  module_load_include('inc', 'linkedin');
  module_load_include('pages.inc', 'linkedin');
  global $theme_path;
  $linkedin_css = $theme_path .'/linkedin.css';
  if (file_exists($linkedin_css)) {
    drupal_add_css($linkedin_css);
  }
  else {
    drupal_add_css(drupal_get_path('module', 'linkedin') .'/linkedin.css');
  }
}

/*
 * hook_menu
*/
function linkedin_menu() {

  $items = array();

  //global settings form
  $items['admin/settings/linkedin'] = array(
          'title' => 'LinkedIn integration',
          'description' => 'linkedin module settings',
          'page callback' => 'drupal_get_form',
          'page arguments' => array('linkedin_admin'),
          'access arguments' => array('administer site configuration'),
          'file' => 'linkedin.pages.inc'
  );

  //user settings form
  $items['user/%user/edit/linkedin'] = array(
          'title' => 'Linkedin',
          'page callback' => 'linkedin_user_settings',
          'page arguments' => array(1),
          'access callback' => '_linkedin_user_access', //access arguments don't support multiple arguments, so create our access handler
          'type' => MENU_LOCAL_TASK,
          'file' => 'linkedin.pages.inc'
  );

  //callback for oauth token request on linkedin API.
  $items['linkedin/token/%'] = array(
          'page callback' => 'linkedin_access_token',
          'page arguments' => array(2),
          'access callback' => '_linkedin_user_access',
          'type' =>MENU_CALLBACK,
          'file' => 'linkedin.inc'
  );

  //LinkedIn profile tab for users
  $items['user/%user/linkedin'] = array(
          'title' => 'Linkedin',
          'page callback' => 'linkedin_profile_display',
          'page arguments' => array(1),
          'type' => MENU_LOCAL_TASK,
          'access callback' => '_linkedin_profile_display_access',
          'access arguments' => array(1),
          'file' => 'linkedin.pages.inc',

  );

  return $items;
}
/*
 * hook_theme()
*/
function linkedin_theme($existing, $type, $theme, $path) {
  return array(
          //Let themers hook into profile elements
          'linkedin_profile' => array(
                  'arguments' => array('profile' => NULL),
          ),
  );
}

/*
 * hook_user()
*/
function linkedin_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'categories':
      return array(
              array(
                      'name' => 'linkedin',
                      'title' => 'LinkedIn',
                      'weight' => 3,
              ),
      );
      break;
  }
}

/*
 * hook_perm()
*/
function linkedin_perm() {
  return array('display LinkedIn profile', 'update LinkedIn status', 'use custom status text');
}

/*
 * Custom access callback for user/%user/edit/linkedin
*/
function _linkedin_user_access () {
  if(user_access('display LinkedIn profile') || user_access('update LinkedIn status')) {
    return TRUE;
  } else {
    return FALSE;
  }
}

/*
 * Custom access callback for user/%user/linkedin
*/
function _linkedin_profile_display_access($account) {
  global $user;
  if(
  variable_get('linkedin_profile_enabled', '0') != '1' ||
          !user_access('access user profiles') ||
          !user_access('display LinkedIn profile', $account) ||
          $account->linkedin_profile_enabled != '1') {
    return FALSE;
  }
  $check = linkedin_get_profile_fields($account->uid, array('id'));
  if($check == 'refused') { // no need to display an empty tab

    return FALSE;
  }
  return TRUE;
}

/*
 * hook_form_alter()
 * Call the user form when needed.
*/
function linkedin_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'signup_form') {// add our form into event signup
    global $user;
    if(variable_get('linkedin_enabled_event_signup', '0') != '1' || !user_access('update LinkedIn status')) {
      return;
    } else {
      //node object is not at same key everytime, so iterate until we find it
      $array = $form['#parameters'];
      $node = new stdClass();
      foreach($array as $k) {
        if(is_object($k) && isset($k->nid) && $k->type == 'event') {
          $node = $k;
          break;
        } else {
          array_pop($array);
        }
      }
      $form['collapse']['signup_user_form']['linkedin'] = drupal_retrieve_form('linkedin_user_status_update_form', $form_state, $user, $node);
      array_unshift($form['#submit'],  'linkedin_user_status_update_form_submit' ); //manually adding our submit handler
    }


  } else {//add our form into nodes edit page
    if(isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {   
      global $user;
      $node = $form['#node'];
      if(variable_get('linkedin_enabled_'. $node->type, '0') != '1' || !user_access('update LinkedIn status')) {
        return;
      } else {
        $form['linkedin'] = drupal_retrieve_form('linkedin_user_status_update_form', $form_state, $user, $node);
        array_unshift($form['#submit'],  'linkedin_user_status_update_form_submit' );
      }
    }
  }
  return $form;
}

/*
 * Themer function for user profile
*/
function theme_linkedin_profile($profile) {
  $output = '';
  $output .= '<a href="'. $profile['public-profile-url'] .'" title="Public profile of '. $profile['first-name'] .' '.$profile['last-name'] .' on www.linkedin.com.">';
  if($profile['picture-url']) {
    $output .= '<img src="'. $profile['picture-url'] .'" alt="'. $profile['first-name'] .' '.$profile['last-name'] .'" />';
  }
  $output .= '<h2 class="linkedin-name">'. $profile['first-name'] .' '.$profile['last-name'] .'</h2></a>';
  $output .= '<h3 class="linkedin-headline">'. $profile['headline'] .' <i>('.$profile['industry'] .')</i></h3>';
  $output .= '<p class="linkedin-location">'. $profile['name'] .'</p>';
  $output .= '<div class="linkedin-summary">'. $profile['summary'] .'</div>';
  $output .= '<div class="linkedin-specialties">'. $profile['specialties'] .'</div>';
  $output .= '<div class="linkedin-status">';
  $output .= '<p class="linkedin-submitted">'. format_date($profile['current-status-timestamp']) .'</p>';
  $output .= $profile['current-status'];
  $output .= '</div>';
  return $output;
}