<?php

/**
 * @file
 * Block functions.
 *
 * Contains all the functions to run the "User Details" blocks.
 */


/**
 * Implements of hook_help().
 *
 * Creates a basic help page(accessible from the modules page).
 */
function user_details_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#user_details":
      require_once drupal_get_path('module', 'user_details') .'/user_details_help.inc';
      $output = drupal_render(drupal_get_form('user_details_help_form'));
  }
  return $output;
}

/**
 * Implements hook_menu().
 */
function user_details_menu() {
  $items['admin/config/user-interface/user-details'] = array(
    'title' => 'User Details',
    'description' => 'Configure user Details.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('user_details_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'user_details_admin_settings.inc',
  );

  return $items;
}

/**
 * Implements hook_theme().
 *
 * Adds the template and variables needed to have the blocks run through a
 * template file.
 */
function user_details_theme() {
  return array(
    'user_details_loggedin' => array(
      'template' => 'templates/user-details-loggedin',
      'variables' => array(
        'user_details_loggedin_avatar' => NULL,
        'user_details_loggedin_points_title' => NULL,
        'user_details_loggedin_points_result' => NULL,
        'user_details_loggedin_joined_title' => NULL,
        'user_details_loggedin_joined_result' => NULL,
        'user_details_loggedin_postcount_title' => NULL,
        'user_details_loggedin_postcount_result' => NULL,
        'user_details_loggedin_rank_title' => NULL,
        'user_details_loggedin_rank_result' => NULL,
        'user_details_loggedin_privatemsg_title' => NULL,
        'user_details_loggedin_privatemsg_result' => NULL,
        'user_details_loggedin_stats_hr' => NULL,
        'user_details_loggedin_frontlink_url' => NULL,
        'user_details_loggedin_frontlink_imgsrc' => NULL,
        'user_details_loggedin_frontlink_imgtitle' => NULL,
        'user_details_loggedin_profilelink_url' => NULL,
        'user_details_loggedin_profilelink_imgsrc' => NULL,
        'user_details_loggedin_profilelink_imgtitle' => NULL,
        'user_details_loggedin_profileeditlink_url' => NULL,
        'user_details_loggedin_profileeditlink_imgsrc' => NULL,
        'user_details_loggedin_profileeditlink_imgtitle' => NULL,
        'user_details_loggedin_privatemsglink_url' => NULL,
        'user_details_loggedin_privatemsglink_imgsrc' => NULL,
        'user_details_loggedin_privatemsglink_imgtitle' => NULL,
        'user_details_loggedin_createlink_url' => NULL,
        'user_details_loggedin_createlink_imgsrc' => NULL,
        'user_details_loggedin_createlink_imgtitle' => NULL,
        'user_details_loggedin_adminlink_url' => NULL,
        'user_details_loggedin_adminlink_imgsrc' => NULL,
        'user_details_loggedin_adminlink_imgtitle' => NULL,
        'user_details_loggedin_panelslink_url' => NULL,
        'user_details_loggedin_panelslink_imgsrc' => NULL,
        'user_details_loggedin_panelslink_imgtitle' => NULL,
        'user_details_loggedin_viewslink_url' => NULL,
        'user_details_loggedin_viewslink_imgsrc' => NULL,
        'user_details_loggedin_viewslink_imgtitle' => NULL,
        'user_details_loggedin_performancelink_url' => NULL,
        'user_details_loggedin_performancelink_imgsrc' => NULL,
        'user_details_loggedin_performancelink_imgtitle' => NULL,
        'user_details_loggedin_logoutlink_url' => NULL,
        'user_details_loggedin_logoutlink_imgsrc' => NULL,
        'user_details_loggedin_logoutlink_imgtitle' => NULL,
        'user_details_loggedin_customlinkone_type' => NULL,
        'user_details_loggedin_customlinkone_url' => NULL,
        'user_details_loggedin_customlinkone_imgsrc' => NULL,
        'user_details_loggedin_customlinkone_imgtitle' => NULL,
        'user_details_loggedin_customlinktwo_url' => NULL,
        'user_details_loggedin_customlinktwo_imgsrc' => NULL,
        'user_details_loggedin_customlinktwo_imgtitle' => NULL,
        'user_details_loggedin_customlinkthree_url' => NULL,
        'user_details_loggedin_customlinkthree_imgsrc' => NULL,
        'user_details_loggedin_customlinkthree_imgtitle' => NULL,
        'user_details_loggedin_customlinkfour_url' => NULL,
        'user_details_loggedin_customlinkfour_imgsrc' => NULL,
        'user_details_loggedin_customlinkfour_imgtitle' => NULL,
        'user_details_loggedin_customlinkfive_url' => NULL,
        'user_details_loggedin_customlinkfive_imgsrc' => NULL,
        'user_details_loggedin_customlinkfive_imgtitle' => NULL,
        'user_details_loggedin_links_hr' => NULL,
        'user_details_loggedin_content' => NULL,
      ),
    ),
    'user_details_authored' => array(
      'template' => 'templates/user-details-authored',
      'variables' => array(
        'user_details_authored_avatar' => NULL,
        'user_details_authored_joined_title' => NULL,
        'user_details_authored_joined_result' => NULL,
        'user_details_loggedin_postcount_title' => NULL,
        'user_details_loggedin_postcount_result' => NULL,
        'user_details_authored_points_title' => NULL,
        'user_details_authored_points_result' => NULL,
        'user_details_authored_rank_title' => NULL,
        'user_details_authored_rank_result' => NULL,
        'user_details_authored_stats_hr' => NULL,
        'user_details_authored_profilelink_url' => NULL,
        'user_details_authored_profilelink_imgsrc' => NULL,
        'user_details_authored_profilelink_imgtitle' => NULL,
        'user_details_authored_privatemsglink_url' => NULL,
        'user_details_authored_privatemsglink_imgsrc' => NULL,
        'user_details_authored_privatemsglink_imgtitle' => NULL,
        'user_details_authored_links_hr' => NULL,
        'user_details_authored_content' => NULL,
      ),
    ),
  );
}
/**
 * Implements hook_block_save().
 *
 * Sends all the checkbox(options) variables to the database to be stored.
 */
