=== added directory 'ca/ca_export'
=== added file 'ca/ca_export/ca_export.info'
--- ca/ca_export/ca_export.info	1970-01-01 00:00:00 +0000
+++ ca/ca_export/ca_export.info	2009-02-10 21:51:14 +0000
@@ -0,0 +1,6 @@
+; $Id$
+name = Export conditional actions
+description = "Export conditional actions"
+package = Ubercart - extra
+core = 6.x
+php = 5.0

=== added file 'ca/ca_export/ca_export.module'
--- ca/ca_export/ca_export.module	1970-01-01 00:00:00 +0000
+++ ca/ca_export/ca_export.module	2009-02-11 22:48:32 +0000
@@ -0,0 +1,280 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * This module imports/exports conditional actions from/to XML using
+ * the SimpleXML library.
+ */
+
+/*******************************************************************************
+ * Drupal Hooks
+ ******************************************************************************/
+
+/**
+ * Implementation of hook_menu().
+ */
+function ca_export_menu() {
+  $items['ca-export'] = array(
+    'title' => 'Export conditional actions',
+    'description' => 'Export conditional actions to XML, by module, CA item type, or name.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ca_export_export_form'),
+    'access arguments' => array('administer conditional actions'),
+  );
+  $items['ca-import'] = array(
+    'title' => 'Import conditional actions',
+    'description' => 'Import conditional actions from an XML file.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ca_export_import_form'),
+    'access arguments' => array('administer conditional actions'),
+  );
+  return $items;
+}
+
+function ca_export_export_form() {
+  $args = func_get_args();
+  $form_state = array_shift($args);
+  
+  $xml = ca_export($args)->asXML();
+  
+  $form['export'] = array(
+    '#type' => 'textarea',
+    '#rows' => 15,
+    '#default_value' => $xml,
+  );
+  
+  $form['download'] = array(
+    '#type' => 'submit',
+    '#value' => t('Download as XML'),
+    '#prefix' => '<div class="container-inline">',
+  );
+  
+  $form['gzip'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('GZIP it first.'),
+    '#suffix' => '</div>',
+  );
+  
+  return $form;
+}
+
+function ca_export_export_form_submit($form, &$form_state) {
+  $xml = $form_state['values']['export'];
+  $filename = 'ca.xml';
+  if ($form_state['values']['gzip']) {
+    $xml = gzencode($xml);
+    $filename .= '.gz';
+    drupal_set_header('Content-Type: application/x-gzip; charset=utf-8');
+  }
+  else {
+    drupal_set_header('Content-Type: text/xml; charset=utf-8');
+  }
+  
+  drupal_set_header('Content-Disposition: attachment; filename="'. $filename .'"');
+  drupal_set_header('Content-Length: '. strlen($xml));
+  
+  // Reset time limit for large files
+  set_time_limit(0);
+
+  // Push the data to the client.
+  print($xml);
+  flush();
+  ob_flush();
+}
+
+function ca_export_import_form($form_state) {
+  $form['import'] = array(
+    '#type' => 'textarea',
+    '#rows' => 15,
+  );
+  
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Import conditional actions',
+  );
+  
+  return $form;
+}
+
+function ca_export_import_form_validate($form, &$form_state) {
+  if (FALSE !== ($xml = simplexml_load_string($form_state['values']['import']))) {
+    $form_state['storage']['xml'] = ca_import($xml);
+  }
+  else {
+    form_set_error('import', t('The XML could not be parsed.'));
+  }
+}
+
+function ca_export_import_form_submit($form, &$form_state) {
+  global $user;
+  
+  $predicates = $form_state['storage']['xml'];
+  foreach ($predicates as $pid => $predicate) {
+    if (is_numeric($pid)) {
+      $predicate['#pid'] = variable_get('ca_predicates_pid', 1);
+      variable_set('ca_predicates_pid', $predicate['#pid'] + 1);
+    }
+    else {
+      $predicate['#pid'] = $pid;
+    }
+    $predicate['#uid'] = $user->uid;
+    
+    ca_save_predicate($predicate);
+  }
+  drupal_set_message(t('Conditional actions imported successfully'));
+}
+
+function ca_export($args) {
+  $ca_data = array();
+  $o_subject = array_shift($args);
+  
+  if (module_exists($o_subject)) {
+    $is_module = TRUE;
+  }
+  if ($is_module) {
+    $modules = array($o_subject);
+  }
+  else {
+    $modules = module_implements('ca_predicate');
+  }
+  foreach ($modules as $module) {
+    $ca_data = array_merge($ca_data, (array) module_invoke($module, 'ca_predicate'));
+  }
+  
+  // Get user-defined predicates...
+  $result = db_query("SELECT * FROM {ca_predicates}");
+  while ($data = db_fetch_array($result)) {
+    // Module defined predicates have string IDs.  When a user modifies one of
+    // these, we unset the module defined predicate and reconsider adding it in.
+    if (!is_numeric($data['pid'])) {
+      unset($ca_data[$data['pid']]);
+    }
+
+    $predicate = ca_prepare_db_predicate($data);
+    $ca_data[$data['pid']] = $predicate;
+  }
+  
+  if (!$is_module) {
+    if (array_key_exists($o_subject, $ca_data)) {
+      $element = $ca_data[$o_subject];
+      $ca_data = array($o_subject => $element);
+    }
+  }
+  
+  // New SimpleXML object.
+  $parent = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><conditional_actions></conditional_actions>');
+  _ca_export_predicates($parent, $ca_data);
+  
+  return $parent;
+}
+  
+function _ca_export_predicates(&$parent, $predicates) {
+  foreach ($predicates as $key => $predicate) {
+    $child = $parent->addChild('predicate');
+    
+    $child->addChild('title', $predicate['#title']);
+    $child->addChild('description', $predicate['#description']);
+    $child->addChild('class', $predicate['#class']);
+    $child->addChild('status', $predicate['#status']);
+    $child->addChild('trigger', $predicate['#trigger']);
+    $child->addChild('weight', $predicate['#weight']);
+    $child->addChild('pid', $key);
+    
+    if (is_array($predicate['#conditions'])) {
+      _ca_export_predicate_conditions($child->addChild('conditions'), $predicate['#conditions']);
+    }
+    
+    if (is_array($predicate['#actions'])) {
+      $actions = $child->addChild('actions');
+      foreach ($predicate['#actions'] as $action) {
+        _ca_export_predicate_element_template($actions->addChild('action'), $action);
+      }
+    }
+  }
+}
+
+function _ca_export_predicate_conditions(&$parent, $conditions) {
+  
+  if (array_key_exists('#operator', $conditions)) {
+    $parent->addChild('operator', $conditions['#operator']);
+    _ca_export_predicate_conditions($parent->addChild('conditions'), $conditions['#conditions']);
+  }
+  else {
+    foreach ($conditions as $condition) {
+      _ca_export_predicate_element_template($parent->addChild('condition'), $condition);
+    }
+  }
+}
+
+function _ca_export_predicate_element_template(&$parent, $element) {
+  $parent->addChild('name', $element['#name']);
+  $parent->addChild('title', $element['#title']);
+  $parent->addChild('description', $element['#description']);
+  
+  if (is_array($element['#argument_map'])) {
+    $map = $parent->addChild('argument_map');
+    foreach ($element['#argument_map'] as $key => $value) {
+      $map->addChild($key, $value);
+    }
+  }
+  
+  if (is_array($element['#settings'])) {
+    $settings = $parent->addChild('settings');
+    foreach ($element['#settings'] as $key => $value) {
+      $settings->addChild($key, base64_encode(serialize($value)));
+    }
+  }
+}
+
+function ca_import($xml) {
+  return _ca_import_predicates($xml);
+}
+
+function _ca_import_predicates($predicates) {
+  $result = array();
+  foreach ($predicates as $predicate) {
+    $predicate = (array) $predicate;
+    
+    foreach (array('title', 'description', 'class', 'status', 'trigger', 'weight') as $key) {
+      $result[$predicate['pid']]['#'. $key] = (string) $predicate[$key];
+    }
+    $result[$predicate['pid']]['#conditions'] = _ca_import_predicate_conditions((array) $predicate['conditions']);
+    
+    foreach ((array) $predicate['actions'] as $action) {
+      $result[$predicate['pid']]['#actions'] = _ca_import_predicate_element_template((array) $action);
+    }
+  }
+  return $result;
+}
+
+function _ca_import_predicate_conditions($conditions) {
+  $result = array();
+  if (array_key_exists('operator', $conditions)) {
+    $result['#operator'] = $conditions['operator'];
+    $result['#conditions'] = _ca_import_predicate_conditions($conditions['conditions']);
+  }
+  else {
+    foreach ($conditions as $condition) {
+      $result[] = _ca_import_predicate_element_template((array) $condition);
+    }
+  }
+  return $result;
+}
+
+function _ca_import_predicate_element_template($element) {
+  $result = array();
+  
+  $result['#name'] = (string) $element['name'];
+  $result['#title'] = (string) $element['title'];
+  $result['#description'] = (string) $element['description'];
+  
+  foreach ((array) $element['argument_map'] as $key => $value) {
+    $result['#argument_map'][$key] = $value;
+  }
+  
+  foreach ((array) $element['settings'] as $key => $value) {
+    $result['#settings'][$key] = unserialize(base64_decode($value));
+  }
+  return $result;
+}

