Changed subfolder summing to only show total for subfolders for a
[squirrelmail.git] / src / left_main.php
index 072763d422d1b41163eb8859a527f73031c7dc78..16f8bd4fef4c70262b41ce83d208cae5f38172de 100644 (file)
@@ -42,22 +42,8 @@ function formatMailboxName($imapConnection, $box_array) {
         $mailbox = $regs[2];
     }
 
-    $unseen = 0;
-
-    if (($unseen_notify == 2 && $real_box == 'INBOX') ||
-        $unseen_notify == 3) {
-        $unseen = sqimap_unseen_messages($imapConnection, $real_box);
-        if ($unseen_type == 1 && $unseen > 0) {
-            $unseen_string = "($unseen)";
-            $unseen_found = TRUE;
-        } else if ($unseen_type == 2) {
-            $numMessages = sqimap_get_num_messages($imapConnection, $real_box);
-            $unseen_string = "<font color=\"$color[11]\">($unseen/$numMessages)</font>";
-            $unseen_found = TRUE;
-        }
-    }
-
-    $special_color = ($use_special_folder_color && isSpecialMailbox( $real_box ) );
+    list($unseen_string, $unseen) = create_unseen_string($real_box, $box_array, $imapConnection);
+    $special_color = ($use_special_folder_color && isSpecialMailbox($real_box));
 
     /* Start off with a blank line. */
     $line = '';
@@ -85,7 +71,7 @@ function formatMailboxName($imapConnection, $box_array) {
     if ($unseen > 0) { $line .= "</B>"; }
 
     /* Print unseen information. */
-    if (isset($unseen_found) && $unseen_found) {
+    if ($unseen_string != '') {
         $line .= "&nbsp;<SMALL>$unseen_string</SMALL>";
     }
 
@@ -162,9 +148,9 @@ function compute_folder_children(&$parbox, $boxcount) {
  * currently appropriate.
  */
 function create_collapse_link($boxnum) {
-    global $boxes;
+    global $boxes, $imapConnection, $unseen_notify, $color;
     $mailbox = urlencode($boxes[$boxnum]['unformatted']);
-
+        
     /* Create the link for this collapse link. */
     $link = '<a target="left" style="text-decoration:none" ' .
             'href="left_main.php?';
@@ -179,6 +165,84 @@ function create_collapse_link($boxnum) {
     return ($link);
 }
 
+/**
+ * create_unseen_string:
+ *
+ * Create unseen and total message count for both this folder and
+ * it's subfolders.
+ *
+ * @param string $boxName name of the current mailbox
+ * @param array $boxArray array for the current mailbox
+ * @param $imapConnection current imap connection in use
+ * @return array[0] unseen message string (for display)
+ * @return array[1] unseen message count
+ */
+function create_unseen_string($boxName, $boxArray, $imapConnection) {
+    global $boxes, $unseen_type, $color;
+
+    /* Initialize the return value. */
+    $result = array();
+
+    /* Initialize the counts for this folder. */
+    $boxUnseenCount = 0;
+    $boxMessageCount = 0;
+    $totalUnseenCount = 0;
+    $totalMessageCount = 0;
+
+    /* Collect the counts for this box alone. */
+    $boxUnseenCount = sqimap_unseen_messages($imapConnection, $boxName);
+    if ($unseen_type == 2) {
+        $boxMessageCount = sqimap_get_num_messages($imapConnection, $boxName);
+    }
+
+    /* Initialize the total counts. */
+
+    if ($boxArray['collapse'] == SM_BOX_COLLAPSED) {
+        /* Collect the counts for this boxes subfolders. */
+        $curBoxLength = strlen($boxName);
+        $boxCount = count($boxes);
+
+        for ($i = 0; $i < $boxCount; ++$i) {
+            /* Initialize the counts for this subfolder. */
+            $subUnseenCount = 0;
+            $subMessageCount = 0;
+
+            /* Collect the counts for this subfolder. */
+            if (($boxName != $boxes[$i]['unformatted'])
+                   && (substr($boxes[$i]['unformatted'], 0, $curBoxLength) == $boxName)
+                   && !in_array('noselect', $boxes[$i]['flags'])) {
+                $subUnseenCount = sqimap_unseen_messages($imapConnection, $boxes[$i]['unformatted']);
+                if ($unseen_type == 2) {
+                    $subMessageCount = sqimap_get_num_messages($imapConnection, $boxes[$i]['unformatted']);
+                }
+
+                /* Add the counts for this subfolder to the total. */
+                $totalUnseenCount += $subUnseenCount;
+                $totalMessageCount += $subMessageCount;
+            }
+        }
+
+        /* Add the counts for all subfolders to that of the box. */
+        $boxUnseenCount += $totalUnseenCount;
+        $boxMessageCount += $totalMessageCount;
+    }
+
+    /* And create the magic unseen count string.     */
+    /* Really a lot more then just the unseen count. */
+    if (($unseen_type == 1) && ($boxUnseenCount > 0)) {
+        $result[0] = "($boxUnseenCount)";
+    } else if ($unseen_type == 2) {
+        $result[0] = "($boxUnseenCount/$boxMessageCount)";
+        $result[0] = "<font color=\"$color[11]\">$result[0]</font>";
+    }
+
+    /* Set the unseen count to return to the outside world. */
+    $result[1] = $boxUnseenCount;
+
+    /* Return our happy result. */
+    return ($result);
+}
+
 /**
  * This simple function checks if a box is another box's parent.
  */
@@ -722,7 +786,7 @@ if ($auto_create_special && !isset($auto_create_done)) {
 echo "\n<BODY BGCOLOR=\"$color[3]\" TEXT=\"$color[6]\" LINK=\"$color[6]\" VLINK=\"$color[6]\" ALINK=\"$color[6]\">\n";
 
 do_hook('left_main_before');
-if ($advanced_tree && $advanced_tree_frame_ctrl) {
+if ($advanced_tree) {
    /* nice future feature, needs layout !! volunteers?   */  
    $right_pos = $left_size - 20;
    echo '<div style="position:absolute;top:0;border=solid;border-width:0.1em;border-color:blue;"><div ID="hidef" style="width=20;font-size:12"><A HREF="javascript:hideframe(true)"><b><<</b></a></div>';