function user_details_block_save($delta = '', $edit = array()) {
  if ($delta == 'loggedin') {
    variable_set('user_details_loggedin_avatar_display', $edit['user_details_loggedin_avatar_display']);
    variable_set('user_details_loggedin_points_display', $edit['user_details_loggedin_points_display']);
    variable_set('user_details_loggedin_joined_display', $edit['user_details_loggedin_joined_display']);
    variable_set('user_details_loggedin_postcount_display', $edit['user_details_loggedin_postcount_display']);
    variable_set('user_details_loggedin_rank_display', $edit['user_details_loggedin_rank_display']);
    variable_set('user_details_loggedin_stats_hr_display', $edit['user_details_loggedin_stats_hr_display']);
    variable_set('user_details_loggedin_privatemsg_display', $edit['user_details_loggedin_privatemsg_display']);
    variable_set('user_details_loggedin_frontlink_display', $edit['user_details_loggedin_frontlink_display']);
    variable_set('user_details_loggedin_frontlink_imgsrc', $edit['user_details_loggedin_frontlink_imgsrc']);
    variable_set('user_details_loggedin_profilelink_display', $edit['user_details_loggedin_profilelink_display']);
    variable_set('user_details_loggedin_profilelink_imgsrc', $edit['user_details_loggedin_profilelink_imgsrc']);
    variable_set('user_details_loggedin_profileeditlink_display', $edit['user_details_loggedin_profileeditlink_display']);
    variable_set('user_details_loggedin_profileeditlink_imgsrc', $edit['user_details_loggedin_profileeditlink_imgsrc']);
    variable_set('user_details_loggedin_privatemsglink_display', $edit['user_details_loggedin_privatemsglink_display']);
    variable_set('user_details_loggedin_privatemsglink_imgsrc', $edit['user_details_loggedin_privatemsglink_imgsrc']);
    variable_set('user_details_loggedin_createlink_display', $edit['user_details_loggedin_createlink_display']);
    variable_set('user_details_loggedin_createlink_imgsrc', $edit['user_details_loggedin_createlink_imgsrc']);
    variable_set('user_details_loggedin_adminlink_display', $edit['user_details_loggedin_adminlink_display']);
    variable_set('user_details_loggedin_adminlink_imgsrc', $edit['user_details_loggedin_adminlink_imgsrc']);
    variable_set('user_details_loggedin_panelslink_display', $edit['user_details_loggedin_panelslink_display']);
    variable_set('user_details_loggedin_panelslink_imgsrc', $edit['user_details_loggedin_panelslink_imgsrc']);
    variable_set('user_details_loggedin_viewslink_display', $edit['user_details_loggedin_panelslink_display']);
    variable_set('user_details_loggedin_viewslink_imgsrc', $edit['user_details_loggedin_panelslink_imgsrc']);
    variable_set('user_details_loggedin_performancelink_display', $edit['user_details_loggedin_performancelink_display']);
    variable_set('user_details_loggedin_performancelink_imgsrc', $edit['user_details_loggedin_performancelink_imgsrc']);
    variable_set('user_details_loggedin_logoutlink_display', $edit['user_details_loggedin_logoutlink_display']);
    variable_set('user_details_loggedin_logoutlink_imgsrc', $edit['user_details_loggedin_logoutlink_imgsrc']);
    variable_set('user_details_loggedin_customlinkone_type', $edit['user_details_loggedin_customlinkone_type']);
    variable_set('user_details_loggedin_customlinkone_url', $edit['user_details_loggedin_customlinkone_url']);
    variable_set('user_details_loggedin_customlinkone_imgsrc', $edit['user_details_loggedin_customlinkone_imgsrc']);
    variable_set('user_details_loggedin_customlinkone_imgtitle', $edit['user_details_loggedin_customlinkone_imgtitle']);
    variable_set('user_details_loggedin_customlinktwo_type', $edit['user_details_loggedin_customlinktwo_type']);
    variable_set('user_details_loggedin_customlinktwo_url', $edit['user_details_loggedin_customlinktwo_url']);
    variable_set('user_details_loggedin_customlinktwo_imgsrc', $edit['user_details_loggedin_customlinktwo_imgsrc']);
    variable_set('user_details_loggedin_customlinkthree_type', $edit['user_details_loggedin_customlinkthree_type']);
    variable_set('user_details_loggedin_customlinkthree_url', $edit['user_details_loggedin_customlinkthree_url']);
    variable_set('user_details_loggedin_customlinkthree_imgsrc', $edit['user_details_loggedin_customlinkthree_imgsrc']);
    variable_set('user_details_loggedin_customlinkthree_imgtitle', $edit['user_details_loggedin_customlinkthree_imgtitle']);
    variable_set('user_details_loggedin_customlinkfour_type', $edit['user_details_loggedin_customlinkfour_type']);
    variable_set('user_details_loggedin_customlinkfour_url', $edit['user_details_loggedin_customlinkfour_url']);
    variable_set('user_details_loggedin_customlinkfour_imgsrc', $edit['user_details_loggedin_customlinkfour_imgsrc']);
    variable_set('user_details_loggedin_customlinkfour_imgtitle', $edit['user_details_loggedin_customlinkfour_imgtitle']);
    variable_set('user_details_loggedin_customlinkfive_type', $edit['user_details_loggedin_customlinkfive_type']);
    variable_set('user_details_loggedin_customlinkfive_url', $edit['user_details_loggedin_customlinkfive_url']);
    variable_set('user_details_loggedin_customlinkfive_imgsrc', $edit['user_details_loggedin_customlinkfive_imgsrc']);
    variable_set('user_details_loggedin_customlinkfive_imgtitle', $edit['user_details_loggedin_customlinkfive_imgtitle']);
    variable_set('user_details_loggedin_links_hr_display', $edit['user_details_loggedin_links_hr_display']);
    variable_set('user_details_loggedin_content_display', $edit['user_details_loggedin_content_display']);
    variable_set('user_details_loggedin_content_amount', $edit['user_details_loggedin_content_amount']);
  }
  if ($delta == 'authored') {
    variable_set('user_details_authored_avatar_display', $edit['user_details_authored_avatar_display']);
    variable_set('user_details_authored_points_display', $edit['user_details_authored_points_display']);
    variable_set('user_details_authored_joined_display', $edit['user_details_authored_joined_display']);
    variable_set('user_details_authored_postcount_display', $edit['user_details_authored_postcount_display']);
    variable_set('user_details_authored_rank_display', $edit['user_details_authored_rank_display']);
    variable_set('user_details_authored_stats_hr_display', $edit['user_details_authored_stats_hr_display']);
    variable_set('user_details_authored_profilelink_display', $edit['user_details_authored_profilelink_display']);
    variable_set('user_details_authored_profilelink_imgsrc', $edit['user_details_authored_profilelink_imgsrc']);
    variable_set('user_details_authored_privatemsglink_display', $edit['user_details_authored_privatemsglink_display']);
    variable_set('user_details_authored_privatemsglink_imgsrc', $edit['user_details_authored_privatemsglink_imgsrc']);
    variable_set('user_details_authored_links_hr_display', $edit['user_details_authored_links_hr_display']);
    variable_set('user_details_authored_content_display', $edit['user_details_authored_content_display']);
    variable_set('user_details_authored_content_amount', $edit['user_details_authored_content_amount']);
  }
  return;
}

