Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact import improvements #9431

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
60 changes: 50 additions & 10 deletions program/actions/contacts/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,14 @@ public function run($args = [])
$map = rcube_utils::get_input_value('_map', rcube_utils::INPUT_GPC);
$map = array_filter($map);

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

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

Expand Down Expand Up @@ -145,7 +144,6 @@ public function run($args = [])
'replace' => $replace,
'target' => $target,
'with_groups' => $with_groups,
'fields' => !empty($fields) ? $fields : [],
];

// Stored separately due to nested array limitations in session
Expand Down Expand Up @@ -370,16 +368,11 @@ public static function import_map($attrib)
$rcmail = rcmail::get_instance();
$params = $_SESSION['contactcsvimport']['params'];

// hide groups field from list when group import disabled
if (empty($params['with_groups'])) {
unset($params['fields']['groups']);
}
$available_fields = self::list_fields(!empty($params['with_groups']));

$fieldlist = new html_select(['name' => '_map[]']);
$fieldlist->add($rcmail->gettext('fieldnotmapped'), '');
foreach ($params['fields'] as $id => $name) {
$fieldlist->add($name, $id);
}
$fieldlist->add(array_values($available_fields), array_keys($available_fields));

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

Expand Down Expand Up @@ -472,4 +465,51 @@ public static function import_group_id($group_name, $contacts, $create, &$import

return $group_id;
}

/**
* Returns the list of contact fields available for import
*/
public static function list_fields($groups)
{
$rcmail = rcmail::get_instance();
$available_fields = [];

foreach (self::$CONTACT_COLTYPES as $id => $field) {
if ($id == 'photo') {
// skip photo field because there are no photos in CSV files
continue;
}

if (!empty($field['subtypes'])) {
$subtype_names = array_map('rcmail_action_contacts_index::get_type_label', $field['subtypes']);

for ($i = 0; $i < count($field['subtypes']); $i++) {
if (!empty($field['childs'])) {
foreach ($field['childs'] as $cid => $child) {
$available_fields[$cid . ':' . $field['subtypes'][$i]] = $child['label'] . ' - ' . $subtype_names[$i];
}
} else {
$available_fields[$id . ':' . $field['subtypes'][$i]] = $field['label'] . ' - ' . $subtype_names[$i];
}
}
} else {
$available_fields[$id] = $field['label'];
}
}

if ($groups) {
// allow importing of group assignments
$available_fields['groups'] = $rcmail->gettext('groups');
}

// add separate birthday date parts fields for thunderbird imports
$available_fields['birthday-d'] = $rcmail->gettext('birth_day');
$available_fields['birthday-m'] = $rcmail->gettext('birth_month');
$available_fields['birthday-y'] = $rcmail->gettext('birth_year');

// sort by label for easy use
asort($available_fields, \SORT_LOCALE_STRING);

return $available_fields;
}
}
2 changes: 1 addition & 1 deletion program/actions/contacts/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class rcmail_action_contacts_index extends rcmail_action
'size' => 40,
'maxlength' => 128,
'label' => 'instantmessenger',
'subtypes' => ['aim', 'icq', 'msn', 'yahoo', 'jabber', 'skype', 'other'],
'subtypes' => ['aim', 'icq', 'msn', 'yahoo', 'jabber', 'skype'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 'other' is removed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's not supported by the rest of the code. It's not included in the vCard, so once you hit save the value is lost.

'category' => 'main',
],
'notes' => [
Expand Down
Loading