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