/**
 * Implements hook_block_info().
 *
 * Creates the title of the block for the block list page.
 */
function user_details_block_info() {
  $blocks['loggedin']['info'] = t('User Details: logged-in user');
  $blocks['authored']['info'] = t('User Details: authored user');
  return $blocks;
}

/**
 * Implements hook_block_view().
 *
 * Creates the block title and content.
 */
function user_details_block_view($delta = '') {
  global $user;
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node_load = node_load(arg(1));
    $uid = $node_load->uid;
    $user_load = user_load($uid);
  }
  if (arg(0) == 'user' && is_numeric(arg(1))) {
    $user_arg = user_load(arg(1));
    $uid = $user_arg->uid;
    $user_load = user_load($uid);
  }
  switch ($delta) {
    case 'loggedin':
      if ($user->uid != 0) {
        $block['subject'] = t('%name', array('%name' => $user->name));
        $block['content'] = user_details_loggedin_block_content();
        return $block;
      }
      else {
        return;
      }
    case 'authored':
      $block['subject'] = t('%name', array('%name' => $user_load->name));
      $block['content'] = user_details_authored_block_content();
      return $block;
  }
}

/**
 * Process variables for user-details-loggedin.tpl.php.
 *
 * This function takes the result of the stored variable(options checkbox) and
 * creates a variable result from it.
 */
