+
+
+ /**
+ * Utility function to return all values of a certain data column
+ * either as flat list or grouped by subtype
+ *
+ * @param string Col name
+ * @param array Record data array as used for saving
+ * @param boolean True to return one array with all values, False for hash array with values grouped by type
+ * @return array List of column values
+ */
+ function get_col_values($col, $data, $flat = false)
+ {
+ $out = array();
+ foreach ($data as $c => $values) {
+ if ($c === $col || strpos($c, $col.':') === 0) {
+ if ($flat) {
+ $out = array_merge($out, (array)$values);
+ }
+ else {
+ list($f, $type) = explode(':', $c);
+ $out[$type] = array_merge((array)$out[$type], (array)$values);
+ }
+ }
+ }
+
+ return $out;
+ }
+
+
+ /**
+ * Normalize the given string for fulltext search.
+ * Currently only optimized for Latin-1 characters; to be extended
+ *
+ * @param string Input string (UTF-8)
+ * @return string Normalized string
+ */
+ protected static function normalize_string($str)
+ {
+ // split by words
+ $arr = explode(" ", preg_replace(
+ array('/[\s;\+\-\/]+/i', '/(\d)[-.\s]+(\d)/', '/\s\w{1,3}\s/'),
+ array(' ', '\\1\\2', ' '),
+ $str));
+
+ foreach ($arr as $i => $part) {
+ if (utf8_encode(utf8_decode($part)) == $part) { // is latin-1 ?
+ $arr[$i] = utf8_encode(strtr(strtolower(strtr(utf8_decode($part),
+ 'ÇçäâàåéêëèïîìÅÉöôòüûùÿøØáíóúñÑÁÂÀãÃÊËÈÍÎÏÓÔõÕÚÛÙýÝ',
+ 'ccaaaaeeeeiiiaeooouuuyooaiounnaaaaaeeeiiioooouuuyy')),
+ array('ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u')));
+ }
+ else
+ $arr[$i] = mb_strtolower($part);
+ }
+
+ return join(" ", $arr);
+ }
+
+
+ /**
+ * Compose a valid display name from the given structured contact data
+ *
+ * @param array Hash array with contact data as key-value pairs
+ * @param bool The name will be used on the list
+ *
+ * @return string Display name
+ */
+ public static function compose_display_name($contact, $list_mode = false)
+ {
+ $contact = rcmail::get_instance()->plugins->exec_hook('contact_displayname', $contact);
+ $fn = $contact['name'];
+
+ if (!$fn)
+ $fn = join(' ', array_filter(array($contact['prefix'], $contact['firstname'], $contact['middlename'], $contact['surname'], $contact['suffix'])));
+
+ // use email address part for name
+ $email = is_array($contact['email']) ? $contact['email'][0] : $contact['email'];
+
+ if ($email && (empty($fn) || $fn == $email)) {
+ // Use full email address on contacts list
+ if ($list_mode)
+ return $email;
+
+ list($emailname) = explode('@', $email);
+ if (preg_match('/(.*)[\.\-\_](.*)/', $emailname, $match))
+ $fn = trim(ucfirst($match[1]).' '.ucfirst($match[2]));
+ else
+ $fn = ucfirst($emailname);
+ }
+
+ return $fn;
+ }
+