Index: content_copy.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cck/content_copy.module,v
retrieving revision 1.1.2.5
diff -u -p -r1.1.2.5 content_copy.module
--- content_copy.module	7 Jan 2007 14:43:32 -0000	1.1.2.5
+++ content_copy.module	14 Feb 2007 02:29:23 -0000
@@ -48,10 +48,23 @@ function content_copy_menu($may_cache) {
       'type' => MENU_LOCAL_TASK,
       'weight' => 4,
     );
+
+    // Surely, there is a better place for this.
+    content_copy_install_default_fields();
   }
   return $items;
 }
 
+function content_copy_install_default_fields() {
+  include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
+  include_once('./'. drupal_get_path('module', 'content') .'/content_crud.inc');
+  
+  $types = module_invoke_all('default_fields');
+  foreach ($types as $type => $fields) {
+    content_copy_add_fields_to_type($type, $fields);
+  }
+}
+
 /**
  *  A form to export field definitions.
  */
@@ -240,7 +253,6 @@ function content_copy_import_form($type_
  *     2) execute the imported field macro to update the settings to the imported values
  */
 function content_copy_import_form_submit($form_id, $form_values) {
-
   // Get the content type we are importing into.
   $type_name = $form_values['type_name'];
   $type_label = node_get_types('name', $type_name);
@@ -268,7 +280,7 @@ function content_copy_import_form_submit
 
   // Perform more pre-import error trapping.
   // If there are potential problems, exit without doing the import.
-  $not_enabled = array();
+  $not_enabled = content_copy_verify_field_modules($imported_fields);
 
   // The groups array could be empty and still valid, make sure to cast it as an array.
   // If there are groups in the import, make sure the fieldgroup module is enabled.
@@ -279,23 +291,8 @@ function content_copy_import_form_submit
     $not_enabled[] = 'fieldgroup';
   }
 
-  // Make sure that all the field and widget modules in the import are enabled in this database.
-  foreach ($imported_fields as $field) {
-    if (empty($field['module'])) {
-      $not_enabled[] = $field['field_name'];
-    }
-    else {
-      $modules = explode(', ', $field['module']);
-      foreach ($modules as $module) {
-        if (!module_exists($module)) {
-          $not_enabled[] = $module;
-        }
-      }
-    }
-  }
-
   // If any required module is not enabled, set an error message and exit.
