Turba::formatName PHP Method

formatName() public static method

If the format is 'none', the full name with all parts is returned. If the format is 'last_first' or 'first_last', only the first name and last name are returned.
public static formatName ( Turba_Object $ob, string $name_format = null ) : string
$ob Turba_Object The object to get a name from.
$name_format string The formatting. One of 'none', 'last_first' or 'first_last'. Defaults to the user preference.
return string The formatted name, either "Firstname Lastname" or "Lastname, Firstname" depending on $name_format or the user's preference.
    public static function formatName(Turba_Object $ob, $name_format = null)
    {
        if (!$name_format) {
            if (!isset(self::$_cache['defaultFormat'])) {
                self::$_cache['defaultFormat'] = $GLOBALS['prefs']->getValue('name_format');
            }
            $name_format = self::$_cache['defaultFormat'];
        }
        /* If no formatting, return original name. */
        if (!in_array($name_format, array('first_last', 'last_first'))) {
            return $ob->getValue('name');
        }
        /* See if we have the name fields split out explicitly. */
        if ($ob->hasValue('firstname') && $ob->hasValue('lastname')) {
            return $name_format == 'last_first' ? $ob->getValue('lastname') . ', ' . $ob->getValue('firstname') : $ob->getValue('firstname') . ' ' . $ob->getValue('lastname');
        }
        /* One field, we'll have to guess. */
        $name = $ob->getValue('name');
        $lastname = self::guessLastname($name);
        if ($name_format == 'last_first' && !is_int(strpos($name, ',')) && Horde_String::length($name) > Horde_String::length($lastname)) {
            return $lastname . ', ' . preg_replace('/\\s+' . preg_quote($lastname, '/') . '/', '', $name);
        }
        if ($name_format == 'first_last' && is_int(strpos($name, ',')) && Horde_String::length($name) > Horde_String::length($lastname)) {
            return preg_replace('/' . preg_quote($lastname, '/') . ',\\s*/', '', $name) . ' ' . $lastname;
        }
        return $name;
    }

Usage Example

Example #1
0
 /**
  */
 protected function _initPages()
 {
     global $injector;
     $this->view->list = array();
     if ($GLOBALS['browse_source_count']) {
         foreach (Turba::getAddressBooks() as $key => $val) {
             if (!empty($val['browse'])) {
                 try {
                     $driver = $injector->getInstance('Turba_Factory_Driver')->create($key);
                 } catch (Turba_Exception $e) {
                     continue;
                 }
                 try {
                     $contacts = $driver->search(array(), null, 'AND', array('__key', 'name'));
                     $contacts->reset();
                 } catch (Turba_Exception $e) {
                     continue;
                 }
                 $url = new Horde_Core_Smartmobile_Url();
                 $url->add('source', $key);
                 $url->setAnchor('entry');
                 $tmp = array();
                 while ($contact = $contacts->next()) {
                     $name = Turba::formatName($contact);
                     $tmp[] = array('group' => $contact->isGroup(), 'name' => strlen($name) ? $name : '[' . _("No Name") . ']', 'url' => strval($url->add('key', $contact->getValue('__key'))));
                 }
                 $this->view->list[$val['title']] = $tmp;
             }
         }
     }
 }
All Usage Examples Of Turba::formatName