? cck-1.patch
? cck-2.patch
? cloneimagefield-200092-19.patch
? cloneimagefield-200092-23.patch
? cloneimagefield-200092-5x-1x-23.patch
? cloneimagefield-200092-5x-2x-24.patch
? node.txt
Index: README.txt
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/node_clone/README.txt,v
retrieving revision 1.5.2.6
diff -u -p -r1.5.2.6 README.txt
--- README.txt	23 May 2008 00:54:08 -0000	1.5.2.6
+++ README.txt	23 May 2008 02:07:51 -0000
@@ -50,6 +50,14 @@ To install this module, copy the folder 
 installation and enable it at /admin/build/modules.  Two new permissions are 
 available, but there are no changes to the database structure.
 
-Note: this module originally derived from code posted by Steve Ringwood 
+The Clone module was originally derived from code posted by Steve Ringwood
 (nevets@drupal) at http://drupal.org/node/73381#comment-137714
 
+An additional helper module, Clone Imagefield, is also present. If this module
+is enabled, files attached using the CCK Imagefield module will be copied when
+the clone is created.  Typically, this means the filename of the copy will have
+a number appended to it to distinguish it form the original file.
+
+Development of the Clone Imagefield module for 5.x sponsored by paulnem
+(paulnem@drupal).
+
Index: cloneimagefield.info
===================================================================
RCS file: cloneimagefield.info
diff -N cloneimagefield.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ cloneimagefield.info	23 May 2008 02:07:51 -0000
@@ -0,0 +1,5 @@
+; $Id$
+; $Name$
+name = Clone Imagefield
+description = "Copies CCK Imagefield attachments when cloning an existing node."
+dependencies = clone content imagefield
Index: cloneimagefield.module
===================================================================
RCS file: cloneimagefield.module
diff -N cloneimagefield.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ cloneimagefield.module	23 May 2008 02:07:51 -0000
@@ -0,0 +1,115 @@
+<?php
+// $Id$
+// $Name$
+
+/**
+ * Implementation of hook_help().
+ */
+function cloneimagefield_help($section) {
+  switch ($section) {
+    case 'admin/help#cloneimagefield':
+      $output = '<p>'. t('The Clone Imagefield module works with the Clone Module and Imagefiled Module (a CCK field-type module) to copy attached images to be made when a node is cloned.') .'</p>';
+      return $output;
+  }
+}
+
+/**
+* Implementation of hook_clone_node_alter() (a clone.module hook).
+*/
+function cloneimagefield_clone_node_alter(&$node, $original_node, $method) {
+
+  $content_type = module_invoke('content', 'types', $node->type);
+  $image_fields = array();
+  // Find all the image fields
+  foreach ($content_type['fields'] as $data) {
+    if ($data['type'] == 'image') {
+       $image_fields[] = $data['field_name'];
+    }
+  }
+  $clone_imagefield = array();
+  // Put back the imagefields from the original node.
+  foreach ($image_fields as $key) {
+    if (isset($original_node->$key)) {
+      $node->$key = $original_node->$key;
+      // Make a copy of the imagefield data.
+      $clone_imagefield[$key] = $original_node->$key;
+    }
+  }
+
+  if ($method == "prepopulate") {
+    // The imagefield data is a flag that we can find during the form_alter.
+    $node->node_clone_imagefield = $clone_imagefield;
+    if (empty($_POST['op'])) {
+      drupal_set_message(t('Each pre-existing Imagefield file will be copied and attached when you submit (unless you select its "Delete" checkbox).'));
+    }
+  }
+  elseif ($method == "save-edit") {
+    foreach ($image_fields as $key) {
+      foreach ($node->$key as $delta => $file) {
+        _cloneimagefield_copy_file($node, $key, $delta);
+      }
+    }
+  }
+}
+
+/**
+ * Copy a specific attached file to a temporary location.
+ */
+function _cloneimagefield_copy_file(&$node, $key, $delta) {
+  $source = $node->{$key}[$delta]['filepath'];
+  // Create temporary name/path for duplicated files. On Windows, tempnam()
+  // requires an absolute path, so we use realpath().
+  $dest = tempnam(realpath(file_directory_temp()), 'tmp_');
+  // Copy the file to the temporary location.
+  if (file_copy($source, $dest)) {
+    // $source now holds the actual filename used for the copy.
+    $node->{$key}[$delta]['filepath'] = $source;
+    $node->{$key}[$delta]['fid'] = 'upload';
+    $node->{$key}[$delta]['nid'] = 0;
+  }
+  else {
+    // Copy failed, remove the file from the list of attachements.
+    unset($node->{$key}[$delta]);
+  }
+}
+
+/**
+ * Implementation of hook_nodeapi().
+ *
+ * Take advantage of the fact that imagefield.module does not use the 'submit'
+ * $op to copy files at the 'submit' phase before the node is actually saved.
+ * Only relevant when the "prepopulate" method is being used.
+ */
+function cloneimagefield_nodeapi(&$node, $op) {
+  if ($op == 'submit' && isset($node->node_clone_imagefield)) {
+    // The data was serialized so it could be passed as a hidden form field.
+    $image_fields = unserialize($node->node_clone_imagefield);
+    foreach (array_keys($image_fields) as $key) {
+      foreach ($node->$key as $delta => $file) {
+        // Check whether this file was attached to the original node, and that 
+        // it is not slated for removal.
+        if (($file['filepath'] == $image_fields[$key][$delta]['filepath']) && empty($file['flags']['delete'])) {
+          _cloneimagefield_copy_file($node, $key, $delta);
+        }
+      }
+    }
+  }
+}
+
+/**
+* Implementation of hook_form_alter().
+*/
+function cloneimagefield_form_alter($form_id, &$form) {
+  // Look for node forms.
+  if (isset($form['type']['#value']) && $form['type']['#value'] .'_node_form' == $form_id) {
+    if (isset($form['#node']->node_clone_imagefield)) {
+      // If we are using the "prepopulate" method, store information about files
+      // attached to the original node as a hidden form field.
+      $form['node_clone_imagefield'] = array(
+        '#type' => 'hidden',
+        '#value' => serialize($form['#node']->node_clone_imagefield),
+      );
+    }
+  }
+}
+
