MFB:
authorJan Schneider <jan@horde.org>
Wed, 30 Dec 2009 14:19:29 +0000 (15:19 +0100)
committerJan Schneider <jan@horde.org>
Wed, 30 Dec 2009 14:19:29 +0000 (15:19 +0100)
[jan] Only export non-empty fields if exporting manually.
[jan] Add export to vCard 3.0.

turba/data.php
turba/docs/CHANGES
turba/lib/Driver.php
turba/templates/data/export.inc

index 7d01149..2d0d596 100644 (file)
@@ -233,8 +233,14 @@ case 'export':
         $sources[$source] = array();
     }
 
+    $exportType = Horde_Util::getFormData('exportID');
+    $vcard = $exportType == EXPORT_VCARD ||
+        $exportType == 'vcard30';
+    if ($vcard) {
+        $version = $exportType == 'vcard30' ? '3.0' : '2.1';
+    }
+
     $data = array();
-    $contacts = array();
     $all_fields = array();
     foreach ($sources as $source => $objectkeys) {
         /* Create a Turba storage instance. */
@@ -264,25 +270,27 @@ case 'export':
         $all_fields = array_merge($all_fields, $fields);
         $params = $driver->getParams();
         foreach ($results as $ob) {
-            $row = array();
-            foreach ($fields as $field) {
-                if (substr($field, 0, 2) != '__') {
-                    $attribute = $ob->getValue($field);
-                    if ($attributes[$field]['type'] == 'date') {
-                        $row[$field] = strftime('%Y-%m-%d', $attribute);
-                    } elseif ($attributes[$field]['type'] == 'time') {
-                        $row[$field] = strftime('%R', $attribute);
-                    } elseif ($attributes[$field]['type'] == 'datetime') {
-                        $row[$field] = strftime('%Y-%m-%d %R', $attribute);
-                    } else {
+            if ($vcard) {
+                $data[] = $driver->tovCard($ob, $version, null, true);
+            } else {
+                $row = array();
+                foreach ($fields as $field) {
+                    if (substr($field, 0, 2) != '__') {
+                        $attribute = $ob->getValue($field);
+                        if ($attributes[$field]['type'] == 'date') {
+                            $row[$field] = strftime('%Y-%m-%d', $attribute);
+                        } elseif ($attributes[$field]['type'] == 'time') {
+                            $row[$field] = strftime('%R', $attribute);
+                        } elseif ($attributes[$field]['type'] == 'datetime') {
+                            $row[$field] = strftime('%Y-%m-%d %R', $attribute);
+                        } else {
                         $row[$field] = Horde_String::convertCharset($attribute, Horde_Nls::getCharset(), $params['charset']);
+                        }
                     }
                 }
+                $data[] = $row;
             }
-            $data[] = $row;
         }
-
-        $contacts = array_merge($contacts, $results);
     }
     if (!count($data)) {
         $notification->push(_("There were no addresses to export."), 'horde.message');
@@ -292,7 +300,7 @@ case 'export':
 
     /* Make sure that all rows have the same columns if exporting from
      * different sources. */
-    if (count($sources) > 1) {
+    if (!$vcard && count($sources) > 1) {
         for ($i = 0; $i < count($data); $i++) {
             foreach ($all_fields as $field) {
                 if (!isset($data[$i][$field])) {
@@ -302,7 +310,7 @@ case 'export':
         }
     }
 
-    switch (Horde_Util::getFormData('exportID')) {
+    switch ($exportType) {
     case Horde_Data::EXPORT_CSV:
         $csv = Horde_Data::singleton('csv');
         $csv->exportFile(_("contacts.csv"), $data, true);
@@ -319,13 +327,9 @@ case 'export':
         exit;
 
     case Horde_Data::EXPORT_VCARD:
-        $cards = array();
-        foreach ($contacts as $contact) {
-            $cards[] = Turba_Driver::tovCard($contact);
-        }
-
+    case 'vcard30':
         $vcard = Horde_Data::singleton('vcard');
-        $vcard->exportFile(_("contacts.vcf"), $cards, true);
+        $vcard->exportFile(_("contacts.vcf"), $data, true);
         exit;
 
     case 'ldif':
index d4066d3..3828f02 100644 (file)
@@ -10,6 +10,8 @@ v3.0-git
 v2.3.4-cvs
 ----------
 
+[jan] Only export non-empty fields if exporting manually.
+[jan] Add export to vCard 3.0.
 [jan] Only synchronize those fields that are supported by the client
       (Request #6658, requires Horde 3.3.7).
 
index a735afc..f72a565 100644 (file)
@@ -948,12 +948,11 @@ class Turba_Driver
      * @param string $version       The vcard version to produce.
      * @param array $fields         Hash of field names and SyncML_Property
      *                              properties with the requested fields.
-     *
-     * @static
+     * @param boolean $skipEmpty    Whether to skip empty fields.
      *
      * @return Horde_iCalendar_vcard  A Horde_iCalendar_vcard object.
      */
-    function tovCard($object, $version = '2.1', $fields = null)
+    function tovCard($object, $version = '2.1', $fields = null, $skipEmpty = false)
     {
         $hash = $object->getAttributes();
         $vcard = new Horde_iCalendar_vcard($version);
@@ -962,6 +961,10 @@ class Turba_Driver
         $geo = null;
 
         foreach ($hash as $key => $val) {
+            if ($skipEmpty && !strlen($val)) {
+                continue;
+            }
+
             if ($version != '2.1') {
                 $val = Horde_String::convertCharset($val, Horde_Nls::getCharset(), 'utf-8');
             }
@@ -1631,15 +1634,24 @@ class Turba_Driver
         }
 
         if (!$formattedname && (!$fields || isset($fields['FN']))) {
-            $val = empty($hash['firstname']) ? $hash['lastname'] : $hash['firstname'] . ' ' . $hash['lastname'];
+            if (!empty($this->alternativeName) &&
+                isset($hash[$this->alternativeName])) {
+                $val = $hash[$this->alternativeName];
+            } elseif (isset($hash['lastname'])) {
+                $val = empty($hash['firstname']) ? $hash['lastname'] : $hash['firstname'] . ' ' . $hash['lastname'];
+            } else {
+                $val = '';
+            }
             $vcard->setAttribute('FN', $val, Horde_Mime::is8bit($val) ? $charset : array());
         }
 
         $org = array();
-        if (array_key_exists('company', $hash)) {
+        if (!empty($hash['company']) ||
+            (!$skipEmpty && array_key_exists('company', $hash))) {
             $org[] = $hash['company'];
         }
-        if (array_key_exists('department', $hash)) {
+        if (!empty($hash['department']) ||
+            (!$skipEmpty && array_key_exists('department', $hash))) {
             $org[] = $hash['department'];
         }
         if (count($org) && (!$fields || isset($fields['ORG']))) {
@@ -1652,14 +1664,23 @@ class Turba_Driver
         }
 
         if ((!$fields || isset($fields['ADR'])) &&
-            (array_key_exists('commonAddress', $hash) ||
-             array_key_exists('commonStreet', $hash) ||
-             array_key_exists('commonPOBox', $hash) ||
-             array_key_exists('commonExtended', $hash) ||
-             array_key_exists('commonCity', $hash) ||
-             array_key_exists('commonProvince', $hash) ||
-             array_key_exists('commonPostalCode', $hash) ||
-             array_key_exists('commonCountry', $hash))) {
+            (!empty($hash['commonAddress']) ||
+             !empty($hash['commonStreet']) ||
+             !empty($hash['commonPOBox']) ||
+             !empty($hash['commonExtended']) ||
+             !empty($hash['commonCity']) ||
+             !empty($hash['commonProvince']) ||
+             !empty($hash['commonPostalCode']) ||
+             !empty($hash['commonCountry']) ||
+             (!$skipEmpty &&
+              (array_key_exists('commonAddress', $hash) ||
+               array_key_exists('commonStreet', $hash) ||
+               array_key_exists('commonPOBox', $hash) ||
+               array_key_exists('commonExtended', $hash) ||
+               array_key_exists('commonCity', $hash) ||
+               array_key_exists('commonProvince', $hash) ||
+               array_key_exists('commonPostalCode', $hash) ||
+               array_key_exists('commonCountry', $hash))))) {
             /* We can't know if this particular Turba source uses a single
              * address field or multiple for
              * street/city/province/postcode/country. Try to deal with
@@ -1703,14 +1724,23 @@ class Turba_Driver
              (isset($fields['ADR']) &&
               (!isset($fields['ADR']->Params['TYPE']) ||
                isset($fields['ADR']->Params['TYPE']->ValEnum['HOME'])))) &&
-            (array_key_exists('homeAddress', $hash) ||
-             array_key_exists('homeStreet', $hash) ||
-             array_key_exists('homePOBox', $hash) ||
-             array_key_exists('homeExtended', $hash) ||
-             array_key_exists('homeCity', $hash) ||
-             array_key_exists('homeProvince', $hash) ||
-             array_key_exists('homePostalCode', $hash) ||
-             array_key_exists('homeCountry', $hash))) {
+            (!empty($hash['homeAddress']) ||
+             !empty($hash['homeStreet']) ||
+             !empty($hash['homePOBox']) ||
+             !empty($hash['homeExtended']) ||
+             !empty($hash['homeCity']) ||
+             !empty($hash['homeProvince']) ||
+             !empty($hash['homePostalCode']) ||
+             !empty($hash['homeCountry']) ||
+             (!$skipEmpty &&
+              (array_key_exists('homeAddress', $hash) ||
+               array_key_exists('homeStreet', $hash) ||
+               array_key_exists('homePOBox', $hash) ||
+               array_key_exists('homeExtended', $hash) ||
+               array_key_exists('homeCity', $hash) ||
+               array_key_exists('homeProvince', $hash) ||
+               array_key_exists('homePostalCode', $hash) ||
+               array_key_exists('homeCountry', $hash))))) {
             if (isset($hash['homeAddress']) && !isset($hash['homeStreet'])) {
                 $hash['homeStreet'] = $hash['homeAddress'];
             }
@@ -1749,14 +1779,23 @@ class Turba_Driver
              (isset($fields['ADR']) &&
               (!isset($fields['ADR']->Params['TYPE']) ||
                isset($fields['ADR']->Params['TYPE']->ValEnum['WORK'])))) &&
-            (array_key_exists('workAddress', $hash) ||
-             array_key_exists('workStreet', $hash) ||
-             array_key_exists('workPOBox', $hash) ||
-             array_key_exists('workExtended', $hash) ||
-             array_key_exists('workCity', $hash) ||
-             array_key_exists('workProvince', $hash) ||
-             array_key_exists('workPostalCode', $hash) ||
-             array_key_exists('workCountry', $hash))) {
+            (!empty($hash['workAddress']) ||
+             !empty($hash['workStreet']) ||
+             !empty($hash['workPOBox']) ||
+             !empty($hash['workExtended']) ||
+             !empty($hash['workCity']) ||
+             !empty($hash['workProvince']) ||
+             !empty($hash['workPostalCode']) ||
+             !empty($hash['workCountry']) ||
+             (!$skipEmpty &&
+              (array_key_exists('workAddress', $hash) ||
+               array_key_exists('workStreet', $hash) ||
+               array_key_exists('workPOBox', $hash) ||
+               array_key_exists('workExtended', $hash) ||
+               array_key_exists('workCity', $hash) ||
+               array_key_exists('workProvince', $hash) ||
+               array_key_exists('workPostalCode', $hash) ||
+               array_key_exists('workCountry', $hash))))) {
             if (isset($hash['workAddress']) && !isset($hash['workStreet'])) {
                 $hash['workStreet'] = $hash['workAddress'];
             }
index 916d5fb..1b85d41 100644 (file)
@@ -13,6 +13,7 @@
   <option value="<?php echo Horde_Data::EXPORT_OUTLOOKCSV ?>"><?php echo _("Comma separated values (Microsoft Outlook)") ?></option>
   <option value="<?php echo Horde_Data::EXPORT_TSV ?>"><?php echo _("Tab separated values") ?></option>
   <option value="<?php echo Horde_Data::EXPORT_VCARD ?>"><?php echo _("vCard") ?></option>
+  <option value="vcard30"><?php echo _("vCard (3.0)") ?></option>
   <option value="ldif"><?php echo _("LDIF Address Book") ?></option>
  </select><br />