[turba] _turba_hook_decode_name

Liam Hoekenga liamr at deathstar.org
Tue Dec 4 21:02:37 UTC 2007


A long time ago, I'd created a new turba hook to reduce the number of 
names returned in the CN field of an LDAP search...

    http://marc.info/?l=turba&m=110210948417973&w=2
    http://bugs.horde.org/ticket/?id=936

at the time, it was suggested that I didn't need a new hook, but could 
do it with _turba_hook_decode_name.  I didn't do anything about it at 
the time, but with the impending arrival of Turba 2.2.x, I thought I'd 
give it a shot.

I believe I see where it's getting called in lib/Object.php (  ~ ln 110 
for 2.2RC1 ):

        if (isset($this->attributes[$attribute]) &&
            !empty($hooks[$attribute])) {
            return call_user_func_array($hooks[$attribute],
                                        
array($this->attributes[$attribute], &$this));
        }

So... here's my hook.  It seems to work as intended (returning the 
shortest name in a comma separated list of names).  Does this look right? 

    if (!function_exists('_turba_hook_decode_name')) {
        function _turba_hook_decode_name($name, &$contact)
        {
            if( !strpos( $name, ',' )) {
                return $name;
            }

            $names = explode( "," , $name );

            $names = array_filter($names,
                create_function('$var',
                        'return (!preg_match("/[0-9]$/", $var));'));

            usort($names,
                create_function('$a, $b',
                '{
                    if ($a == $b) return 0;
                    return (strlen($a) < strlen($b)) ? -1 : 1;
                }'));

            $names = array_values($names);

            return $names[0];
        }
    }


Liam


More information about the turba mailing list