You are here

author_pane.module in Author Pane 5

File

author_pane.module
View source
<?php

/**
 * @file
 * Gathers information from user related contrib modules into one template.
 */
include_once drupal_get_path('module', 'author_pane') . '/d6_compat.inc';

// DRUPAL HOOKS **************************************************************/

/**
 * Implementation of hook_block().
 */
function author_pane_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Author Pane');
      return $blocks;

    // Block settings
    case 'configure':

      // Template file
      $form['author_pane_block_template'] = array(
        '#type' => 'textfield',
        '#title' => t('Template file'),
        '#description' => t('Template file to use. Do not include .tpl.php. Default is author-pane-block.'),
        '#default_value' => variable_get('author_pane_block_template', 'author-pane-block'),
      );

      // Image path
      $form['author_pane_block_image_path'] = array(
        '#type' => 'textfield',
        '#title' => t('Image path'),
        '#description' => t('Path to author pane images.  Leave blank to use default.'),
        '#default_value' => variable_get('author_pane_block_image_path', ''),
      );

      // Node types
      $types = node_get_types();
      $options = array();
      foreach ($types as $type) {
        $options[$type->type] = $type->name;
      }
      $form['author_pane_block_display_types'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Node types to display on'),
        '#options' => $options,
        '#default_value' => variable_get('author_pane_block_display_types', array()),
      );

      // Imagecache preset
      if (module_exists('imagecache') && function_exists('imagecache_presets')) {
        $options = array(
          '' => '',
        );
        $presets = imagecache_presets();
        foreach ($presets as $preset) {
          $options[$preset['presetname']] = $preset['presetname'];
        }
        $form['author_pane_block_user_picture_preset'] = array(
          '#type' => 'select',
          '#title' => t('User picture preset'),
          '#options' => $options,
          '#description' => t('Imagecache preset to use for the user picture on this block. Leave blank to not use this feature.'),
          '#default_value' => variable_get('author_pane_block_user_picture_preset', ''),
        );
      }
      return $form;
    case 'save':
      variable_set('author_pane_block_image_path', $edit['author_pane_block_image_path']);
      variable_set('author_pane_block_template', $edit['author_pane_block_template']);
      variable_set('author_pane_block_display_types', $edit['author_pane_block_display_types']);
      variable_set('author_pane_block_user_picture_preset', $edit['author_pane_block_user_picture_preset']);
      return;
    case 'view':
      $block = array();
      $block['subject'] = t('Author Information');
      $block['content'] = author_pane_get_block();
      return $block;
  }
}

// TEMPLATE PREPROCESS *******************************************************/

/**
 * Load author pane files on behalf of modules.
 *
 * This function, taken from the views include system, allows us to include
 * an appropriately named include file bundled with any enabled module.
 * It is currently used only to load the MODULE.author-pane.inc files which
 * allow other modules to add to the author pane.
 */
function author_pane_include($file) {
  $includes = array();
  $author_pane_path = drupal_get_path('module', 'author_pane') . '/modules';
  foreach (module_list() as $module) {
    $module_path = drupal_get_path('module', $module);
    if (file_exists("{$module_path}/{$module}.{$file}")) {
      $includes[] = "./{$module_path}/{$module}.{$file}";
    }
    else {
      if (file_exists("{$module_path}/includes/{$module}.{$file}")) {
        $includes[] = "./{$module_path}/includes/{$module}.{$file}";
      }
      else {
        if (file_exists("{$author_pane_path}/{$module}.{$file}")) {
          $includes[] = "./{$author_pane_path}/{$module}.{$file}";
        }
      }
    }
  }
  if (!empty($includes)) {
    foreach ($includes as $include) {
      require_once $include;
    }
  }
}

/**
 * Theme function wrapper around D6 style preprocess function.
 */
function theme_author_pane($account, $image_path = NULL, $template_suggestion = NULL) {
  $variables = array();
  $variables['account'] = $account;
  $variables['image_path'] = $image_path;
  $variables['template_suggestion'] = $template_suggestion;

  // Load up all the integration files from other modules.
  author_pane_include('author-pane.inc');

  // Call our preprocess function to create all the variables
  author_pane_call_preprocess('author_pane', $variables);
  return _phptemplate_callback("author_pane", $variables, array(
    "author-pane",
  ));
}

/**
 * Preprocesses template variables for the author info template.
 */
