Year of the post
[banana.git] / banana / spool.inc.php
CommitLineData
810ac1df 1<?php
2/********************************************************************************
3* include/spool.inc.php : spool subroutines
4* -----------------------
5*
6* This file is part of the banana distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
7027794f 10require_once dirname(__FILE__) . '/banana.inc.php';
3ca86dfe 11
dc5f77ad 12define('BANANA_SPOOL_VERSION', '0.5');
01681efd 13
810ac1df 14/** Class spoolhead
15 * class used in thread overviews
16 */
d4c19591 17class BananaSpoolHead
e785d91c 18{
19 /** date (timestamp) */
7027794f 20 public $date;
e785d91c 21 /** subject */
7027794f 22 public $subject;
e785d91c 23 /** author */
7027794f 24 public $from;
e785d91c 25 /** reference of parent */
7027794f 26 public $parent = null;
e785d91c 27 /** paren is direct */
7027794f 28 public $parent_direct;
e785d91c 29 /** array of children */
7027794f 30 public $children = Array();
e785d91c 31 /** true if post is read */
7027794f 32 public $isread;
e785d91c 33 /** number of posts deeper in this branch of tree */
7027794f 34 public $desc;
e785d91c 35 /** same as desc, but counts only unread posts */
7027794f 36 public $descunread;
37
38 /** storage data */
39 public $storage = array();
810ac1df 40
e785d91c 41 /** constructor
42 * @param $_date INTEGER timestamp of post
43 * @param $_subject STRING subject of post
44 * @param $_from STRING author of post
45 * @param $_desc INTEGER desc value (1 for a new post)
46 * @param $_read BOOLEAN true if read
47 * @param $_descunread INTEGER descunread value (0 for a new post)
48 */
7027794f 49 public function __construct(array &$message)
e785d91c 50 {
7027794f 51 $this->date = $message['date'];
b87c9103 52 $this->subject = $message['subject'];
7027794f 53 $this->from = $message['from'];
54 $this->desc = 1;
55 $this->isread = true;
56 $this->descunread = 0;
e785d91c 57 }
810ac1df 58}
59
3ca86dfe 60
d4c19591 61class BananaSpool
e785d91c 62{
7027794f 63 private $version;
e9360b11 64 private $mode;
7027794f 65
e785d91c 66 /** group name */
7027794f 67 public $group;
68 /** spool */
69 public $overview;
e785d91c 70 /** array msgid => msgnum */
7027794f 71 public $ids;
cced14b6 72 /** thread starts */
7027794f 73 public $roots;
dc5f77ad 74 /** protocole specific data */
75 public $storage = array();
7027794f 76
d265608a 77 private $unreadnb = 0;
810ac1df 78
e785d91c 79 /** constructor
e785d91c 80 * @param $_group STRING group name
81 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
82 * @param $_since INTEGER time stamp (used for read/unread)
83 */
7027794f 84 protected function __construct($group)
e785d91c 85 {
7027794f 86 $this->version = BANANA_SPOOL_VERSION;
e9360b11 87 $this->mode = Banana::SPOOL_ALL;
7027794f 88 $this->group = $group;
89 }
fb6428c8 90
e02edbfe 91 public static function &getSpool($group, $since = 0, $clean = false)
7027794f 92 {
93 if (!is_null(Banana::$spool) && Banana::$spool->group == $group) {
b3d11736 94 $spool =& Banana::$spool;
82c17a91 95 } else {
e02edbfe 96 $spool =& BananaSpool::readFromFile($group);
7027794f 97 }
98 if (is_null($spool)) {
99 $spool = new BananaSpool($group);
82c17a91 100 }
7027794f 101 Banana::$spool =& $spool;
102 $spool->build();
b3d11736 103 if ($clean) {
104 $spool->markAllAsRead();
105 }
7027794f 106 $spool->updateUnread($since);
107 return $spool;
108 }
e785d91c 109
7027794f 110 private static function spoolFilename($group)
111 {
edff14de 112 $file = Banana::$spool_root . '/' . Banana::$protocole->name() . '/';
7027794f 113 if (!is_dir($file)) {
114 mkdir($file);
dd7d1c59 115 }
e9360b11 116 return $file . Banana::$protocole->filename();
dd7d1c59 117 }
118
e02edbfe 119 private static function &readFromFile($group)
dd7d1c59 120 {
e02edbfe 121 $spool = null;
7027794f 122 $file = BananaSpool::spoolFilename($group);
123 if (!file_exists($file)) {
e02edbfe 124 return $spool;
dd7d1c59 125 }
7027794f 126 $spool = unserialize(file_get_contents($file));
e9360b11 127 if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) {
e02edbfe 128 $spool = null;
129 return $spool;
7027794f 130 }
d265608a 131 $spool->markAllAsRead();
7027794f 132 return $spool;
133 }
134
135 private function compare($a, $b)
136 {
345c3a85 137 return ($this->overview[$b]->date >= $this->overview[$a]->date);
dd7d1c59 138 }
139
7027794f 140 private function saveToFile()
dd7d1c59 141 {
7027794f 142 $file = BananaSpool::spoolFilename($this->group);
dd7d1c59 143
144 $this->roots = Array();
345c3a85 145 foreach($this->overview as $id=>&$msg) {
dd7d1c59 146 if (is_null($msg->parent)) {
147 $this->roots[] = $id;
148 }
149 }
345c3a85 150 usort($this->roots, array($this, 'compare'));
2c606d23 151
e9360b11 152 if ($this->mode == Banana::SPOOL_ALL) {
153 file_put_contents($file, serialize($this));
154 }
dd7d1c59 155 }
156
7027794f 157 private function build()
158 {
159 $threshold = 0;
160
161 // Compute the range of indexes
162 list($msgnum, $first, $last) = Banana::$protocole->getIndexes();
163 if ($last < $first) {
6bd49b4b 164 $threshold = $first + $msgnum - $last;
7027794f 165 $threshold = (int)(log($threshold)/log(2));
166 $threshold = (2 ^ ($threshold + 1)) - 1;
167 }
e9360b11 168 if (Banana::$spool_max && Banana::$spool_max < $msgnum) {
169 $first = $last - Banana::$spool_max;
7027794f 170 if ($first < 0) {
171 $first += $threshold;
172 }
173 }
174 $clean = $this->clean($first, $last, $msgnum);
175 $update = $this->update($first, $last, $msgnum);
176
177 if ($clean || $update) {
178 $this->saveToFile();
179 }
180 }
181
182 private function clean(&$first, &$last, $msgnum)
9090c673 183 {
7027794f 184 $do_save = false;
185 if (is_array($this->overview)) {
186 $mids = array_keys($this->overview);
187 foreach ($mids as $id) {
188 if (($first <= $last && ($id < $first || $id > $last))
e9360b11 189 || ($first > $last && $id < $first && $id > $last)) {
7027794f 190 $this->delid($id, false);
191 $do_save = true;
192 }
193 }
194 if (!empty($this->overview)) {
195 $first = max(array_keys($this->overview))+1;
196 }
197 }
198 return $do_save;
9090c673
PHM
199 }
200
7027794f 201 private function update(&$first, &$last, $msgnum)
dd7d1c59 202 {
2f0aa8ce 203 if ($first > $last || !$msgnum) {
7027794f 204 return false;
dd7d1c59 205 }
206
7027794f 207 $messages =& Banana::$protocole->getMessageHeaders($first, $last,
208 array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To'));
dd7d1c59 209
7027794f 210 if (!is_array($this->ids)) {
211 $this->ids = array();
212 }
213 foreach ($messages as $id=>&$message) {
214 $this->ids[$message['message-id']] = $id;
215 }
216
9c118ac9 217 if (!is_array($this->overview)) {
218 $this->overview = array();
219 }
7027794f 220 foreach ($messages as $id=>&$message) {
221 if (!isset($this->overview[$id])) {
222 $this->overview[$id] = new BananaSpoolHead($message);
3316e34e 223 }
7027794f 224 $msg =& $this->overview[$id];
225 $msgrefs = BananaMessage::formatReferences($message);
226 $parents = preg_grep('/^\d+$/', $msgrefs);
227 $msg->parent = array_pop($parents);
228 $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
e785d91c 229
7027794f 230 if (!is_null($p = $msg->parent)) {
dd7d1c59 231 if (empty($this->overview[$p])) {
7027794f 232 $this->overview[$p] = new BananaSpoolHead($messages[$p]);
4ced5065 233 }
dd7d1c59 234 $this->overview[$p]->children[] = $id;
4ced5065 235
7027794f 236 while (!is_null($p)) {
dd7d1c59 237 $this->overview[$p]->desc += $msg->desc;
7027794f 238 if ($p != $this->overview[$p]->parent) {
239 $p = $this->overview[$p]->parent;
240 } else {
241 $p = null;
242 }
e785d91c 243 }
810ac1df 244 }
810ac1df 245 }
7027794f 246 Banana::$protocole->updateSpool($messages);
247 return true;
dd7d1c59 248 }
e785d91c 249
bffb37b4 250 public function updateUnread($since)
75ff2f64 251 {
7027794f 252 if (empty($since)) {
253 return;
254 }
255
256 $newpostsids = Banana::$protocole->getNewIndexes($since);
b3d11736 257
7027794f 258 if (empty($newpostsids)) {
259 return;
260 }
261
262 if (!is_array($this->ids)) {
263 $this->ids = array();
264 }
265 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
266 foreach ($newpostsids as $mid) {
267 $id = $this->ids[$mid];
268 if ($this->overview[$id]->isread) {
d265608a 269 $this->overview[$id]->isread = false;
270 $this->unreadnb++;
7027794f 271 while (isset($id)) {
d265608a 272 $this->overview[$id]->descunread++;
7027794f 273 $id = $this->overview[$id]->parent;
e785d91c 274 }
810ac1df 275 }
7027794f 276 }
277 }
dd7d1c59 278
7027794f 279 public function setMode($mode)
280 {
e9360b11 281 $this->mode = $mode;
7027794f 282 switch ($mode) {
283 case Banana::SPOOL_UNREAD:
284 foreach ($this->roots as $k=>$i) {
285 if ($this->overview[$i]->descunread == 0) {
286 $this->killdesc($i);
287 unset($this->roots[$k]);
e785d91c 288 }
810ac1df 289 }
7027794f 290 break;
810ac1df 291 }
cced14b6 292 }
293
e9360b11 294 /** Mark the given id as read
295 * @param id MSGNUM of post
296 */
297 public function markAsRead($id)
298 {
299 if (!$this->overview[$id]->isread) {
300 $this->overview[$id]->isread = true;
d265608a 301 $this->unreadnb--;
e9360b11 302 while (isset($id)) {
303 $this->overview[$id]->descunread--;
304 $id = $this->overview[$id]->parent;
305 }
306 }
307 }
308
309 /** Mark all unread messages as read
310 */
311 public function markAllAsRead(array &$array = null)
312 {
d265608a 313 if (!$this->unreadnb) {
314 return;
315 }
c4f176d8 316 if (is_null($array) && is_array($this->roots)) {
e9360b11 317 $array =& $this->roots;
c4f176d8 318 } elseif (is_null($array)) {
c28d3016 319 return;
320 }
e9360b11 321 foreach ($array as $id) {
322 if (!$this->overview[$id]->isread) {
323 $this->markAsRead($id);
d265608a 324 if (!$this->unreadnb) {
325 return;
326 }
e9360b11 327 }
328 if ($this->overview[$id]->descunread) {
329 $this->markAllAsRead($this->overview[$id]->children);
330 }
331 }
332 }
333
e785d91c 334 /** kill post and childrens
335 * @param $_id MSGNUM of post
336 */
7027794f 337 private function killdesc($_id)
e785d91c 338 {
339 if (sizeof($this->overview[$_id]->children)) {
340 foreach ($this->overview[$_id]->children as $c) {
341 $this->killdesc($c);
342 }
343 }
344 unset($this->overview[$_id]);
dd7d1c59 345 if (($msgid = array_search($_id, $this->ids)) !== false) {
e785d91c 346 unset($this->ids[$msgid]);
347 }
810ac1df 348 }
e785d91c 349
350 /** delete a post from overview
351 * @param $_id MSGNUM of post
352 */
7027794f 353 public function delid($_id, $write = true)
e785d91c 354 {
355 if (isset($this->overview[$_id])) {
d265608a 356 $overview =& $this->overview[$_id];
357 if (!$overview->isread) {
358 $this->markAsRead($_id);
359 }
360 if ($overview->parent) {
361 $p = $overview->parent;
362 $parent =& $this->overview[$p];
363 $parent->children = array_diff($parent->children, array($_id));
364 if (sizeof($overview->children)) {
365 $parent->children = array_merge($parent->children, $overview->children);
366 foreach ($overview->children as $c) {
367 $this->overview[$c]->parent = $p;
e785d91c 368 $this->overview[$c]->parent_direct = false;
369 }
370 }
d265608a 371 while (isset($p)) {
e785d91c 372 $this->overview[$p]->desc--;
373 $p = $this->overview[$p]->parent;
374 }
d265608a 375 } elseif ($overview->children) {
376 foreach ($overview->children as $c) {
e785d91c 377 $this->overview[$c]->parent = null;
378 }
379 }
d265608a 380 unset($overview);
e785d91c 381 unset($this->overview[$_id]);
3ca86dfe 382 $msgid = array_search($_id, $this->ids);
cfb7fe5d 383 if ($msgid !== false) {
e785d91c 384 unset($this->ids[$msgid]);
385 }
cfb7fe5d 386 $msgid = array_search($_id, $this->roots);
387 if ($msgid !== false) {
388 unset($this->roots[$msgid]);
389 }
e785d91c 390
7027794f 391 if ($write) {
392 $this->saveToFile();
393 }
e785d91c 394 }
35ca8036 395 }
810ac1df 396
7027794f 397 private function formatDate($stamp)
398 {
399 $today = intval(time() / (24*3600));
400 $dday = intval($stamp / (24*3600));
401
402 if ($today == $dday) {
403 $format = "%H:%M";
404 } elseif ($today == 1 + $dday) {
405 $format = _b_('hier')." %H:%M";
406 } elseif ($today < 7 + $dday) {
407 $format = '%a %H:%M';
d924433f 408 } elseif ($today < 90 + $dday) {
7027794f 409 $format = '%a %e %b';
d924433f 410 } else {
411 $format = '%a %e %b %Y';
7027794f 412 }
22b95309 413 return strftime($format, $stamp);
7027794f 414 }
415
e785d91c 416 /** displays children tree of a post
417 * @param $_id INTEGER MSGNUM of post
418 * @param $_index INTEGER linear number of post in the tree
419 * @param $_first INTEGER linear number of first post displayed
420 * @param $_last INTEGER linear number of last post displayed
421 * @param $_ref STRING MSGNUM of current post
422 * @param $_pfx_node STRING prefix used for current node
423 * @param $_pfx_end STRING prefix used for children of current node
424 * @param $_head BOOLEAN true if first post in thread
3204d440 425 *
b87c9103 426 * If you want to analyse subject, you can define the function hook_formatDisplayHeader
e785d91c 427 */
96e1e874 428 private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true, $_pfx_id="")
75ff2f64 429 {
7027794f 430 static $spfx_f, $spfx_n, $spfx_Tnd, $spfx_Lnd, $spfx_snd, $spfx_T, $spfx_L, $spfx_s, $spfx_e, $spfx_I;
431 if (!isset($spfx_f)) {
432 $spfx_f = Banana::$page->makeImg(Array('img' => 'k1', 'alt' => 'o', 'height' => 21, 'width' => 9));
433 $spfx_n = Banana::$page->makeImg(Array('img' => 'k2', 'alt' => '*', 'height' => 21, 'width' => 9));
434 $spfx_Tnd = Banana::$page->makeImg(Array('img' => 'T-direct', 'alt' => '+', 'height' => 21, 'width' => 12));
435 $spfx_Lnd = Banana::$page->makeImg(Array('img' => 'L-direct', 'alt' => '`', 'height' => 21, 'width' => 12));
436 $spfx_snd = Banana::$page->makeImg(Array('img' => 's-direct', 'alt' => '-', 'height' => 21, 'width' => 5));
437 $spfx_T = Banana::$page->makeImg(Array('img' => 'T', 'alt' => '+', 'height' => 21, 'width' => 12));
438 $spfx_L = Banana::$page->makeImg(Array('img' => 'L', 'alt' => '`', 'height' => 21, 'width' => 12));
439 $spfx_s = Banana::$page->makeImg(Array('img' => 's', 'alt' => '-', 'height' => 21, 'width' => 5));
440 $spfx_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => '&nbsp;', 'height' => 21, 'width' => 12));
441 $spfx_I = Banana::$page->makeImg(Array('img' => 'I', 'alt' => '|', 'height' => 21, 'width' => 12));
810ac1df 442 }
e785d91c 443
7027794f 444 $overview =& $this->overview[$_id];
445 if ($_index + $overview->desc < $_first || $_index > $_last) {
446 return '';
447 }
cced14b6 448
7027794f 449 $res = '';
450 if ($_index >= $_first) {
451 $hc = empty($overview->children);
452
96e1e874 453 $res .= '<tr id="'.$_pfx_id.$_id.'" class="' . ($_index%2 ? 'pair' : 'impair') . ($overview->isread ? '' : ' new') . "\">\n";
7027794f 454 $res .= '<td class="date">' . $this->formatDate($overview->date) . " </td>\n";
951030b7 455 $res .= '<td class="subj' . ($_index == $_ref ? ' cur' : '') . '"><div class="tree">'
456 . $_pfx_node .($hc ? ($_head ? $spfx_f : ($overview->parent_direct ? $spfx_s : $spfx_snd)) : $spfx_n)
457 . '</div>';
4f7c063d 458 $popup = $subject = $overview->subject;
b87c9103 459 if (function_exists('hook_formatDisplayHeader')) {
460 list($subject, $link) = hook_formatDisplayHeader('subject', $subject, true);
461 } else {
4f7c063d 462 $subject = banana_catchFormats(banana_entities(stripslashes($subject)));
b87c9103 463 $link = null;
464 }
7027794f 465 if (empty($subject)) {
79405147 466 $subject = _b_('(pas de sujet)');
467 }
7027794f 468 if ($_index != $_ref) {
469 $subject = Banana::$page->makeLink(Array('group' => $this->group, 'artid' => $_id,
4f7c063d 470 'text' => $subject, 'popup' => $popup));
cced14b6 471 }
7027794f 472 $res .= '&nbsp;' . $subject . $link;
473 $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>";
dd7d1c59 474
7027794f 475 if ($hc) {
476 return $res;
477 }
cced14b6 478 }
479
dd7d1c59 480 $_index ++;
7027794f 481 $children = $overview->children;
e785d91c 482 while ($child = array_shift($children)) {
7027794f 483 $overview =& $this->overview[$child];
484 if ($_index > $_last) {
485 return $res;
486 }
487 if ($_index + $overview->desc >= $_first) {
e785d91c 488 if (sizeof($children)) {
65d96b1f 489 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
7027794f 490 $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd),
96e1e874 491 $_pfx_end . $spfx_I, false,$_id.'_');
e785d91c 492 } else {
65d96b1f 493 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
7027794f 494 $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd),
96e1e874 495 $_pfx_end . $spfx_e, false,$_id.'_');
e785d91c 496 }
497 }
7027794f 498 $_index += $overview->desc;
810ac1df 499 }
65d96b1f 500
501 return $res;
810ac1df 502 }
810ac1df 503
e785d91c 504 /** Displays overview
505 * @param $_first INTEGER MSGNUM of first post
506 * @param $_last INTEGER MSGNUM of last post
507 * @param $_ref STRING MSGNUM of current/selectionned post
508 */
7027794f 509 public function toHtml($first = 0, $overview = false)
75ff2f64 510 {
96e1e874 511 $res = Banana::$page->makeJs('jquery');
512 $res .= Banana::$page->makeJs('spool_toggle');
7027794f 513
514 if (!$overview) {
515 $_first = $first;
e9360b11 516 $_last = $first + Banana::$spool_tmax - 1;
7027794f 517 $_ref = null;
d8e2470c 518 } else {
7027794f 519 $_ref = $this->getNdx($first);
e9360b11 520 $_last = $_ref + Banana::$spool_tafter;
521 $_first = $_ref - Banana::$spool_tbefore;
7027794f 522 if ($_first < 0) {
523 $_last -= $_first;
524 }
65d96b1f 525 }
e785d91c 526 $index = 1;
7027794f 527 foreach ($this->roots as $id) {
528 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
529 $index += $this->overview[$id]->desc ;
530 if ($index > $_last) {
531 break;
e785d91c 532 }
4f75645f 533 }
7027794f 534 return $res;
810ac1df 535 }
810ac1df 536
e785d91c 537 /** computes linear post index
538 * @param $_id INTEGER MSGNUM of post
539 * @return INTEGER linear index of post
540 */
7027794f 541 public function getNdX($_id)
75ff2f64 542 {
cced14b6 543 $ndx = 1;
544 $id_cur = $_id;
4ced5065 545 while (true) {
cced14b6 546 $id_parent = $this->overview[$id_cur]->parent;
4ced5065 547 if (is_null($id_parent)) break;
cced14b6 548 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
4ced5065 549
cced14b6 550 for ($i = 0; $i < $pos ; $i++) {
e785d91c 551 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
552 }
951030b7 553 $ndx++; //noeud père
cced14b6 554
555 $id_cur = $id_parent;
810ac1df 556 }
cced14b6 557
558 foreach ($this->roots as $i) {
559 if ($i==$id_cur) {
e785d91c 560 break;
561 }
cced14b6 562 $ndx += $this->overview[$i]->desc;
e785d91c 563 }
564 return $ndx;
810ac1df 565 }
d8e2470c 566
567 /** Return root message of the given thread
568 * @param id INTEGER id of a message
569 */
7027794f 570 public function root($id)
571 {
d8e2470c 572 $id_cur = $id;
573 while (true) {
574 $id_parent = $this->overview[$id_cur]->parent;
575 if (is_null($id_parent)) break;
576 $id_cur = $id_parent;
577 }
578 return $id_cur;
579 }
580
bffb37b4 581 /** Return the last post id with the given subject
582 * @param subject
583 */
584 public function getPostId($subject)
585 {
586 $subject = trim($subject);
587 $id = max(array_keys($this->overview));
588 while (isset($this->overview[$id])) {
589 $test = $this->overview[$id]->subject;
590 if (function_exists('hook_formatDisplayHeader')) {
591 $val = hook_formatDisplayHeader('subject', $test, true);
592 if (is_array($val)) {
0a5b736c 593 $test = banana_html_entity_decode($val[0]);
bffb37b4 594 } else {
0a5b736c 595 $test = banana_html_entity_decode($val);
bffb37b4 596 }
597 }
598 $test = trim($test);
bffb37b4 599 if ($test == $subject) {
600 return $id;
601 }
602 $id--;
603 }
604 return -1;
605 }
606
d8e2470c 607 /** Returns previous thread root index
608 * @param id INTEGER message number
609 */
7027794f 610 public function prevThread($id)
d8e2470c 611 {
612 $root = $this->root($id);
613 $last = null;
614 foreach ($this->roots as $i) {
615 if ($i == $root) {
616 return $last;
617 }
618 $last = $i;
619 }
620 return $last;
621 }
622
623 /** Returns next thread root index
624 * @param id INTEGER message number
625 */
7027794f 626 public function nextThread($id)
d8e2470c 627 {
628 $root = $this->root($id);
629 $ok = false;
630 foreach ($this->roots as $i) {
631 if ($ok) {
632 return $i;
633 }
634 if ($i == $root) {
635 $ok = true;
636 }
637 }
638 return null;
639 }
640
641 /** Return prev post in the thread
642 * @param id INTEGER message number
643 */
7027794f 644 public function prevPost($id)
d8e2470c 645 {
646 $parent = $this->overview[$id]->parent;
647 if (is_null($parent)) {
648 return null;
649 }
650 $last = $parent;
651 foreach ($this->overview[$parent]->children as $child) {
652 if ($child == $id) {
653 return $last;
654 }
655 $last = $child;
656 }
657 return null;
658 }
659
660 /** Return next post in the thread
661 * @param id INTEGER message number
662 */
7027794f 663 public function nextPost($id)
d8e2470c 664 {
665 if (count($this->overview[$id]->children) != 0) {
666 return $this->overview[$id]->children[0];
667 }
668
669 $cur = $id;
670 while (true) {
671 $parent = $this->overview[$cur]->parent;
672 if (is_null($parent)) {
673 return null;
674 }
675 $ok = false;
676 foreach ($this->overview[$parent]->children as $child) {
677 if ($ok) {
678 return $child;
679 }
680 if ($child == $cur) {
681 $ok = true;
682 }
683 }
684 $cur = $parent;
685 }
686 return null;
687 }
d634c13c 688
689 /** Look for an unread message in the thread rooted by the message
690 * @param id INTEGER message number
691 */
7027794f 692 private function _nextUnread($id)
d634c13c 693 {
694 if (!$this->overview[$id]->isread) {
695 return $id;
696 }
697 foreach ($this->overview[$id]->children as $child) {
bdad7c9d 698 $unread = $this->_nextUnread($child);
699 if (!is_null($unread)) {
700 return $unread;
701 }
d634c13c 702 }
703 return null;
704 }
705
706 /** Find next unread message
707 * @param id INTEGER message number
708 */
7027794f 709 public function nextUnread($id = null)
d634c13c 710 {
d265608a 711 if (!$this->unreadnb) {
712 return null;
713 }
714
b7d59a47 715 if (!is_null($id)) {
716 // Look in message children
717 foreach ($this->overview[$id]->children as $child) {
718 $next = $this->_nextUnread($child);
719 if (!is_null($next)) {
720 return $next;
721 }
d634c13c 722 }
723 }
724
725 // Look in current thread
726 $cur = $id;
b7d59a47 727 do {
728 $parent = is_null($cur) ? null : $this->overview[$cur]->parent;
729 $ok = is_null($cur) ? true : false;
1e016f3a 730 if (!is_null($parent)) {
d634c13c 731 $array = &$this->overview[$parent]->children;
732 } else {
733 $array = &$this->roots;
734 }
735 foreach ($array as $child) {
736 if ($ok) {
737 $next = $this->_nextUnread($child);
738 if (!is_null($next)) {
739 return $next;
740 }
741 }
742 if ($child == $cur) {
743 $ok = true;
744 }
745 }
746 $cur = $parent;
b7d59a47 747 } while(!is_null($cur));
d634c13c 748 return null;
749 }
810ac1df 750}
751
598a1c53 752// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
810ac1df 753?>