Thomas Bruederli
2013-09-08 0272081d05e8cb60f1a2ac032698332b639cc527
Add option to import vcards with group assignments (pull-request 105)
3 files modified
78 ■■■■■ changed files
program/lib/Roundcube/rcube_csv2vcard.php 2 ●●●●● patch | view | raw | blame | history
program/localization/en_US/labels.inc 5 ●●●● patch | view | raw | blame | history
program/steps/addressbook/import.inc 71 ●●●●● patch | view | raw | blame | history
program/lib/Roundcube/rcube_csv2vcard.php
@@ -145,6 +145,7 @@
        'work_mobile'           => 'phone:work,cell',
        'work_title'            => 'jobtitle',
        'work_zip'              => 'zipcode:work',
        'group'                 => 'groups',
    );
    /**
@@ -268,6 +269,7 @@
        'work_mobile'       => "Work Mobile",
        'work_title'        => "Work Title",
        'work_zip'          => "Work Zip",
        'groups'            => "Group",
    );
    protected $local_label_map = array();
program/localization/en_US/labels.inc
@@ -367,8 +367,11 @@
$labels['import'] = 'Import';
$labels['importcontacts'] = 'Import contacts';
$labels['importfromfile'] = 'Import from file:';
$labels['importtarget'] = 'Add new contacts to address book:';
$labels['importtarget'] = 'Add contacts to';
$labels['importreplace'] = 'Replace the entire address book';
$labels['importgroups'] = 'Import group assignments';
$labels['importgroupsall'] = 'All (create groups if necessary)';
$labels['importgroupsexisting'] = 'Only for existing groups';
$labels['importdesc'] = 'You can upload contacts from an existing address book.<br/>We currently support importing addresses from the <a href="http://en.wikipedia.org/wiki/VCard">vCard</a> or CSV (comma-separated) data format.';
$labels['done'] = 'Done';
program/steps/addressbook/import.inc
@@ -40,6 +40,7 @@
    'multiple' => 'multiple',
  ));
  $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . $upload->show());
  $table = new html_table(array('cols' => 2));
  // addressbook selector
  if (count($writable_books) > 1) {
@@ -48,17 +49,31 @@
    foreach ($writable_books as $book)
        $select->add($book['name'], $book['id']);
    $form .= html::p(null, html::label('rcmimporttarget', rcube_label('importtarget'))
        . $select->show($target));
    $table->add('title', html::label('rcmimporttarget', rcube_label('importtarget')));
    $table->add(null, $select->show($target));
  }
  else {
    $abook = new html_hiddenfield(array('name' => '_target', 'value' => key($writable_books)));
    $form .= $abook->show();
  }
  // selector for group import options
  if (count($writable_books) > 1 || $writable_books[0]->groups) {
    $select = new html_select(array('name' => '_groups', 'id' => 'rcmimportgroups', 'is_escaped' => true));
    $select->add(rcube_label('none'), '0');
    $select->add(rcube_label('importgroupsall'), '1');
    $select->add(rcube_label('importgroupsexisting'), '2');
    $table->add('title', html::label('rcmimportgroups', rcube_label('importgroups')));
    $table->add(null, $select->show(get_input_value('_groups', RCUBE_INPUT_GPC)));
  }
  // checkbox to replace the entire address book
  $check_replace = new html_checkbox(array('name' => '_replace', 'value' => 1, 'id' => 'rcmimportreplace'));
  $form .= html::p(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)) .
    html::label('rcmimportreplace', rcube_label('importreplace')));
  $table->add('title', html::label('rcmimportreplace', rcube_label('importreplace')));
  $table->add(null, $check_replace->show(get_input_value('_replace', RCUBE_INPUT_GPC)));
  $form .= $table->show(array('id' => null) + $attrib);
  $OUTPUT->set_env('writable_source', !empty($writable_books));
  $OUTPUT->add_label('selectimportfile','importwait');
@@ -134,18 +149,49 @@
}
/**
 * Returns the matching group id. If group doesn't exist, it'll be created if allowed.
 */
function rcmail_import_group_id($group_name, $CONTACTS, $create, &$import_groups)
{
    $group_id = 0;
    foreach ($import_groups as $key => $group) {
        if (strtolower($group['name']) == strtolower($group_name)) {
            $group_id = $group['ID'];
            break;
        }
    }
    // create a new group
    if (!$group_id && $create) {
        $new_group = $CONTACTS->create_group($group_name);
        if (!$new_group['ID'])
            $new_group['ID'] = $new_group['id'];
        $import_groups[] = $new_group;
        $group_id = $new_group['ID'];
    }
    return $group_id;
}
/** The import process **/
$importstep = 'rcmail_import_form';
if (is_array($_FILES['_file'])) {
    $replace  = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
    $target   = get_input_value('_target', RCUBE_INPUT_GPC);
    $replace      = (bool)get_input_value('_replace', RCUBE_INPUT_GPC);
    $target       = get_input_value('_target', RCUBE_INPUT_GPC);
    $with_groups  = intval(get_input_value('_groups', RCUBE_INPUT_GPC));
    $vcards       = array();
    $upload_error = null;
    $CONTACTS = $RCMAIL->get_address_book($target, true);
    if (!$CONTACTS->groups) {
        $with_groups = false;
    }
    if ($CONTACTS->readonly) {
        $OUTPUT->show_message('addresswriterror', 'error');
@@ -206,6 +252,10 @@
            $CONTACTS->delete_all();
        }
        if ($with_groups) {
            $import_groups = $CONTACTS->list_groups();
        }
        foreach ($vcards as $vcard) {
            $a_record = $vcard->get_assoc();
@@ -258,6 +308,15 @@
                $success = $plugin['result'];
            if ($success) {
                // assign groups for this contact (if enabled)
                if ($with_groups && !empty($a_record['groups'])) {
                    foreach (explode(',', $a_record['groups'][0]) as $group_name) {
                        if ($group_id = rcmail_import_group_id($group_name, $CONTACTS, $with_groups == 1, $import_groups)) {
                            $CONTACTS->add_to_group($group_id, $success);
                        }
                    }
                }
                $IMPORT_STATS->inserted++;
                $IMPORT_STATS->names[] = $a_record['name'] ? $a_record['name'] : $email;
            }