From 33919b2bb77fde13be27086e263f413cbb3c9211 Mon Sep 17 00:00:00 2001
From: Paolo Pustorino <paolo@heterogenius.it>
Date: Tue, 30 Aug 2011 22:54:11 +0200
Subject: [PATCH] Added support for rule action

---
 node_convert.module    |    3 +-
 node_convert.rules.inc |   70 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletions(-)

diff --git a/node_convert.module b/node_convert.module
index dc72eac..33aa787 100755
--- a/node_convert.module
+++ b/node_convert.module
@@ -752,7 +752,7 @@ function node_convert_convert_action_form($context) {
   $form['template'] = array(
     '#type' => 'select',
     '#title' => t("Template"),
-    '#description' => t("Convesion template to use when the action is fired."),
+    '#description' => t("Conversion template to use when the action is fired."),
     '#options' => $templates,
     '#default_value' => isset($default_template) ? $default_template : '',
   );
@@ -834,6 +834,7 @@ function node_convert_node_convert($nid, $dest_node_type, $source_fields, $dest_
   }*/
   cache_clear_all('field:node:' . $node->nid, 'cache_field');
   $converted_node = node_load($nid, NULL, TRUE);
+  
   try {
     node_save($converted_node);
   }
diff --git a/node_convert.rules.inc b/node_convert.rules.inc
index 2aca465..117ff23 100755
--- a/node_convert.rules.inc
+++ b/node_convert.rules.inc
@@ -23,3 +23,73 @@ function node_convert_rules_event_info() {
     ),
   );
 }
+
+function node_convert_rules_action_info() {
+  return array(
+    'node_convert_convert_node'=> array(
+      'label' => t('Convert node to new type'),
+      'group' => t('Node'),
+      'parameter' => array(
+        'node' => array(
+          'type' => 'node',
+          'label' => t('Node to convert'),
+          'save' => TRUE,
+        ),
+        'template' => array(
+          'type' => 'list<integer>',
+          'options list' => 'node_convert_get_templates',
+          'label' => t('Node Convert template to apply'),
+          'restriction' => 'input',
+        ),
+      ),
+      'callbacks' => array(
+        'execute' => 'node_convert_convert_node_rules_action',
+      ),
+    ),
+  );
+}
+
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
+function node_convert_get_templates() {
+  $result = db_query("SELECT * FROM {node_convert_templates} ORDER BY name");
+  foreach ($result as $row) {
+    $rows[$row->nctid] = $row->name;
+  }
+  return $rows;
+}
+
+/**
+ * @todo Please document this function.
+ * @see http://drupal.org/node/1354
+ */
+function node_convert_convert_node_rules_action($node, $template) {
+  
+  if ($template === "0") {
+    drupal_set_message(t("Dummy template. No action is being performed."), 'warning', FALSE);
+    return FALSE;
+  }
+  $template = node_convert_load_template($template);
+
+  $access = node_convert_check_template_permission_user(array('template' => $template));
+  if ($access == FALSE) {
+    drupal_set_message(t("You don't have permission to use this conversion template"), 'warning', FALSE);
+    return;
+  }
+
+  if ($node->type != $template['source_type']) {
+    drupal_set_message(t("Node %nid doesn't match the template source type. Discarded.", array('nid' => $node->nid)), 'warning');
+  }
+  else {
+    $result = node_convert_node_convert($node->nid, $template['destination_type'], $template['data']['fields']['source'], $template['data']['fields']['destination'], $template['data']['no_fields'], $template['data']['hook_options']);
+    // We display errors if any, or the default success message
+    node_convert_messages($result, array('nid' => $node->nid));
+    // This is node_load necessary. It loads the new data from the DB, which gets passed down the action chain, where it is saved
+    return array(
+      'node' => node_load($node->nid, NULL, TRUE),
+      'template' => $template['nctid'],
+    );
+  }
+}
\ No newline at end of file
-- 
1.7.4.1