function template_preprocess_author_pane(&$variables) {
  if (!empty($variables['template_suggestion'])) {

    // Allow the calling function to suggest a template file.
    // This allows AF to set a seperate template from APK.
    $variables['template_files'][] = $variables['template_suggestion'];
  }

  // Set the image path
  if (empty($variables['image_path'])) {

    // Default images
    $variables['image_path'] = drupal_get_path('module', 'author_pane') . '/images';
  }
  else {
    $path_to_theme = path_to_theme();

    // Replace [theme_path] token with actual path to theme.
    $variables['image_path'] = str_replace('[theme_path]', $path_to_theme, $variables['image_path']);
  }
  $image_path = $variables['image_path'];

  // Give it some basic formatting.
  drupal_add_css(drupal_get_path('module', 'author_pane') . '/author_pane.css');

  // --- Set some basic variables not related to any module. ---
  // This $account refers to the user who's info is in the pane.
  $account = $variables['account'];
  $account_id = $account->uid;

  // Username
  $variables['account_name'] = theme('username', $account);
  $variables['account_id'] = $account_id;

  // Avatar
  $block_template = variable_get('author_pane_block_template', 'author-pane-block');
  if ($variables['template_suggestion'] == $block_template) {

    // Imagecache'd resizing is only available for the block
    $sized_picture = theme('author_pane_block_user_picture', $variables['account']);
    if (!empty($sized_picture)) {
      $variables['picture'] = $sized_picture;
    }
  }
  if (empty($variables['picture'])) {
    $variables['picture'] = theme('user_picture', $account);
  }
  if ($account_id != 0) {

    // Join date / since
    $just_date = str_replace(array(
      'H:i',
      'g:ia',
      ' - ',
    ), '', variable_get('date_format_short', 'm/d/Y - H:i'));
    $variables['joined'] = format_date($account->created, 'custom', $just_date);
    $variables['joined_ago'] = format_interval(time() - $account->created);

    // Online status
    $variables['last_active'] = format_interval(time() - $account->access);
    if (round((time() - $account->access) / 60) < 15) {
      $variables['online_icon'] = theme('image', "{$image_path}/user-online.png", t('User is online'), t('User is online'));
      $variables['online_status'] = t('Online');
    }
    else {
      $variables['online_icon'] = theme('image', "{$image_path}/user-offline.png", t('User offline. Last seen @time ago.', array(
        '@time' => $variables['last_active'],
      )), t('User offline. Last seen @time ago.', array(
        '@time' => $variables['last_active'],
      )));
      $variables['online_status'] = t('Offline');
    }
  }
}

// PANELS *********************************************************************

/**
 * Implementation of hook_panels_include_directory().
 */
function author_pane_panels_include_directory($plugintype) {
  if ($plugintype == 'content_types') {
    return 'panels_content_types';
  }
}

// GENERAL FUNCTIONS *********************************************************/
function author_pane_get_block() {
  $image_path = variable_get('author_pane_block_image_path', NULL);
  $template = variable_get('author_pane_block_template', NULL);
  $area = arg(0);

  // Check that we're in the right area.
  if (!($area == 'user' || $area == 'node' || $area == 'blog') || !is_numeric(arg(1)) || arg(2) != '') {
    return;
  }
  if ($area == 'user' || $area == 'blog') {

    // On the user page or the user's blog listing. Get the UID from the URL.
    $uid = arg(1);
  }
  else {

    // We're on a node page so load the node.
    $node = node_load(arg(1));
    $allowed_types = variable_get('author_pane_block_display_types', array());
    if (empty($allowed_types[$node->type])) {

      // Not a type we want to show on.
      return;
    }
    $uid = $node->uid;
  }

  // Load up the user object
  $account = user_load(array(
    'uid' => $uid,
  ));
  $author_pane = theme('author_pane', $account, $image_path, $template);
  return $author_pane;
}

// THEME FUNCTIONS ***********************************************************/

/**
 * Theme function to return imagecached version of the user picture.
 */
function theme_author_pane_block_user_picture($account) {

  // Get the imagecache preset to use, if any.
  $preset = variable_get('author_pane_block_user_picture_preset', '');

  // Only proceed if user pictures are enabled, and there is a preset set, and
  // imagecache is enabled. For performace reasons, we return nothing if these
  // critera aren't met because we only call this function when the non
  // imagecached version is already created. If you use this function elsewhere
  // you will need to provide a picture when imagecache is not used.
  if (variable_get('user_pictures', 0) && !empty($preset) && module_exists('imagecache')) {
    if ($account->picture && file_exists($account->picture)) {

      // User has picture, so use that
      $alt = t("@user's picture", array(
        '@user' => $account->name ? $account->name : 'Visitor',
      ));
      $picture = theme('imagecache', $preset, $account->picture);
    }
    else {

      // User doesn't have a picture so use the default, if any
      $default_picture = variable_get('user_picture_default', '');
      if ($default_picture) {
        $picture = theme('imagecache', $preset, $default_picture);
      }
    }
    if (!empty($picture)) {

      // If the user has access to view profiles, link the picture
      if (!empty($account->uid) && user_access('access user profiles')) {
        $picture = l($picture, "user/{$account->uid}", array(
          'title' => t('View user profile.'),
        ), NULL, NULL, FALSE, TRUE);
      }
    }
    return '<div class="picture">' . $picture . '</div>';
  }
}

Functions

Namesort descending Description
author_pane_block Implementation of hook_block().
author_pane_get_block
author_pane_include Load author pane files on behalf of modules.
author_pane_panels_include_directory Implementation of hook_panels_include_directory().
template_preprocess_author_pane Preprocesses template variables for the author info template.
theme_author_pane Theme function wrapper around D6 style preprocess function.
theme_author_pane_block_user_picture Theme function to return imagecached version of the user picture.