You are here

imagecache_profiles.module in ImageCache Profiles 6

Same filename and directory in other branches
  1. 5 imagecache_profiles.module
  2. 7 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 imagecache presets for user profile pictures.');
  }
}

/**
 * Overrides process variables for user-picture.tpl.php
 *
 * Originally variables are 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) {
  $default = $variables['picture'];
  if (variable_get('user_pictures', 0)) {
    $account = $variables['account'];
    if (isset($account->imagecache_preset)) {

      // Manually set preset (e.g. Views)
      $preset = $account->imagecache_preset;
    }
    elseif (variable_get('user_picture_imagecache_profiles_default', '')) {

      // Default user picture preset.
      $preset = variable_get('user_picture_imagecache_profiles_default', '');
    }
    if (!empty($account->picture) && file_exists($account->picture)) {
      $picture = $account->picture;
    }
    elseif (variable_get('user_picture_default', '')) {
      $picture = variable_get('user_picture_default', '');
    }
    if (isset($picture)) {
      $alt = t("@user's picture", array(
        '@user' => $account->name ? $account->name : variable_get('anonymous', t('Anonymous')),
      ));
      if (isset($preset)) {
        $preset = is_numeric($preset) ? imagecache_preset($preset) : imagecache_preset_by_name($preset);
      }
      if (empty($preset)) {
        $variables['picture'] = $default;

        //theme('image', $picture, $alt, $alt, '', FALSE);
      }
      else {
        if (!empty($account->uid) && user_access('access user profiles')) {
          $attributes = array(
            'attributes' => array(
              'title' => t('View user profile.'),
            ),
            'html' => TRUE,
          );
          $image = theme('imagecache', $preset['presetname'], $picture, $alt, $alt);
          $variables['picture'] = l($image, "user/{$account->uid}", $attributes);
        }
        else {
          $variables['picture'] = theme('imagecache', $preset['presetname'], $picture, $alt, $alt);
        }
      }
    }
  }
}

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

  // Load imagecache presets
  $presets = array();
  $presets[] = '';
  foreach (imagecache_presets() as $preset) {
    $presets[$preset['presetname']] = check_plain($preset['presetname']);
  }
  $form['pictures']['settings']['user_picture_imagecache_profiles'] = array(
    '#type' => 'select',
    '#title' => t('Profile picture preset'),
    '#default_value' => variable_get('user_picture_imagecache_profiles', ''),
    '#options' => $presets,
    '#description' => t("This will set the picture size when viewing a user's profile page."),
  );
  $form['pictures']['settings']['user_picture_imagecache_comments'] = array(
    '#type' => 'select',
    '#title' => t('Comment picture preset'),
    '#default_value' => variable_get('user_picture_imagecache_comments', ''),
    '#options' => $presets,
    '#description' => t("This will set the picture size when viewing a comment post."),
  );
  $form['pictures']['settings']['user_picture_imagecache_nodes'] = array(
    '#type' => 'select',
    '#title' => t('Node picture preset'),
    '#default_value' => variable_get('user_picture_imagecache_nodes', ''),
    '#options' => $presets,
    '#description' => t("This will set the picture size when viewing a node post."),
  );
  $form['pictures']['settings']['user_picture_imagecache_profiles_default'] = array(
    '#type' => 'select',
    '#title' => t('Default picture preset'),
    '#default_value' => variable_get('user_picture_imagecache_profiles_default', ''),
    '#options' => $presets,
    '#description' => t('This will set the default user picture size throughout the site.'),
  );
  $form['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['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_form_FORM_ID_alter().
 *
 * @see imagecache_profiles_imagecache_ui_preset_form_submit()
 */
function imagecache_profiles_form_imagecache_ui_preset_form_alter(&$form, $form_state) {
  $form['#submit'][] = 'imagecache_profiles_imagecache_ui_preset_form_submit';
}

/**
 * Renames changed preset.
 */
