You are here

imagecache_profiles.module in ImageCache Profiles 7

Same filename and directory in other branches
  1. 5 imagecache_profiles.module
  2. 6 imagecache_profiles.module

File

imagecache_profiles.module
View source
<?php

/**
 * Implements hook_help().
 */
function imagecache_profiles_help($path, $arg) {
  switch ($path) {
    case 'admin/help#imagecache_profiles':
      return t('Utilizes image styles for user profile pictures.');
  }
}

/**
 * Overrides process variables for user-picture.tpl.php
 *
 * originally built by template_preprocess_user_picture()
 * locate in core user.module.
 *
 * The $variables array contains the following arguments:
 * - $account
 *
 * @see user-picture.tpl.php
 */
function imagecache_profiles_preprocess_user_picture(&$variables) {
  if (variable_get('user_pictures', 0)) {
    $account = $variables['account'];
    if (!empty($account->picture)) {

      // @TODO: Ideally this function would only be passed file objects, but
      // since there's a lot of legacy code that JOINs the {users} table to
      // {node} or {comments} and passes the results into this function if we
      // a numeric value in the picture field we'll assume it's a file id
      // and load it for them. Once we've got user_load_multiple() and
      // comment_load_multiple() functions the user module will be able to load
      // the picture files in mass during the object's load process.
      if (is_numeric($account->picture)) {
        $account->picture = file_load($account->picture);
      }
      if (!empty($account->picture->uri)) {
        $filepath = $account->picture->uri;
      }
    }
    elseif (variable_get('user_picture_default', '')) {
      $filepath = variable_get('user_picture_default', '');
    }
    if (isset($variables['user_picture_style'])) {
      $style = $variables['user_picture_style'];
    }
    elseif (isset($account->picture->style_name)) {
      $style = $account->picture->style_name;
    }
    elseif (isset($account->user_picture_style)) {
      $style = $account->user_picture_style;
    }
    if (isset($filepath) && file_valid_uri($filepath) && !empty($style) && $style != variable_get('user_picture_style', '')) {
      $alt = t("@user's picture", array(
        '@user' => format_username($account),
      ));
      $variables['user_picture'] = theme('image_style', array(
        'style_name' => $style,
        'path' => $filepath,
        'alt' => $alt,
        'title' => $alt,
      ));
      if (!empty($account->uid) && user_access('access user profiles')) {
        $attributes = array(
          'attributes' => array(
            'title' => t('View user profile.'),
          ),
          'html' => TRUE,
        );
        $variables['user_picture'] = l($variables['user_picture'], "user/{$account->uid}", $attributes);
      }
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Extends user settings.
 */
function imagecache_profiles_form_user_admin_settings_alter(&$form, $form_state) {

  // Load image styles.
  $presets = image_style_options(TRUE);
  $form['personalization']['pictures']['settings']['user_picture_style_profiles'] = array(
    '#type' => 'select',
    '#title' => t('Profile picture display style'),
    '#default_value' => variable_get('user_picture_style_profiles', ''),
    '#options' => $presets,
    '#description' => t("This will set the picture size when viewing a user's profile page."),
  );
  $form['personalization']['pictures']['settings']['user_picture_style_comments'] = array(
    '#type' => 'select',
    '#title' => t('Comment picture display style'),
    '#default_value' => variable_get('user_picture_style_comments', ''),
    '#options' => $presets,
    '#description' => t("This will set the picture size when viewing a comment post."),
  );
  $form['personalization']['pictures']['settings']['user_picture_style_nodes'] = array(
    '#type' => 'select',
    '#title' => t('Node picture display style'),
    '#default_value' => variable_get('user_picture_style_nodes', ''),
    '#options' => $presets,
    '#description' => t("This will set the picture size when viewing a node post."),
  );
  $form['personalization']['pictures']['settings']['user_picture_imagecache_profiles_min_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Picture minimum width'),
    '#default_value' => variable_get('user_picture_imagecache_profiles_min_width', ''),
    '#description' => t('Minimum width dimension for picture, in pixels. To prevent upscaling this value should be set to your maximum imagecache preset width.'),
    '#size' => 10,
  );
  $form['personalization']['pictures']['settings']['user_picture_imagecache_profiles_min_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Picture minimum height'),
    '#default_value' => variable_get('user_picture_imagecache_profiles_min_height', ''),
    '#description' => t('Minimum height dimension for picture, in pixels. To prevent upscaling this value should be set to your maximum imagecache preset height.'),
    '#size' => 10,
  );
}

/**
 * Implements hook_file_validate().
 */
function imagecache_profiles_file_validate($file) {
  if (!empty($file->source) && $file->source == 'picture_upload') {
    $width = variable_get('user_picture_imagecache_profiles_min_width', 0);
    $height = variable_get('user_picture_imagecache_profiles_min_height', 0);
    $minimum_dimensions = $width . 'x' . $height;
    return file_validate_image_resolution($file, 0, $minimum_dimensions);
  }
}

/**
 * Implements hook_image_style_delete().
 */
function imagecache_profiles_image_style_delete($style) {

  // If a style is deleted, update the variables.
  // Administrators choose a replacement style when deleting.
  imagecache_profiles_image_style_save($style);
}

/**
 * Implements hook_image_style_save().
 */
function imagecache_profiles_image_style_save($style) {

  // If a style is renamed, update the variables that use it.
  if (isset($style['old_name'])) {

    // TODO: Make list of vars alterable.
    $vars = array(
      'user_picture_style_profiles',
      'user_picture_style_comments',
      'user_picture_style_nodes',
    );
    foreach ($vars as $var) {
      if ($style['old_name'] == variable_get($var)) {
        variable_set($var, $style['name']);
      }
    }
  }
}

/**
 * Implements hook_user_view_alter().
 *
 * Change the user profile picture to use the preferred image style.
 */
function imagecache_profiles_user_view_alter(&$build) {
  if (isset($build['user_picture']['#markup']) && ($style = variable_get('user_picture_style_profiles', ''))) {
    $picture = theme('user_picture', array(
      'account' => $build['#account'],
      'user_picture_style' => $style,
    ));
    $build['user_picture']['#markup'] = $picture;
  }
}

/**
 * Implements hook_comment_view().
 *
 * Change comment user pictures to use the preferred image style.
 */
function imagecache_profiles_comment_view($comment) {
  if (theme_get_setting('toggle_comment_user_picture')) {

    // If this is a comment preview, we need to manually add $comment->picture.
    if (!empty($comment->uid) && !isset($comment->picture)) {
      $comment->picture = db_query("SELECT picture FROM {users} WHERE uid = :uid", array(
        ':uid' => $comment->uid,
      ))
        ->fetchField();
    }
    if ($style = variable_get('user_picture_style_comments', '')) {
      $comment->user_picture_style = $style;
    }
  }
}

/**
 * Implements hook_node_view().
 *
 * Change node user pictures to use the preferred imagecache preset.
 */
function imagecache_profiles_node_view($node, $view_mode) {

  // TODO: Test with panels module, probably uses $op = alter to render nodes.
  if ($style = variable_get('user_picture_style_nodes', '')) {
    $node->user_picture_style = $style;
  }
}

/**
 * Implements hook_views_api().
 */
function imagecache_profiles_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implements hook_views_data_alter().
 */
function imagecache_profiles_views_data_alter(&$data) {
  $data['users']['picture']['field']['handler'] = 'imagecache_profiles_handler_field_user_picture';
}