From eeb693b52f63a593a1aadd505a1439657c865a1a Mon Sep 17 00:00:00 2001
From: Christopher Gervais <chris@ergonlogic.com>
Date: Fri, 20 Jan 2012 11:32:46 -0500
Subject: [PATCH] Issue #1213898: Backported patch to the 6.x-4.x branch

---
 corresponding_node_references.install |   13 +++++++-
 corresponding_node_references.module  |   56 ++++++++++++++++++++++++++++----
 2 files changed, 61 insertions(+), 8 deletions(-)

diff --git a/corresponding_node_references.install b/corresponding_node_references.install
index ad2f413..c4278b7 100644
--- a/corresponding_node_references.install
+++ b/corresponding_node_references.install
@@ -12,6 +12,17 @@ function corresponding_node_references_install() {
 function corresponding_node_references_schema() {
   $schema['corresponding_node_references'] = array(
     'description' => t('Saves the content types and node reference fields for which the corresponding node reference is enabled'),
+    'export' => array(
+      'key' => 'node_types_content_fields',
+      'identifier' => 'preset', // Exports will be defined as $preset
+      'default hook' => 'default_corresponding_node_references_preset',  // Function hook name.
+      'api' => array(
+        'owner' => 'corresponding_node_references',
+        'api' => 'default_corresponding_node_references_presets',  // Base name for api include files.
+        'minimum_version' => 1,
+        'current_version' => 1,
+      ),
+    ),    
     'fields' => array (
       'node_types_content_fields' => array('type' => 'varchar','length' => 200,'not null' => TRUE,'default' => ''),
       'enabled' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),      
@@ -67,4 +78,4 @@ function corresponding_node_references_update_6400() {
   }
   
   return $ret;
-}
\ No newline at end of file
+}
diff --git a/corresponding_node_references.module b/corresponding_node_references.module
index 9ee82db..fcf987f 100644
--- a/corresponding_node_references.module
+++ b/corresponding_node_references.module
@@ -90,7 +90,7 @@ function corresponding_node_references_settings_form() {
         );
         $form['values'][$count]['enabled_' . $count] = array(
           '#type' => 'checkbox', 
-          '#default_value' => db_result(db_query("SELECT enabled FROM {corresponding_node_references} WHERE node_types_content_fields = '%s'", $formated_key)),
+          '#default_value' => corresponding_node_references_preset_enabled($formated_key),
           '#title' => $formated_label,
         );
         $count++;        
@@ -116,7 +116,7 @@ function corresponding_node_references_settings_form() {
  * Submit function for settings form
  */ 
 function corresponding_node_references_settings_form_submit($form, $form_values) {
-  db_query("UPDATE {corresponding_node_references} SET enabled = %d", 0);
+  ctools_include('export');
   $query_values = array();
   
   $mapping = unserialize($form_values['values']['mapping']);
@@ -127,9 +127,22 @@ function corresponding_node_references_settings_form_submit($form, $form_values)
     }
   }  
   
+  // load all existing presets
+  $presets = ctools_export_crud_load_all('corresponding_node_references');
   foreach($query_values as $key => $value) {
-    db_query("DELETE FROM {corresponding_node_references} WHERE node_types_content_fields='%s'", $key);  
-	  db_query("INSERT INTO {corresponding_node_references} (node_types_content_fields, enabled) VALUES ('%s', %d)", $key,$value);   
+    // get preset object (create new one if it doesn't exist already).
+    $preset = empty($presets[$key]) ? ctools_export_crud_new('corresponding_node_references') : $presets[$key];
+    // set and save value
+    $preset->enabled = $value;
+    // we might want to load the schema to get the name of this property
+    $preset->node_types_content_fields = $key;
+    ctools_export_crud_save('corresponding_node_references', $preset);
+    // remove from list of presets, so we know which ones are still used
+    if (isset($presets[$key])) unset($presets[$key]);
+  }
+  // delete old presets
+  foreach ($presets as $preset) {
+    ctools_export_crud_delete($preset->table, $preset);
   }
   
   drupal_set_message(t('The configuration has been saved'));
@@ -159,6 +172,30 @@ function corresponding_node_references_nodeapi(&$node, $op, $a3 = NULL, $a4 = NU
 }
  
 /**
+ * Load enabled CNR presets.
+ */
+function corresponding_node_references_preset_load_enabled() {
+  ctools_include('export');
+  return ctools_export_load_object('corresponding_node_references', 'conditions', array('enabled' => 1));
+}
+
+/**
+ * Return CNR preset by key.
+ */
+function corresponding_node_references_preset_load($key) {
+  ctools_include('export');
+  return ctools_export_crud_load('corresponding_node_references', $key);
+}
+
+/**
+ * Return 1 if CNR preset specified by given key is enabled.
+ */
+function corresponding_node_references_preset_enabled($key) {
+  $preset = corresponding_node_references_preset_load($key);
+  return empty($preset) ? 0 : $preset->enabled;
+}
+
+/**
  * Process a node's corresponding node references.
  *
  * @param $op the operation being performed on the node.
@@ -168,8 +205,8 @@ function corresponding_node_references_nodeapi(&$node, $op, $a3 = NULL, $a4 = NU
 function corresponding_node_references_process_node($op, $node, $process_unchanged = FALSE) {
   module_load_include('inc', 'corresponding_node_references', 'corresponding_node_references.crud');
 
-  $result = db_query("SELECT node_types_content_fields FROM {corresponding_node_references} WHERE enabled = %d", 1);
-  while ($row = db_fetch_object($result)) {
+  $result = corresponding_node_references_preset_load_enabled();
+  while ($row = array_shift($result)) {
     $key = explode('*', $row->node_types_content_fields);
 
     switch ($node->type) {
@@ -292,7 +329,7 @@ function corresponding_node_references_batch_update_existing_nodes($types, $limi
 
   $nids = array();
   $args = $types;
-  $args[] = $context['sandbox']['current_node'];
+  $args['current_node'] = $context['sandbox']['current_node'];
   // Get node IDs to update.
   $result = db_query_range("SELECT nid
                               FROM {node}
@@ -342,3 +379,8 @@ function corresponding_node_references_batch_update_existing_finished($success,
   drupal_set_message($message, $type);
 }
 
+function corresponding_node_references_ctools_plugin_api($owner, $api) {
+  if ($owner == 'corresponding_node_references' && $api == 'default_corresponding_node_references_presets') {
+    return array('version' => 1);
+  }
+}
-- 
1.7.2.5