-  if ($not_enabled) {
+  if (!empty($not_enabled)) {
     form_set_error('macro', t('The following modules must be enabled for this import to work: %modules.', array(
         '%modules' => implode(', ', array_unique($not_enabled))
         )));
@@ -352,10 +349,65 @@ function content_copy_import_form_submit
     // Reset the static variable in fieldgroup_groups() with new data.
     fieldgroup_groups('', FALSE, TRUE);
   }
+  
+  $errors = content_copy_add_fields_to_type($type_name, $imported_fields);
 
-  // Iterate through the field forms in the import and execute each.
-  foreach ($imported_fields as $field) {
+  if (empty($errors)) {
+    if (sizeof($imported_fields) > 0 || sizeof($imported_groups) > 0) {
+      drupal_goto('admin/content/types/'. $content_info['content types'][$type_name]['url_str'] .'/fields');
+    }
+    else {
+      drupal_goto('admin/content/types');
+    }
+  }
+}
+
+function content_copy_verify_field_modules($fields) {
+  // Perform more pre-import error trapping.
+  // If there are potential problems, exit without doing the import.
+  $not_enabled = array();
+
+  // Make sure that all the field and widget modules in the import are enabled in this database.
+  foreach ($fields as $field) {
+    if (empty($field['module'])) {
+      $not_enabled[] = $field['field_name'];
+    }
+    else {
+      $modules = explode(', ', $field['module']);
+      foreach ($modules as $module) {
+        if (!module_exists($module)) {
+          $not_enabled[] = $module;
+        }
+      }
+    }
+  }
+  return $not_enabled;
+}
+
+function content_copy_add_fields_to_type($type_name, $fields) {
+  // Get all type and field info for this database.
+  $content_info = _content_type_info();
+
+  $errors = array();
+
+  // We srequire that the content type already exist.
+  if (!is_array($content_info['content types'][$type_name])) {
+    $errors[] = array('type_missing' => $type_name);
+    return $errors;
+  }
 
+  // Perform more pre-import error trapping.
+  // If there are potential problems, exit without doing the import.
+  $not_enabled = content_copy_verify_field_modules($fields);
+
+  // If any required module is not enabled, set an error message and exit.
+  if ($not_enabled) {
+    $errors[] = array('modules_missing' => $not_enabled);
+    return $errors;
+  }
+
+  // Iterate through the field forms in the import and execute each.
+  foreach ($fields as $field) {
     // Make sure the field doesn't already exist in the type.
     // If so, do nothing, fields can't be duplicated within a content type.
     $field_name   = $field['field_name'];
@@ -363,14 +415,9 @@ function content_copy_import_form_submit
     $field_label  = $field_values['label'];
 
     if (!empty($field['field_name']) && is_array($content_info['content types'][$type_name]['fields'][$field_name])) {
-      drupal_set_message(t("The imported field %field_label (%field_name) was not added to %type because that field already exists in %type.", array(
-          '%field_label' => $field_label,
-          '%field_name' => $field_name,
-          '%type' => $type_name
-          )));
+      $errors[] = array('field_exists' => array($field_label, $field_name, $type_name));
     }
     else {
-
       $values = array();
       $errors = FALSE;
 
@@ -384,11 +431,7 @@ function content_copy_import_form_submit
         $values['type_name']         = $type_name;
         $field_url  = drupal_execute('_content_admin_field_add_new', $values, $type_name, $field_name);
         if (form_get_errors()) {
-          drupal_set_message(t("An error has occured adding the field %field_label (%field_name).<br/>Please check the errors displayed for more details.", array(
-              '%field_label' => $field_label,
-              '%field_name' => $field_name
-              )));
-          $errors = TRUE;
+          $errors[] = array('field_execute_error' => array($field_label, $field_name));
         }
         else {
           // Retrieve the new field name by picking out the arg from the url returned by
@@ -403,37 +446,22 @@ function content_copy_import_form_submit
         $values['type_name']         = $type_name;
         drupal_execute('_content_admin_field_add_existing', $values, $type_name);
         if (form_get_errors()) {
-          drupal_set_message(t("An error has occured adding the field %field_label (%field_name).<br/>Please check the errors displayed for more details.", array(
-              '%field_label' => $field_label,
-              '%field_name' => $field_name
-              )));
-          $errors = TRUE;
+          $errors[] = array('field_execute_error' => array($field_label, $field_name));
         }
       }
 
       // Once the field has been added, update the settings with the macro values.
       // Replace the export type name with the import type name.
-      if (!$errors) {
+      if (empty($errors)) {
         $field_values['type_name']   = $type_name;
         drupal_execute('_content_admin_field', $field_values, $type_name, $field_name);
         if (form_get_errors()) {
-           drupal_set_message(t("The field %field_label (%field_name) was added to the content type %type, but an error has occured updating the field settings.<br/>Please check the errors displayed for more details.", array(
-               '%field_label' => $field_label,
-               '%field_name' => $field_name,
-               '%type' => $type_label
-               )));
+          $errors[] = array('type_execute_error' => array($field_label, $field_name, $type_name));
         }
       }
     }
   }
-  if (!form_get_errors()) {
-    if (sizeof($imported_fields) > 0 || sizeof($imported_groups) > 0) {
-      drupal_goto('admin/content/types/'. $content_info['content types'][$type_name]['url_str'] .'/fields');
-    }
-    else {
-      drupal_goto('admin/content/types');
-    }
-  }
+  return $errors;
 }
 
 /**
@@ -451,7 +479,6 @@ function content_copy_form_alter($form_i
  *  Get all the fields for a content type.
  */
 function content_copy_fields($type_name) {
-
   $fields = array();
   if (!$type_name) {
     return $fields;
