From d72a41dc8e9bf523b45e04d4226f63fb7502c349 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Wed, 30 Oct 2013 04:35:03 -0400
Subject: [PATCH] Make sure all folders in the path are created - ie. if archive folder is configured as Archive/YYYY/MM, create both Archive/YYYY and Archive/YYYY/MM.

---
 plugins/archive/package.xml |    4 ++--
 plugins/archive/archive.php |   37 +++++++++++++++++++++++++------------
 2 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/plugins/archive/archive.php b/plugins/archive/archive.php
index 7a81606..4ef46e4 100644
--- a/plugins/archive/archive.php
+++ b/plugins/archive/archive.php
@@ -110,19 +110,20 @@
    */
   function move_messages()
   {
-    $rcmail = rcmail::get_instance();
     $this->add_texts('localization');
 
-    $storage = $rcmail->get_storage();
+    $rcmail         = rcmail::get_instance();
+    $storage        = $rcmail->get_storage();
+    $delimiter      = $storage->get_hierarchy_delimiter();
+    $archive_folder = $rcmail->config->get('archive_mbox');
+    $archive_type   = $rcmail->config->get('archive_type', '');
+
     $storage->set_folder(($current_mbox = rcube_utils::get_input_value('_mbox', RCUBE_INPUT_POST)));
 
-    $delimiter = $storage->get_hierarchy_delimiter();
-    $archive_folder = $rcmail->config->get('archive_mbox');
-    $archive_type = $rcmail->config->get('archive_type', '');
+    $result  = array('reload' => false, 'update' => false, 'errors' => array());
+    $uids    = explode(',', rcube_utils::get_input_value('_uid', RCUBE_INPUT_POST));
+    $folders = array();
 
-    $result = array('reload' => false, 'update' => false, 'errors' => array());
-
-    $uids = explode(',', rcube_utils::get_input_value('_uid', RCUBE_INPUT_POST));
     foreach ($uids as $uid) {
       if (!$archive_folder || !($message = $rcmail->storage->get_message($uid))) {
         continue;
@@ -164,12 +165,24 @@
       }
 
       // compose full folder path
-      $folder =  $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
+      $folder = $archive_folder . ($subfolder ? $delimiter . $subfolder : '');
 
       // create archive subfolder if it doesn't yet exist
-      if (!$storage->folder_exists($folder, false)) {
-        if ($storage->create_folder($folder, true))
-          $result['reload'] = true;
+      // we'll create all folders in the path
+      if (!in_array($folder, $folders)) {
+        $list = $storage->list_folders('', $archive_folder . '*', 'mail', null, true);
+        $path = explode($delimiter, $folder);
+
+        for ($i=0; $i<count($path); $i++) {
+          $_folder = implode($delimiter, array_slice($path, 0, $i+1));
+          if (!in_array($_folder, $list)) {
+            if ($storage->create_folder($_folder, true)) {
+              $result['reload'] = true;
+            }
+          }
+        }
+
+        $folders[] = $folder;
       }
 
       // move message to target folder
diff --git a/plugins/archive/package.xml b/plugins/archive/package.xml
index 62a009a..9b37579 100644
--- a/plugins/archive/package.xml
+++ b/plugins/archive/package.xml
@@ -13,9 +13,9 @@
 		<email>roundcube@gmail.com</email>
 		<active>yes</active>
 	</lead>
-	<date>2013-01-20</date>
+	<date>2013-10-30</date>
 	<version>
-		<release>2.0</release>
+		<release>2.1</release>
 		<api>2.0</api>
 	</version>
 	<stability>

--
Gitblit v1.9.1