--- uc_importer.module	2008-08-20 11:42:25.000000000 +1000
+++ uc_importer.module.csv	2008-08-20 12:23:53.000000000 +1000
@@ -195,6 +195,7 @@ function uc_importer_export_page() {
     'nodesg' => 'product',
     'nodepl' => 'products',
     'multi' => 'true',
+    'multic' => 'true',
     'select' => 'buffer_products("'. file_create_url('') .'")',
   );
 
@@ -282,6 +283,14 @@ function uc_importer_export_buffer_form(
   );
   $form['products'] = array('#type' => 'hidden',
   );
+  $form['format'] = array(
+    '#type' => 'select',
+    '#multiple' => false,
+    '#title' => t('Format'),
+    '#options' => array(
+      'xml' => t('XML'), 
+      'csv' => t('CSV')),
+  );
   $form['reset'] = array('#type' => 'submit',
     '#value' => t('Reset'),
   );
@@ -306,8 +315,10 @@ function uc_importer_export_buffer_form_
   else {
     $products = array_filter(explode('/', $form_values['products']));
     //drupal_set_message('<pre>'. print_r($products, true) .'</pre>');
-    $xml = uc_importer_export($products);
-    if ($file = file_save_data($xml, file_directory_temp() .'/uc_export.xml', FILE_EXISTS_REPLACE)) {
+    $format = $form_values['format'];
+    $output = uc_importer_export($products, array('format'=>$format));
+    if ($output != '') {
+      if ($file = file_save_data($output, file_directory_temp() ."/uc_export.$format", FILE_EXISTS_REPLACE)) {
       //drupal_set_message(print_r($file, true));
       file_transfer($file, array(
         'Content-Type: application/xml',
@@ -315,6 +326,9 @@ function uc_importer_export_buffer_form_
         'Content-Disposition: attachment; filename="'. $file .'"',
       ));
     }
+    } else {
+      drupal_set_message(t("No data to export. This might be due to an invalid output format being selected"));
+    }
   }
 }
 
@@ -362,14 +376,17 @@ function uc_importer_import_form_submit(
 }
 
 /**
- * Constructs the XML representation of the store from the ids given.
+ * Constructs the selected representation of the store from the ids given.
  *
  * @param $nids
  *   An array of product ids.
+ * @param $settings
+ *   Array of key => value pairs to control export. Standard keys are:
+ *   'format' => String specifying the format of the exported data. Currently supported formats are XML ('xml') and CSV ('csv'). Default is XML.
  * @return
- *   XML data to be sent.
+ *   Exported data to be sent.
  */
