/**
* Returns a field value.
*
* @param string $address Contact email address.
* @param string $field Field to get.
* @param array $sources Sources to check.
* @param boolean $strict Match the email address strictly.
* @param boolean $multiple Return more than one entry if found and true,
* return an error if this is false.
*
* @return array An array of field value(s).
* @throws Turba_Exception
*/
public function getField($address = '', $field = '', $sources = array(), $strict = false, $multiple = false)
{
global $cfgSources, $attributes;
if (empty($address)) {
throw new Turba_Exception(_("Invalid email"));
}
if (!isset($cfgSources) || !is_array($cfgSources)) {
return array();
}
if (!count($sources)) {
$sources = array(Turba::getDefaultAddressbook());
}
$driver = $GLOBALS['injector']->getInstance('Turba_Factory_Driver');
$result = array();
foreach ($sources as $source) {
if (!isset($cfgSources[$source])) {
continue;
}
$criterium = array();
$sdriver = $driver->create($source);
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, 'OR', array(), $strict ? array_keys($criterium) : array());
} catch (Turba_Exception $e) {
Horde::log($e, 'ERR');
}
if ($list instanceof Turba_List) {
while ($ob = $list->next()) {
if ($ob->hasValue($field)) {
$result[] = $ob->getValue($field);
}
}
}
}
if (count($result) > 1) {
if ($multiple) {
return $result;
} else {
throw new Turba_Exception(_("More than 1 entry found"));
}
} elseif (empty($result)) {
throw new Turba_Exception(sprintf(_("No %s entry found for %s"), $field, $address));
}
return reset($result);
}