From 43875379d5fcfc4e4d29e2edcbbed614a2050f7d Mon Sep 17 00:00:00 2001
From: thomascube <thomas@roundcube.net>
Date: Thu, 02 Feb 2012 17:18:10 -0500
Subject: [PATCH] Allow users to choose cols for contacts list sorting

---
 program/steps/addressbook/list.inc    |    5 +-
 CHANGELOG                             |    1 
 program/include/rcube_mdb2.php        |    2 +
 program/include/rcube_addressbook.php |   19 +++++++++
 program/include/rcube_ldap.php        |   26 +++---------
 program/include/rcmail.php            |    4 ++
 program/steps/settings/func.inc       |   28 ++++++++++---
 config/main.inc.php.dist              |    3 +
 program/steps/settings/save_prefs.inc |    1 
 program/include/rcube_contacts.php    |   15 ++++++-
 10 files changed, 73 insertions(+), 31 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 56755cd..0806154 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Make contacts list sorting configurable for the admin/user
 - Revert SORT=DISPLAY support, removed by mistake (#1488327)
 - Fix autoselect_host() for login (#1488297)
 - Fix drafts update issues when edited from preview pane (#1488314)
diff --git a/config/main.inc.php.dist b/config/main.inc.php.dist
index 7dd5025..73967fb 100644
--- a/config/main.inc.php.dist
+++ b/config/main.inc.php.dist
@@ -664,6 +664,9 @@
 // show up to X items in contacts list view
 $rcmail_config['addressbook_pagesize'] = 50;
 
+// sort contacts by this col (preferably either one of name, firstname, surname)
+$rcmail_config['addressbook_sort_col'] = 'surname';
+
 // 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/rcmail.php b/program/include/rcmail.php
index 5965099..1c34759 100644
--- a/program/include/rcmail.php
+++ b/program/include/rcmail.php
@@ -452,6 +452,10 @@
         true, true);
     }
 
+    // set configured sort order
+    if ($sort_col = $this->config->get('addressbook_sort_col'))
+        $contacts->set_sort_order($sort_col);
+
     // add to the 'books' array for shutdown function
     $this->address_books[$id] = $contacts;
 
diff --git a/program/include/rcube_addressbook.php b/program/include/rcube_addressbook.php
index f7a5268..308aee4 100644
--- a/program/include/rcube_addressbook.php
+++ b/program/include/rcube_addressbook.php
@@ -5,7 +5,7 @@
  | program/include/rcube_addressbook.php                                 |
  |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
- | Copyright (C) 2006-2011, The Roundcube Dev Team                       |
+ | Copyright (C) 2006-2012, The Roundcube Dev Team                       |
  |                                                                       |
  | Licensed under the GNU General Public License version 3 or            |
  | any later version with exceptions for skins & plugins.                |
@@ -47,6 +47,8 @@
     public $group_id = null;
     public $list_page = 1;
     public $page_size = 10;
+    public $sort_col = 'name';
+    public $sort_order = 'ASC';
     public $coltypes = array('name' => array('limit'=>1), 'firstname' => array('limit'=>1), 'surname' => array('limit'=>1), 'email' => array('limit'=>1));
 
     protected $error;
@@ -183,6 +185,21 @@
         $this->page_size = (int)$size;
     }
 
