### Eclipse Workspace Patch 1.0
#P media_mover_6
Index: contrib/mm_cck/mm_cck.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/media_mover/contrib/mm_cck/Attic/mm_cck.module,v
retrieving revision 1.1.4.5
diff -u -r1.1.4.5 mm_cck.module
--- contrib/mm_cck/mm_cck.module	22 Feb 2010 15:11:20 -0000	1.1.4.5
+++ contrib/mm_cck/mm_cck.module	26 Feb 2010 17:00:16 -0000
@@ -361,146 +361,108 @@
  *   new file location
  */
 function mm_cck_node_save($configuration, &$file) {
-  // Get the path to the file to operate on. We need this to pass out of the function
-  $filepath = media_mover_api_config_current_file($file);
-
   // Is there a node attached to this file?
   if ($node = media_mover_api_get_node_from_file($file) ) {
     // Get CCK content
     content_load($node);
   }
+  // Should we create a new node?
+  elseif ($configuration['mm_cck_new_node_' . $configuration['verb']]) {
+    $node = mm_cck_node_build($configuration, $file);
+  }
+  // There was no node and we can not create one.
+  if (! $node) {
+    watchdog('mm_cck', 'No node was found on the file and the configuration did not specify creating a new node', array(), WATCHDOG_ERROR);
+    return false;
+  }
 
-  // get the field that we will save to
-  $field = content_fields($configuration['mm_cck_field_save_'. $configuration['verb']]);
-
-  // We have to have a file on the local file system so that CCK does not die
-  // @TODO PHP Stream Wrappers should address this
-  if (! $local_file = mm_cck_find_local_file($file)) {
-    watchdog('MM CCK', 'Failed to find a file on the local file system to attache to the CCK field');
-    return $filepath;
+  // Copy the file into the specified CCK directory.
+  if (! $new_file = mm_cck_field_filefield($configuration, $file, $node) ) {
+    return false;
   }
 
   // If the admin requested that source material be deleted, delete it now. DANGER CAKES!
-  // Note that we only do this if the current file path is NOT the source material.
-  if ($configuration['mm_cck_file_source_delete_'. $configuration['verb']]  &&
-    $filepath != $file['harvest_file'] ) {
-    file_delete($file['harvest_file']);
+  // Since the file has already been copied, we can delete all of the previous files
+  if ($configuration['mm_cck_file_source_delete_' . $configuration['verb']]) {
+    foreach (media_mover_api_verbs() as $verb) {
+      if ($verb != $configuration['verb']) {
+        file_delete($file[$verb . '_file']);
+      }
+    }
     watchdog('MM CCK', 'Deleted source material %file', array(
       '%file' => $file['harvest_file'],
     ), WATCHDOG_INFO, 'node/' . $node->nid);
   }
 
   // If we have an fid, and should just replace the file in the CCK field, return the current file.
-  if ($file['fid'] && $configuration['mm_cck_file_replace_'. $configuration['verb']] &&
-    mm_cck_find_file_id($node, $field, $file['fid']) &&
-    ! $configuration['mm_cck_new_node_'. $configuration['verb']]) {
-
+  // @TODO this should still copy the file to the correct location
+  if ($file['fid'] && $configuration['mm_cck_file_replace_' . $configuration['verb']] &&
+    mm_cck_find_file_id($node, $field, $file['fid']) && ! $configuration['mm_cck_new_node_' . $configuration['verb']]) {
     // Update the current entry in the files table
-    db_query('UPDATE {files} SET filename = "%s", filepath = "%s" WHERE fid = %d', basename($filepath), $filepath, $file['fid']);
-
+    db_query('UPDATE {files} SET filename = "%s", filepath = "%s" WHERE fid = %d', basename($new_file['filepath']), $new_file['filepath'], $file['fid']);
     // clear the node cache to make sure we can see this content
     cache_clear_all('content:'. $node->nid .':'. $node->vid, 'cache_content');
-
     watchdog('MM CCK', 'Replaced source material %file; replaced with: %new_file', array(
       '%file' => $file['harvest_file'],
-      '%new_file' => $filepath,
+      '%new_file' => $new_file['filepath'],
     ), WATCHDOG_INFO, 'node/' . $node->nid);
-
-    return $filepath;
   }
 
-  // is there a node attached to the $file array and we are not supposed to create a new node
-  if (! $node && ! $configuration['mm_cck_new_node_'. $configuration['verb']]) {
-    // set an alert
-    watchdog('MM CCK', 'No node data was passed for storing file data in a specified CCK field: !file',
-      array('!file', l(t($file['mmfid']),  'admin/media_mover/file/edit/'. $file['mmfid'])), WATCHDOG_ERROR);
-    // we can not save the file; exit
-    return;
-  }
+  return $new_file['filepath'];
+}
 
-  // Should we start building a new node?
-  if ($configuration['mm_cck_new_node_'. $configuration['verb']]) {
-	  // start building the node
-	  $node = new STDClass();
-	  // set node type
-	  $node->type = $configuration['mm_cck_save_type_'. $configuration['verb']];
-
-	  // set the title
-	  if (! $node->title = $configuration['mm_cck_title_default_'. $configuration['verb']]) {
-	  	if (! $node->title = $file['data']['node']->title) {
-	  		$node->title = basename($filepath);
-	  	}
-	  }
-
-	  // set the body, cascade from default to nothing
-	  if (! $node->body = $configuration['mm_cck_body_default_'. $configuration['verb']]) {
-	  	if (! $node->body = $file['data']['node']->body) {
-	  		$node->body = '';
-	  	}
-	  }
-
-	  // if we have a language setting
-	  $node->language = $configuration['mm_cckn_save_language_'. $configuration['verb']] ? $configuration['mm_cck_save_language_' . $configuration['verb']] : null;
-
-	  // node options
-	  $node->comment = $configuration['mm_cck_save_options_'. $configuration['verb']]['comment'] ? 2 : 0;
-	  $node->status = $configuration['mm_cck_save_options_'. $configuration['verb']]['status'] ? 1 : 0;
-	  $node->sticky = $configuration['mm_cck_save_options_'. $configuration['verb']]['sticky'] ? 1 : 0;
-	  $node->promote = $configuration['mm_cck_save_options_'. $configuration['verb']]['promote'] ? 1 : 0;
-
-	  // Now build the node author
-  	if (! $user = $file['data']['user']) {
-  	  if (! $user = user_load(array('name' => $configuration['mm_cck_save_author_'. $configuration['verb']]))) {
-  		  $user = user_load(array('uid' => 0));
-  	  }
-  	}
-
-  	// attach the user data to the node
-    $node->uid = $user->uid;
-    $node->name = $user->name;
-    $node->mail = $user->mail;
-
-  	// save the new node to prepare to add the CCK data
-  	node_save($node);
-  	$file['nid'] = $node->nid;
 
+/**
+ * Utility function to build a node for saving
+ * @param unknown_type $configuration
+ * @param unknown_type $file
+ * @return unknown_type
+ */
+function mm_cck_node_build($configuration, &$file) {
+  // start building the node
+  $node = new STDClass();
+  // set node type
+  $node->type = $configuration['mm_cck_save_type_' . $configuration['verb']];
+
+  // set the title
+  if (! $node->title = $configuration['mm_cck_title_default_' . $configuration['verb']]) {
+    if (! $node->title = $file['data']['node']->title) {
+      $node->title = basename($filepath);
+    }
   }
 
-  // Now copy the file to the new location
-  if (! file_copy($local_file,  mm_cck_field_widget_files_directory($field))) {
-    watchdog('MM CCK', 'Failed to copy. %data', array('%data' => "Source: $filepath Destination: $local_file"), WATCHDOG_ERROR);
-    return false;
+  // set the body, cascade from default to nothing
+  if (! $node->body = $configuration['mm_cck_body_default_'. $configuration['verb']]) {
+    if (! $node->body = $file['data']['node']->body) {
+      $node->body = '';
+    }
   }
 
-  // we need to add an new file to the node
-  switch ($field['type']) {
-    case 'text':
-      $node->{$field['field_name']} = array(array('value' => $local_file));
-  	  // save the node
-      node_save($node);
-      // clear the cache
-      cache_clear_all('content:'. $node->nid .':'. $node->vid, 'cache_content');
-  	  $file['filepath'] = $local_file;
-    break;
+  // if we have a language setting
+  $node->language = $configuration['mm_cckn_save_language_'. $configuration['verb']] ? $configuration['mm_cck_save_language_' . $configuration['verb']] : null;
 
-    // handle CCK image and CCK file field cases
-  	case 'image':
-  	case 'filefield':
-  	  if (! $new_file = mm_cck_field_filefield($node, $field, $local_file, $configuration)) {
-  	  	return;
-  	  }
-  	break;
-  }
-
-  // attach the data to the node
-  $file['data']['node'] = $node;
-  // assign the new fid
-  $file['fid'] = $new_file['fid'];
-
-  // return the cck file
-  if (! strstr($filepath, 'http://')) {
-    return $new_file['filepath'];
+  // node options
+  $node->comment = $configuration['mm_cck_save_options_'. $configuration['verb']]['comment'] ? 2 : 0;
+  $node->status = $configuration['mm_cck_save_options_'. $configuration['verb']]['status'] ? 1 : 0;
+  $node->sticky = $configuration['mm_cck_save_options_'. $configuration['verb']]['sticky'] ? 1 : 0;
+  $node->promote = $configuration['mm_cck_save_options_'. $configuration['verb']]['promote'] ? 1 : 0;
+
+  // Now build the node author
+  if (! $user = $file['data']['user']) {
+    if (! $user = user_load(array('name' => $configuration['mm_cck_save_author_'. $configuration['verb']]))) {
+      $user = user_load(array('uid' => 0));
+    }
   }
+
+  // attach the user data to the node
+  $node->uid = $user->uid;
+  $node->name = $user->name;
+  $node->mail = $user->mail;
+
+  // Save the new node
+  node_save($node);
+  $file['nid'] = $node->nid;
+  return $node;
 }
 
 
@@ -515,7 +477,16 @@
  * @param array $configuration
  * @return array
  */
-function mm_cck_field_filefield(&$node, $field, $filepath, $configuration) {
+function mm_cck_field_filefield($configuration, &$file, &$node) {
+  // get the field that we will save to
+  $field = content_fields($configuration['mm_cck_field_save_' . $configuration['verb']]);
+
+  // We have to have a file on the local file system so that CCK does not die
+  if (! $filepath = mm_cck_file_get_local_file($file)) {
+    watchdog('MM CCK', 'Failed to find a file on the local file system to attach to the CCK field');
+    return FALSE;
+  }
+
   // is there a specified directory to use with this field?
   $destination_path = mm_cck_field_widget_files_directory($field);
 
@@ -526,12 +497,14 @@
   if ($field['type'] == 'image') {
     $validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
   }
-  else { $validators = filefield_widget_upload_validators($field);}
+  else {
+    $validators = filefield_widget_upload_validators($field);
+  }
 
   // now get the new field
   if (! $new_file = field_file_save_file($filepath, $validators, $destination_path)) {
     watchdog('MM CCK', 'Could not save file with field_file_save_file() %file', array('%file' => print_r($file, true)), WATCHDOG_ERROR);
-    return;
+    return FALSE;
   }
 
   // we need to alter the file object slightly so it will display correctly
@@ -552,6 +525,19 @@
     $node->{$field['field_name']}[] = $new_file;
   }
 
+  // we need to add an new file to the node
+  switch ($field['type']) {
+    case 'text':
+      $node->{$field['field_name']} = array(array('value' => $new_file['filepath']));
+    break;
+
+    // handle CCK image and CCK file field cases
+    case 'image':
+    case 'filefield':
+      $file['fid'] = $new_file['fid'];
+    break;
+  }
+
   // Save the node
   node_save($node);
   // Clear the cache
@@ -621,9 +607,11 @@
  * This is a helper function to find a local file in the files list so that
  * CCK does not die when we try to save the file to it.
  * @param $file
- * @return unknown_type
+ *   Media Mover file- array
+ * @return $filename
+ *   String
  */
-function mm_cck_find_local_file($file) {
+function mm_cck_file_get_local_file(&$file) {
   for ($i = MMA_FILE_STATUS_COMPLETE_COMPLETE; $i >= 0; $i--) {
     if ($filename = $file[media_mover_api_verb_base_status($i) .'_file']) {
       if (! strstr($filename, 'http://') && count($filename)) {
