Improvements to folder summary blocks.
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 26 Jan 2010 20:09:28 +0000 (13:09 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 26 Jan 2010 20:42:49 +0000 (13:42 -0700)
Consolidate duplicate code.
Use LIST-STATUS output, if available.

imp/lib/Block/Foldersummary.php
imp/lib/Block/summary.php
imp/lib/Ui/Block.php [new file with mode: 0644]

index 352d2bf..743841c 100644 (file)
@@ -14,61 +14,10 @@ class IMP_Block_Foldersummary extends Horde_Block
 
     protected function _content()
     {
-        /* Filter on INBOX display, if requested. */
-        if ($GLOBALS['prefs']->getValue('filter_on_display')) {
-            $imp_filter = new IMP_Filter();
-            $imp_filter->filter('INBOX');
-        }
+        $imp_ui = new IMP_Ui_Block();
+        list($html,) = $imp_ui->folderSummary('dimp');
 
-        /* Get list of mailboxes to poll. */
-        $imptree = IMP_Imap_Tree::singleton();
-        $folders = $imptree->getPollList(true);
-
-        $anyUnseen = false;
-        $newmsgs = array();
-        $html = '<table cellspacing="0" width="100%">';
-
-        foreach ($folders as $folder) {
-            if (($folder == 'INBOX') ||
-                ($_SESSION['imp']['protocol'] != 'pop')) {
-                $info = $imptree->getElementInfo($folder);
-                if (!empty($info)) {
-                    if (empty($this->_params['show_unread']) ||
-                        !empty($info['unseen'])) {
-                        if (!empty($info['newmsg'])) {
-                            $newmsgs[$folder] = $info['newmsg'];
-                        }
-                        $html .= '<tr style="cursor:pointer" class="text" onclick="DimpBase.go(\'folder:' . htmlspecialchars($folder) . '\');return false;"><td>';
-                        if (!empty($info['unseen'])) {
-                            $html .= '<strong>';
-                            $anyUnseen = true;
-                        }
-                        $html .= '<a>' . IMP::displayFolder($folder) . '</a>';
-                        if (!empty($info['unseen'])) {
-                            $html .= '</strong>';
-                        }
-                        $html .= '</td><td>' .
-                            (!empty($info['unseen']) ? '<strong>' . $info['unseen'] . '</strong>' : '0') .
-                            (!empty($this->_params['show_total']) ? '</td><td>(' . $info['messages'] . ')' : '') .
-                            '</td></tr>';
-                    }
-                }
-            }
-        }
-
-        $html .= '</table>';
-
-        if (count($newmsgs) == 0 && !empty($this->_params['show_unread'])) {
-            if (count($folders) == 0) {
-                $html = _("No folders are being checked for new mail.");
-            } elseif (!$anyUnseen) {
-                $html = '<em>' . _("No folders with unseen messages") . '</em>';
-            } elseif ($GLOBALS['prefs']->getValue('nav_popup')) {
-                $html = '<em>' . _("No folders with new messages") . '</em>';
-            }
-        }
-
-        return $html;
+        return '<table cellspacing="0" width="100%">' . $html . '</table>';
     }
 
 }