+    /**
+     * Set internal sort settings
+     *
+     * @param string $sort_col Sort column
+     * @param string $sort_order Sort order
+     */
+    function set_sort_order($sort_col, $sort_order = null)
+    {
+        if ($sort_col != null && ($this->coltypes[$sort_col] || in_array($sort_col, $this->coltypes))) {
+            $this->sort_col = $sort_col;
+        }
+        if ($sort_order != null) {
+            $this->sort_order = strtoupper($sort_order) == 'DESC' ? 'DESC' : 'ASC';
+        }
+    }
 
     /**
      * Check the given data before saving.
diff --git a/program/include/rcube_contacts.php b/program/include/rcube_contacts.php
index 1cfebb5..1a4950d 100644
--- a/program/include/rcube_contacts.php
+++ b/program/include/rcube_contacts.php
@@ -5,7 +5,7 @@
  | program/include/rcube_contacts.php                                    |
  |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
- | Copyright (C) 2006-2011, The Roundcube Dev Team                       |
+ | Copyright (C) 2006-2012, The Roundcube Dev Team                       |
  |                                                                       |
  | Licensed under the GNU General Public License version 3 or            |
  | any later version with exceptions for skins & plugins.                |
@@ -217,6 +217,16 @@
             $join = " LEFT JOIN ".get_table_name($this->db_groupmembers)." AS m".
                 " ON (m.contact_id = c.".$this->primary_key.")";
 
+        $order_col = (in_array($this->sort_col, $this->table_cols) ? $this->sort_col : 'name');
+        $order_cols = array('c.'.$order_col);
+        if ($order_col == 'firstname')
+            $order_cols[] = 'c.surname';
+        else if ($order_col == 'surname')
+            $order_cols[] = 'c.firstname';
+        if ($order_col != 'name')
+            $order_cols[] = 'c.name';
+        $order_cols[] = 'c.email';
+
         $sql_result = $this->db->limitquery(
             "SELECT * FROM ".get_table_name($this->db_name)." AS c" .
             $join .
@@ -224,7 +234,8 @@
                 " AND c.user_id=?" .
                 ($this->group_id ? " AND m.contactgroup_id=?" : "").
                 ($this->filter ? " AND (".$this->filter.")" : "") .
-            " ORDER BY ". $this->db->concat('c.name', 'c.email'),
+            " ORDER BY ". $this->db->concat($order_cols) .
+            " " . $this->sort_order,
             $start_row,
             $length,
             $this->user_id,
diff --git a/program/include/rcube_ldap.php b/program/include/rcube_ldap.php
index 0d7b7d5..c3893d8 100644
--- a/program/include/rcube_ldap.php
+++ b/program/include/rcube_ldap.php
@@ -4,7 +4,7 @@
  | program/include/rcube_ldap.php                                        |
  |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
- | Copyright (C) 2006-2011, The Roundcube Dev Team                       |
+ | Copyright (C) 2006-2012, The Roundcube Dev Team                       |
  | Copyright (C) 2011, Kolab Systems AG                                  |
  |                                                                       |
  | Licensed under the GNU General Public License version 3 or            |
@@ -38,8 +38,6 @@
     public $readonly = true;
     public $ready = false;
     public $group_id = 0;
-    public $list_page = 1;
-    public $page_size = 10;
     public $coltypes = array();
 
     /** private properties */
@@ -50,7 +48,6 @@
     protected $filter = '';
     protected $result = null;
     protected $ldap_result = null;
-    protected $sort_col = '';
     protected $mail_domain = '';
     protected $debug = false;
 
@@ -417,24 +414,15 @@
 
 
     /**
-     * Set internal list page
+     * Set internal sort settings
      *
-     * @param number $page Page number to list
+     * @param string $sort_col Sort column
+     * @param string $sort_order Sort order
      */
-    function set_page($page)
+    function set_sort_order($sort_col, $sort_order = null)
     {
-        $this->list_page = (int)$page;
-    }
-
-
-    /**
-     * Set internal page size
-     *
-     * @param number $size Number of messages to display on one page
-     */
-    function set_pagesize($size)
-    {
-        $this->page_size = (int)$size;
+        if ($this->fieldmap[$sort_col])
+            $this->sort_col = $this->fieldmap[$sort_col];
     }
 
 
