diff --git a/turba/lib/Driver.php b/turba/lib/Driver.php index 8b6d12989cb..06fd7a96ab4 100644 --- a/turba/lib/Driver.php +++ b/turba/lib/Driver.php @@ -2535,19 +2535,6 @@ public function toHash(Horde_Icalendar_Vcard $vcard) } } - // Ensure we have an 'email' field since we don't know for sure what - // the source is, therefore we don't know the mappings available. Fixes - // importing vCards that have all EMAIL properties with a TYPE - // attribute. - // See Bug: 12955 - if (!isset($hash['email'])) { - if (!empty($hash['homeEmail'])) { - $hash['email'] = Horde_Icalendar_Vcard::getBareEmail($hash['homeEmail']); - } else if (!empty($hash['workEmail'])) { - $hash['email'] = Horde_Icalendar_Vcard::getBareEmail($hash['workEmail']); - } - } - return $hash; } diff --git a/turba/lib/Object.php b/turba/lib/Object.php index 40a19bc68b0..dd966af20c3 100644 --- a/turba/lib/Object.php +++ b/turba/lib/Object.php @@ -58,6 +58,15 @@ class Turba_Object */ protected $_vfs; + /** + * Local cache of available email addresses. Needed to ensure we + * populate the email field correctly. See See Bug: 12955 and Bug: 14046. + * A hash with turba attribute names as key. + * + * @var array + */ + protected $_emailFields = array(); + /** * Constructs a new Turba_Object object. * @@ -166,7 +175,7 @@ public function getValue($attribute) */ public function setValue($attribute, $value) { - global $injector; + global $injector, $attributes; $hooks = $injector->getInstance('Horde_Core_Hooks'); @@ -188,6 +197,14 @@ public function setValue($attribute, $value) if (isset($this->driver->map[$attribute]) && is_array($this->driver->map[$attribute]) && !isset($this->driver->map[$attribute]['attribute'])) { + + // If we don't know the attribute, and it's an email field, save it + // in case we need to populate an email field on save. + if (isset($attributes[$attribute]) && + $attributes[$attribute]['type'] == 'email') { + $this->_emailFields[$attribute] = $value; + } + return; } @@ -548,6 +565,7 @@ public function vfsEditUrl($file) */ public function store() { + $this->_ensureEmail(); return $this->setValue('__key', $this->driver->save($this)); } @@ -570,4 +588,36 @@ public function vfsInit() return $this->_vfs; } + /** + * Ensure we have an email address set, if available. Needed to cover the + * case where a contact might have been imported via vCard with email TYPEs + * that do not match the configured attributes for this source. E.g., the + * vCard contains a TYPE=HOME but we only have the generic 'email' field + * available. + * + * @return [type] [description] + */ + protected function _ensureEmail() + { + global $attributes; + + foreach ($this->attributes as $attribute => $value) { + if ($attributes[$attribute]['type'] = 'email') { + // We have an email defined, no need to check. + return; + } + } + + // No email defined yet, see if we have any available: + foreach ($this->_emailFields as $attribute => $email) { + if (!empty($this->driver->map[$attribute])) { + $this->attributes[$attribute] = $email; + break; + } elseif (!empty($this->driver->map['email'])) { + $this->attribute['email'] = $email; + break; + } + } + } + } diff --git a/turba/lib/Object/Group.php b/turba/lib/Object/Group.php index 5062a6b0889..70e109579cb 100644 --- a/turba/lib/Object/Group.php +++ b/turba/lib/Object/Group.php @@ -286,4 +286,9 @@ public static function createGroup( return $out; } + protected function _ensureEmail() + { + // Noop for groups + } + }