You are here

author_pane.module in Author Pane 6.2

Gathers information from user related modules into one template.

File

author_pane.module
View source
<?php

/**
 * @file
 * Gathers information from user related modules into one template.
 */

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

/**
 * Implementation of hook_theme().
 */
function author_pane_theme() {
  author_pane_include('author-pane.inc');
  $items['author_pane'] = array(
    'template' => 'author-pane',
    'arguments' => array(
      'account' => NULL,
      'caller' => NULL,
      'picture_preset' => NULL,
      'context' => NULL,
      'disable_css' => NULL,
    ),
  );
  $items['author_pane_user_picture'] = array(
    'template' => 'author-pane-user-picture',
    'arguments' => array(
      'account' => NULL,
      'caller' => NULL,
      'picture_preset' => NULL,
    ),
  );
  return $items;
}

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

      // We don't want the block to cache since what is displayed depends on
      // both the user viewing and the user being viewed.
      $blocks[0]['cache'] = BLOCK_NO_CACHE;
      return $blocks;
    case 'configure':

      // Get a list of all node types.
      $types = node_get_types();
      $options = array();
      foreach ($types as $type) {
        $options[$type->type] = $type->name;
      }

      // Allow user to choose which node types will display the block.
      $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()),
      );
      if (module_exists('imagecache') && function_exists('imagecache_presets')) {

        // Get all the imagecache presets on the site.
        $options = array(
          '' => '',
        );
        $presets = imagecache_presets();
        foreach ($presets as $preset) {
          $options[$preset['presetname']] = $preset['presetname'];
        }

        // Allow the user to choose a preset to use.
        $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_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}";
    }
    elseif (file_exists("{$module_path}/includes/{$module}.{$file}")) {
      $includes[] = "./{$module_path}/includes/{$module}.{$file}";
    }
    elseif (file_exists("{$author_pane_path}/{$module}.{$file}")) {
      $includes[] = "./{$author_pane_path}/{$module}.{$file}";
    }
  }
  if (!empty($includes)) {
    foreach ($includes as $include) {
      require_once $include;
    }
  }
}

/**
 * Preprocesses template variables for the author info template.
 *
 * Available variables (All optional except 'account'):
 * $variables['account']: User account object.
 * $variables['caller']:  String identifying who called the theme function.
 *    Usually the name of the module but doesn't have to be.
 * $variables['picture_preset']: Imagecache preset to use to format the
 *    user picture.
 * $variables['context']: Information about where the Author Pane will be
 *    appearing. For nodes, this will be the node object. For comments,
 *    the comment object.
 * $variables['disable_css']: TRUE if the preprocess should skip loading the
 *    default CSS. This is used by modules such as AF that has its own CSS.
 */