diff --git a/program/include/rcube_mdb2.php b/program/include/rcube_mdb2.php
index 5fd620f..c103f9a 100644
--- a/program/include/rcube_mdb2.php
+++ b/program/include/rcube_mdb2.php
@@ -636,6 +636,8 @@
     {
         $func = '';
         $args = func_get_args();
+        if (is_array($args[0]))
+            $args = $args[0];
 
         switch($this->db_provider) {
             case 'mysql':
diff --git a/program/steps/addressbook/list.inc b/program/steps/addressbook/list.inc
index 4665185..de7149b 100644
--- a/program/steps/addressbook/list.inc
+++ b/program/steps/addressbook/list.inc
@@ -5,7 +5,7 @@
  | program/steps/addressbook/list.inc                                    |
  |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
- | Copyright (C) 2005-2007, The Roundcube Dev Team                       |
+ | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
  |                                                                       |
  | Licensed under the GNU General Public License version 3 or            |
  | any later version with exceptions for skins & plugins.                |
@@ -34,6 +34,7 @@
         $page = isset($_SESSION['page']) ? $_SESSION['page'] : 1;
 
     $_SESSION['page'] = $page;
+    $sort_col = $this->config->get('addressbook_sort_col', 'name');
 
     // Get records from all sources
     foreach ($search as $s => $set) {
@@ -49,7 +50,7 @@
 
         while ($row = $result->next()) {
             $row['sourceid'] = $s;
-            $key = $row['name'] . ':' . $row['sourceid'];
+            $key = $row[$sort_col] . ':' . $row['sourceid'];
             $records[$key] = $row;
         }
         unset($result);
diff --git a/program/steps/settings/func.inc b/program/steps/settings/func.inc
index 18a4660..48230f5 100644
--- a/program/steps/settings/func.inc
+++ b/program/steps/settings/func.inc
@@ -5,7 +5,7 @@
  | program/steps/settings/func.inc                                       |
  |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
- | Copyright (C) 2005-2007, The Roundcube Dev Team                       |
+ | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
  |                                                                       |
  | Licensed under the GNU General Public License version 3 or            |
  | any later version with exceptions for skins & plugins.                |
@@ -651,13 +651,17 @@
       );
     }
 
-    if (!isset($no_override['autocomplete_single'])) {
-      $field_id = 'rcmfd_autocomplete_single';
-      $checkbox = new html_checkbox(array('name' => '_autocomplete_single', 'id' => $field_id, 'value' => 1));
+    // show addressbook sort column
+    if (!isset($no_override['addressbook_sort_col'])) {
+      $field_id = 'rcmfd_addressbook_sort_col';
+      $select_sort = new html_select(array('name' => '_addressbook_sort_col', 'id' => $field_id));
+      $select_sort->add(rcube_label('name'), 'name');
+      $select_sort->add(rcube_label('firstname'), 'firstname');
+      $select_sort->add(rcube_label('surname'), 'surname');
 
-      $blocks['main']['options']['autocomplete_single'] = array(
-        'title' => html::label($field_id, Q(rcube_label('autocompletesingle'))),
-        'content' => $checkbox->show($config['autocomplete_single']?1:0),
+      $blocks['main']['options']['sort_col'] = array(
+        'title' => html::label($field_id, Q(rcube_label('listsorting'))),
+        'content' => $select_sort->show($config['addressbook_sort_col']),
       );
     }
 
@@ -674,6 +678,16 @@
       );
     }
 
+    if (!isset($no_override['autocomplete_single'])) {
+      $field_id = 'rcmfd_autocomplete_single';
+      $checkbox = new html_checkbox(array('name' => '_autocomplete_single', 'id' => $field_id, 'value' => 1));
+
+      $blocks['main']['options']['autocomplete_single'] = array(
+        'title' => html::label($field_id, Q(rcube_label('autocompletesingle'))),
+        'content' => $checkbox->show($config['autocomplete_single']?1:0),
+      );
+    }
+
     break;
 
     // Special IMAP folders
diff --git a/program/steps/settings/save_prefs.inc b/program/steps/settings/save_prefs.inc
index 88a0548..87d06ef 100644
--- a/program/steps/settings/save_prefs.inc
+++ b/program/steps/settings/save_prefs.inc
@@ -97,6 +97,7 @@
     $a_user_prefs = array(
       '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_pagesize' => is_numeric($_POST['_addressbook_pagesize']) ? max(2, intval($_POST['_addressbook_pagesize'])) : $CONFIG['addressbook_pagesize'],
     );
 

--
Gitblit v1.9.1