From b7e472877c1519634486f88fad2d482a5a155a92 Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Wed, 30 Dec 2009 15:19:29 +0100 Subject: [PATCH] MFB: [jan] Only export non-empty fields if exporting manually. [jan] Add export to vCard 3.0. --- turba/data.php | 50 +++++++++++---------- turba/docs/CHANGES | 2 + turba/lib/Driver.php | 99 ++++++++++++++++++++++++++++------------- turba/templates/data/export.inc | 1 + 4 files changed, 99 insertions(+), 53 deletions(-) diff --git a/turba/data.php b/turba/data.php index 7d0114994..2d0d5969e 100644 --- a/turba/data.php +++ b/turba/data.php @@ -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': diff --git a/turba/docs/CHANGES b/turba/docs/CHANGES index d4066d34f..3828f02bb 100644 --- a/turba/docs/CHANGES +++ b/turba/docs/CHANGES @@ -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). diff --git a/turba/lib/Driver.php b/turba/lib/Driver.php index a735afc2e..f72a565ab 100644 --- a/turba/lib/Driver.php +++ b/turba/lib/Driver.php @@ -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']; } diff --git a/turba/templates/data/export.inc b/turba/templates/data/export.inc index 916d5fb0b..1b85d4108 100644 --- a/turba/templates/data/export.inc +++ b/turba/templates/data/export.inc @@ -13,6 +13,7 @@ +
-- 2.11.0