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