From 4c162153d6904e54bf51e2fdb949523f73c4c3bf Mon Sep 17 00:00:00 2001 From: nathangray Date: Tue, 6 Nov 2018 15:15:36 -0700 Subject: [PATCH] * Addressbook - add option to import that deletes contacts from addressbook that are not in the import list --- ...ss.addressbook_import_contacts_csv.inc.php | 54 +++++++++++++++++++ .../importexport_wizard_chooseowner.xet | 3 ++ 2 files changed, 57 insertions(+) diff --git a/addressbook/inc/class.addressbook_import_contacts_csv.inc.php b/addressbook/inc/class.addressbook_import_contacts_csv.inc.php index 047991f26c..88d4d595de 100644 --- a/addressbook/inc/class.addressbook_import_contacts_csv.inc.php +++ b/addressbook/inc/class.addressbook_import_contacts_csv.inc.php @@ -42,6 +42,29 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv { */ private $type_warned = false; + /** + * To empty addressbook before importing, we actually keep track of + * what's imported and delete the others to keep history. + * + * @var type + */ + private $ids = array(); + + /** + * imports entries according to given definition object. + * @param resource $_stream + * @param string $_charset + * @param definition $_definition + */ + public function import( $_stream, importexport_definition $_definition ) { + parent::import($_stream, $_definition); + + if($_definition->plugin_options['empty_addressbook']) + { + $this->empty_addressbook($this->user, $this->ids); + } + } + /** * imports entries according to given definition object. * @param resource $_stream @@ -302,6 +325,8 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv { unset($_data['owner']); } + $this->ids[] = $_data['id']; + // Merge to deal with fields not in import record $_data = array_merge($old, $_data); $changed = $this->tracking->changed_fields($_data, $old); @@ -334,6 +359,7 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv { if(!$result) { $this->errors[$record_num] = $this->bocontacts->error; } else { + $this->ids[] = $result; $this->results[$_action]++; // This does nothing (yet?) but update the identifier $record->save($result); @@ -346,6 +372,34 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv { } } + + /** + * Delete all contacts from the addressbook, except the given list + * + * @param int $addressbook Addressbook to clear + * @param array $ids Contacts to keep + */ + protected function empty_addressbook($addressbook, $ids) + { + // Get all IDs in addressbook + $contacts = $this->bocontacts->search(array('owner' => $addressbook), true); + $contacts = array_column($contacts, 'id'); + + $delete = array_diff($contacts, $ids); + + foreach($delete as $id) + { + if($this->dry_run || $this->bocontacts->delete($id)) + { + $this->results['deleted']++; + } + else + { + $this->warnings[] = lang('Unable to delete') . ': ' . Api\Link::title('addressbook', $id); + } + } + } + /** * returns translated name of plugin * diff --git a/addressbook/templates/default/importexport_wizard_chooseowner.xet b/addressbook/templates/default/importexport_wizard_chooseowner.xet index 9c4d0c6e5a..ddedc44079 100644 --- a/addressbook/templates/default/importexport_wizard_chooseowner.xet +++ b/addressbook/templates/default/importexport_wizard_chooseowner.xet @@ -27,6 +27,9 @@ + + +