Skip to content

Commit

Permalink
Allow getAvailableEmailFields to perform this check.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Jul 21, 2016
1 parent 13037e3 commit 7ae0a5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
19 changes: 7 additions & 12 deletions lib/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ public function addField($address = '', $name = '', $field = '',
public function getField($address = '', $field = '', $sources = array(),
$strict = false, $multiple = false)
{
global $cfgSources, $attributes;
global $cfgSources, $attributes, $injector;

if (empty($address)) {
throw new Turba_Exception(_("Invalid email"));
Expand All @@ -1761,24 +1761,19 @@ public function getField($address = '', $field = '', $sources = array(),
$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;
}
}

$criterium = array_fill_keys(
Turba::getAvailableEmailFields($source),
$address
);
$driver = $injector->getInstance('Turba_Factory_Driver')->create($source);
try {
$list = $sdriver->search($criterium, null, 'OR', array(), $strict ? array_keys($criterium) : array());
$list = $driver->search($criterium, null, 'OR', array(), $strict ? array_keys($criterium) : array());
} catch (Turba_Exception $e) {
Horde::log($e, 'ERR');
continue;
Expand Down
23 changes: 19 additions & 4 deletions lib/Turba.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,19 +722,34 @@ static public function addBrowseJs()
}

/**
* Return an array of all available attributes of type 'email'.
* Return an array of all available attributes of type 'email'. Optionally,
* ensure the field is defined in the specified $source.
*
* @param $source string An optional source identifier.
* @param $searchable boolean If true, and $source is provided, ensure that
* the email field is a configured searchable
* field.
*
* @return array An array of email fields.
* @since 4.2.9
*/
public static function getAvailableEmailFields()
public static function getAvailableEmailFields($source = null, $searchable = true)
{
global $attributes;
global $attributes, $injector, $cfgSources;

if (!empty($source)) {
$driver = $injector->getInstance('Turba_Factory_Driver')
->create($source);
}

$emailFields = array();
foreach ($attributes as $field => $data) {
if ($data['type'] == 'email') {
$emailFields[] = $field;
if (empty($source) || (!empty($source) &&
in_array($field, array_keys($driver->map)) &&
(!$searchable || ($searchable && in_array($field, $cfgSources[$source]['search'])))))

$emailFields[] = $field;
}
}

Expand Down

0 comments on commit 7ae0a5e

Please sign in to comment.