$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. */
$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');
/* 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])) {
}
}
- switch (Horde_Util::getFormData('exportID')) {
+ switch ($exportType) {
case Horde_Data::EXPORT_CSV:
$csv = Horde_Data::singleton('csv');
$csv->exportFile(_("contacts.csv"), $data, true);
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':
* @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);
$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');
}
}
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']))) {
}
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
(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'];
}
(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'];
}