You are here

simplecrop.module in SimpleCrop 7

Contains main hook definitions for SimpleCrop module.

File

simplecrop.module
View source
<?php

/**
 * @file
 * Contains main hook definitions for SimpleCrop module.
 */

// Include module files from /includes folder.
simplecrop_include('field');
simplecrop_include('effects');
simplecrop_include('api');
simplecrop_include('theme');

/**
 * Define contstants for image view modes.
 */
define('SIMPLECROP_DISPLAY_CROPPED_IMAGE', 'cropped_image');
define('SIMPLECROP_DISPLAY_ORIGINAL_IMAGE', 'original_image');

/**
 * Define constants for initial crop area selection.
 */
define('SIMPLECROP_CROP_AREA_MINIMIZE', 'minimize');
define('SIMPLECROP_CROP_AREA_MAXIMIZE', 'maximize');
define('SIMPLECROP_CROP_AREA_NONE', 'none');

/**
 * Implements hook_menu().
 */
function simplecrop_menu() {
  $items['simplecrop/ajax'] = array(
    'page callback' => 'simplecrop_ajax_rebuild_image',
    'delivery callback' => 'ajax_deliver',
    'access arguments' => array(
      'access content',
    ),
    'theme callback' => 'ajax_base_page_theme',
    'file' => 'simplecrop.pages.inc',
    'file path' => drupal_get_path('module', 'simplecrop') . '/includes',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function simplecrop_theme() {
  return array(
    'simplecrop_widget' => array(
      'render element' => 'element',
      'template' => 'simplecrop-widget',
      'path' => drupal_get_path('module', 'simplecrop') . '/templates',
    ),
  );
}

/**
 * Implements hook_image_default_styles().
 */
function simplecrop_image_default_styles() {
  $styles['simplecrop'] = array(
    'label' => 'SimpleCrop (100x100)',
    'effects' => array(
      array(
        'name' => 'simplecrop',
        'weight' => 0,
      ),
      array(
        'name' => 'image_scale',
        'data' => array(
          'width' => 100,
          'height' => 100,
          'upscale' => 1,
        ),
        'weight' => 1,
      ),
    ),
  );
  return $styles;
}

/**
 * Implements hook_filefield_sources_widgets().
 * Provides integration with Filefield Source module.
 * Gives possibility to use filefield source for simplecrop widget.
 */
function simplecrop_filefield_sources_widgets() {
  return array(
    'simplecrop_widget',
  );
}

/**
 * Implements hook_file_delete().
 */
function simplecrop_file_delete($file) {

  // Delete crop info on file deletion.
  if (!empty($file->uri)) {
    simplecrop_crop_delete($file->uri);
  }
}

/**
 * Implements hook_file_move().
 */
function simplecrop_file_move($file, $source) {
  $crop = simplecrop_crop_load($source->uri, TRUE);
  if (!empty($crop)) {
    simplecrop_crop_save($file->uri, $crop->data);
    simplecrop_crop_delete($source->uri);
  }
}

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

  // Search for already added simplecrop effect.
  $style = $form_state['image_style'];
  if (!empty($style['effects'])) {
    foreach ($style['effects'] as $effect) {
      if ($effect['module'] == 'simplecrop') {
        $simplecrop_effect_exists = TRUE;
      }
    }
  }

  // If SimpleCrop effect already added, then hide possibility to add it again
  // to prevent some possible issues with cropping.
  if (!empty($simplecrop_effect_exists)) {
    unset($form['effects']['new']['new']['#options']['simplecrop']);
  }

  // Submit handler for image effect form.
  // Forces simplecrop effect to be the first in list.
  $submit_callback = 'simplecrop_image_style_form_force_order';
  $form['#submit'][] = $submit_callback;
  $form['effects']['new']['add']['#submit'][] = $submit_callback;
}

/**
 * Submit handler for image effects form.
 * Forces simplecrop effect to be the first in a list.
 */
function simplecrop_image_style_form_force_order($form, &$form_state) {
  $submitted_style = $form_state['image_style'];

  // Always reload image style to get actual info from other submit callbacks.
  $style = image_style_load($submitted_style['name'], $submitted_style['isid']);

  // Pick SimpleCrop effect and minimal weight of all effects.
  foreach ($style['effects'] as $effect) {
    if ($effect['module'] == 'simplecrop') {
      $simplecrop_effect = $effect;
    }
    if (!isset($min_weight) || $min_weight > $effect['weight']) {
      $min_weight = $effect['weight'];
    }
  }

  // Force simplecrop effect always to be the first in list.
  if (isset($simplecrop_effect) && isset($min_weight) && $simplecrop_effect['weight'] != $min_weight) {
    $simplecrop_effect['weight'] = $min_weight - 1;
    image_effect_save($simplecrop_effect);
    drupal_set_message(t('SimpleCrop effect was forced to be the first to prevent possible crop issues.'));
  }
}

/**
 * Returns list of image styles which use simplecrop effect.
 */
function simplecrop_imagestyles_with_crop_effect() {

  // Provide static caching for the current operation results, because it can
  // be executed a lot of times per page request.
  $styles_with_crop =& drupal_static(__FUNCTION__);
  if (isset($styles_with_crop)) {
    return $styles_with_crop;
  }

  // Load all image styles.
  $styles = image_styles();
  $styles_with_crop = array();

  // Walk through each image style to find those who has effects from the
  // current module.
  foreach ($styles as $name => $style) {

    // We don't care about empty image styles.
    if (empty($style['effects'])) {
      continue;
    }
    foreach ($style['effects'] as $effect) {
      if (!empty($effect['module']) && $effect['module'] == 'simplecrop') {
        $styles_with_crop[$name] = $name;
      }
    }
  }

  // Return a list of image styles which use simplecrop effect.
  return $styles_with_crop;
}

/**
 * Load file from /includes folder.
 */
function simplecrop_include($file) {
  module_load_include('inc', 'simplecrop', 'includes/simplecrop.' . $file);
}

Functions

Namesort descending Description
simplecrop_filefield_sources_widgets Implements hook_filefield_sources_widgets(). Provides integration with Filefield Source module. Gives possibility to use filefield source for simplecrop widget.
simplecrop_file_delete Implements hook_file_delete().
simplecrop_file_move Implements hook_file_move().
simplecrop_form_image_style_form_alter Implements hook_form_FORM_ID_alter().
simplecrop_imagestyles_with_crop_effect Returns list of image styles which use simplecrop effect.
simplecrop_image_default_styles Implements hook_image_default_styles().
simplecrop_image_style_form_force_order Submit handler for image effects form. Forces simplecrop effect to be the first in a list.
simplecrop_include Load file from /includes folder.
simplecrop_menu Implements hook_menu().
simplecrop_theme Implements hook_theme().

Constants

Namesort descending Description
SIMPLECROP_CROP_AREA_MAXIMIZE
SIMPLECROP_CROP_AREA_MINIMIZE Define constants for initial crop area selection.
SIMPLECROP_CROP_AREA_NONE
SIMPLECROP_DISPLAY_CROPPED_IMAGE Define contstants for image view modes.
SIMPLECROP_DISPLAY_ORIGINAL_IMAGE