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