From 49d70b604ede0ce313c4f816b37f613c719421a9 Mon Sep 17 00:00:00 2001
From: OnkelTem <OnkelTem@239962.no-reply.drupal.org>
Date: Thu, 7 Mar 2013 19:53:13 +0400
Subject: [PATCH] Issue #1936424 by OnkelTem: New Effect: Automatically select
 applied crop effect

---
 manualcrop.admin.inc |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++
 manualcrop.module    |   42 +++++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+)

diff --git a/manualcrop.admin.inc b/manualcrop.admin.inc
index c83b778..de34173 100644
--- a/manualcrop.admin.inc
+++ b/manualcrop.admin.inc
@@ -224,6 +224,49 @@ function manualcrop_reuse_form($data) {
 }
 
 /**
+ * Form Builder; Configuration settings for the select effect.
+ *
+ * @param $data
+ *   The current configuration for this reuse effect.
+ *
+ * @return
+ *   The form structure array.
+ */
+function manualcrop_select_form($data) {
+  // Load all image styles that have a cropping effect and exclude the style
+  // that's currently being edited.
+  $styles = manualcrop_styles_with_crop();
+  $current = arg(5);
+  if (isset($styles[$current])) {
+    unset($styles[$current]);
+  }
+
+  $options = array('' => t('None'));
+
+  if (count($styles)) {
+    $a = '<ul><li>' . implode('</li><li>', array_keys($styles)) . '</li></ul>';
+    $options = array_merge($options, drupal_map_assoc(array_keys($styles)));
+  } else {
+    $a = 'Not crop styles found. Using only fallback style.';
+  }
+  $form['manualcrop_select_help'] = array(
+    '#type' => 'markup',
+    '#markup' => '<label>' . t('List of available styles') . '</label>' . $a,
+  );
+
+  $form['fallback_style'] = array(
+    '#type' => 'select',
+    '#title' => t('Fallback image style'),
+    '#description' => t('Image style to use if no cropping selections is found for an image.'),
+    '#options' => $options,
+    '#default_value' => (isset($data['fallback_style']) ? $data['fallback_style'] : ''),
+    '#required' => !count($styles),
+  );
+
+  return $form;
+}
+
+/**
  * Returns HTML for a summary of an image crop selection reuse effect.
  *
  * @param $variables
@@ -240,6 +283,19 @@ function theme_manualcrop_reuse_summary($variables) {
 }
 
 /**
+ * Returns HTML for a summary of an image crop selection select effect.
+ *
+ * @param $variables
+ *   An associative array containing:
+ *   - data: The current configuration for this crop selection select effect.
+ *
+ * @ingroup themeable
+ */
+function theme_manualcrop_select_summary($variables) {
+  return '(' . t('using fallback style @style', array('@style' => _manualcrop_image_style_name($variables['data']['fallback_style']))) . ')';
+}
+
+/**
  * Implements hook_form_FORM_ID_alter().
  */
 function manualcrop_form_image_effect_form_alter(&$form, &$form_state) {
diff --git a/manualcrop.module b/manualcrop.module
index 16e6454..b2eec2e 100644
--- a/manualcrop.module
+++ b/manualcrop.module
@@ -973,6 +973,13 @@ function manualcrop_image_effect_info() {
       'form callback' => 'manualcrop_reuse_form',
       'summary theme' => 'manualcrop_reuse_summary',
     ),
+    'manualcrop_select' => array(
+      'label' => t('Select applied Manual Crop selection'),
+      'help' => 'Select applied crop selection from a set of Manual Crop enabled image styles.',
+      'effect callback' => 'manualcrop_select_effect',
+      'form callback' => 'manualcrop_select_form',
+      'summary theme' => 'manualcrop_select_summary',
+    ),
   );
 }
 
@@ -1099,6 +1106,41 @@ function manualcrop_reuse_effect(&$image, $data) {
 }
 
 /**
+ * Image effect callback; Select applied Manual Crop effect.
+ *
+ * @param $image
+ *   An image object returned by image_load().
+ * @param $data
+ *   An array of settings, needed to perform the reuse effect, with the
+ *   following items:
+ *   - "reusestyle": The image style to reuse.
+ *
+ * @return
+ *   TRUE on success, FALSE on failure to reuse the effect.
+ *
+ * @see image_crop_effect()
+ */
+function manualcrop_select_effect(&$image, $data) {
+  // Get the list of all selections for the file
+  $selections = manualcrop_load_crop_selection($image->source);
+
+  if (!empty($selections)) {
+    $selection = array_shift($selections);
+    $style = image_style_load($selection->style_name);
+  } else if (!empty($data['fallback_style'])) {
+    $style = image_style_load($data['fallback_style']);
+  }
+  if (isset($style)) {
+    $effect = reset($style['effects']);
+    // Apply the Manual Crop cropping effect.
+    image_effect_apply($image, $effect);
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/**
  * Implements hook_insert_styles().
  */
 function manualcrop_insert_styles() {
-- 
1.7.9.5

