[turba] importing TSV files

liamr@umich.edu liamr@umich.edu
Fri, 19 Apr 2002 00:56:42 -0400 (EDT)


I'm trying to impliment an TSV import as the first step towards allowing people
to import their pine, eudora, and mulberry mailboxes.  It's based on the
CSV stuff, but instead of using fgetcsv, i'm using a combination of
explode and fgets.

Anyhoo... php is complaining that there's no Data_tsv class, but I defined
on in horde/lib/Data.php:

[19-Apr-2002 00:50:29] PHP Fatal error:  Cannot instantiate non-existent
class:  data_tsv in /usr/local/projects/webmail/html-ssl/horde-2.1/turba/data.php on line 197

I've attached the diffs for what I've done so far.  I'm really not sure
why it thinks that data_tsv didn't get defined someplace.

Any ideas?
Liam

diff -p -N -U 4 -r horde-2.1/lib/Data.php horde-2.1UM/lib/Data.php
--- horde-2.1/lib/Data.php	Fri Apr 19 00:40:28 2002
+++ horde-2.1UM/lib/Data.php	Fri Apr 19 00:35:15 2002
@@ -20,8 +20,11 @@ define('IMPORT_ICALENDAR', 5);

 /** @constant IMPORT_VCARD Import vCards. */
 define('IMPORT_VCARD', 6);

+/** @constant IMPORT_TSV Import generic tsv data. */
+define('IMPORT_TSV', 7);
+
 define('EXPORT_CSV', 100);
 define('EXPORT_ICALENDAR', 101);
 define('EXPORT_VCARD', 102);

@@ -338,8 +341,52 @@ class Data_rfc2425 extends Data {
             $date_arr['sec'] = $times[2];
         }

         return $date_arr;
+    }
+}
+
+class Data_tsv extends Data {
+
+    function importFile($filename, $header=false, $delimiter="\t")
+    {
+        $fp = fopen($filename, 'r');
+        if (!$fp) return false;
+
+        $data = array();
+
+        if ($header) $head = explode($delimiter, fgets($fp, 1024));
+
+        while ($line = explode($delimiter, fgets($fp, 1024)) {
+            if (!isset($head)) $data[] = $line;
+            else {
+                $newline = array();
+                for ($i=0; $i<count($head); $i++) {
+                    //if (!empty($line[$i]))
+                    $newline[$head[$i]] = empty($line[$i]) ? '' : $line[$i];
+                }
+                $data[] = $newline;
+            }
+        }
+
+        fclose($fp);
+        return $data;
+    }
+
+    function import()
+    {
+    }
+
+    function export()
+    {
+    }
+
+    function exportFile()
+    {
+    }
+
+    function mapDate()
+    {
     }

 }
 ?>
diff -p -N -U 4 -r horde-2.1/turba/data.php horde-2.1UM/turba/data.php
--- horde-2.1/turba/data.php	Fri Apr 19 00:40:34 2002
+++ horde-2.1UM/turba/data.php	Fri Apr 19 00:34:20 2002
@@ -134,8 +134,11 @@ if ($actionID == HORDE_IMPORT) {
                 switch ($import_type) {
                 case IMPORT_CSV:
                     $type = "CSV";
                     break;
+                case IMPORT_TSV:
+                    $type = "TSV";
+                    break;
                 case IMPORT_OUTLOOK:
                     $type = "Outlook";
                     break;
                 }
@@ -188,8 +191,37 @@ if ($actionID == HORDE_IMPORT) {
                     }
                 }

                 break;
+
+            case IMPORT_TSV:
+                $tsv = new Data_tsv();
+                if (!isset($header)) {
+                    $header = Horde::getFormData('header');
+                }
+                $importData = $tsv->importFile($HTTP_POST_FILES['import_file']['tmp_name'], $header, "\t");
+                if (!isset($importData) || !is_array($importData)) {
+                    Horde::raiseMessage(_("There was an error importing the uploaded file"), HORDE_ERROR);
+                    $error = true;
+                } else {
+                    $dest = Horde::getFormData('dest');
+                    include_once HORDE_BASE . '/lib/SessionCache.php';
+                    $cache = new SessionCache();
+                    $cacheID = $cache->putObject(array($importData, $dest));
+                    $actionUrl = Horde::applicationUrl('data.php');
+
+                    $appFields = array();
+                    reset($cfgSources[$dest]['map']);
+                    while (list($key, ) = each($cfgSources[$dest]['map'])) {
+                        if (substr($key, 0, 2) != '__') {
+                            $appFields[$key] = $attributes[$key]['desc'];
+                        }
+                    }
+                }
+
+                break;
+

             case IMPORT_VCARD:
                 $vcf = new Data_rfc2425();
                 $data = $vcf->importFile($HTTP_POST_FILES['import_file']['tmp_name']);
@@ -367,8 +399,9 @@ if ($actionID == NO_ACTION || $error) {
     switch ($importID) {

     case IMPORT_OUTLOOK:
     case IMPORT_CSV:
+    case IMPORT_TSV:
         include $registry->getParam('templates', 'horde') . '/data/csvmap.inc';
         break;

     case IMPORT_MAPPED:
diff -p -N -U 4 -r horde-2.1/turba/templates/data/import.inc horde-2.1UM/turba/templates/data/import.inc
--- horde-2.1/turba/templates/data/import.inc	Fri Apr 19 00:40:38 2002
+++ horde-2.1UM/turba/templates/data/import.inc	Thu Apr 18 18:03:24 2002
@@ -14,8 +14,9 @@
     <table border="0"><tr><td>
         <?= _("Select the format to import from:") ?><br />
         <select name="importID" onchange="if (this.value == '<?= IMPORT_OUTLOOK ?>') document.import_form.header.checked=true;">
             <option value="<?= IMPORT_CSV ?>">CSV</option>
+            <option value="<?= IMPORT_TSV ?>">TSV</option>
             <option value="<?= IMPORT_OUTLOOK ?>">Outlook</option>
             <option value="<?= IMPORT_VCARD ?>">vCard</option>
         </select><br />
         <input type="checkbox" name="header" value="1" />