Skip to content

Commit

Permalink
Allow Turba_Api::getField to search any attribute of type 'email'.
Browse files Browse the repository at this point in the history
Any email attribute present in the source's map and 'search' settings
will be considered. Fixes FB urls not being found for email addresses
not contained in 'email' field.
  • Loading branch information
mrubinsk committed Sep 8, 2015
1 parent 1bf98da commit cd3595f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 7 additions & 9 deletions turba/lib/Api.php
Expand Up @@ -1787,7 +1787,7 @@ public function addField($address = '', $name = '', $field = '',
public function getField($address = '', $field = '', $sources = array(),
$strict = false, $multiple = false)
{
global $cfgSources;
global $cfgSources, $attributes;

if (empty($address)) {
throw new Turba_Exception(_("Invalid email"));
Expand All @@ -1808,19 +1808,17 @@ public function getField($address = '', $field = '', $sources = array(),
if (!isset($cfgSources[$source])) {
continue;
}

$criterium = array();
$sdriver = $driver->create($source);
$criterium = array('email' => $address);
if (!isset($sdriver->map['email'])) {
if (isset($sdriver->map['emails'])) {
$criterium = array('emails' => $address);
} else {
continue;
foreach (Turba::getAvailableEmailFields() as $cfgField) {
if (in_array($cfgField, array_keys($sdriver->map)) &&
in_array($cfgField, $cfgSources[$source]['search'])) {
$criterium[$cfgField] = $address;
}
}

try {
$list = $sdriver->search($criterium, null, 'AND', array(), $strict ? array('email') : array());
$list = $sdriver->search($criterium, null, 'OR', array(), $strict ? array_keys($criterium) : array());
} catch (Turba_Exception $e) {
Horde::log($e, 'ERR');
}
Expand Down
20 changes: 20 additions & 0 deletions turba/lib/Turba.php
Expand Up @@ -739,4 +739,24 @@ public static function addBrowseJs()
'TurbaBrowse.submit' => _("Are you sure that you want to delete the selected contacts?")
));
}

/**
* Return an array of all available attributes of type 'email'.
*
* @return array An array of email fields.
*/
public static function getAvailableEmailFields()
{
global $attributes;

$emailFields = array();
foreach ($attributes as $field => $data) {
if ($data['type'] == 'email') {
$emailFields[] = $field;
}
}

return $emailFields;
}

}

0 comments on commit cd3595f

Please sign in to comment.