* Addressbook - add option to import that deletes contacts from addressbook that are not in the import list

This commit is contained in:
nathangray 2018-11-06 15:15:36 -07:00
parent ec1c6360f0
commit 4c162153d6
2 changed files with 57 additions and 0 deletions

View File

@ -42,6 +42,29 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv {
*/ */
private $type_warned = false; 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. * imports entries according to given definition object.
* @param resource $_stream * @param resource $_stream
@ -302,6 +325,8 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv {
unset($_data['owner']); unset($_data['owner']);
} }
$this->ids[] = $_data['id'];
// Merge to deal with fields not in import record // Merge to deal with fields not in import record
$_data = array_merge($old, $_data); $_data = array_merge($old, $_data);
$changed = $this->tracking->changed_fields($_data, $old); $changed = $this->tracking->changed_fields($_data, $old);
@ -334,6 +359,7 @@ class addressbook_import_contacts_csv extends importexport_basic_import_csv {
if(!$result) { if(!$result) {
$this->errors[$record_num] = $this->bocontacts->error; $this->errors[$record_num] = $this->bocontacts->error;
} else { } else {
$this->ids[] = $result;
$this->results[$_action]++; $this->results[$_action]++;
// This does nothing (yet?) but update the identifier // This does nothing (yet?) but update the identifier
$record->save($result); $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 * returns translated name of plugin
* *

View File

@ -27,6 +27,9 @@
<menupopup type="select-bool" label="Change addressbook when updating" id="change_owner"/> <menupopup type="select-bool" label="Change addressbook when updating" id="change_owner"/>
</menulist> </menulist>
</row> </row>
<row>
<checkbox label="Empty addressbook before importing" id="empty_addressbook"/>
</row>
</rows> </rows>
</grid> </grid>
</template> </template>