function template_preprocess_user_details_loggedin(&$variables) {
  global $user;
  $co = $output = NULL;
  drupal_add_css(drupal_get_path('module', 'user_details') . '/css/user_details_loggedin.css');

  if (variable_get('user_details_loggedin_avatar_display') !=0) {
    $variables['user_details_loggedin_avatar'] = theme('user_picture', array('account' => $user));
  }
  if (variable_get('user_details_loggedin_joined_display') != 0) {
    $user_details_loggedin_date = date('c', $user->created);
    $variables['user_details_loggedin_joined_title'] = t('Joined');
    $variables['user_details_loggedin_joined_result'] = date('M j, Y', strtotime($user_details_loggedin_date));
  }
  if (variable_get('user_details_loggedin_postcount_display') != 0) {
    $user_details_loggedin_postcount_query = db_select('node', 'n');
    $user_details_loggedin_postcount_query
      ->condition('n.uid', $user->uid, '=')
      ->fields('n', array('uid'));
    $variables['user_details_loggedin_postcount_title'] = t('Posts');
    $variables['user_details_loggedin_postcount_result'] = t('%postcount', array('%postcount' => $user_details_loggedin_postcount_query->countQuery()->execute()->fetchField()));
  }
  if (variable_get('user_details_loggedin_points_display') !=0 && module_exists('userpoints')) {
    $variables['user_details_loggedin_points_title'] = t('!Points', userpoints_translation());
    $variables['user_details_loggedin_points_result'] = userpoints_get_current_points($user->uid, 'all');
  }
  if (variable_get('user_details_loggedin_rank_display') != 0) {
    $user_details_loggedin_rank_query = db_select('users_roles');
    $user_details_loggedin_rank_query
      ->condition('uid', $user->uid, '=')
      ->fields('users_roles', array('rid'));
    $user_details_loggedin_rank_fetchfield = $user_details_loggedin_rank_query->execute()->fetchField();
    if (($user_details_loggedin_rank_fetchfield) == NULL) {
      $user_details_loggedin_rank_fetchfield = 2;
    }
    $user_details_loggedin_rankname_query = db_select('role', 'r');
    $user_details_loggedin_rankname_query
      ->condition('rid', $user_details_loggedin_rank_fetchfield, '=')
      ->fields('r', array('name'));
    $user_details_loggedin_rankname_fetchfield = $user_details_loggedin_rankname_query->execute()->fetchField();
    $variables['user_details_loggedin_rank_title'] = t('Rank');
    $variables['user_details_loggedin_rank_result'] = t('%rank', array('%rank' => $user_details_loggedin_rankname_fetchfield));
  }
  if (variable_get('user_details_loggedin_privatemsg_display') != 0 && module_exists('privatemsg')) {
    $count = privatemsg_unread_count();
    $variables['user_details_loggedin_privatemsg_count_title'] = t('New messages');
    $variables['user_details_loggedin_privatemsg_count_result'] = t('%rank', array('%rank' => $count));
  }
  if (variable_get('user_details_loggedin_avatar_display') !=0 && variable_get('user_details_loggedin_stats_hr_display') != 0 || variable_get('user_details_loggedin_joined_display') != 0 && variable_get('user_details_loggedin_stats_hr_display') != 0 || variable_get('user_details_loggedin_postcount_display') != 0 && variable_get('user_details_loggedin_stats_hr_display') != 0 || variable_get('user_details_loggedin_points_display') !=0 && module_exists('userpoints') && variable_get('user_details_loggedin_stats_hr_display') != 0 || variable_get('user_details_loggedin_rank_display') != 0 && variable_get('user_details_loggedin_stats_hr_display') != 0) {
    $variables['user_details_loggedin_stats_hr'] = '<hr>';
  }
  if (variable_get('user_details_loggedin_frontlink_display') != 0) {
    $variables['user_details_loggedin_frontlink_url'] = t('/');
    $variables['user_details_loggedin_frontlink_imgsrc'] = variable_get('user_details_loggedin_frontlink_imgsrc');
    $variables['user_details_loggedin_frontlink_imgtitle'] = t('Front page');
  }
  if (variable_get('user_details_loggedin_profilelink_display') != 0) {
    $variables['user_details_loggedin_profilelink_url'] = t('/user/' . $user->uid);
    $variables['user_details_loggedin_profilelink_imgsrc'] = variable_get('user_details_loggedin_profilelink_imgsrc');
    $variables['user_details_loggedin_profilelink_imgtitle'] = t('My profile');
  }
  if (variable_get('user_details_loggedin_profileeditlink_display') != 0) {
    $variables['user_details_loggedin_profileeditlink_url'] = t('/user/' . $user->uid . '/edit');
    $variables['user_details_loggedin_profileeditlink_imgsrc'] = variable_get('user_details_loggedin_profileeditlink_imgsrc');
    $variables['user_details_loggedin_profileeditlink_imgtitle'] = t('Edit my profile');
  }
  if (variable_get('user_details_loggedin_privatemsglink_display') != 0 && module_exists('privatemsg')) {
    $variables['user_details_loggedin_privatemsglink_url'] = t('/messages/' . $user->uid);
    $variables['user_details_loggedin_privatemsglink_imgsrc'] = variable_get('user_details_loggedin_privatemsglink_imgsrc');
    $variables['user_details_loggedin_privatemsglink_imgtitle'] = t('My private messages');
  }
  if (variable_get('user_details_loggedin_createlink_display') != 0) {
    $variables['user_details_loggedin_createlink_url'] = t('/node/add');
    $variables['user_details_loggedin_createlink_imgsrc'] = variable_get('user_details_loggedin_createlink_imgsrc');
    $variables['user_details_loggedin_createlink_imgtitle'] = t('Create content');
  }
  if (variable_get('user_details_loggedin_adminlink_display') != 0 && user_access('administor content')) {
    $variables['user_details_loggedin_adminlink_url'] = t('/admin');
    $variables['user_details_loggedin_adminlink_imgsrc'] = variable_get('user_details_loggedin_adminlink_imgsrc');
    $variables['user_details_loggedin_adminlink_imgtitle'] = t('Admin');
  }
  if (variable_get('user_details_loggedin_panelslink_display') != 0 && module_exists('panels') && user_access('administor content')) {
    $variables['user_details_loggedin_panelslink_url'] = t('/admin/structure/panels');
    $variables['user_details_loggedin_panelslink_imgsrc'] = variable_get('user_details_loggedin_panelslink_imgsrc');
    $variables['user_details_loggedin_panelslink_imgtitle'] = t('Panels');
  }
  if (variable_get('user_details_loggedin_viewslink_display') != 0 && module_exists('views') && user_access('administor content')) {
    $variables['user_details_loggedin_viewslink_url'] = t('/admin/structure/views');
    $variables['user_details_loggedin_viewslink_imgsrc'] = variable_get('user_details_loggedin_viewslink_imgsrc');
    $variables['user_details_loggedin_viewslink_imgtitle'] = t('Views');
  }
  if (variable_get('user_details_loggedin_performancelink_display') != 0 && user_access('administor content')) {
    $variables['user_details_loggedin_performancelink_url'] = t('/admin/config/development/performance');
    $variables['user_details_loggedin_performancelink_imgsrc'] = variable_get('user_details_loggedin_performancelink_imgsrc');
    $variables['user_details_loggedin_performancelink_imgtitle'] = t('Performance');
  }
  if (variable_get('user_details_loggedin_logoutlink_display') != 0) {
    $variables['user_details_loggedin_logoutlink_url'] = t('/user/logout');
    $variables['user_details_loggedin_logoutlink_imgsrc'] = variable_get('user_details_loggedin_logoutlink_imgsrc');
    $variables['user_details_loggedin_logoutlink_imgtitle'] = t('Logout');
  }
  if (variable_get('user_details_loggedin_customlinkone_type') != 0) {
    $variables['user_details_loggedin_customlinkone_url'] = variable_get('user_details_loggedin_customlinkone_url');
    $variables['user_details_loggedin_customlinkone_imgsrc'] = variable_get('user_details_loggedin_customlinkone_imgsrc');
    $variables['user_details_loggedin_customlinkone_imgtitle'] = variable_get('user_details_loggedin_customlinkone_imgtitle');
  }
  if (variable_get('user_details_loggedin_customlinktwo_type') != 0) {
    $variables['user_details_loggedin_customlinktwo_url'] = variable_get('user_details_loggedin_customlinktwo_url');
    $variables['user_details_loggedin_customlinktwo_imgsrc'] = variable_get('user_details_loggedin_customlinktwo_imgsrc');
    $variables['user_details_loggedin_customlinktwo_imgtitle'] = variable_get('user_details_loggedin_customlinktwo_imgtitle');
  }
  if (variable_get('user_details_loggedin_customlinkthree_type') != 0) {
    $variables['user_details_loggedin_customlinkthree_url'] = variable_get('user_details_loggedin_customlinkthree_url');
    $variables['user_details_loggedin_customlinkthree_imgsrc'] = variable_get('user_details_loggedin_customlinkthree_imgsrc');
    $variables['user_details_loggedin_customlinkthree_imgtitle'] = variable_get('user_details_loggedin_customlinkthree_imgtitle');
  }
  if (variable_get('user_details_loggedin_customlinkfour_type') != 0) {
    $variables['user_details_loggedin_customlinkfour_url'] = variable_get('user_details_loggedin_customlinkfour_url');
    $variables['user_details_loggedin_customlinkfour_imgsrc'] = variable_get('user_details_loggedin_customlinkfour_imgsrc');
    $variables['user_details_loggedin_customlinkfour_imgtitle'] = variable_get('user_details_loggedin_customlinkfour_imgtitle');
  }
  if (variable_get('user_details_loggedin_customlinkfive_type') != 0) {
    $variables['user_details_loggedin_customlinkfive_url'] = variable_get('user_details_loggedin_customlinkfive_url');
    $variables['user_details_loggedin_customlinkfive_imgsrc'] = variable_get('user_details_loggedin_customlinkfive_imgsrc');
    $variables['user_details_loggedin_customlinkfive_imgtitle'] = variable_get('user_details_loggedin_customlinkfive_imgtitle');
  }
  // User links HR
  if (variable_get('user_details_loggedin_frontlink_display') != 0 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_profilelink_display') != 0 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_profileeditlink_display') != 0 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_privatemsglink_display') != 0 && module_exists('privatemsg') && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_createlink_display') != 0 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_logoutlink_display') != 0 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkone_type') == 2 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinktwo_type') == 2 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkthree_type') == 2 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkfour_type') == 2 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkfive_type') == 2 && variable_get('user_details_loggedin_links_hr_display') != 0) {
    $variables['user_details_loggedin_links_hr'] = '<hr>';
  }
  // Admin links HR
  if (variable_get('user_details_loggedin_adminlink_display') != 0 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_panelslink_display') != 0 && module_exists('panels') && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_viewslink_display') != 0 && module_exists('views') && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_performancelink_display') != 0 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkone_type') == 3 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinktwo_type') == 3 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkthree_type') == 3 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkfour_type') == 3 && variable_get('user_details_loggedin_links_hr_display') != 0 || variable_get('user_details_loggedin_customlinkfive_type') == 3 && variable_get('user_details_loggedin_links_hr_display') != 0) {
    $variables['user_details_loggedin_links_hr'] = '<hr>';
  }
  if (variable_get('user_details_loggedin_content_amount') !=0 && variable_get('user_details_loggedin_content_display') != 0) {
    $user_details_loggedin_content_amount = variable_get('user_details_loggedin_content_amount');
  }
  if (variable_get('user_details_loggedin_content_display') != 0 && variable_get('user_details_loggedin_content_amount') != 0) {
    unset($output);
    if ($user->uid) {
    // Sql query.
      $user_details_loggedin_content_query = db_select('node', 'n');
      $user_details_loggedin_content_query
        ->condition('n.uid', $user->uid, '=')
        ->fields('n', array('created', 'title', 'nid', 'changed', 'type', 'status'))
        ->range(0, $user_details_loggedin_content_amount)
        ->orderBy('changed', 'DESC');
      $user_details_loggedin_content_fetchfield = $user_details_loggedin_content_query->execute();
      $output = '';
      foreach ($user_details_loggedin_content_fetchfield as $user_details_loggedin_content_node ) {
        $co++;
        $output .= '<li>';
        $output .= l($user_details_loggedin_content_node->title, "node/$user_details_loggedin_content_node->nid");
        $output .= '</li>';
      }
      $output .= '';
      // Show only if user have made some content.
      if ($co>0) {
        $variables['user_details_loggedin_content'] = $output;
      }
      if ($co == 0) {
        $variables['user_details_loggedin_content'] = t('No content to list.');
      }
    }
  }
}

