Add a 'next unread message' button in spool view
[banana.git] / banana / spool.inc.php
index 2a6be2e..9b634a0 100644 (file)
@@ -405,9 +405,25 @@ class BananaSpool
         $new .= '</div>';
         
         if (is_null($_ref)) {
-            $res .= '<tr><th>' . _b_('Date') . '</th>';
-            $res .= '<th>' . $new . _b_('Sujet') . '</th>';
-            $res .= '<th>' . _b_('Auteur') . '</th></tr>';
+            $next = $this->nextUnread();
+            if (!is_null($next)) {
+                $next = '<div class="banana_menu">'
+                      . makeImgLink(Array('group' => $this->group,
+                                          'artid' => $next),
+                                    'next_unread.gif',
+                                    _b_('Message non-lu suivant'))
+                      . '</div>';
+            }
+            $new  = '<div class="banana_action">'
+                  . makeImgLink(Array('group'  => $this->group,
+                                      'action' => 'new'),
+                                'post.gif',
+                                _b_('Nouveau message'))
+                  . '</div>';
+
+            $res .= '<tr><th>' . $next . _b_('Date') . '</th>';
+            $res .= '<th>' .  _b_('Sujet') . '</th>';
+            $res .= '<th>' . $new . _b_('Auteur') . '</th></tr>';
         } else {
             $res .= '<tr><th colspan="3">' . _b_('Aperçu de ')
                  . makeHREF(Array('group' => $this->group),
@@ -561,6 +577,64 @@ class BananaSpool
         }
         return null;
     }
+
+    /** Look for an unread message in the thread rooted by the message
+     * @param id INTEGER message number
+     */
+    function _nextUnread($id)
+    {
+        if (!$this->overview[$id]->isread) {
+            return $id;
+        }
+        foreach ($this->overview[$id]->children as $child) {
+            $unread = $this->_nextUnread($child);
+            if (!is_null($unread)) {
+                return $unread;
+            }    
+        }
+        return null;
+    }
+
+    /** Find next unread message
+     * @param id INTEGER message number
+     */
+    function nextUnread($id = null)
+    {
+        if (!is_null($id)) {
+            // Look in message children
+            foreach ($this->overview[$id]->children as $child) {
+                $next = $this->_nextUnread($child);
+                if (!is_null($next)) {
+                    return $next;
+                }
+            }
+        }
+
+        // Look in current thread
+        $cur = $id;
+        do {
+            $parent = is_null($cur) ? null : $this->overview[$cur]->parent;
+            $ok     = is_null($cur) ? true : false;
+            if (!is_null($parent)) {
+                $array = &$this->overview[$parent]->children;
+            } else {
+                $array = &$this->roots;
+            }
+            foreach ($array as $child) {
+                if ($ok) {
+                    $next = $this->_nextUnread($child);
+                    if (!is_null($next)) {
+                        return $next;
+                    }
+                }
+                if ($child == $cur) {
+                    $ok = true;
+                }
+            }
+            $cur = $parent;
+        } while(!is_null($cur));
+        return null;
+    }    
 }
 
 // vim:set et sw=4 sts=4 ts=4