From f9a967763e2a4cc595b44f40ac1ca666b2a02af6 Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Sat, 11 Feb 2012 06:28:58 -0500
Subject: [PATCH] User configurable setting how to display contact names in list

---
 CHANGELOG                             |    1 
 program/steps/addressbook/save.inc    |    3 -
 program/steps/addressbook/func.inc    |    8 +---
 program/localization/de_CH/labels.inc |    1 
 config/main.inc.php.dist              |    7 +++
 program/steps/mail/list_contacts.inc  |    2 
 program/steps/settings/save_prefs.inc |    1 
 program/localization/de_DE/labels.inc |    1 
 program/include/rcube_addressbook.php |   48 +++++++++++++++++++++--
 program/steps/settings/func.inc       |   15 +++++++
 program/localization/en_US/labels.inc |    1 
 program/include/rcube_contacts.php    |    7 ---
 program/steps/mail/addcontact.inc     |    5 ++
 13 files changed, 78 insertions(+), 22 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 3b3a5e7..328d8da 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,7 @@
 - Handle identity details box with an iframe (#1487020)
 - Fix issue where some text from original message was missing on reply (#1488340)
 - Fix parse errors in DDL files for MS SQL Server
+- User configurable setting how to display contact names in list
 - Make contacts list sorting configurable for the admin/user
 - Revert SORT=DISPLAY support, removed by mistake (#1488327)
 - Fix autoselect_host() for login (#1488297)
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 73967fb..be82a5f 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -667,6 +667,13 @@
 // sort contacts by this col (preferably either one of name, firstname, surname)
 $rcmail_config['addressbook_sort_col'] = 'surname';
 
+// the way how contact names are displayed in the list
+// 0: display name
+// 1: (prefix) firstname middlename surname (suffix)
+// 2: (prefix) surname firstname middlename (suffix)
+// 3: (prefix) surname, firstname middlename (suffix)
+$rcmail_config['addressbook_name_listing'] = 0;
+
 // use this timezone to display date/time
 // valid timezone identifers are listed here: php.net/manual/en/timezones.php
 // 'auto' will use the browser's timezone settings
diff --git a/program/include/rcube_addressbook.php b/program/include/rcube_addressbook.php
index 308aee4..b56b58a 100644
--- a/program/include/rcube_addressbook.php
+++ b/program/include/rcube_addressbook.php
@@ -472,24 +472,24 @@
      * 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
+     * @param bool   Don't attempt to extract components from the email address
      *
      * @return string Display name
      */
-    public static function compose_display_name($contact, $list_mode = false)
+    public static function compose_display_name($contact, $full_email = false)
     {
         $contact = rcmail::get_instance()->plugins->exec_hook('contact_displayname', $contact);
         $fn = $contact['name'];
 
-        if (!$fn)
+        if (!$fn)  // default display name composition according to vcard standard
             $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 full email
+            if ($full_email)
                 return $email;
 
             list($emailname) = explode('@', $email);
@@ -502,5 +502,43 @@
         return $fn;
     }
 
+
+    /**
+     * Compose the name to display in the contacts list for the given contact record.
+     * This respects the settings parameter how to list conacts.
+     *
+     * @param array  Hash array with contact data as key-value pairs
+     * @return string List name
+     */
+    public static function compose_list_name($contact)
+    {
+        static $compose_mode;
+
+        if (!isset($compose_mode))  // cache this
+            $compose_mode = rcmail::get_instance()->config->get('addressbook_name_listing', 0);
+
+        if ($compose_mode == 3)
+            $fn = join(' ', array($contact['surname'] . ',', $contact['firstname'], $contact['middlename']));
+        else if ($compose_mode == 2)
+            $fn = join(' ', array($contact['surname'], $contact['firstname'], $contact['middlename']));
+        else if ($compose_mode == 1)
+            $fn = join(' ', array($contact['firstname'], $contact['middlename'], $contact['surname']));
+        else
+            $fn = !empty($contact['name']) ? $contact['name'] : join(' ', array($contact['prefix'], $contact['firstname'], $contact['middlename'], $contact['surname'], $contact['suffix']));
+
+        $fn = trim($fn, ', ');
+
+        // fallback to display name
+        if (empty($fn) && $contact['name'])
+            $fn = $contact['name'];
+
+        // fallback to email address
+        $email = is_array($contact['email']) ? $contact['email'][0] : $contact['email'];
+        if (empty($fn) && $email)
+            return $email;
+
+        return $fn;
+    }
+
 }
 
diff --git a/program/include/rcube_contacts.php b/program/include/rcube_contacts.php
index 1a4950d..11fbde0 100644
--- a/program/include/rcube_contacts.php
+++ b/program/include/rcube_contacts.php
@@ -254,13 +254,6 @@
                 $sql_arr['email'] = array_map('trim', $sql_arr['email']);
             }
 
-            // make sure we have a name to display
-            if (empty($sql_arr['name'])) {
-                if (empty($sql_arr['email']))
-                  $sql_arr['email'] = $this->get_col_values('email', $sql_arr, true);
-                $sql_arr['name'] = $sql_arr['email'][0];
-            }
-
             $this->result->add($sql_arr);
         }
 
diff --git a/program/localization/de_CH/labels.inc b/program/localization/de_CH/labels.inc
index a12a82f..cdd93da 100644
--- a/program/localization/de_CH/labels.inc
+++ b/program/localization/de_CH/labels.inc
@@ -385,6 +385,7 @@
 $labels['replysamefolder'] = 'Antworten im selben Ordner wie Original speichern';
 $labels['defaultaddressbook'] = 'Neue Kontakte speichern in';
 $labels['autocompletesingle'] = 'Keine alternativen E-Mail-Adressen in Autovervollständigung';
+$labels['listnamedisplay'] = 'Kontakte auflisten als';
 $labels['spellcheckbeforesend'] = 'Rechtscheibung vor dem Senden prüfen';
 $labels['spellcheckoptions'] = 'Rechtschreibprüfungsoptionen';
 $labels['spellcheckignoresyms'] = 'Wörter mit Symbolen überspringen';
diff --git a/program/localization/de_DE/labels.inc b/program/localization/de_DE/labels.inc
index db921a3..ba5b52c 100644
--- a/program/localization/de_DE/labels.inc
+++ b/program/localization/de_DE/labels.inc
@@ -385,6 +385,7 @@
 $labels['replysamefolder'] = 'Antworten im selben Ordner wie Original speichern';
 $labels['defaultaddressbook'] = 'Neue Kontakte zum ausgewählten Adressbuch hinzufügen';
 $labels['autocompletesingle'] = 'Alternative E-Mailadressen bei der Auto-Vervollständigung nicht berücksichtigen';
+$labels['listnamedisplay'] = 'Kontakte auflisten als';
 $labels['spellcheckbeforesend'] = 'Rechtschreibprüfung vor dem Absenden der Nachricht';
 $labels['spellcheckoptions'] = 'Rechtschreibprüfungsoptionen';
 $labels['spellcheckignoresyms'] = 'Wörter mit Symbolen überspringen';
diff --git a/program/localization/en_US/labels.inc b/program/localization/en_US/labels.inc
index 4e64f38..f42c1af 100644
--- a/program/localization/en_US/labels.inc
+++ b/program/localization/en_US/labels.inc
@@ -444,6 +444,7 @@
 $labels['replysamefolder'] = 'Place replies in the folder of the message being replied to';
 $labels['defaultaddressbook'] = 'Add new contacts to the selected addressbook';
 $labels['autocompletesingle'] = 'Skip alternative email addresses in autocompletion';
+$labels['listnamedisplay'] = 'List contacts as';
 $labels['spellcheckbeforesend'] = 'Check spelling before sending a message';
 $labels['spellcheckoptions'] = 'Spellcheck Options';
 $labels['spellcheckignoresyms'] = 'Ignore words with symbols';
diff --git a/program/steps/addressbook/func.inc b/program/steps/addressbook/func.inc
index f6ce79e..f261527 100644
--- a/program/steps/addressbook/func.inc
+++ b/program/steps/addressbook/func.inc
@@ -347,13 +347,9 @@
 
         // format each col
         foreach ($a_show_cols as $col) {
-            $val = $row[$col];
-            if ($val == '' && $col == 'name') {
-                $val = rcube_addressbook::compose_display_name($row, true);
-            }
-
+            $val = $col == 'name' ? rcube_addressbook::compose_list_name($row) : $row[$col];
             $a_row_cols[$col] = Q($val);
-	}
+        }
 
         $OUTPUT->command($prefix.'add_contact_row', $row['ID'], $a_row_cols);
     }
