From 0272081d05e8cb60f1a2ac032698332b639cc527 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Sun, 08 Sep 2013 10:47:00 -0400
Subject: [PATCH] Add option to import vcards with group assignments (pull-request 105)

---
 program/steps/addressbook/import.inc      |   71 ++++++++++++++++++++++++++++++++---
 program/localization/en_US/labels.inc     |    5 ++
 program/lib/Roundcube/rcube_csv2vcard.php |    2 +
 3 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/program/lib/Roundcube/rcube_csv2vcard.php b/program/lib/Roundcube/rcube_csv2vcard.php
index fb8d8f1..00e6d4e 100644
--- a/program/lib/Roundcube/rcube_csv2vcard.php
+++ b/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();
diff --git a/program/localization/en_US/labels.inc b/program/localization/en_US/labels.inc
index 1865bcb..840c935 100644
--- a/program/localization/en_US/labels.inc
+++ b/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';
 
diff --git a/program/steps/addressbook/import.inc b/program/steps/addressbook/import.inc
index 915aac8..1c03571 100644
--- a/program/steps/addressbook/import.inc
+++ b/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;
             }

--
Gitblit v1.9.1