You are here

linkedin_profile.module in LinkedIn Integration 7

Same filename and directory in other branches
  1. 6 linkedin_profile/linkedin_profile.module

File

linkedin_profile/linkedin_profile.module
View source
<?php

/**
 * @file Main hooks implementation for LinkedIn Profile module
 */
module_load_include('pages.inc', 'linkedin_profile');

/*
 * Implementation of hook_init()
 */
function linkedin_profile_init() {
  global $theme_path;
  $li_css = $theme_path . '/linkedin_profile.css';
  if (file_exists($li_css)) {
    drupal_add_css($li_css);
  }
  else {
    drupal_add_css(drupal_get_path('module', 'linkedin_profile') . '/linkedin_profile.css');
  }
}

/*
 * Implementation of hook_menu
 */
function linkedin_profile_menu() {
  $items = array();
  if (variable_get('linkedin_profile_user_page_enabled', 0) === 1) {

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

/*
 * Custom access callback for user/%user/linkedin
 */
function linkedin_profile_display_access($account) {
  if (variable_get('linkedin_profile_user_page_enabled', 0) < 1) {
    return FALSE;
  }
  if (!user_access('display LinkedIn profile', $account)) {
    return FALSE;
  }
  if (!isset($account->data['linkedin_profile_user_page_enabled']) || $account->data['linkedin_profile_user_page_enabled'] != 1) {
    return FALSE;
  }
  global $user;
  if (!user_access('access user profiles') && $user->uid != $account->uid) {
    return FALSE;
  }
  $check = linkedin_get_profile_fields($account->uid, array(
    'id',
  ));
  if (isset($check['error-code'])) {

    // no need to display an empty tab
    return FALSE;
  }
  return TRUE;
}

/*
 * Implementation of hook_perm()
 */
function linkedin_profile_permission() {
  return array(
    'display LinkedIn profile' => array(
      'title' => t('display LinkedIn profile'),
      'description' => t('Display own LinkedIn profile on user page.'),
    ),
  );
}

/*
 * Implementation of hook_linkedin_user_edit_perms :
 * Let Linkedin module know what permissions are available at user/%user/edit/linkedin
 */
function linkedin_profile_linkedin_user_edit_perms() {
  return array(
    'display LinkedIn profile',
  );
}

/**
 * Implements hook_user_load().
 */
function linkedin_profile_user_load($users) {
  if (variable_get('linkedin_profile_user_page_enabled', 0) > 0) {
    foreach ($users as $account) {
      module_load_include('inc', 'linkedin', 'linkedin');
      $fields = _linkedin_profile_vget_user_page_linkedin_fields();
      $profile = linkedin_get_profile_fields($account->uid, $fields);
      if (!isset($profile['error-code'])) {

        //Let themers hook into items
        $multiples = array(
          'positions' => 'position',
          'educations' => 'education',
        );
        foreach ($profile as $key => $value) {
          $item = array(
            'name' => $key,
            'value' => $value,
          );

          //deal with multi-values structured fields
          if (array_key_exists($key, $multiples)) {
            $items = array();
            $items_val = $profile[$key][$multiples[$key]];
            if (is_array($items_val)) {
              if ($items_val['id']) {
                $variables[$multiples[$key]] = $items_val;
                $items[] = theme('linkedin_profile_user_page_' . $multiples[$key] . '_item', $variables);
              }
              else {
                foreach ($items_val as $item_val) {
                  $variables[$multiples[$key]] = $item_val;
                  $items[] = theme('linkedin_profile_user_page_' . $multiples[$key] . '_item', $variables);
                }
              }
              $variables[$key] = $items;
              $item['value'] = theme('linkedin_profile_user_page_' . $key, $variables);
            }
            else {
              unset($item['value']);
            }
          }

          //Simpler fields go directly through generic theme function
          if (!empty($item['value'])) {
            $account->linkedin[$key] = theme('linkedin_profile_user_page_item', array(
              'item' => $item,
            ));
          }
        }
      }
    }
  }
}

/**
 * Implements hook_user_view().
 */
function linkedin_profile_user_view($account, $view_mode) {
  if (variable_get('linkedin_profile_user_page_enabled', 0) == 2 && linkedin_profile_display_access($account)) {
    $account->content['linkedin'] = array(
      '#type' => 'user_profile_item',
      '#title' => t('Linkedin'),
      '#markup' => theme('linkedin_profile_user_page', array(
        'profile' => $account->linkedin,
      )),
      '#weight' => variable_get('linkedin_profile_user_page_weight', 2),
    );
  }
}

/*
 * Implements hook_block_info().
 */
function linkedin_profile_block_info() {
  $blocks = array();
  if (variable_get('linkedin_profile_user_page_enabled', 0) == 3) {
    $blocks['linkedin_profile'] = array(
      'info' => t('LinkedIn Profile'),
      'description' => t('User\'s LinkedIn profile'),
    );
  }
  return $blocks;
}

/*
 * Implements hook_block_view().
 */
function linkedin_profile_block_view($delta = '') {
  if (variable_get('linkedin_profile_user_page_enabled', '0') == 3 && arg(0) == 'user' && is_numeric(arg(1))) {
    $account = user_load(arg(1));
    if (linkedin_profile_display_access($account)) {
      $block['subject'] = t('Linkedin Profile');
      $block['content'] = theme('linkedin_profile_user_page', array(
        'profile' => $account->linkedin,
      ));
      return $block;
    }
  }
  return;
}

/*
 * Implementation of hook_theme()
 */

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function linkedin_profile_theme($existing, $type, $theme, $path) {
  $return = array();

  //Define the main template file for users profile
  $return['linkedin_profile_user_page'] = array(
    'variables' => array(
      'profile' => NULL,
    ),
    'template' => 'linkedin-user-page',
  );

  //Deal with multi-structured fields
  $multiples = array(
    'positions' => 'position',
    'educations' => 'education',
  );
  foreach ($multiples as $key => $val) {

    //Template files for multi-values structured fields
    $return['linkedin_profile_user_page_' . $key] = array(
      'variables' => array(
        $key => NULL,
      ),
      'template' => 'linkedin-user-page-' . $key,
    );

    //Template file for multi-values structured fields items
    $return['linkedin_profile_user_page_' . $val . '_item'] = array(
      'variables' => array(
        $val => NULL,
      ),
      'template' => 'linkedin-user-page-' . $val . '-item',
    );
  }

  //Let themers hook into other profile items
  $return['linkedin_profile_user_page_item'] = array(
    'variables' => array(
      'item' => NULL,
    ),
    'file' => 'linkedin_profile.theme.inc',
  );
  return $return;
}

/*
 * Helper function : defines default values
 */
function _linkedin_profile_list_default_fields() {
  $default_fields = array(
    'first-name',
    'last-name',
    'headline',
    'location',
    'industry',
    'summary',
    'specialties',
    'interests',
    'picture-url',
    'public-profile-url',
  );
  return $default_fields;
}

/*
 * Helper function : wraps around variable_get
 */
function _linkedin_profile_vget_user_page_linkedin_fields() {
  $fields = array();
  $var_fields = variable_get('linkedin_profile_user_page_linkedin_fields', _linkedin_profile_list_default_fields());
  foreach ($var_fields as $field) {
    if ($field !== 0) {
      $fields[] = $field;
    }
  }
  return $fields;
}