[dev] [framework-patch] csv export: Outlook friendly

Francois Marier francois at nit.ca
Fri Jul 30 15:15:57 PDT 2004


Here's a patch to make Horde CSV files importable into MS Outlook.

It does 3 things:

  - rename the fields so that Outlook will match them
  - double quotes ( e.g. "Bob "big guy" Jones"  =>  "Bob ""big guy"" Jones" )
  - use \r\n as the EOL

All of the above are necessary to have Outlook compatibility.

Now, I also modified the import function so that the doubled quotes
are replaced by single ones and that the field names are restored to
ones that Horde can match automatically.

I have made sure that we can still import CSV files that were exported
using a previous version of Horde.  There are only two issues that
this change brings with respect to importing new CSVs inside an older
Horde:

  1- older versions will not match the fields automatically
  2- older versions will import the doubled quotes

Francois
-------------- next part --------------
diff -rpuN -X ../ignorelist ../build/framework/Data/Data/csv.php framework/Data/Data/csv.php
--- ../build/framework/Data/Data/csv.php	Fri Apr 16 13:20:03 2004
+++ framework/Data/Data/csv.php	Fri Jul 30 18:00:25 2004
@@ -62,6 +62,29 @@ class Horde_Data_csv extends Horde_Data 
 
         $data = array();
 
+        if ($header) {
+            $import_mapping = array(
+                'firstname' => 'first_name',
+                'middlename' => 'middle_name',
+                'lastname' => 'last_name',
+                'e-mail' => 'email',
+                'homeaddress' => 'homeAddress',
+                'businessaddress' => 'workAddress',
+                'homephone' => 'homePhone',
+                'businessphone' => 'workPhone',
+                'mobilephone' => 'cellPhone',
+                'businessfax' => 'fax',
+                'jobtitle' => 'title',
+                'company' => 'company',
+                'notes' => 'notes',
+                'name' => 'name',
+                'internetfreebusy' => 'freebusyUrl',
+                'nickname' => 'alias',
+                'pgpPublicKey' => 'pgpPublicKey',
+                'smimePublicKey' => 'smimePublicKey',
+                );
+        }
+
         /* File_CSV is present. */
         if (class_exists('File_CSV')) {
             /* File_CSV is a bit picky at what parameter it
@@ -93,7 +116,12 @@ class Horde_Data_csv extends Horde_Data 
                 } else {
                     $newline = array();
                     for ($i = 0; $i < count($head); $i++) {
-                        $newline[$head[$i]] = empty($line[$i]) ? '' : $line[$i];
+                        if (isset($import_mapping[$head[$i]])) {
+                            $head[$i] = $import_mapping[$head[$i]];
+                        }
+                        $cell = $line[$i];
+                        $cell = preg_replace("/\"\"/", "\"", $cell);
+                        $newline[$head[$i]] = empty($cell) ? '' : $cell;
                     }
                     $data[] = $newline;
                 }
@@ -122,7 +150,12 @@ class Horde_Data_csv extends Horde_Data 
                 } else {
                     $newline = array();
                     for ($i = 0; $i < count($head); $i++) {
-                        $newline[$head[$i]] = empty($line[$i]) ? '' : $line[$i];
+                        if (isset($import_mapping[$head[$i]])) {
+                            $head[$i] = $import_mapping[$head[$i]];
+                        }
+                        $cell = $line[$i];
+                        $cell = preg_replace("/\"/", "\"\"", $cell);
+                        $newline[$head[$i]] = empty($cell) ? '' : $cell;
                     }
                     $data[] = $newline;
                 }
@@ -153,15 +186,37 @@ class Horde_Data_csv extends Horde_Data 
         }
 
         $export = '';
+        $eol = "\r\n";
         if ($header) {
+            $export_mapping = array(
+                'first_name' => 'firstname',
+                'middle_name' => 'middlename',
+                'last_name' => 'lastname',
+                'email' => 'e-mail',
+                'homeAddress' => 'homeaddress',
+                'workAddress' => 'businessaddress',
+                'homePhone' => 'homephone',
+                'workPhone' => 'businessphone',
+                'cellPhone' => 'mobilephone',
+                'fax' => 'businessfax',
+                'title' => 'jobtitle',
+                'company' => 'company',
+                'notes' => 'notes',
+                'name' => 'name',
+                'freebusyUrl' => 'internetfreebusy',
+                'alias' => 'nickname',
+                'pgpPublicKey' => 'pgpPublicKey',
+                'smimePublicKey' => 'smimePublicKey',
+                );
+
             $head = current($data);
             foreach (array_keys($head) as $key) {
                 if (!empty($key)) {
-                    $export .= '"' . $key . '"';
+                    $export .= '"' . $export_mapping[$key] . '"';
                 }
                 $export .= ',';
             }
-            $export = substr($export, 0, -1) . "\n";
+            $export = substr($export, 0, -1) . $eol;
         }
 
         foreach ($data as $row) {
@@ -171,7 +226,7 @@ class Horde_Data_csv extends Horde_Data 
                 }
                 $export .= ',';
             }
-            $export = substr($export, 0, -1) . "\n";
+            $export = substr($export, 0, -1) . $eol;
         }
 
         return $export;


More information about the dev mailing list