function template_preprocess_author_pane(&$variables) {

  // Indicates who called the theme function.
  $caller = !empty($variables['caller']) ? $variables['caller'] : '';

  /* Add CSS */
  if (empty($variables['disable_css'])) {

    // Some modules have their own Author Pane CSS. Because Author Pane is
    // called in a theme function, this CSS would get added after and clobber
    // the CSS in those modules. So we don't load the CSS in that case.
    drupal_add_css(drupal_get_path('module', 'author_pane') . '/author_pane.css');
  }

  /* Account ID & Name */

  // This $account refers to the user whose info is in the pane.
  $variables['account']->uid = empty($variables['account']->uid) ? 0 : $variables['account']->uid;
  $account = $variables['account'];
  $account_id = $account->uid;
  $variables['account_name'] = theme('username', $account);
  $variables['account_id'] = $account_id;

  /* Avatar */
  static $user_pictures;
  if (!empty($user_pictures[$account_id])) {

    // This user's picture is cached so pull it from there.
    $variables['picture'] = $user_pictures[$account_id];
  }
  else {
    $preset = !empty($variables['picture_preset']) ? $variables['picture_preset'] : '';
    $variables['picture'] = theme('author_pane_user_picture', $variables['account'], $caller, $preset);
    $user_pictures[$account_id] = $variables['picture'];
  }

  /* Join date & online status */
  if ($account_id != 0) {

    // Join date (uses short date format) / 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 - uses the settings for the who's online block.
    $variables['last_active'] = $account->access ? format_interval(time() - $account->access) : t("Never");
    if (time() - $account->access < variable_get('user_block_seconds_online', 900)) {
      $variables['online_status'] = t('Online');
      $variables['online_status_class'] = 'author-online';
    }
    else {
      $variables['online_status'] = t('Offline');
      $variables['online_status_class'] = 'author-offline';
    }
  }
  else {

    // Set the variables to empty to avoid notices when the template is displayed.
    $variables['joined'] = $variables['joined_ago'] = $variables['online_class'] = $variables['online_status'] = '';
  }

  // This variable is no longer used, but previous integrations are expecting
  // it. Pass it the path to the images so they don't break.
  $variables['image_path'] = drupal_get_path('module', 'author_pane') . '/images';

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

/**
 * Preprocesses template variables for the author pane picture template.
 *
 * Available variables (All optional except 'account'):
 * $variables['account']: User account object.
 * $variables['caller']:  String identifying who called the theme function.
 *    Usually the name of the module but doesn't have to be.
 * $variables['picture_preset']: Imagecache preset to use to format the
 *    user picture.
 */
function template_preprocess_author_pane_user_picture(&$variables) {
  $variables['picture'] = '';
  $account = $variables['account'];

  // Get the imagecache preset, if any.
  $preset = !empty($variables['picture_preset']) ? $variables['picture_preset'] : '';

  // If user pictures are enabled...
  if (variable_get('user_pictures', 0)) {

    // Get the user's avatar if they have one or the default picture if exists.
    if (!empty($account->picture) && file_exists($account->picture)) {

      // We only want to get the full URL if not using imagecache.
      $picture = !empty($preset) && module_exists('imagecache') ? $account->picture : file_create_url($account->picture);
    }
    elseif (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }

    // If we have a picture...
    if (isset($picture)) {

      // If there's a preset set and imagecache is enabled...
      if (!empty($preset) && module_exists('imagecache')) {

        // Toss the picture over to imagecache for sizing
        $alt = t("@user's picture", array(
          '@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous')),
        ));
        $variables['picture'] = theme('imagecache', $preset, $picture, $alt, $alt);
        $variables['imagecache_used'] = TRUE;
      }
      else {

        // Just run the picture through theme_image. Note that we don't link
        // the picture here since it doesn't make sense for many uses of AP.
        // If the picture needs to be linked, it can be done in the template.
        $alt = t("@user's picture", array(
          '@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous')),
        ));
        $variables['picture'] = theme('image', $picture, $alt, $alt, '', FALSE);
        $variables['imagecache_used'] = FALSE;
      }
    }
  }
}

// PANELS / CTOOLS **********************************************************/

/**
 * Implementation of hook_ctools_plugin_directory().
 */
function author_pane_ctools_plugin_directory($module, $plugin) {
  if ($module == 'ctools') {
    return 'plugins/' . $plugin;
  }
}

// GENERAL FUNCTIONS ********************************************************/

/**
 * Defines an API version.
 */
function author_pane_api() {
  return "2";
}

/**
 * Creates the contents of the block. Called from author_pane_block().
 */
function author_pane_get_block() {
  $area = arg(0);
  $context = NULL;

  // Check that we're in the right area. The block only works on the user pages,
  // node full view pages, and the blog listing pages. It also does not work on
  // the "edit" subpath.
  if (!($area == 'user' || $area == 'node' || $area == 'blog') || !is_numeric(arg(1)) || arg(2) == 'edit') {
    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 = menu_get_object();
    $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;

    // When we're displaying along with a node, we'll want to send the node
    // object into the theme function.
    $context = $node;
  }

  // Load up the user object
  $account = user_load($uid);

  // Build the author pane
  $author_pane = theme('author_pane', $account, 'author_pane_block', variable_get('author_pane_block_user_picture_preset', ''), $context);
  return $author_pane;
}

/**
 * Determines if a given preprocess should run for a given caller.
 */
function author_pane_run_preprocess($module, $caller) {
  $caller_disabled_list = variable_get("author_pane_disable_for_{$caller}", NULL);
  if (!is_null($caller_disabled_list) && isset($caller_disabled_list[$module])) {

    // If this caller has a list of disabled modules and if this module
    // is listed, then return the opposite of the value for this caller.
    // (The variable is TRUE to disable and we want to return TRUE to run it)
    return !$caller_disabled_list[$module];
  }
  return TRUE;
}

Functions

Namesort descending Description
author_pane_api Defines an API version.
author_pane_block Implementation of hook_block().
author_pane_ctools_plugin_directory Implementation of hook_ctools_plugin_directory().
author_pane_get_block Creates the contents of the block. Called from author_pane_block().
author_pane_include Load Author Pane files on behalf of modules.
author_pane_run_preprocess Determines if a given preprocess should run for a given caller.
author_pane_theme Implementation of hook_theme().
template_preprocess_author_pane Preprocesses template variables for the author info template.
template_preprocess_author_pane_user_picture Preprocesses template variables for the author pane picture template.