From 8a12538e24d89140d06e385b96692d1acc0673e6 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Fri, 12 Jul 2013 00:04:18 +0200 Subject: [PATCH] Add mergeFromVCard() method to Contact. This is to be used on import, if a contact with identical UID exists. --- lib/contact.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/contact.php b/lib/contact.php index 948b9a41..0a0ed4ed 100644 --- a/lib/contact.php +++ b/lib/contact.php @@ -581,13 +581,14 @@ class Contact extends VObject\VCard implements IPIMObject { /** * Merge in data from a multi-dimentional array * - * NOTE: This is *NOT* tested! + * NOTE: The data has actually already been merged client side! * The data array has this structure: * * array( * 'EMAIL' => array(array('value' => 'johndoe@example.com', 'parameters' = array('TYPE' => array('HOME','VOICE')))) * ); * @param array $data + * @return bool */ public function mergeFromArray(array $data) { foreach($data as $name => $properties) { @@ -617,6 +618,53 @@ class Contact extends VObject\VCard implements IPIMObject { return true; } + /** + * Merge in data from another VCard + * Used on import if a matching UID is found. Returns true if any updates + * take place, otherwise false. + * + * @param VCard $vcard + * @return bool + */ + public function mergeFromVCard(VCard $vcard) { + $updated = false; + foreach($vcard->children as $property) { + if(in_array($property->name, array('REV', 'UID'))) { + continue; + } + \OCP\Util::writeLog('contacts', __METHOD__.' merging: ' .$property->name, \OCP\Util::DEBUG); + if(in_array($property->name, Utils\Properties::$multi_properties)) { + $ownproperties = $this->select($property->name); + if(count($ownproperties) === 0) { + // We don't have any instances of this property, so just add it. + $this->add($property); + $updated = true; + continue; + } else { + foreach($ownproperties as $ownproperty) { + if(strtolower($property->value) === strtolower($ownproperty->value)) { + // We already have this property, so skip both loops + continue 2; + } + } + $this->add($property); + $updated = true; + } + } else { + if(!isset($this->{$name})) { + $this->add($property); + $updated = true; + } else { + $this->setPropertyByName($property->name, $property->value, $property->parameters); + } + } + } + if($updated) { + $this->setSaved(false); + } + return $updated; + } + // TODO: Cleanup these parameters public function cacheThumbnail(\OC_Image $image = null, $remove = false, $update = false) { $key = self::THUMBNAIL_PREFIX . $this->combinedKey();