-function uc_importer_export($nids) {
+function uc_importer_export($nids, $settings=array('format'=>'xml')) {
   $data = array(
     'vocabularies' => array(),
     'categories' => array(),
@@ -403,42 +420,56 @@ function uc_importer_export($nids) {
   foreach ($data as $type => $ids) {
     $data[$type] = array_unique($ids);
   }
-  drupal_set_message('<pre>'. print_r($data, true) .'</pre>');
-  $xml = '<?xml version="1.0" encoding="utf-8" ?>'. "\n";
-  $xml .= '<store xmlns="http://www.ubercart.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ubercart.org http://www.ubercart.org/files/store.xsd">';
+  //drupal_set_message('<pre>'. print_r($data, true) .'</pre>');
+  
+  $output = '';
+  //add format dependent prefix to output
+  switch ($settings['format']) {
+    case 'xml':
+      $output .= '<?xml version="1.0" encoding="utf-8" ?>'. "\n";
+      $output .= '<store xmlns="http://www.ubercart.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ubercart.org http://www.ubercart.org/files/store.xsd">';
+      break;
+  }
+  
+  //add store data to output
   if (is_array($data['vocabularies']) && !empty($data['categories'])) {
-    $xml .= _uc_importer_export_vocabularies($data['vocabularies']);
+    $output .= _uc_importer_export_vocabularies($data['vocabularies'], $settings);
   }
   if (is_array($data['categories']) && !empty($data['categories'])) {
-    $xml .= _uc_importer_export_categories($data['categories']);
+    $output .= _uc_importer_export_categories($data['categories'], $settings);
   }
   if (is_array($data['manufacturers']) && !empty($data['manufacturers'])) {
-    $xml .= _uc_importer_export_manufacturers($data['manufacturers']);
+    $output .= _uc_importer_export_manufacturers($data['manufacturers'], $settings);
   }
   if (is_array($data['attributes']) && !empty($data['attributes'])) {
-    $xml .= _uc_importer_export_attributes($data['attributes']);
+    $output .= _uc_importer_export_attributes($data['attributes'], $settings);
   }
   if (is_array($data['classes']) && !empty($data['classes'])) {
-    $xml .= _uc_importer_export_classes($data['classes']);
+    $output .= _uc_importer_export_classes($data['classes'], $settings);
   }
   if (is_array($data['products']) && !empty($data['products'])) {
-    $xml .= _uc_importer_export_products($data['products']);
+    $output .= _uc_importer_export_products($data['products'], $settings);
   }
   if (is_array($data['orders']) && !empty($data['orders'])) {
-    $xml .= _uc_importer_export_orders($data['orders']);
+    $output .= _uc_importer_export_orders($data['orders'], $settings);
   }
-  $xml .= uc_importer_invoke('export', 'store', $nids);
-  $xml .= '</store>';
+  $output .= uc_importer_invoke('export', 'store', $nids, $settings);
 
-  //drupal_set_message(htmlspecialchars($xml));
-  return $xml;
+  //add format dependent suffix to output
+  switch ($settings['format']) {
+    case 'xml':
+      $output .= '</store>';
+  }
+  
+  return $output;
 }
 
 /**
  * Export vocabularies as XML.
  */
-function _uc_importer_export_vocabularies($vocabularies) {
-  $xml .= '<vocabularies>';
+function _uc_importer_export_vocabularies($vocabularies, $settings=array('format'=>'xml')) {
+  switch ($settings['format']) {
+    case 'xml':$xml .= '<vocabularies>';
   foreach ($vocabularies as $vid) {
     $xml .= '<vocabulary>';
     $vocabulary = taxonomy_get_vocabulary($vid);
@@ -459,12 +490,20 @@ function _uc_importer_export_vocabularie
   }
   $xml .= '</vocabularies>';
   return $xml;
+      
+    case 'csv':
+    
+    default:
+      return null; //invalid format specified
+   }
 }
 
 /**
  * Export categories as XML.
  */
-function _uc_importer_export_categories($categories) {
+function _uc_importer_export_categories($categories, $settings=array('format'=>'xml')) {
+  switch ($settings['format']) {
+    case 'xml':
   $xml .= '<categories>';
   foreach ($categories as $tid) {
     $xml .= '<category>';
@@ -482,13 +521,20 @@ function _uc_importer_export_categories(
   }
   $xml .= '</categories>';
   return $xml;
+      
+    case 'csv':
+    
+    default:
+      return null; //invalid format specified
+   }
 }
 
 /**
  * Export manufacturers as XML.
  */
-function _uc_importer_export_manufacturers($manufacturers) {
-  $xml = '<manufacturers>';
+function _uc_importer_export_manufacturers($manufacturers, $settings=array('format'=>'xml')) {
+  switch ($settings['format']) {
+    case 'xml':$xml = '<manufacturers>';
   foreach ($manufacturers as $tid) {
     $manufacturer = uc_manufacturer_load($tid);
     $xml .= '<manufacturer>';
@@ -502,13 +548,20 @@ function _uc_importer_export_manufacture
   }
   $xml .= '</manufacturers>';
   return $xml;
+      
+    case 'csv':
+    
+    default:
+      return null; //invalid format specified
+   }
 }
 
 /**
  * Export product attributes as XML.
  */
-function _uc_importer_export_attributes($attributes) {
-  $xml = '<attributes>';
+function _uc_importer_export_attributes($attributes, $settings=array('format'=>'xml')) {
+  switch ($settings['format']) {
+    case 'xml':$xml = '<attributes>';
   foreach ($attributes as $aid) {
     $attribute = uc_attribute_load($aid);
     $xml .= '<attribute>';
@@ -534,13 +587,20 @@ function _uc_importer_export_attributes(
   }
   $xml .= '</attributes>';
   return $xml;
+      
+    case 'csv':
+    
+    default:
+      return null; //invalid format specified
+   }
 }
 
 /**
  * Export product node types as XML.
  */
-function _uc_importer_export_classes($classes) {
-  $xml = '<classes>';
+function _uc_importer_export_classes($classes, $settings=array('format'=>'xml')) {
+  switch ($settings['format']) {
+    case 'xml':$xml = '<classes>';
   foreach ($classes as $pcid) {
     $class = uc_product_class_load($pcid);
     $xml .= '<class>';
@@ -552,12 +612,20 @@ function _uc_importer_export_classes($cl
   }
   $xml .= '</classes>';
   return $xml;
+      
+    case 'csv':
+    
+    default:
+      return null; //invalid format specified
+   }
 }
 
 /**
- * Export products as XML.
+ * Export products.
  */
-function _uc_importer_export_products($products) {
+function _uc_importer_export_products($products, $settings=array('format'=>'xml')) {
+  switch ($settings['format']) {
+    case 'xml':
   $xml = '<products>';
   foreach ($products as $nid) {
     $xml .= '<product>';
@@ -680,13 +748,121 @@ function _uc_importer_export_products($p
   }
   $xml .= '</products>';
   return $xml;
+      
+    case 'csv':
+      //headings
+      $csv = 'unique_hash, id, type, name, description, model';
+      if (module_exists('uc_manufacturer')) $csv .= ',manufacturer';
+      $csv .= ',list_price, cost, sell_price, weight, weight_units, length, width, height, length_units, pkg_qty, default_qty, shippable';
+      if (module_exists('taxonomy')) {
+        $csv .= ', category IDs, category names';
+      }
+      if (module_exists('uc_attribute')) {
+        $attributes = array();
+        $db_result_attr = db_query("SELECT aid FROM {uc_attributes}");
+        $attribute_ids = array();
+        while ($aid = db_fetch_object($db_result_attr)) 
+          $attribute_ids[] = $aid->aid;
+        foreach ($attribute_ids as $aid) {
+          $attributes[$aid] = uc_attribute_load($aid);
+          foreach ($attributes[$aid]->options as $option) {
+            $csv .= ','.$attributes[$aid]->name. ': '. $option->name. ': enabled';
+            $csv .= ','.$attributes[$aid]->name. ': '. $option->name. ': cost';
+            $csv .= ','.$attributes[$aid]->name. ': '. $option->name. ': price';
+            $csv .= ','.$attributes[$aid]->name. ': '. $option->name. ': weight';
+            $csv .= ','.$attributes[$aid]->name. ': '. $option->name. ': ordering';
+            $csv .= uc_importer_invoke('export', 'product-option-headings', $product->nid, $option->oid, $settings);
+          }
+        }
+      }
+      
+      //product data
+      foreach ($products as $nid) {
+        $product = node_load($nid);
+        $csv .= "\n";
+        $csv .= $product->unique_hash;
+        $csv .= ','. $product->nid;
+        $csv .= ','. $product->type;
+        $csv .= ','. $product->title;
+        $csv .= ',"'. $product->body. '"';
+        $csv .= ','. $product->model;
+        if (module_exists('uc_manufacturer')) {
+          $manufacturer = uc_product_get_manufacturer($product->nid);
+          $csv .= ','. (isset($manufacturer->tid) ? $manufacturer->name : '');
+        }
+        $csv .= ','. (isset($product->list_price) ? ($product->list_price) : '');
+        $csv .= ','. (isset($product->cost) ? ($product->cost) : '');
+        $csv .= ','. $product->sell_price;
+        $csv .= ','. $product->weight;
+        $csv .= ','. $product->weight_units;
+        $csv .= ','. $product->length;
+        $csv .= ','. $product->width;
+        $csv .= ','. $product->height;
+        $csv .= ','. $product->length_units;
+        $csv .= ','. $product->pkg_qty;
+        $csv .= ','. $product->default_qty;
+        $csv .= ','. $product->shippable;
+        if (module_exists('taxonomy')) {
+          $terms = taxonomy_node_get_terms($product->nid);
+          //if (isset($terms[0]))
+          //  $csv .= ','$terms[0]->tid. ',"'. $terms[0]->name. '"';
+          $csv .= ',"';
+          $first = true;
+          foreach ($terms as $term) {
+            $csv .= ($first ? '' : '|'). $term->tid;
+            $first = false;
+          }
+          $csv .= '","';
+          $first = true;
+          foreach ($terms as $term) {
+            $csv .= ($first ? '' : '|'). $term->name;
+            $first = false;
+          }
+          $csv .= '"';
+        }
+        if (module_exists('uc_attribute')) {
+          //get attribute data for product
+          $pas = uc_product_get_attributes($product->nid);
+          $product_attributes = array();
+          foreach ($pas as $pa) {
+            $product_attributes[$pa->aid] = $pa;
+            $pa->options_by_id = array();
+            foreach ($pa->options as $option) {
+              $pa->options_by_id[$option->oid] = $option;
+            }
+            unset($pa->options);
+          }
+          
+          foreach ($attributes as $aid => $attr) {
+            $product_attr = $product_attributes[$aid];
+            foreach ($attr->options as $option) {
+              if ($product_attr != null && ($product_attr_opt = $product_attr->options_by_id[$option->oid]) != null) {
+                $csv .= ',yes';
+                $csv .= ','.$product_attr_opt->cost;
+                $csv .= ','.$product_attr_opt->price;
+                $csv .= ','.$product_attr_opt->weight;
+                $csv .= ','.$product_attr_opt->ordering;
+                $csv .= uc_importer_invoke('export', 'product-option', $product->nid, $option->oid, $settings);
+              } else {
+                $csv .= ',no,,,,'; //attribute or option not enabled for product
+              }
+            }
+          }
+        }
+        $csv .= uc_importer_invoke('export', 'product', $product->nid, $settings);
+      }
+      return $csv;
+    default:
+      return null; //invalid format specified
+   }
 }
 
 /**
  * Export orders as XML.
  */
-function _uc_importer_export_orders($orders) {
-  $xml = '<orders>';
+function _uc_importer_export_orders($orders, $settings=array('format'=>'xml')) {
+  switch ($settings['format']) {
+    case 'xml':$xml = '<orders>';
   foreach ($orders as $order_id) {
     $order = uc_order_load($order_id);
     if (!empty($order)) {
@@ -761,6 +937,12 @@ function _uc_importer_export_orders($ord
   }
   $xml .= '</orders>';
   return $xml;
+      
+    case 'csv':
+    
+    default:
+      return null; //invalid format specified
+   }
 }
 
 /**

