You are here

linkedin_profile.module in LinkedIn Integration 6

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

Main hooks implementation for LinkedIn Profile module

File

linkedin_profile/linkedin_profile.module
View source
<?php

/**
 * @file Main hooks implementation for LinkedIn Profile module
 */

/*
 * 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');
  }
  module_load_include('pages.inc', 'linkedin_profile');
}

/*
 * 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) {
  global $user;
  if (variable_get('linkedin_profile_user_page_enabled', '0') < '1' || !user_access('display LinkedIn profile', $account) || $account->linkedin_profile_user_page_enabled != '1') {
    return FALSE;
  }
  if (!user_access('access user profiles') && $user->uid != $account->uid) {
    return FALSE;
  }
  module_load_include('inc', 'linkedin', 'linkedin');
  $check = linkedin_get_profile_fields($account->uid, array(
    'id',
  ));
  if ($check['error-code']) {

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

/*
 * Implementation of hook_perm()
 */
function linkedin_profile_perm() {
  return array(
    'display LinkedIn profile',
  );
}

/*
 * 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',
  );
}

/*
 * Implementation of hook_user()
 */
function linkedin_profile_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'categories':
      return array(
        array(
          'name' => 'linkedin',
          'title' => 'LinkedIn',
          'weight' => 3,
        ),
      );
      break;
    case 'load':
      if (variable_get('linkedin_profile_user_page_enabled', '0') > 0) {
        static $profile;

        // no need to ask for each user_load
        if (!isset($profile)) {
          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']) {
                  $items[] = theme('linkedin_profile_user_page_' . $multiples[$key] . '_item', $items_val);
                }
                else {

                  //iterate
                  foreach ($items_val as $item_val) {
                    $items[] = theme('linkedin_profile_user_page_' . $multiples[$key] . '_item', $item_val);
                  }
                }
                $item['value'] = theme('linkedin_profile_user_page_' . $key, $items);
              }
              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', $item);
            }
          }
        }
      }
      break;
    case 'view':
      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'),
          '#value' => theme('linkedin_profile_user_page', $account->linkedin),
          '#weight' => variable_get('linkedin_profile_user_page_weight', 2),
        );
      }
      break;
  }
}

/*
 * Implementation of hook_block
 */
function linkedin_profile_block($op = 'list', $delta = 0, $edit = array()) {
  if (variable_get('linkedin_profile_user_page_enabled', '0') == 3) {
    switch ($op) {
      case 'list':
        $blocks[0]['info'] = t('LinkedIn Profile');
        return $blocks;
      case 'view':
        if (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', $account->linkedin);
            return $block;
          }
        }
    }
  }
}

/*
 * Implementation of hook_theme()
 */
function linkedin_profile_theme($existing, $type, $theme, $path) {
  $return = array();

  //Define the main template file for users profile
  $return['linkedin_profile_user_page'] = array(
    'arguments' => 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(
      'arguments' => array(
        $key => NULL,
      ),
      'template' => 'linkedin-user-page-' . $key,
    );

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

  //Let themers hook into other profile items
  $return['linkedin_profile_user_page_item'] = array(
    'arguments' => 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;
}