diff --git a/program/steps/addressbook/save.inc b/program/steps/addressbook/save.inc
index f584a0e..038b905 100644
--- a/program/steps/addressbook/save.inc
+++ b/program/steps/addressbook/save.inc
@@ -135,8 +135,7 @@
     $a_js_cols = array();
     $record = $CONTACTS->get_record($newcid ? $newcid : $cid, true);
     $record['email'] = reset($CONTACTS->get_col_values('email', $record, true));
-    if (empty($record['name']))
-      $record['name']  = rcube_addressbook::compose_display_name($record, true);
+    $record['name']  = rcube_addressbook::compose_list_name($record);
 
     foreach (array('name', 'email') as $col)
       $a_js_cols[] = Q((string)$record[$col]);
diff --git a/program/steps/mail/addcontact.inc b/program/steps/mail/addcontact.inc
index 8e11272..7476811 100644
--- a/program/steps/mail/addcontact.inc
+++ b/program/steps/mail/addcontact.inc
@@ -61,7 +61,10 @@
     }
 
     $contact['email'] = rcube_idn_to_utf8($contact['email']);
-    $contact['name'] = rcube_addressbook::compose_display_name($contact);
+    $contact = $RCMAIL->plugins->exec_hook('contact_displayname', $contact);
+
+    if (empty($contact['firstname']) || empty($contact['surname']))
+      $contact['name'] = rcube_addressbook::compose_display_name($contact);
 
     // validate contact record
     if (!$CONTACTS->validate($contact, true)) {
diff --git a/program/steps/mail/list_contacts.inc b/program/steps/mail/list_contacts.inc
index 23804b1..0791ae3 100644
--- a/program/steps/mail/list_contacts.inc
+++ b/program/steps/mail/list_contacts.inc
@@ -68,7 +68,7 @@
     else if (!empty($result) && $result->count > 0) {
         // create javascript list
         while ($row = $result->next()) {
-            $name = rcube_addressbook::compose_display_name($row, true);
+            $name = rcube_addressbook::compose_list_name($row);
 
             // add record for every email address of the contact
             foreach ($CONTACTS->get_col_values('email', $row, true) as $i => $email) {
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index 48230f5..35e015d 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -651,6 +651,21 @@
       );
     }
 
+    // show addressbook listing mode selection
+    if (!isset($no_override['addressbook_name_listing'])) {
+      $field_id = 'rcmfd_addressbook_name_listing';
+      $select_listing = new html_select(array('name' => '_addressbook_name_listing', 'id' => $field_id));
+      $select_listing->add(rcube_label('name'), 0);
+      $select_listing->add(rcube_label('firstname') . ' '  . rcube_label('surname'), 1);
+      $select_listing->add(rcube_label('surname')   . ' '  . rcube_label('firstname'), 2);
+      $select_listing->add(rcube_label('surname')   . ', ' . rcube_label('firstname'), 3);
+
+      $blocks['main']['options']['list_name_listing'] = array(
+        'title' => html::label($field_id, Q(rcube_label('listnamedisplay'))),
+        'content' => $select_listing->show($config['addressbook_name_listing']),
+      );
+    }
+
     // show addressbook sort column
     if (!isset($no_override['addressbook_sort_col'])) {
       $field_id = 'rcmfd_addressbook_sort_col';
diff --git a/program/steps/settings/save_prefs.inc b/program/steps/settings/save_prefs.inc
index 87d06ef..4af4a3f 100644
--- a/program/steps/settings/save_prefs.inc
+++ b/program/steps/settings/save_prefs.inc
@@ -98,6 +98,7 @@
       'default_addressbook'  => get_input_value('_default_addressbook', RCUBE_INPUT_POST, true),
       'autocomplete_single'  => isset($_POST['_autocomplete_single']) ? TRUE : FALSE,
       'addressbook_sort_col' => get_input_value('_addressbook_sort_col', RCUBE_INPUT_POST),
+      'addressbook_name_listing' => intval(get_input_value('_addressbook_name_listing', RCUBE_INPUT_POST)),
       'addressbook_pagesize' => is_numeric($_POST['_addressbook_pagesize']) ? max(2, intval($_POST['_addressbook_pagesize'])) : $CONFIG['addressbook_pagesize'],
     );
 

--
Gitblit v1.9.1