index 2cefa9a..de9bf08 100644 (file)
@@ -33,92 +33,34 @@ class Horde_Block_imp_summary extends Horde_Block
 
     protected function _content()
     {
-        global $notification, $prefs, $registry;
+        $imp_ui = new IMP_Ui_Block();
+        list($html_out, $newmsgs) = $imp_ui->folderSummary('imp');
 
         $html = '<table cellspacing="0" width="100%">';
 
-        /* Filter on INBOX display, if requested. */
-        if ($prefs->getValue('filter_on_display')) {
-            $imp_filter = new IMP_Filter();
-            $imp_filter->filter('INBOX');
-        }
-
-        /* Get list of mailboxes to poll. */
-        $imaptree = IMP_Imap_Tree::singleton();
-        $folders = $imaptree->getPollList(true);
-
         /* Quota info, if available. */
         $quota_msg = Horde_Util::bufferOutput(array('IMP', 'quota'));
         if (!empty($quota_msg)) {
             $html .= '<tr><td colspan="3">' . $quota_msg . '</td></tr>';
         }
 
-        $anyUnseen = false;
-        $mbox_url = Horde::applicationUrl('mailbox.php', true);
-        $newmsgs = array();
-
-        foreach ($folders as $folder) {
-            if (($folder == 'INBOX') ||
-                ($_SESSION['imp']['protocol'] != 'pop')) {
-                $info = $imaptree->getElementInfo($folder);
-                if (!empty($info)) {
-                    if (empty($this->_params['show_unread']) ||
-                        !empty($info['unseen'])) {
-                        if (!empty($info['recent'])) {
-                            $newmsgs[$folder] = $info['recent'];
-                        }
-                        $url = $mbox_url->copy()->add(array('no_newmail_popup' => 1, 'mailbox' => $folder));
-                        $html .= '<tr style="cursor:pointer" class="text" onclick="self.location=\'' . $url . '\'"><td>';
-                        if (!empty($info['unseen'])) {
-                            $html .= '<strong>';
-                            $anyUnseen = true;
-                        }
-                        $html .= Horde::link($url) . IMP::displayFolder($folder) . '</a>';
-                        if (!empty($info['unseen'])) {
-                            $html .= '</strong>';
-                        }
-                        $html .= '</td><td>' .
-                            (!empty($info['unseen']) ? '<strong>' . $info['unseen'] . '</strong>' : '0') .
-                            (!empty($this->_params['show_total']) ? '</td><td>(' . $info['messages'] . ')' : '') .
-                            '</td></tr>';
-                    }
-                }
-            }
-        }
-
-        $html .= '</table>';
-
         /* Check to see if user wants new mail notification, but only
          * if the user is logged into IMP. */
-        if ($prefs->getValue('nav_popup')) {
-            // Always include these scripts so they'll be there if
-            // there's new mail in later dynamic updates.
+        if ($GLOBALS['prefs']->getValue('nav_popup')) {
+            /* Always include these scripts so they'll be there if there's
+             * new mail in later dynamic updates. */
             Horde::addScriptFile('effects.js', 'horde');
             Horde::addScriptFile('redbox.js', 'horde');
         }
 
-        if (!empty($newmsgs)) {
-            /* Open the mailbox R/W to ensure the 'recent' flags are cleared
-             * from the current mailbox. */
-            foreach ($newmsgs as $mbox => $nm) {
-                $GLOBALS['imp_imap']->ob()->openMailbox($mbox, Horde_Imap_Client::OPEN_READWRITE);
-            }
-
-            if ($prefs->getValue('nav_audio') || $prefs->getValue('nav_popup')) {
-                $html .= Horde_Util::bufferOutput(IMP::newmailAlerts($newmsgs)) .
-                    Horde_Util::bufferOutput(array($notification, 'notify'), array('listeners' => 'audio'));
-            }
-        } elseif (!empty($this->_params['show_unread'])) {
-            if (count($folders) == 0) {
-                $html .= _("No folders are being checked for new mail.");
-            } elseif (!$anyUnseen) {
-                $html .= '<em>' . _("No folders with unseen messages") . '</em>';
-            } elseif ($prefs->getValue('nav_popup')) {
-                $html .= '<em>' . _("No folders with new messages") . '</em>';
-            }
+        if (!empty($newmsgs) &&
+            ($GLOBALS['prefs']->getValue('nav_audio') ||
+             $GLOBALS['prefs']->getValue('nav_popup'))) {
+            $html .= Horde_Util::bufferOutput(IMP::newmailAlerts($newmsgs)) .
+                Horde_Util::bufferOutput(array($GLOBALS['notification'], 'notify'), array('listeners' => 'audio'));
         }
 
-        return $html;
+        return $html . $html_out . '</table>';
     }
 
 }
diff --git a/imp/lib/Ui/Block.php b/imp/lib/Ui/Block.php
new file mode 100644 (file)
index 0000000..b408d71
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+/**
+ * The IMP_Ui_Block:: class is designed to provide a place to store common
+ * code shared among IMP's various block views.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package IMP
+ */
+class IMP_Ui_Block
+{
+    /**
+     * Render folder summary information.
+     *
+     * @param string $mode  Either 'dimp' or 'imp' - defines how links are
+     *                      generated.
+     *
+     * @return array  The HTML code and an array with mailboxes containing new
+     *                messages as the keys and the number of recent messages
+     *                as the values.
+     */
+    public function folderSummary($mode)
+    {
+        /* Filter on INBOX display, if requested. */
+        if ($GLOBALS['prefs']->getValue('filter_on_display')) {
+            $imp_filter = new IMP_Filter();
+            $imp_filter->filter('INBOX');
+        }
+
+        /* Get list of mailboxes to poll. */
+        $imptree = IMP_Imap_Tree::singleton();
+        $poll = $imptree->getPollList(true);
+        $status = $GLOBALS['imp_imap']->ob()->statusMultiple($poll, Horde_Imap_Client::STATUS_UNSEEN | Horde_Imap_Client::STATUS_MESSAGES | Horde_Imap_Client::STATUS_RECENT);
+
+        $anyUnseen = false;
+        $html = '';
+        $newmsgs = array();
+
+        if ($mode == 'imp') {
+            $mbox_url = Horde::applicationUrl('mailbox.php', true);
+        }
+
+        foreach ($poll as $folder) {
+            if (isset($status[$folder]) &&
+                (($folder == 'INBOX') ||
+                 ($_SESSION['imp']['protocol'] != 'pop')) &&
+                (empty($this->_params['show_unread']) ||
+                 !empty($status[$folder]['unseen']))) {
+                if (!empty($status[$folder]['recent'])) {
+                    $newmsgs[$folder] = $status[$folder]['recent'];
+                }
+
+                if ($mode == 'imp') {
+                    $onclick = 'self.location=\'' . $mbox_url->copy()->add(array('no_newmail_popup' => 1, 'mailbox' => $folder)) . '\'';
+                } else {
+                    $onclick = 'DimpBase.go(\'folder:' . htmlspecialchars($folder) . '\');return false;';
+                }
+
+                $html .= '<tr style="cursor:pointer" class="text" onclick="' . $onclick . '"><td>';
+
+                if (!empty($status[$folder]['unseen'])) {
+                    $html .= '<strong>';
+                    $anyUnseen = true;
+                }
+
+                $html .= (($mode == 'imp') ? Horde::link($url) : '<a>') . IMP::displayFolder($folder) . '</a>';
+
+                if (!empty($status[$folder]['unseen'])) {
+                    $html .= '</strong>';
+                }
+                $html .= '</td><td>' .
+                    (!empty($status[$folder]['unseen']) ? '<strong>' . $status[$folder]['unseen'] . '</strong>' : '0') .
+                    (!empty($this->_params['show_total']) ? '</td><td>(' . $status[$folder]['messages'] . ')' : '') .
+                    '</td></tr>';
+            }
+        }
+
+        $html .= '</table>';
+
+        if (!empty($newmsgs)) {
+            /* Open the mailbox R/W to ensure the 'recent' flags are cleared
+             * from the current mailbox. */
+            foreach ($newmsgs as $mbox => $nm) {
+                $GLOBALS['imp_imap']->ob()->openMailbox($mbox, Horde_Imap_Client::OPEN_READWRITE);
+            }
+        } elseif (!empty($this->_params['show_unread'])) {
+            if (count($folders) == 0) {
+                $html = _("No folders are being checked for new mail.");
+            } elseif (!$anyUnseen) {
+                $html = '<em>' . _("No folders with unseen messages") . '</em>';
+            } elseif ($GLOBALS['prefs']->getValue('nav_popup')) {
+                $html = '<em>' . _("No folders with new messages") . '</em>';
+            }
+        }
+
+        return array($html, $newmsgs);
+    }
+
+}