function imagecache_profiles_imagecache_ui_preset_form_submit($form, &$form_state) {
  $preset = array(
    'presetid' => $form_state['values']['presetid'],
    'presetname_new' => $form_state['values']['presetname'],
    'presetname' => $form['#parameters'][2]['presetname'],
  );
  _imagecache_profiles_change_variables($preset, FALSE);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see imagecache_profiles_imagecache_ui_preset_delete_form_submit()
 */
function imagecache_profiles_form_imagecache_ui_preset_delete_form_alter(&$form, $form_state) {
  $form['#submit'][] = 'imagecache_profiles_imagecache_ui_preset_delete_form_submit';
}

/**
 * Unsets deleted preset.
 */
function imagecache_profiles_imagecache_ui_preset_delete_form_submit($form, &$form_state) {
  $preset = array(
    'presetid' => $form_state['values']['presetid'],
    'presetname_new' => $form['#parameters'][2]['presetname'],
    'presetname' => $form['#parameters'][2]['presetname'],
  );
  _imagecache_profiles_change_variables($preset, TRUE);
}

/**
 * Changes or deletes modules variables.
 */
function _imagecache_profiles_change_variables($preset, $delete = FALSE) {
  $vars = array(
    'user_picture_imagecache_profiles' => t('Profile picture preset'),
    'user_picture_imagecache_comments' => t('Comment picture preset'),
    'user_picture_imagecache_nodes' => t('Node picture preset'),
    'user_picture_imagecache_profiles_default' => t('Default picture preset'),
  );
  $presetid = $preset['presetid'];
  $presetname = $preset['presetname'];

  // Find and unset variabales that use this preset.
  $messages = array();
  foreach ($vars as $key => $param) {
    $value = variable_get($key, '');
    if (!empty($value)) {
      if (is_numeric($value) && $value == $presetid) {
        $messages[$key] = $param;
      }
      else {
        if ($value == $presetname) {
          $messages[$key] = $param;
        }
      }
    }
  }

  // Update variables and notify user.
  foreach ($messages as $var => $param) {
    if ($delete) {
      variable_del($var);
      drupal_set_message(t('The imagecache profile setting %setting was deleted.', array(
        '%setting' => $param,
      )));
    }
    else {
      if ($preset['presetname'] != $preset['presetname_new']) {
        variable_set($var, $preset['presetname_new']);
        drupal_set_message(t('The imagecache profile setting %setting was changed.', array(
          '%setting' => $param,
        )));
      }
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * @see imagecache_profiles_user_profile_form_validate()
 */
function imagecache_profiles_form_user_profile_form_alter(&$form, $form_state) {
  if (!isset($form['picture'])) {
    return;
  }
  $width = variable_get('user_picture_imagecache_profiles_min_width', '');
  $height = variable_get('user_picture_imagecache_profiles_min_height', '');
  $description = '';
  if (!empty($width)) {
    if (!empty($height)) {
      $description = t('Minimum dimensions are %dimensions.', array(
        '%dimensions' => $width . 'x' . $height,
      ));
    }
    else {
      $description = t('Minimum width is %width.', array(
        '%width' => $width,
      ));
    }
  }
  else {
    $description = t('Minimum height is %height.', array(
      '%height' => $height,
    ));
  }
  if (!empty($description)) {
    $form['picture']['picture_upload']['#description'] .= ' ' . $description;
  }
  $form['#validate'][] = 'imagecache_profiles_user_profile_form_validate';
}

/**
 * Custom form validation callback.
 *
 * @todo: Investigage validate $op of hook_user()
 * as this is not working correctly for the user_profile_form.
 * See - http://drupal.org/node/239676
 */
function imagecache_profiles_user_profile_form_validate($form, &$form_state) {
  if (isset($form_state['values']['picture'])) {
    $image_info = image_get_info($form_state['values']['picture']);
    if ($image_info['width'] < variable_get('user_picture_imagecache_profiles_min_width', 0) || $image_info['height'] < variable_get('user_picture_imagecache_profiles_min_height', 0)) {
      form_set_error('picture_upload', t('Your picture must be at least @min_user_picture_width pixels wide and @min_user_picture_height pixels tall (your image was @width x @height pixels).', array(
        '@min_user_picture_width' => variable_get('user_picture_imagecache_profiles_min_width', 0),
        '@min_user_picture_height' => variable_get('user_picture_imagecache_profiles_min_height', 0),
        '@width' => $image_info['width'],
        '@height' => $image_info['height'],
      )));
    }
  }
}

/**
 * Implements hook_user().
 *
 * Flush imagecache picture if user is updating their account picture.
 */
function imagecache_profiles_user($op, &$edit, &$account, $category = NULL) {
  if ($op == 'submit' && $category == 'account') {
    if ($edit['picture_delete']) {
      imagecache_image_flush($account->picture);
    }
    if (isset($edit['picture'])) {
      imagecache_image_flush($edit['picture']);
    }
  }
  elseif ($op == 'delete' && isset($account->picture)) {
    imagecache_image_flush($account->picture);
  }
}

/**
 * Implements hook_profile_alter().
 *
 * Change the user profile picture to use the preferred imagecache preset.
 */
function imagecache_profiles_profile_alter(&$account) {
  if (isset($account->content['user_picture']['#value']) && ($preset = variable_get('user_picture_imagecache_profiles', ''))) {
    $account->imagecache_preset = $preset;
    $account->content['user_picture']['#value'] = theme('user_picture', $account);
  }
}

/**
 * Implements hook_comment().
 *
 * Change comment user pictures to use the preferred imagecache preset.
 */
function imagecache_profiles_comment(&$comment, $op) {
  if ($op == 'view' && 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_result(db_query("SELECT picture FROM {users} WHERE uid = %d", $comment->uid));
    }
    if ($preset = variable_get('user_picture_imagecache_comments', '')) {
      $comment->imagecache_preset = $preset;
    }
  }
}

/**
 * Implements hook_nodeapi().
 *
 * Change node user pictures to use the preferred imagecache preset.
 */
function imagecache_profiles_nodeapi(&$node, $op) {

  // Panels module uses $op = alter to render nodes.
  if ($op == 'view' || $op == 'alter') {
    if ($preset = variable_get('user_picture_imagecache_nodes', '')) {
      $node->imagecache_preset = $preset;
    }
  }
}

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