/**
 * Process variables for user-details-authored.tpl.php.
 *
 * This function takes the result of the stored variable(options checkbox) and
 * creates a variable result from it.
 */
function template_preprocess_user_details_authored(&$variables) {
  drupal_add_css(drupal_get_path('module', 'user_details') . '/css/user_details_authored.css');
  $img_dir = drupal_get_path('module', 'user_details') . "/images/";
  // If the page is a node we need to load the user data from the node ID(nid)
  // profile.
  $co = $output = NULL;
  if (arg(0) == 'node') {
    $node_load = node_load(arg(1));
    $uid = $node_load->uid;
    $user_load = user_load($uid);
  }
  // If the page is a user profile we need to load the user data from the user profile.
  if (arg(0) == 'user') {
    $user_arg = user_load(arg(1));
    $uid = $user_arg->uid;
    $user_load = user_load($uid);
  }
  if (variable_get('user_details_authored_avatar_display') !=0) {
    $variables['user_details_authored_avatar'] = theme('user_picture', array('account' => $user_load));
  }
  if (variable_get('user_details_authored_joined_display') != 0) {
    $user_details_authored_date = date('c', $user_load->created);
    $user_details_authored_dateconvert = date('M j, Y', strtotime($user_details_authored_date));
    $variables['user_details_authored_joined_title'] = t('Joined');
    $variables['user_details_authored_joined_result'] = t('&nbsp;%joined', array('%joined' => $user_details_authored_dateconvert));
  }
  if (variable_get('user_details_authored_postcount_display') != 0) {
    $user_details_authored_postcount_query = db_select('node', 'n');
    $user_details_authored_postcount_query
      ->condition('n.uid', $user_load->uid, '=')
      ->fields('n', array('uid'));
    $user_details_authored_postcount_fetchfield = $user_details_authored_postcount_query->countQuery()->execute()->fetchField();
    $variables['user_details_authored_postcount_title'] = t('Posts');
    $variables['user_details_authored_postcount_result'] = t('%postcount', array('%postcount' => $user_details_authored_postcount_fetchfield));
  }  
  if (variable_get('user_details_authored_points_display') !=0 && module_exists('userpoints')) {
    $variables['user_details_authored_points_title'] = t('!Points', userpoints_translation());
    $variables['user_details_authored_points_result'] = userpoints_get_current_points($user_load->uid, 'all');
  }
  if (variable_get('user_details_authored_rank_display') != 0) {
    $user_details_authored_rank_query = db_select('users_roles');
    $user_details_authored_rank_query
      ->condition('uid', $user_load->uid, '=')
      ->fields('users_roles', array('rid'));
    $user_details_authored_rank_fetchfield = $user_details_authored_rank_query->execute()->fetchField();
    if (($user_details_authored_rank_fetchfield) == NULL) {
      $user_details_authored_rank_fetchfield = 2;
    }
    $user_details_authored_rankname_query = db_select('role', 'r');
    $user_details_authored_rankname_query
      ->condition('rid', $user_details_authored_rank_fetchfield, '=')
      ->fields('r', array('name'));
    $user_details_authored_rankname_fetchfield = $user_details_authored_rankname_query->execute()->fetchField();
    $variables['user_details_authored_rank_title'] = t('Role');
    $variables['user_details_authored_rank_result'] = t('%rank', array('%rank' => $user_details_authored_rankname_fetchfield));
  }
  if (variable_get('user_details_authored_avatar_display') !=0 && variable_get('user_details_authored_stats_hr_display') != 0 || variable_get('user_details_authored_joined_display') != 0 && variable_get('user_details_authored_stats_hr_display') != 0 || variable_get('user_details_authored_postcount_display') != 0 && variable_get('user_details_authored_stats_hr_display') != 0 || variable_get('user_details_authored_points_display') !=0 && module_exists('userpoints') && variable_get('user_details_authored_stats_hr_display') != 0 || variable_get('user_details_authored_rank_display') != 0 && variable_get('user_details_authored_stats_hr_display') != 0) {
    $variables['user_details_authored_stats_hr'] = '<hr>';
  }
  if (variable_get('user_details_authored_profilelink_display') != 0) {
    $variables['user_details_authored_profilelink_url'] = t('/user/' . $user_load->uid);
    $variables['user_details_authored_profilelink_imgsrc'] = variable_get('user_details_loggedin_profilelink_imgsrc');
    $variables['user_details_authored_profilelink_imgtitle'] = t('Author\'s profile');
  }
  if (variable_get('user_details_authored_privatemsglink_display') != 0 && module_exists('privatemsg')) {
    $variables['user_details_authored_privatemsglink_url'] = t('/messages/new/' . $user_load->uid);
    $variables['user_details_authored_privatemsglink_imgsrc'] = variable_get('user_details_loggedin_privatemsglink_imgsrc');
    $variables['user_details_authored_privatemsglink_imgtitle'] = t('Private message the author');
  }
  if (variable_get('user_details_authored_profilelink_display') != 0 && variable_get('user_details_authored_links_hr_display') != 0 || variable_get('user_details_authored_privatemsglink_display') != 0 && module_exists('privatemsg') && variable_get('user_details_authored_links_hr_display') != 0) {
    $variables['user_details_authored_links_hr'] = '<hr>';
  }
  if (variable_get('user_details_authored_content_amount') !=0 && variable_get('user_details_authored_content_display') !=0) {
    $user_details_authored_content_amount = variable_get('user_details_authored_content_amount');
  }
  if (variable_get('user_details_authored_content_display') != 0 && variable_get('user_details_authored_content_amount') != 0) {
    unset($output);
    if ($user_load->uid) {
      // Sql query.
      $user_details_authored_content_query = db_select('node', 'n');
      $user_details_authored_content_query
        ->condition('n.uid', $user_load->uid, '=')
        ->fields('n', array('created', 'title', 'nid', 'changed', 'type', 'status'))
        ->range(0, 3)
        ->orderBy('changed', 'DESC');
      $user_details_authored_content_fetchfield = $user_details_authored_content_query->execute();
      $output = '';
      foreach ($user_details_authored_content_fetchfield as $user_details_authored_content_node ) {
        $co++;
        $output .= '<li>';
        $output .= l($user_details_authored_content_node->title, "node/$user_details_authored_content_node->nid");
        $output .= '</li>';
      }
      $output .= '';
      if ($co>0) {
        $variables['user_details_authored_content'] = $output;
      }
      if ($co == 0) {
        $variables['user_details_authored_content'] = t('No content to list.');
      }
    }
  }
}

