diff options
Diffstat (limited to 'program/steps/addressbook')
-rw-r--r-- | program/steps/addressbook/export.inc | 106 | ||||
-rw-r--r-- | program/steps/addressbook/import.inc | 71 | ||||
-rw-r--r-- | program/steps/addressbook/save.inc | 25 |
3 files changed, 136 insertions, 66 deletions
diff --git a/program/steps/addressbook/export.inc b/program/steps/addressbook/export.inc index 761f26b75..1e988feab 100644 --- a/program/steps/addressbook/export.inc +++ b/program/steps/addressbook/export.inc @@ -5,7 +5,7 @@ | program/steps/addressbook/export.inc | | | | This file is part of the Roundcube Webmail client | - | Copyright (C) 2008-2011, The Roundcube Dev Team | + | Copyright (C) 2008-2013, The Roundcube Dev Team | | Copyright (C) 2011, Kolab Systems AG | | | | Licensed under the GNU General Public License version 3 or | @@ -21,6 +21,46 @@ +-----------------------------------------------------------------------+ */ + +/** + * Copy contact record properties into a vcard object + */ +function prepare_for_export(&$record, $source = null) +{ + $groups = $source && $source->groups && $source->export_groups ? $source->get_record_groups($record['ID']) : null; + + if (empty($record['vcard'])) { + $vcard = new rcube_vcard(); + if ($source) { + $vcard->extend_fieldmap($source->vcard_map); + } + $vcard->load($record['vcard']); + $vcard->reset(); + + foreach ($record as $key => $values) { + list($field, $section) = explode(':', $key); + foreach ((array)$values as $value) { + if (is_array($value) || @strlen($value)) { + $vcard->set($field, $value, strtoupper($section)); + } + } + } + + // append group names + if ($groups) { + $vcard->set('groups', join(',', $groups), null); + } + + $record['vcard'] = $vcard->export(true); + } + // patch categories to alread existing vcard block + else if ($record['vcard'] && !empty($groups) && !strpos($record['vcard'], 'CATEGORIES:')) { + $vgroups = 'CATEGORIES:' . rcube_vcard::vcard_quote(join(',', $groups)); + $record['vcard'] = str_replace('END:VCARD', $vgroups . rcube_vcard::$eol . 'END:VCARD', $record['vcard']); + } +} + + // Use search result if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']])) { @@ -42,23 +82,7 @@ if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search while ($record = $result->next()) { // because vcard_map is per-source we need to create vcard here - if (empty($record['vcard']) || empty($record['name'])) { - $vcard = new rcube_vcard(); - $vcard->extend_fieldmap($source->vcard_map); - $vcard->load($record['vcard']); - $vcard->reset(); - - foreach ($record as $key => $values) { - list($field, $section) = explode(':', $key); - foreach ((array)$values as $value) { - if (is_array($value) || @strlen($value)) { - $vcard->set($field, $value, strtoupper($section)); - } - } - } - - $record['vcard'] = $vcard->export(true); - } + prepare_for_export($record, $source); $record['sourceid'] = $s; $key = rcube_addressbook::compose_contact_key($record, $sort_col); @@ -90,23 +114,7 @@ else if (!empty($_REQUEST['_cid'])) { while ($record = $result->next()) { // because vcard_map is per-source we need to create vcard here - if (empty($record['vcard']) || empty($record['name'])) { - $vcard = new rcube_vcard(); - $vcard->extend_fieldmap($source->vcard_map); - $vcard->load($record['vcard']); - $vcard->reset(); - - foreach ($record as $key => $values) { - list($field, $section) = explode(':', $key); - foreach ((array)$values as $value) { - if (is_array($value) || @strlen($value)) { - $vcard->set($field, $value, strtoupper($section)); - } - } - } - - $record['vcard'] = $vcard->export(true); - } + prepare_for_export($record, $source); $record['sourceid'] = $s; $key = rcube_addressbook::compose_contact_key($record, $sort_col); @@ -136,30 +144,12 @@ header('Content-Type: text/x-vcard; charset='.RCMAIL_CHARSET); header('Content-Disposition: attachment; filename="contacts.vcf"'); while ($result && ($row = $result->next())) { - // we already have a vcard record - if ($row['vcard'] && $row['name']) { - // fix folding and end-of-line chars - $row['vcard'] = preg_replace('/\r|\n\s+/', '', $row['vcard']); - $row['vcard'] = preg_replace('/\n/', rcube_vcard::$eol, $row['vcard']); - echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol; - } - // copy values into vcard object - else { - $vcard = new rcube_vcard(); - $vcard->extend_fieldmap($CONTACTS->vcard_map); - $vcard->load($row['vcard']); - $vcard->reset(); + prepare_for_export($row, $CONTACTS); - foreach ($row as $key => $values) { - list($field, $section) = explode(':', $key); - foreach ((array)$values as $value) { - if (is_array($value) || @strlen($value)) - $vcard->set($field, $value, strtoupper($section)); - } - } - - echo $vcard->export(true) . rcube_vcard::$eol; - } + // fix folding and end-of-line chars + $row['vcard'] = preg_replace('/\r|\n\s+/', '', $row['vcard']); + $row['vcard'] = preg_replace('/\n/', rcube_vcard::$eol, $row['vcard']); + echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol; } exit; diff --git a/program/steps/addressbook/import.inc b/program/steps/addressbook/import.inc index 915aac884..60f5d7b61 100644 --- a/program/steps/addressbook/import.inc +++ b/program/steps/addressbook/import.inc @@ -40,6 +40,7 @@ function rcmail_import_form($attrib) '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 @@ function rcmail_import_form($attrib) 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,19 +149,50 @@ function rcmail_import_buttons($attrib) } +/** + * 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 @@ if (is_array($_FILES['_file'])) { $CONTACTS->delete_all(); } + if ($with_groups) { + $import_groups = $CONTACTS->list_groups(); + } + foreach ($vcards as $vcard) { $a_record = $vcard->get_assoc(); @@ -258,6 +308,15 @@ if (is_array($_FILES['_file'])) { $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; } diff --git a/program/steps/addressbook/save.inc b/program/steps/addressbook/save.inc index e7e5efc63..2adc53bcf 100644 --- a/program/steps/addressbook/save.inc +++ b/program/steps/addressbook/save.inc @@ -59,15 +59,34 @@ foreach ($GLOBALS['CONTACT_COLTYPES'] as $col => $colprop) { } // assign values and subtypes else if (is_array($_POST[$fname])) { - $values = get_input_value($fname, RCUBE_INPUT_POST, true); + $values = get_input_value($fname, RCUBE_INPUT_POST, true); $subtypes = get_input_value('_subtype_' . $col, RCUBE_INPUT_POST); + foreach ($values as $i => $val) { + if ($col == 'email') { + // extract email from full address specification, e.g. "Name" <addr@domain.tld> + $addr = rcube_mime::decode_address_list($val, 1, false); + if (!empty($addr) && ($addr = array_pop($addr)) && $addr['mailto']) { + $val = $addr['mailto']; + } + } + $subtype = $subtypes[$i] ? ':'.$subtypes[$i] : ''; $a_record[$col.$subtype][] = $val; } } else if (isset($_POST[$fname])) { $a_record[$col] = get_input_value($fname, RCUBE_INPUT_POST, true); + + // normalize the submitted date strings + if ($colprop['type'] == 'date') { + if ($timestamp = rcube_utils::strtotime($a_record[$col])) { + $a_record[$col] = date('Y-m-d', $timestamp); + } + else { + unset($a_record[$col]); + } + } } } @@ -75,8 +94,10 @@ foreach ($GLOBALS['CONTACT_COLTYPES'] as $col => $colprop) { if (empty($a_record['name'])) { $a_record['name'] = rcube_addressbook::compose_display_name($a_record, true); // Reset it if equals to email address (from compose_display_name()) - if ($a_record['name'] == $a_record['email'][0]) + $email = rcube_addressbook::get_col_values('email', $a_record, true); + if ($a_record['name'] == $email[0]) { $a_record['name'] = ''; + } } // do input checks (delegated to $CONTACTS instance) |