? .directory
? imagecache-374202-43.patch
? imagecache-374202.patch
? imagecache_374202_0.patch
Index: imagecache.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/imagecache/imagecache.module,v
retrieving revision 1.112.2.8
diff -u -p -r1.112.2.8 imagecache.module
--- imagecache.module	25 May 2010 20:49:44 -0000	1.112.2.8
+++ imagecache.module	26 May 2010 20:38:22 -0000
@@ -384,7 +384,10 @@ function imagecache_cache_private() {
 }
 
 /**
- * handle request validation and responses to imagecache requests.
+ * Handle request validation and responses to ImageCache requests.
+ *
+ * @see imagecache_generate_image() if you're writing code that needs to have
+ *   ImageCache generate images but not send them to a browser.
  */
 function _imagecache_cache($presetname, $path) {
   if (!$preset = imagecache_preset_by_name($presetname)) {
@@ -560,8 +563,8 @@ function imagecache_build_derivative($ac
     return FALSE;
   }
 
-  // file_check_directory() has an annoying habit of displaying "directory ... 
-  // has been created" status messages. To avoid confusing visitors we clear 
+  // file_check_directory() has an annoying habit of displaying "directory ...
+  // has been created" status messages. To avoid confusing visitors we clear
   // out all the status messages for non-ImageCache admins. This might affect
   // some other messages but errors and warnings should still be displayed.
   if (!user_access('administer imagecache')) {
@@ -1116,3 +1119,174 @@ function imagecache_action_delete($actio
   imagecache_preset_flush($preset);
   imagecache_presets(TRUE);
 }
+
+/**
+ * Implementation of hook_action_info().
+ *
+ * Note: These are actions in the Drupal core trigger.module sense, not
+ * ImageCache actions.
+ */
+function imagecache_action_info() {
+  $actions = array();
+
+  if (module_exists('filefield')) {
+    $actions['imagecache_flush_action'] = array(
+      'type' => 'node',
+      'description' => t("ImageCache: Flush ALL presets for this node's filefield images"),
+      'configurable' => FALSE,
+      'hooks' => array(
+        'nodeapi' => array('presave', 'delete', 'insert', 'update'),
+      )
+    );
+    $actions['imagecache_generate_all_action'] = array(
+      'type' => 'node',
+      'description' => t("ImageCache: Generate ALL presets for this node's filefield images"),
+      'configurable' => FALSE,
+      'hooks' => array(
+        'nodeapi' => array('presave', 'insert', 'update'),
+      )
+    );
+    $actions['imagecache_generate_action'] = array(
+      'type' => 'node',
+      'description' => t("ImageCache: Generate Configured preset(s) for this node's filefield images"),
+      'configurable' => TRUE,
+      'hooks' => array(
+        'nodeapi' => array('presave', 'insert', 'update'),
+      )
+    );
+  }
+
+  return $actions;
+}
+
+/**
+ * Flush all imagecache presets for a given node.
+ *
+ * @param $node
+ *   A node object.
+ * @param $context
+ *   Contains values from the calling action.
+ *
+ * @see imagecache_action_info()
+ */
+function imagecache_flush_action(&$node, $context) {
+  $files = imagecache_get_images_in_node($node);
+  if (!empty($files)) {
+    foreach ($files as $file) {
+      imagecache_image_flush($file['filepath']);
+    }
+  }
+}
+
+/**
+ * Generate all imagecache presets for the given node.
+ *
+ * @param $node
+ *   A node object.
+ * @param $context
+ *   Contains values from the calling action.
+ *
+ * @see imagecache_action_info()
+ */
+function imagecache_generate_all_action(&$node, $context) {
+  $files = imagecache_get_images_in_node($node);
+  $presets = imagecache_presets();
+  if (!empty($files) && !empty($presets)) {
+    foreach ($files as $file) {
+      foreach ($presets as $presetname) {
+        imagecache_generate_image($presetname['presetname'], $file['filepath']);
+      }
+    }
+  }
+}
+
+/**
+ * Generate imagecache presets for the given node and presets.
+ *
+ * @param $node
+ *   A node object.
+ * @param $context
+ *   Contains values from the calling action.
+ *
+ * @see imagecache_action_info()
+ * @see imagecache_generate_action_form()
+ */
+function imagecache_generate_action(&$node, $context) {
+  $files = imagecache_get_images_in_node($node);
+  if (!empty($files) && !empty($context['imagecache_presets'])) {
+    foreach ($files as $file) {
+      foreach ($context['imagecache_presets'] as $presetname) {
+        imagecache_generate_image($presetname, $file['filepath']);
+      }
+    }
+  }
+}
+
+/**
+ * Form for configuring the generate action.
+ *
+ * @see imagecache_generate_action()
+ */
+function imagecache_generate_action_form($context) {
+  $options = array();
+  foreach (imagecache_presets() as $preset) {
+    $options[$preset['presetname']] = $preset['presetname'];
+  }
+  $form['presets'] = array(
+    '#type' => 'checkboxes',
+    '#options' => $options,
+    '#description' => t('Select which imagecache presets will be effected'),
+    '#required' => TRUE,
+    '#default_value' => isset($context['imagecache_presets']) ? $context['imagecache_presets'] : array(),
+  );
+  // Filter out false checkboxes: http://drupal.org/node/61760#comment-402631
+  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
+  return $form;
+}
+
+/**
+ * Generate a derivative image given presetname and filepath.
+ *
+ * This is a developer friendly version of _imagecache_cache(), it doesn't worry
+ * about sending HTTP headers or an image back to the client so it's much
+ * simpler.
+ *
+ * @param $presetname
+ *   Imagecache preset.
+ * @param $filepath
+ *   filepath from the files table.
+ * @return
+ *   A Boolean indicating if the operation succeeded.
+ */
+function imagecache_generate_image($presetname, $filepath) {
+  $preset = imagecache_preset_by_name($presetname);
+  if (empty($preset['presetname'])) {
+    return FALSE;
+  }
+  $destination = imagecache_create_path($preset['presetname'], $filepath);
+  if (file_exists($destination)) {
+    return TRUE;
+  }
+  return imagecache_build_derivative($preset['actions'], $filepath, $destination);
+}
+
+/**
+ * Given a node, get all images associated with it.
+ *
+ * @param $node
+ *   node object
+ * @return
+ *   An array of info from the files table.
+ */
+function imagecache_get_images_in_node(&$node) {
+  $files = array();
+  if (module_exists('filefield')) {
+    $data = filefield_get_node_files($node);
+    foreach ($data as $key => $value) {
+      if (stristr($value['filemime'], 'image')) {
+        $files[$key] = $value;
+      }
+    }
+  }
+  return $files;
+}
