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