Skip to content

Commit db2e201

Browse files
authored
Contact import improvements (#9431)
* contact import: correct mismapped fields * contacts: remove im:other field from UI, it does not exist in the vCard * vcard: add some more maps for common vcard types to roundcube types * contact import: list all possible roundcube contact fields in csv import UI, remove hard coded $local_map * add SORT_LOCALE_STRING flag * fix typos * remove unwanted label * move field list to csv2vcard * move rcube_csv2vcard::list_fields to rcmail_action_contacts_import::list_fields as it relies on rcmail_action_contacts * use single field map for csv2vcard imports, remove hardcoded version * fix test * small cs fix * reformat csv2vcard.inc * fix failing test * restore existance check * fix failing test again
1 parent 0eed806 commit db2e201

File tree

24 files changed

+1001
-1176
lines changed

24 files changed

+1001
-1176
lines changed

program/actions/contacts/import.php

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,14 @@ public function run($args = [])
101101
$map = rcube_utils::get_input_value('_map', rcube_utils::INPUT_GPC);
102102
$map = array_filter($map);
103103

104-
$csv->set_map($map);
104+
$csv->set_map($map, array_keys(self::list_fields($with_groups)));
105105
$csv->import($file_content, false, $skip_head);
106106

107107
unlink($filepath);
108108
} else {
109109
// save uploaded file for the real import in the next step
110110
$temp_csv = rcube_utils::temp_filename('csvimpt');
111111
if (move_uploaded_file($filepath, $temp_csv) && file_exists($temp_csv)) {
112-
$fields = $csv->get_fields();
113112
$last_map = $map;
114113
$map = $csv->import($file_content, true);
115114

@@ -145,7 +144,6 @@ public function run($args = [])
145144
'replace' => $replace,
146145
'target' => $target,
147146
'with_groups' => $with_groups,
148-
'fields' => !empty($fields) ? $fields : [],
149147
];
150148

151149
// Stored separately due to nested array limitations in session
@@ -385,16 +383,11 @@ public static function import_map($attrib)
385383
$rcmail = rcmail::get_instance();
386384
$params = $_SESSION['contactcsvimport']['params'];
387385

388-
// hide groups field from list when group import disabled
389-
if (empty($params['with_groups'])) {
390-
unset($params['fields']['groups']);
391-
}
386+
$available_fields = self::list_fields(!empty($params['with_groups']));
392387

393388
$fieldlist = new html_select(['name' => '_map[]']);
394389
$fieldlist->add($rcmail->gettext('fieldnotmapped'), '');
395-
foreach ($params['fields'] as $id => $name) {
396-
$fieldlist->add($name, $id);
397-
}
390+
$fieldlist->add(array_values($available_fields), array_keys($available_fields));
398391

399392
$field_table = new html_table(['cols' => 2] + $attrib);
400393

@@ -499,4 +492,51 @@ public static function import_group_id($group_name, $contacts, $create, &$import
499492

500493
return $group_id;
501494
}
495+
496+
/**
497+
* Returns the list of contact fields available for import
498+
*/
499+
public static function list_fields($groups)
500+
{
501+
$rcmail = rcmail::get_instance();
502+
$available_fields = [];
503+
504+
foreach (self::$CONTACT_COLTYPES as $id => $field) {
505+
if ($id == 'photo') {
506+
// skip photo field because there are no photos in CSV files
507+
continue;
508+
}
509+
510+
if (!empty($field['subtypes'])) {
511+
$subtype_names = array_map('rcmail_action_contacts_index::get_type_label', $field['subtypes']);
512+
513+
for ($i = 0; $i < count($field['subtypes']); $i++) {
514+
if (!empty($field['childs'])) {
515+
foreach ($field['childs'] as $cid => $child) {
516+
$available_fields[$cid . ':' . $field['subtypes'][$i]] = $child['label'] . ' - ' . $subtype_names[$i];
517+
}
518+
} else {
519+
$available_fields[$id . ':' . $field['subtypes'][$i]] = $field['label'] . ' - ' . $subtype_names[$i];
520+
}
521+
}
522+
} else {
523+
$available_fields[$id] = $field['label'];
524+
}
525+
}
526+
527+
if ($groups) {
528+
// allow importing of group assignments
529+
$available_fields['groups'] = $rcmail->gettext('groups');
530+
}
531+
532+
// add separate birthday date parts fields for thunderbird imports
533+
$available_fields['birthday-d'] = $rcmail->gettext('birth_day');
534+
$available_fields['birthday-m'] = $rcmail->gettext('birth_month');
535+
$available_fields['birthday-y'] = $rcmail->gettext('birth_year');
536+
537+
// sort by label for easy use
538+
asort($available_fields, \SORT_LOCALE_STRING);
539+
540+
return $available_fields;
541+
}
502542
}

program/actions/contacts/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class rcmail_action_contacts_index extends rcmail_action
201201
'size' => 40,
202202
'maxlength' => 128,
203203
'label' => 'instantmessenger',
204-
'subtypes' => ['aim', 'icq', 'msn', 'yahoo', 'jabber', 'skype', 'other'],
204+
'subtypes' => ['aim', 'icq', 'msn', 'yahoo', 'jabber', 'skype'],
205205
'category' => 'main',
206206
],
207207
'notes' => [

0 commit comments

Comments
 (0)