/**
 * Used to push the variables to the theme layer for the "logged-in" block.
 */
function user_details_loggedin_block_content() {
  $content = array(
    $user_details_loggedin_avatar = 'user_details_loggedin_avatar',
    $user_details_loggedin_points_title = 'user_details_loggedin_points_title',
    $user_details_loggedin_points_result = 'user_details_loggedin_points_result',
    $user_details_loggedin_joined_title = 'user_details_loggedin_joined_title',
    $user_details_loggedin_joined_result = 'user_details_loggedin_joined_result',
    $user_details_loggedin_postcount_title = 'user_details_loggedin_postcount_title',
    $user_details_loggedin_postcount_result = 'user_details_loggedin_postcount_result',
    $user_details_loggedin_rank_title = 'user_details_loggedin_rank_title',
    $user_details_loggedin_rank_result = 'user_details_loggedin_rank_result',
    $user_details_loggedin_privatemsg_count_title = 'user_details_loggedin_privatemsg_title',
    $user_details_loggedin_privatemsg_count_result = 'user_details_loggedin_privatemsg_result',
    $user_details_loggedin_stats_hr = 'user_details_loggedin_stats_hr',
    $user_details_loggedin_frontlink_url = 'user_details_loggedin_profilelink_url',
    $user_details_loggedin_frontlink_imgsrc = 'user_details_loggedin_profilelink_imgsrc',
    $user_details_loggedin_frontlink_imgtitle = 'user_details_loggedin_profilelink_imgtitle',
    $user_details_loggedin_profilelink_url = 'user_details_loggedin_profilelink_url',
    $user_details_loggedin_profilelink_imgsrc = 'user_details_loggedin_profilelink_imgsrc',
    $user_details_loggedin_profilelink_imgtitle = 'user_details_loggedin_profilelink_imgtitle',
    $user_details_loggedin_profileeditlink_url = 'user_details_loggedin_profilelink_url',
    $user_details_loggedin_profileeditlink_imgsrc = 'user_details_loggedin_profileeditlink_imgsrc',
    $user_details_loggedin_profileeditlink_imgtitle = 'user_details_loggedin_profilelink_imgtitle',
    $user_details_loggedin_privatemsglink_url = 'user_details_loggedin_privatemsglink',
    $user_details_loggedin_privatemsglink_imgsrc = 'user_details_loggedin_privatemsglink_imgsrc',
    $user_details_loggedin_privatemsglink_imgtitle = 'user_details_loggedin_privatemsglinkimgtitle',
    $user_details_loggedin_createlink_url = 'user_details_loggedin_createlink_url',
    $user_details_loggedin_createlink_imgsrc = 'user_details_loggedin_createlink_imgsrc',
    $user_details_loggedin_createlink_imgtitle = 'user_details_loggedin_createlink_imgtitle',
    $user_details_loggedin_adminlink_url = 'user_details_loggedin_adminlink_url',
    $user_details_loggedin_adminlink_imgsrc = 'user_details_loggedin_adminlink_imgsrc',
    $user_details_loggedin_adminlink_imgtitle = 'user_details_loggedin_adminlink_imgtitle',
    $user_details_loggedin_panelslink_url = 'user_details_loggedin_panelslink_url',
    $user_details_loggedin_panelslink_imgsrc = 'user_details_loggedin_panelslink_imgsrc',
    $user_details_loggedin_panelslink_imgtitle = 'user_details_loggedin_panelslink_imgtitle',
    $user_details_loggedin_viewslink_url = 'user_details_loggedin_viewslink_url',
    $user_details_loggedin_viewslink_imgsrc = 'user_details_loggedin_viewslink_imgsrc',
    $user_details_loggedin_viewslink_imgtitle = 'user_details_loggedin_viewslink_imgtitle',
    $user_details_loggedin_performancelink_url = 'user_details_loggedin_performancelink_url',
    $user_Details_loggedin_performancelink_imgsrc = 'user_details_loggedin_performancelink_imgsrc',
    $user_details_loggedin_performancelink_imgtitle = 'user_details_loggedin_performancelink_imgtitle',
    $user_details_loggedin_logoutlink_url = 'user_details_loggedin_logoutlink_url',
    $user_details_loggedin_logoutlink_imgsrc = 'user_details_loggedin_logoutlink_imgsrc',
    $user_details_loggedin_logoutlink_imgtitle = 'user_details_loggedin_logoutlink_imgtitle',
    $user_details_loggedin_customlinkone_url = 'user_details_loggedin_customlinkone_url',
    $user_details_loggedin_customlinkone_imgsrc = 'user_details_loggedin_customlinkone_imgsrc',
    $user_details_loggedin_customlinkone_imgtitle = 'user_details_loggedin_customlinkone_imgtitle',
    $user_details_loggedin_customlinktwo_url = 'user_details_loggedin_customlinktwo_url',
    $user_details_loggedin_customlinktwo_imgsrc = 'user_details_loggedin_customlinktwo_imgsrc',
    $user_details_loggedin_customlinktwo_imgtitle = 'user_details_loggedin_customlinktwo_imgtitle',
    $user_details_loggedin_customlinkthree_url = 'user_details_loggedin_customlinkthree_url',
    $user_details_loggedin_customlinkthree_imgsrc = 'user_details_loggedin_customlinkthree_imgsrc',
    $user_details_loggedin_customlinkthree_imgtitle = 'user_details_loggedin_customlinkthree_imgtitle',
    $user_details_loggedin_customlinkfour_url = 'user_details_loggedin_customlinkfour_url',
    $user_details_loggedin_customlinkfour_imgsrc = 'user_details_loggedin_customlinkfour_imgsrc',
    $user_details_loggedin_customlinkfour_imgtitle = 'user_details_loggedin_customlinkfour_imgtitle',
    $user_details_loggedin_customlinkfive_url = 'user_details_loggedin_customlinkfive_url',
    $user_details_loggedin_customlinkfive_imgsrc = 'user_details_loggedin_customlinkfive_imgsrc',
    $user_details_loggedin_customlinkfive_imgtitle = 'user_details_loggedin_customlinkfive_imgtitle',
    $user_details_loggedin_links_hr = 'user_details_loggedin_links_hr',
    $user_details_loggedin_content = 'user_details_loggedin_content',
  );
  $output = theme('user_details_loggedin', $content);
  return $output;
}

