Skip to content

Commit

Permalink
Bug: 14046 Fix populating incorrect email field.
Browse files Browse the repository at this point in the history
See also Bug: 12955 as this reverts the fix for that bug and properly
fixes it here.
  • Loading branch information
mrubinsk committed Aug 14, 2015
1 parent 43fbb8e commit dd75365
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
13 changes: 0 additions & 13 deletions turba/lib/Driver.php
Expand Up @@ -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;
}

Expand Down
52 changes: 51 additions & 1 deletion turba/lib/Object.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -166,7 +175,7 @@ public function getValue($attribute)
*/
public function setValue($attribute, $value)
{
global $injector;
global $injector, $attributes;

$hooks = $injector->getInstance('Horde_Core_Hooks');

Expand All @@ -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;
}

Expand Down Expand Up @@ -548,6 +565,7 @@ public function vfsEditUrl($file)
*/
public function store()
{
$this->_ensureEmail();
return $this->setValue('__key', $this->driver->save($this));
}

Expand All @@ -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;
}
}
}

}
5 changes: 5 additions & 0 deletions turba/lib/Object/Group.php
Expand Up @@ -286,4 +286,9 @@ public static function createGroup(
return $out;
}

protected function _ensureEmail()
{
// Noop for groups
}

}

0 comments on commit dd75365

Please sign in to comment.