Some cleanup of search code
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 28 Jul 2009 06:47:18 +0000 (00:47 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 28 Jul 2009 06:47:18 +0000 (00:47 -0600)
imp/lib/Auth.php
imp/lib/Folder.php
imp/lib/IMP.php
imp/lib/Imap/Tree.php
imp/lib/Search.php
imp/lib/api.php
imp/lib/prefs.php
imp/search.php

index 0801e80..e6c391b 100644 (file)
@@ -475,7 +475,7 @@ class IMP_Auth
                   $conf['cookie']['domain']);
 
         /* Set up search information for the session. */
-        $GLOBALS['imp_search']->sessionSetup();
+        $GLOBALS['imp_search']->initialize();
 
         /* If the user wants to run filters on login, make sure they get
            run. */
index b453bcd..cfd168a 100644 (file)
@@ -217,7 +217,7 @@ class IMP_Folder
         $this->clearFlistCache();
 
         /* Recreate Virtual Folders. */
-        $GLOBALS['imp_search']->sessionSetup(true);
+        $GLOBALS['imp_search']->initialize(true);
 
         /* Clear the folder from the sort prefs. */
         foreach ($deleted as $val) {
@@ -287,7 +287,7 @@ class IMP_Folder
         $imaptree->insert($folder);
 
         /* Recreate Virtual Folders. */
-        $GLOBALS['imp_search']->sessionSetup(true);
+        $GLOBALS['imp_search']->initialize(true);
 
         return true;
     }
index 44c4216..cf0ddc6 100644 (file)
@@ -231,7 +231,7 @@ class IMP
 
         /* Add the list of virtual folders to the list. */
         if (!empty($options['inc_vfolder'])) {
-            $vfolders = $GLOBALS['imp_search']->listQueries(true);
+            $vfolders = $GLOBALS['imp_search']->listQueries(IMP_Search::LIST_VFOLDER);
             if (!empty($vfolders)) {
                 $vfolder_sel = $GLOBALS['imp_search']->searchMboxID();
                 $text .= '<option value="" disabled="disabled">- - - - - - - - -</option>' . "\n";
index 5bdcd5c..e1697c0 100644 (file)
@@ -486,7 +486,7 @@ class IMP_Imap_Tree
         $this->insert($this->_getList($this->_showunsub));
 
         /* Add virtual folders to the tree. */
-        $this->insertVFolders($GLOBALS['imp_search']->listQueries(true));
+        $this->insertVFolders($GLOBALS['imp_search']->listQueries(IMP_Search::LIST_VFOLDER));
     }
 
     /**
index e58b906..7b2f6f6 100644 (file)
@@ -51,6 +51,10 @@ class IMP_Search
     /* The mailbox search prefix. */
     const MBOX_PREFIX = 'impsearch\0';
 
+    /* Bitmask constants for listQueries(). */
+    const LIST_SEARCH = 1;
+    const LIST_VFOLDER = 2;
+
     /**
      * The ID of the current search query in use.
      *
@@ -77,7 +81,7 @@ class IMP_Search
      *
      * @param array $params  Available parameters:
      * <pre>
-     * 'id'  --  The ID of the search query in use.
+     * 'id' - (string) The ID of the search query in use.
      * </pre>
      */
     public function __construct($params = array())
@@ -88,11 +92,11 @@ class IMP_Search
     }
 
     /**
-     * Set up IMP_Search variables for the current session.
+     * Initialize the class.
      *
      * @param boolean $no_vf  Don't readd the Virtual Folders.
      */
