Skip to content

Commit

Permalink
Ensure we have a 'name' attribute for new contacts created on EAS cli…
Browse files Browse the repository at this point in the history
…ent.
  • Loading branch information
mrubinsk committed Oct 22, 2017
1 parent fb1c8d6 commit d7c6a62
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions lib/Driver.php
Expand Up @@ -2546,26 +2546,7 @@ public function toHash(Horde_Icalendar_Vcard $vcard)
}

/* Ensure we have a valid name field. */
if (empty($hash['name'])) {
/* If name is a composite field, it won't be present in the
* $this->fields array, so check for that as well. */
if (isset($this->map['name']) &&
is_array($this->map['name']) &&
!empty($this->map['name']['attribute'])) {
$fieldarray = array();
foreach ($this->map['name']['fields'] as $mapfields) {
$fieldarray[] = isset($hash[$mapfields]) ?
$hash[$mapfields] : '';
}
$hash['name'] = Turba::formatCompositeField($this->map['name']['format'], $fieldarray);
} else {
$hash['name'] = isset($hash['firstname']) ? $hash['firstname'] : '';
if (!empty($hash['lastname'])) {
$hash['name'] .= ' ' . $hash['lastname'];
}
$hash['name'] = trim($hash['name']);
}
}
$hash = $this->_parseName($hash);

return $hash;
}
Expand Down Expand Up @@ -2786,6 +2767,9 @@ public function fromASContact(Horde_ActiveSync_Message_Contact $message)
}
}

// Try our best to get a name attribute;
$hash = $this->_parseName($hash);

/* Requires special handling */

try {
Expand Down Expand Up @@ -2897,6 +2881,41 @@ public function fromASContact(Horde_ActiveSync_Message_Contact $message)
return $hash;
}

/**
* Checks $hash for the presence of a 'name' attribute. If not found,
* attempt to build one from other available values.
*
* @param array $hash A hash of turba attributes.
*
* @return array Hash of Turba attributes, with the 'name' attribute
* populated.
*/
protected function _parseName(array $hash)
{
if (empty($hash['name'])) {
/* If name is a composite field, it won't be present in the
* $this->fields array, so check for that as well. */
if (isset($this->map['name']) &&
is_array($this->map['name']) &&
!empty($this->map['name']['attribute'])) {
$fieldarray = array();
foreach ($this->map['name']['fields'] as $mapfields) {
$fieldarray[] = isset($hash[$mapfields]) ?
$hash[$mapfields] : '';
}
$hash['name'] = Turba::formatCompositeField($this->map['name']['format'], $fieldarray);
} else {
$hash['name'] = isset($hash['firstname']) ? $hash['firstname'] : '';
if (!empty($hash['lastname'])) {
$hash['name'] .= ' ' . $hash['lastname'];
}
$hash['name'] = trim($hash['name']);
}
}

return $hash;
}

/**
* Checks if the current user has the requested permissions on this
* address book.
Expand Down

0 comments on commit d7c6a62

Please sign in to comment.