Add IMP_Folder::getAllSubfolders().
authorMichael M Slusarz <slusarz@curecanti.org>
Thu, 18 Nov 2010 23:37:17 +0000 (16:37 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Fri, 19 Nov 2010 20:10:48 +0000 (13:10 -0700)
imp/lib/Folder.php

index 5d8c5de..4270a93 100644 (file)
@@ -211,11 +211,7 @@ class IMP_Folder
         $deleted = array($old);
         $inserted = array($new);
 
-        $imaptree = $GLOBALS['injector']->getInstance('IMP_Imap_Tree');
-        $imaptree->setIteratorFilter(IMP_Imap_Tree::FLIST_NOCONTAINER | IMP_Imap_Tree::FLIST_UNSUB | IMP_Imap_Tree::FLIST_NOBASE, $old);
-
-        /* Get list of any folders that are underneath this one. */
-        $all_folders = array_merge(array($old), array_keys(iterator_to_array($imaptree)));
+        $all_folders = $this->getAllSubfolders($old);
 
         try {
             $GLOBALS['injector']->getInstance('IMP_Injector_Factory_Imap')->create()->renameMailbox($old, $new);
@@ -426,4 +422,25 @@ class IMP_Folder
         return $msgcount ? $msgcount : false;
     }
 
+    /**
+     * Get list of all folders under a given mailbox.
+     *
+     * @param string $mbox           The base mailbox.
+     * @param boolean $include_base  Include the base mailbox in results?
+     *
+     * @return array  All mailboxes under the base mailbox.
+     */
+    public function getAllSubfolders($mbox, $include_base = true)
+    {
+        $imaptree = $GLOBALS['injector']->getInstance('IMP_Imap_Tree');
+        $imaptree->setIteratorFilter(IMP_Imap_Tree::FLIST_NOCONTAINER | IMP_Imap_Tree::FLIST_UNSUB | IMP_Imap_Tree::FLIST_NOBASE, $mbox);
+
+        $out = array_keys(iterator_to_array($imaptree));
+        if ($include_base && $this->exists($mbox)) {
+            $out = array_merge(array($mbox), $out);
+        }
+
+        return $out;
+    }
+
 }