-    public function sessionSetup($no_vf = false)
+    public function initialize($no_vf = false)
     {
         if (!$no_vf) {
             $imaptree = IMP_Imap_Tree::singleton();
@@ -283,9 +287,7 @@ class IMP_Search
 
         $vfolder = $GLOBALS['prefs']->getValue('vfolder');
         if (!empty($vfolder)) {
-            $old_error = error_reporting(0);
-            $vfolder = unserialize($vfolder);
-            error_reporting($old_error);
+            $vfolder = @unserialize($vfolder);
         }
 
         if (empty($vfolder) || !is_array($vfolder)) {
@@ -305,6 +307,7 @@ class IMP_Search
     protected function _saveVFolderList($vfolder)
     {
         $GLOBALS['prefs']->setValue('vfolder', serialize($vfolder));
+        unset(self::$_vfolder);
     }
 
     /**
@@ -354,13 +357,12 @@ class IMP_Search
         /* Create Virtual Trash with new folder list. */
         $imp_folder = IMP_Folder::singleton();
         $fl = $imp_folder->flist();
-        $flist = array();
+        $flist = array('INBOX');
         foreach ($fl as $mbox) {
             if (!empty($mbox['val'])) {
                 $flist[] = $mbox['val'];
             }
         }
-        array_unshift($flist, 'INBOX');
 
         $query = new Horde_Imap_Client_Search_Query();
         $query->flag('\\deleted', true);
@@ -397,9 +399,6 @@ class IMP_Search
      */
     public function createVINBOXFolder()
     {
-        /* Initialize IMP_Imap_Tree. */
-        $imaptree = IMP_Imap_Tree::singleton();
-
         /* Delete the current Virtual Inbox folder, if it exists. */
         $vinbox_id = $GLOBALS['prefs']->getValue('vinbox_id');
         if (!empty($vinbox_id)) {
@@ -412,6 +411,7 @@ class IMP_Search
 
         /* Create Virtual INBOX with nav_poll list. Filter out any nav_poll
          * entries that don't exist. Sort the list also. */
+        $imaptree = IMP_Imap_Tree::singleton();
         $flist = $imaptree->getPollList(true, true);
 
         $query = new Horde_Imap_Client_Search_Query();
@@ -461,29 +461,44 @@ class IMP_Search
     }
 
     /**
-     * Return a list of IDs and query labels, sorted by the label.
+     * Return a list of queryies.
      *
-     * @param boolean $vfolder  If true, only return Virtual Folders?
+     * @param integer $mask   A bitmask of the query types to return.
+     *                        IMP_Search::LIST_SEARCH and/or
+     *                        IMP_Search::LIST_VFOLDER.
+     * @param boolean $label  If true, returns the label. Otherwise, returns
+     *                        a textual representation.
      *
      * @return array  An array with the folder IDs as the key and the labels
      *                as the value.
      */
-    public function listQueries($vfolder = false)
+    public function listQueries($mask = null, $label = true)
     {
-        $vfolders = array();
+        $folders = array();
 
         if (empty($_SESSION['imp']['search'])) {
-            return $vfolders;
+            return $folders;
+        }
+
+        if (is_null($mask)) {
+            $mask = self::LIST_SEARCH | self::LIST_VFOLDER;
         }
 
         foreach ($_SESSION['imp']['search'] as $key => $val) {
-            if (!$vfolder || !empty($val['vfolder'])) {
-                $vfolders[$key] = $this->getLabel($key);
+            if ((($mask & self::LIST_VFOLDER) && !empty($val['vfolder'])) ||
+                (($mask & self::LIST_SEARCH) && empty($val['vfolder']))) {
+                $vfolders[$key] = $label
+                    ? $this->getLabel($key)
+                    : $this->searchQueryText($key);
             }
         }
-        natcasesort($vfolders);
 
-        return $vfolders;
+        if ($label) {
+            natcasesort($folders);
+            return $folders;
+        }
+
+        return array_reverse($folders, true);
     }
 
     /**
@@ -503,31 +518,6 @@ class IMP_Search
     }
 
     /**
-     * Return a list of search queries valid only for the current session
-     * (i.e. no virtual folders).
-     *
-     * @return array  Keys are the search ids, values are a textual
-     *                description of the search.
-     */
-    public function getSearchQueries()
-    {
-        $retarray = array();
-
-        if (empty($_SESSION['imp']['search'])) {
-            return $retarray;
-        }
-
-        foreach ($_SESSION['imp']['search'] as $key => $val) {
-            if (!$this->isVFolder($key) &&
-                ($text = $this->searchQueryText($key))) {
-                $retarray[$key] = $text;
-            }
-        }
-
-        return array_reverse($retarray, true);
-    }
-
-    /**
      * Return search query text representation for a given search ID.
      *
      * @param string $id  The search query id to use (by default, will use
index ba3fcdf..3d8b7f0 100644 (file)
@@ -546,7 +546,7 @@ function _imp_changeLanguage()
     $imp_folder->clearFlistCache();
     $imaptree = IMP_Imap_Tree::singleton();
     $imaptree->init();
-    $GLOBALS['imp_search']->sessionSetup(true);
+    $GLOBALS['imp_search']->initialize(true);
 }
 
 /**
index 0a17348..6b0eef3 100644 (file)
@@ -215,7 +215,7 @@ function prefs_callback()
 
     if ($prefs->isDirty('use_vtrash') || $prefs->isDirty('use_vinbox')) {
         $imp_search = new IMP_Search();
-        $imp_search->sessionSetup(true);
+        $imp_search->initialize(true);
     }
 
     if ($prefs->isDirty('subscribe') || $prefs->isDirty('tree_view')) {
index fb0b8a8..48040b3 100644 (file)
@@ -172,7 +172,7 @@ $t->set('label_or', Horde::label('search_match_or', _("Match Any Query")));
 $t->set('match_and', ($search['match'] == null) || ($search['match'] == 'and'));
 $t->set('label_and', Horde::label('search_match_and', _("Match All Queries")));
 
-$saved_searches = $imp_search->getSearchQueries();
+$saved_searches = $imp_search->listQueries(IMP_Search::LIST_SEARCH, false);
 if (!empty($saved_searches)) {
     $ss = array();
     foreach ($saved_searches as $key => $val) {