Turba::getAvailableEmailFields PHP Method

getAvailableEmailFields() public static method

Return an array of all available attributes of type 'email'. Optionally, ensure the field is defined in the specified $source.
Since: 4.2.9
public static getAvailableEmailFields ( $source = null, $searchable = true ) : array
$source string An optional source identifier.
$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.
    public static function getAvailableEmailFields($source = null, $searchable = true)
    {
        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') {
                if (empty($source) || !empty($source) && in_array($field, array_keys($driver->map)) && (!$searchable || $searchable && in_array($field, $cfgSources[$source]['search']))) {
                    $emailFields[] = $field;
                }
            }
        }
        return $emailFields;
    }

Usage Example

Example #1
0
 /**
  * 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);
 }