+
+ /** Return root message of the given thread
+ * @param id INTEGER id of a message
+ */
+ function root($id)
+ {
+ $id_cur = $id;
+ while (true) {
+ $id_parent = $this->overview[$id_cur]->parent;
+ if (is_null($id_parent)) break;
+ $id_cur = $id_parent;
+ }
+ return $id_cur;
+ }
+
+ /** Returns previous thread root index
+ * @param id INTEGER message number
+ */
+ function prevThread($id)
+ {
+ $root = $this->root($id);
+ $last = null;
+ foreach ($this->roots as $i) {
+ if ($i == $root) {
+ return $last;
+ }
+ $last = $i;
+ }
+ return $last;
+ }
+
+ /** Returns next thread root index
+ * @param id INTEGER message number
+ */
+ function nextThread($id)
+ {
+ $root = $this->root($id);
+ $ok = false;
+ foreach ($this->roots as $i) {
+ if ($ok) {
+ return $i;
+ }
+ if ($i == $root) {
+ $ok = true;
+ }
+ }
+ return null;
+ }
+
+ /** Return prev post in the thread
+ * @param id INTEGER message number
+ */
+ function prevPost($id)
+ {
+ $parent = $this->overview[$id]->parent;
+ if (is_null($parent)) {
+ return null;
+ }
+ $last = $parent;
+ foreach ($this->overview[$parent]->children as $child) {
+ if ($child == $id) {
+ return $last;
+ }
+ $last = $child;
+ }
+ return null;
+ }
+
+ /** Return next post in the thread
+ * @param id INTEGER message number
+ */
+ function nextPost($id)
+ {
+ if (count($this->overview[$id]->children) != 0) {
+ return $this->overview[$id]->children[0];
+ }
+
+ $cur = $id;
+ while (true) {
+ $parent = $this->overview[$cur]->parent;
+ if (is_null($parent)) {
+ return null;
+ }
+ $ok = false;
+ foreach ($this->overview[$parent]->children as $child) {
+ if ($ok) {
+ return $child;
+ }
+ if ($child == $cur) {
+ $ok = true;
+ }
+ }
+ $cur = $parent;
+ }
+ 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;
+ }