Use display name sorting, if possible, for to/from sorts.
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 22 Oct 2009 20:16:43 +0000 (14:16 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Thu, 22 Oct 2009 20:16:43 +0000 (14:16 -0600)
imp/docs/CHANGES
imp/docs/RFCS
imp/lib/Mailbox.php
imp/lib/Search.php

index 9b16363..004b95b 100644 (file)
@@ -2,6 +2,7 @@
 v5.0-git
 --------
 
+[mms] Sort by display name for to/from fields if supported on IMAP server.
 [mms] Add ability to quickly filter by flags in DIMP.
 [mms] Add ability to select sent-mail mailbox when composing in DIMP.
 [mms] Add ability to save drafts in MIMP.
index 82967da..06a4763 100644 (file)
@@ -44,6 +44,8 @@ RFC 5267        ESORT
 RFC 5464        METADATA
 RFC 5550        Lemonade Profile (specifically [2.8] - $Forwarded flag)
 
+draft-ietf-morg-sortdisplay-02    SORT=DISPLAY
+
 
 POP3
 ====
index 995a777..43017d1 100644 (file)
@@ -340,7 +340,7 @@ class IMP_Mailbox
                     $query->flag('\\deleted', false);
                 }
                 try {
-                    $res = $GLOBALS['imp_imap']->ob()->search($this->_mailbox, $query, array('sort' => array($sortpref['by']), 'reverse' => (bool)$sortpref['dir']));
+                    $res = $GLOBALS['imp_search']->imapSearch($this->_mailbox, $query, array('sort' => array($sortpref['by']), 'reverse' => (bool)$sortpref['dir']));
                     $this->_sorted = $res['sort'];
                 } catch (Horde_Imap_Client_Exception $e) {
                     $this->_sorted = array();
index 6e325cf..1fb9dfb 100644 (file)
@@ -275,7 +275,7 @@ class IMP_Search
         }
 
         foreach ($search['f'] as $val) {
-            $results = $GLOBALS['imp_imap']->ob()->search($val, $query, array('reverse' => $sortpref['dir'], 'sort' => array($sortpref['by'])));
+            $results = $this->imapSearch($val, $query, array('reverse' => $sortpref['dir'], 'sort' => array($sortpref['by'])));
             foreach ($results['sort'] as $val2) {
                 $sorted[] = $val2 . IMP::IDX_SEP . $val;
             }
@@ -301,7 +301,7 @@ class IMP_Search
                                    $sortdir = null)
     {
         try {
-            $results = $GLOBALS['imp_imap']->ob()->search($mailbox, $query, array('reverse' => $sortdir, 'sort' => array($sortby)));
+            $results = $this->imapSearch($mailbox, $query, array('reverse' => $sortdir, 'sort' => array($sortby)));
             return $results['sort'];
         } catch (Horde_Imap_Client_Exception $e) {
             return array();
@@ -309,6 +309,47 @@ class IMP_Search
     }
 
     /**
+     * Performs the IMAP search query on the server. Use this function,
+     * instead of directly calling Horde_Imap_Client's search() function,
+     * because certain configuration parameters may need to be dynamically
+     * altered.
+     *
+     * @param string $mailbox  The mailbox to search.
+     * @param object $query    The search query object
+     *                         (Horde_Imap_Client_Search_Query).
+     * @param array $opts      Additional search options.
+     *
+     * @return array  Search results.
+     */
+    public function imapSearch($mailbox, $query, $opts = array())
+    {
+        /* If doing a from/to search, use display sorting if possible.
+         * Although there is a fallback to a PHP-based display sort, for
+         * performance reasons only do a display sort if it is supported
+         * on the server. */
+        if (($_SESSION['imp']['protocol'] == 'imap') &&
+            isset($opts['sort']) &&
+            (in_array(Horde_Imap_Client::SORT_FROM, $opts['sort']) ||
+             in_array(Horde_Imap_Client::SORT_TO, $opts['sort']))) {
+            $sort_cap = $GLOBALS['imp_imap']->ob()->queryCapability('SORT');
+            print_r($sort_cap);
+            if (is_array($sort_cap) && in_array('DISPLAY', $sort_cap)) {
+                $pos = array_search(Horde_Imap_Client::SORT_FROM, $opts['sort']);
+                if ($pos !== false) {
+                    $opts['sort'][$pos] = Horde_Imap_Client::SORT_DISPLAYFROM;
+                }
+
+                $pos = array_search(Horde_Imap_Client::SORT_TO, $opts['sort']);
+                if ($pos !== false) {
+                    $opts['sort'][$pos] = Horde_Imap_Client::SORT_DISPLAYTO;
+                }
+            }
+        }
+
+        return $GLOBALS['imp_imap']->ob()->search($mailbox, $query, $opts);
+    }
+
+    /**
      * Creates the IMAP search query in the IMP session.
      *
      * @param object $query    The search query object