Index: rules_admin/rules_admin.export.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/rules/rules_admin/Attic/rules_admin.export.inc,v
retrieving revision 1.1.2.5
diff -u -p -r1.1.2.5 rules_admin.export.inc
--- rules_admin/rules_admin.export.inc	20 Apr 2009 11:26:45 -0000	1.1.2.5
+++ rules_admin/rules_admin.export.inc	25 Jun 2009 08:33:49 -0000
@@ -150,18 +150,63 @@ function rules_admin_form_export($form_s
         '#default_value' => 0,
       );
     }
+
+    $desc = '<p>'. t('Optional; You can export your rule and rule-set configuration to a file. This is better than having the rules in the database, as it allows having better control over your rules and their alterations.') .'</p>';
+    $desc .= '<p>'. t('Note that you rules are prefixed with the module name you provided - this will lower the risk of rule name collisions, and is considered a good practice.') .'</p>';
+
+    $form['export_module'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Export to module'),
+      '#description' => $desc,
+    );
+    $form['export_module']['module'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Module name'),
+    );
     $form['button'] = array('#type' => 'submit', '#weight' => 10, '#value' => t('Export'));
   }
   else {
-    //show a textarea containg the exported configs
-    $form['result'] = array(
-      '#type' => 'textarea',
-      '#title' => t('Exported rule configurations'),
-      '#description' => t('Copy these data and paste them into the import page, to import.'),
-      '#rows' => 15,
-      '#attributes' => array('readonly' => 'readonly'),
-      '#default_value' => var_export($form_state['export'], TRUE),
-    );
+    if (empty($form_state['values']['module'])) {
+      // Show a textarea containg the exported configs.
+      $lines = substr_count($form_state['export'], "\n");
+      $form['result'] = array(
+        '#type' => 'textarea',
+        '#title' => t('Exported rule configurations'),
+        '#description' => t('Copy these data and paste them into the import page, to import.'),
+        '#rows' => min($lines, 150),
+        '#value' => $form_state['export'],
+        '#required' => FALSE,
+      );
+    }
+    else {
+      // Show export and default module information.
+      $module = check_plain($form_state['values']['module']);
+      $lines = substr_count($form_state['export'], "\n");
+
+      $form['info_file'] = array(
+        '#title' => t('Put this in @module.info in your modules/@module directory', array('@module' => $module)),
+        '#type' => 'textarea',
+        '#id' => 'export-api-textarea',
+        '#name' => 'export-api-textarea',
+        '#attributes' => array(),
+        '#rows' => 9,
+        '#cols' => 60,
+        '#value' => rules_admin_export_default_module_info($module),
+        '#required' => FALSE,
+      );
+
+      // Add the exported data.
+      $form['result'] = array(
+        '#title' => t('Put this in @module.rules_default.inc in your modules/@module directory or modules/@module/includes directory', array('@module' => $module)),
+        '#type' => 'textarea',
+        '#id' => 'export-textarea',
+        '#name' => 'export-textarea',
+        '#attributes' => array(),
+        '#rows' => min($lines, 150),
+        '#value' => $form_state['export'],
+        '#required' => FALSE,
+      );
+    }
   }
   return $form;
 }
@@ -188,9 +233,15 @@ function rules_admin_form_export_submit(
     foreach (array_keys($export) as $item_type) {
       // Allow item specific adaption before exporting
       foreach ($export[$item_type] as $item_name => $item) {
-        rules_item_type_invoke($item_type, 'export', array($item_name, &$export[$item_type][$item_name], &$export));
+        rules_item_type_invoke($item_type, 'export', array($item_name, &$export[$item_type][$item_name], &$export, $form_state));
       }
     }
+    if (!empty($form_state['values']['module'])) {
+      rules_admin_form_export_module($export, $form_state['values']['module']);
+    }
+    else {
+      $export = var_export($export, TRUE);
+    }
     $form_state['export'] = $export;
   }
   else {
@@ -200,14 +251,71 @@ function rules_admin_form_export_submit(
 }
 
 /**
+ * Prepare the export to a default module.
+ */
+function rules_admin_form_export_module(&$export, $module_name) {
+  $code = '  return '. var_export($export, TRUE) .";\n";
+  // Make code Drupal standard.
+  $code = substr_replace(str_replace("\n", "\n  ", $code), '', -2, 2);
+
+  $api = "/**\n";
+  $api .= " * Implementation of hook_rules_defaults().\n";
+  $api .= " */\n";
+  $api .= "function @module_rules_defaults() {\n";
+  $api .= $code;
+  $api .= "}";
+
+  $api = strtr($api, array('@module' => check_plain(str_replace(' ', '_', $module_name))));
+  $export = $api;
+}
+
+/**
+ * Add info file information to the exported data.
+ */
+function rules_admin_export_default_module_info($module = '') {
+  $output = ';$Id:$'."\n";
+  $output .= "name = $module export module\n";
+  $output .= "description = Exports rules configuration of $module\n";
+  $output .= "dependencies[] = rules\n";
+  $output .= "core = 6.x\n";
+  return $output;
+}
+
+/**
+ * Prefix exported rules with the module name and change the status to default.
+ */
+function rules_item_rule_export($item_name, &$rule_export, &$export, $form_state) {
+  if (!empty($form_state['values']['module']) && strpos($item_name, 'rules_') === 0) {
+    $prefix = check_plain(str_replace(' ', '_', $form_state['values']['module']));
+    // Change #status to 'default'.
+    $export['rules'][$item_name]['#status'] = 'default';
+    $export['rules'][$prefix .'_'. $item_name] = $export['rules'][$item_name];
+    // Unset the non-prefixed item.
+    unset($export['rules'][$item_name]);
+  }
+}
+
+/**
  * Item type callback: When exporting a rule set, add its rules to the export.
  */
-function rules_item_rule_set_export($set_name, &$rule_set, &$export) {
+function rules_item_rule_set_export($set_name, &$rule_set, &$export, $form_state) {
   $rules = rules_get_configured_items('rules');
   foreach ($rules as $name => $rule) {
     if ($rule['#set'] == $set_name) {
+      if (!empty($form_state['values']['module']) && strpos($name, 'rules_') === 0) {
+        $prefix = check_plain(str_replace(' ', '_', $form_state['values']['module']));
+        // Change rule status to 'default'.
+        $rule['#status'] = 'default';
+        $name = $prefix .'_'. $name;
+      }
       $export['rules'][$name] = $rule;
     }
   }
+  // Change the rule set status to default.
+  if (!empty($form_state['values']['module'])) {
+    foreach ($export['rule_sets'] as &$value) {
+      $value['status'] = 'default';
+    }
+  }
 }