/**
 * Used to push the variables to the theme layer for the "Authored by" block.
 */
function user_details_authored_block_content() {
  $content = array(
    $user_details_authored_avatar = 'user_details_authored_avatar',
    $user_details_authored_joined = 'user_details_authored_joined',
    $user_details_authored_postcount_title = 'user_details_authored_postcount_title',
    $user_details_authored_postcount_result = 'user_details_authored_postcount_result',
    $user_details_authored_points_title = 'user_details_authored_points_title',
    $user_details_authored_points_result = 'user_details_authored_points_result',
    $user_details_authored_rank_title = 'user_details_authored_rank_title',
    $user_details_authored_rank_result = 'user_details_authored_rank_result',
    $user_details_authored_stats_hr = 'user_details_authored_stats_hr',
    $user_details_authored_profilelink_url = 'user_details_authored_profilelink_url',
    $user_details_authored_profilelink_imgsrc = 'user_details_authored_profilelink_imgsrc',
    $user_details_authored_profilelink_imgtitle = 'user_details_authored_profilelink_imgtitle',
    $user_details_authored_privatemsglink_url = 'user_details_authored_privatemsglink',
    $user_details_authored_privatemsglink_imgsrc = 'user_details_authored_privatemsglink_imgsrc',
    $user_details_authored_privatemsglink_imgtitle = 'user_details_authored_privatemsglinkimgtitle',
    $user_details_authored_links_hr = 'user_details_authored_links_hr',
    $user_details_authored_content = 'user_details_authored_content',
  );
  $output = theme('user_details_authored', $content);
  return $output;
}