78cd27b3 |
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 | |
ab02e8a9 |
10 | require_once dirname(__FILE__) . '/banana.inc.php'; |
78cd27b3 |
11 | |
ab02e8a9 |
12 | define('BANANA_SPOOL_VERSION', '0.3'); |
78cd27b3 |
13 | |
14 | /** Class spoolhead |
15 | * class used in thread overviews |
16 | */ |
17 | class BananaSpoolHead |
18 | { |
19 | /** date (timestamp) */ |
ab02e8a9 |
20 | public $date; |
78cd27b3 |
21 | /** subject */ |
ab02e8a9 |
22 | public $subject; |
78cd27b3 |
23 | /** author */ |
ab02e8a9 |
24 | public $from; |
78cd27b3 |
25 | /** reference of parent */ |
ab02e8a9 |
26 | public $parent = null; |
78cd27b3 |
27 | /** paren is direct */ |
ab02e8a9 |
28 | public $parent_direct; |
78cd27b3 |
29 | /** array of children */ |
ab02e8a9 |
30 | public $children = Array(); |
78cd27b3 |
31 | /** true if post is read */ |
ab02e8a9 |
32 | public $isread; |
78cd27b3 |
33 | /** number of posts deeper in this branch of tree */ |
ab02e8a9 |
34 | public $desc; |
78cd27b3 |
35 | /** same as desc, but counts only unread posts */ |
ab02e8a9 |
36 | public $descunread; |
37 | |
38 | /** storage data */ |
39 | public $storage = array(); |
78cd27b3 |
40 | |
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 | */ |
ab02e8a9 |
49 | public function __construct(array &$message) |
78cd27b3 |
50 | { |
ab02e8a9 |
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; |
78cd27b3 |
57 | } |
58 | } |
59 | |
78cd27b3 |
60 | |
61 | class BananaSpool |
62 | { |
ab02e8a9 |
63 | private $version; |
7972645b |
64 | private $mode; |
ab02e8a9 |
65 | |
78cd27b3 |
66 | /** group name */ |
ab02e8a9 |
67 | public $group; |
68 | /** spool */ |
69 | public $overview; |
78cd27b3 |
70 | /** array msgid => msgnum */ |
ab02e8a9 |
71 | public $ids; |
78cd27b3 |
72 | /** thread starts */ |
ab02e8a9 |
73 | public $roots; |
74 | |
78cd27b3 |
75 | |
76 | /** constructor |
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 | */ |
ab02e8a9 |
81 | protected function __construct($group) |
78cd27b3 |
82 | { |
ab02e8a9 |
83 | $this->version = BANANA_SPOOL_VERSION; |
7972645b |
84 | $this->mode = Banana::SPOOL_ALL; |
ab02e8a9 |
85 | $this->group = $group; |
86 | } |
9d18c838 |
87 | |
ab02e8a9 |
88 | public static function getSpool($group, $since = 0) |
89 | { |
90 | if (!is_null(Banana::$spool) && Banana::$spool->group == $group) { |
91 | $spool = Banana::$spool; |
78cd27b3 |
92 | } else { |
ab02e8a9 |
93 | $spool = BananaSpool::readFromFile($group); |
94 | } |
95 | if (is_null($spool)) { |
96 | $spool = new BananaSpool($group); |
78cd27b3 |
97 | } |
ab02e8a9 |
98 | Banana::$spool =& $spool; |
99 | $spool->build(); |
100 | $spool->updateUnread($since); |
101 | return $spool; |
102 | } |
78cd27b3 |
103 | |
ab02e8a9 |
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); |
78cd27b3 |
110 | } |
7972645b |
111 | return $file . Banana::$protocole->filename(); |
78cd27b3 |
112 | } |
113 | |
ab02e8a9 |
114 | private static function readFromFile($group) |
78cd27b3 |
115 | { |
ab02e8a9 |
116 | $file = BananaSpool::spoolFilename($group); |
117 | if (!file_exists($file)) { |
118 | return null; |
78cd27b3 |
119 | } |
ab02e8a9 |
120 | $spool = unserialize(file_get_contents($file)); |
7972645b |
121 | if ($spool->version != BANANA_SPOOL_VERSION || $spool->mode != Banana::SPOOL_ALL) { |
ab02e8a9 |
122 | return null; |
123 | } |
124 | return $spool; |
125 | } |
126 | |
127 | private function compare($a, $b) |
128 | { |
129 | return ($b->date >= $a->date); |
78cd27b3 |
130 | } |
131 | |
ab02e8a9 |
132 | private function saveToFile() |
78cd27b3 |
133 | { |
ab02e8a9 |
134 | $file = BananaSpool::spoolFilename($this->group); |
135 | uasort($this->overview, array($this, 'compare')); |
78cd27b3 |
136 | |
137 | $this->roots = Array(); |
138 | foreach($this->overview as $id=>$msg) { |
139 | if (is_null($msg->parent)) { |
140 | $this->roots[] = $id; |
141 | } |
142 | } |
01972ace |
143 | |
7972645b |
144 | if ($this->mode == Banana::SPOOL_ALL) { |
145 | file_put_contents($file, serialize($this)); |
146 | } |
78cd27b3 |
147 | } |
148 | |
ab02e8a9 |
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 | } |
7972645b |
160 | if (Banana::$spool_max && Banana::$spool_max < $msgnum) { |
161 | $first = $last - Banana::$spool_max; |
ab02e8a9 |
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) |
78cd27b3 |
175 | { |
ab02e8a9 |
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)) |
7972645b |
181 | || ($first > $last && $id < $first && $id > $last)) { |
ab02e8a9 |
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; |
78cd27b3 |
191 | } |
192 | |
ab02e8a9 |
193 | private function update(&$first, &$last, $msgnum) |
78cd27b3 |
194 | { |
ded3974d |
195 | if ($first > $last || !$msgnum) { |
ab02e8a9 |
196 | return false; |
78cd27b3 |
197 | } |
198 | |
ab02e8a9 |
199 | $messages =& Banana::$protocole->getMessageHeaders($first, $last, |
200 | array('Date', 'Subject', 'From', 'Message-ID', 'References', 'In-Reply-To')); |
78cd27b3 |
201 | |
ab02e8a9 |
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); |
78cd27b3 |
212 | } |
ab02e8a9 |
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)); |
78cd27b3 |
218 | |
ab02e8a9 |
219 | if (!is_null($p = $msg->parent)) { |
78cd27b3 |
220 | if (empty($this->overview[$p])) { |
ab02e8a9 |
221 | $this->overview[$p] = new BananaSpoolHead($messages[$p]); |
78cd27b3 |
222 | } |
223 | $this->overview[$p]->children[] = $id; |
224 | |
ab02e8a9 |
225 | while (!is_null($p)) { |
78cd27b3 |
226 | $this->overview[$p]->desc += $msg->desc; |
ab02e8a9 |
227 | if ($p != $this->overview[$p]->parent) { |
228 | $p = $this->overview[$p]->parent; |
229 | } else { |
230 | $p = null; |
231 | } |
78cd27b3 |
232 | } |
233 | } |
234 | } |
ab02e8a9 |
235 | Banana::$protocole->updateSpool($messages); |
236 | return true; |
78cd27b3 |
237 | } |
238 | |
ab02e8a9 |
239 | private function updateUnread($since) |
78cd27b3 |
240 | { |
ab02e8a9 |
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; |
ab02e8a9 |
259 | while (isset($id)) { |
260 | $this->overview[$id]->descunread ++; |
261 | $id = $this->overview[$id]->parent; |
78cd27b3 |
262 | } |
263 | } |
ab02e8a9 |
264 | } |
265 | } |
78cd27b3 |
266 | |
ab02e8a9 |
267 | public function setMode($mode) |
268 | { |
7972645b |
269 | $this->mode = $mode; |
ab02e8a9 |
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]); |
78cd27b3 |
276 | } |
277 | } |
ab02e8a9 |
278 | break; |
78cd27b3 |
279 | } |
280 | } |
281 | |
7972645b |
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 | |
78cd27b3 |
313 | /** kill post and childrens |
314 | * @param $_id MSGNUM of post |
315 | */ |
ab02e8a9 |
316 | private function killdesc($_id) |
78cd27b3 |
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]); |
324 | if (($msgid = array_search($_id, $this->ids)) !== false) { |
325 | unset($this->ids[$msgid]); |
326 | } |
327 | } |
328 | |
329 | /** delete a post from overview |
330 | * @param $_id MSGNUM of post |
331 | */ |
ab02e8a9 |
332 | public function delid($_id, $write = true) |
78cd27b3 |
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]); |
357 | $msgid = array_search($_id, $this->ids); |
358 | if ($msgid) { |
359 | unset($this->ids[$msgid]); |
360 | } |
361 | |
ab02e8a9 |
362 | if ($write) { |
363 | $this->saveToFile(); |
364 | } |
78cd27b3 |
365 | } |
366 | } |
367 | |
ab02e8a9 |
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 | |
78cd27b3 |
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 |
e2347ffc |
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. |
78cd27b3 |
398 | */ |
ab02e8a9 |
399 | private function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
78cd27b3 |
400 | { |
ab02e8a9 |
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)); |
78cd27b3 |
413 | } |
414 | |
ab02e8a9 |
415 | $overview =& $this->overview[$_id]; |
416 | if ($_index + $overview->desc < $_first || $_index > $_last) { |
417 | return ''; |
418 | } |
78cd27b3 |
419 | |
ab02e8a9 |
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)) { |
71d6c865 |
430 | $subject = _b_('(pas de sujet)'); |
431 | } |
e2347ffc |
432 | $link = null; |
433 | if (function_exists('hook_getSubject')) { |
434 | $link = hook_getSubject($subject); |
435 | } |
ab02e8a9 |
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)); |
78cd27b3 |
440 | } |
ab02e8a9 |
441 | $res .= ' ' . $subject . $link; |
442 | $res .= "</td>\n<td class='from'>" . BananaMessage::formatFrom($overview->from) . "</td>\n</tr>"; |
78cd27b3 |
443 | |
ab02e8a9 |
444 | if ($hc) { |
445 | return $res; |
446 | } |
78cd27b3 |
447 | } |
448 | |
449 | $_index ++; |
ab02e8a9 |
450 | $children = $overview->children; |
78cd27b3 |
451 | while ($child = array_shift($children)) { |
ab02e8a9 |
452 | $overview =& $this->overview[$child]; |
453 | if ($_index > $_last) { |
454 | return $res; |
455 | } |
456 | if ($_index + $overview->desc >= $_first) { |
78cd27b3 |
457 | if (sizeof($children)) { |
458 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
ab02e8a9 |
459 | $_pfx_end . ($overview->parent_direct ? $spfx_T : $spfx_Tnd), |
460 | $_pfx_end . $spfx_I, false); |
78cd27b3 |
461 | } else { |
462 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
ab02e8a9 |
463 | $_pfx_end . ($overview->parent_direct ? $spfx_L : $spfx_Lnd), |
464 | $_pfx_end . $spfx_e, false); |
78cd27b3 |
465 | } |
466 | } |
ab02e8a9 |
467 | $_index += $overview->desc; |
78cd27b3 |
468 | } |
469 | |
470 | return $res; |
471 | } |
472 | |
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 | */ |
ab02e8a9 |
478 | public function toHtml($first = 0, $overview = false) |
78cd27b3 |
479 | { |
ab02e8a9 |
480 | $res = ''; |
481 | |
482 | if (!$overview) { |
483 | $_first = $first; |
7972645b |
484 | $_last = $first + Banana::$spool_tmax - 1; |
ab02e8a9 |
485 | $_ref = null; |
2ba32e89 |
486 | } else { |
ab02e8a9 |
487 | $_ref = $this->getNdx($first); |
7972645b |
488 | $_last = $_ref + Banana::$spool_tafter; |
489 | $_first = $_ref - Banana::$spool_tbefore; |
ab02e8a9 |
490 | if ($_first < 0) { |
491 | $_last -= $_first; |
492 | } |
78cd27b3 |
493 | } |
78cd27b3 |
494 | $index = 1; |
ab02e8a9 |
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; |
78cd27b3 |
500 | } |
e230b41a |
501 | } |
ab02e8a9 |
502 | return $res; |
78cd27b3 |
503 | } |
504 | |
505 | /** computes linear post index |
506 | * @param $_id INTEGER MSGNUM of post |
507 | * @return INTEGER linear index of post |
508 | */ |
ab02e8a9 |
509 | public function getNdX($_id) |
78cd27b3 |
510 | { |
511 | $ndx = 1; |
512 | $id_cur = $_id; |
513 | while (true) { |
514 | $id_parent = $this->overview[$id_cur]->parent; |
515 | if (is_null($id_parent)) break; |
516 | $pos = array_search($id_cur, $this->overview[$id_parent]->children); |
517 | |
518 | for ($i = 0; $i < $pos ; $i++) { |
519 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
520 | } |
d8d416c4 |
521 | $ndx++; //noeud père |
78cd27b3 |
522 | |
523 | $id_cur = $id_parent; |
524 | } |
525 | |
526 | foreach ($this->roots as $i) { |
527 | if ($i==$id_cur) { |
528 | break; |
529 | } |
530 | $ndx += $this->overview[$i]->desc; |
531 | } |
532 | return $ndx; |
533 | } |
2ba32e89 |
534 | |
535 | /** Return root message of the given thread |
536 | * @param id INTEGER id of a message |
537 | */ |
ab02e8a9 |
538 | public function root($id) |
539 | { |
2ba32e89 |
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 | */ |
ab02e8a9 |
552 | public function prevThread($id) |
2ba32e89 |
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 | */ |
ab02e8a9 |
568 | public function nextThread($id) |
2ba32e89 |
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 | */ |
ab02e8a9 |
586 | public function prevPost($id) |
2ba32e89 |
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 | */ |
ab02e8a9 |
605 | public function nextPost($id) |
2ba32e89 |
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 | } |
dc12019c |
630 | |
631 | /** Look for an unread message in the thread rooted by the message |
632 | * @param id INTEGER message number |
633 | */ |
ab02e8a9 |
634 | private function _nextUnread($id) |
dc12019c |
635 | { |
636 | if (!$this->overview[$id]->isread) { |
637 | return $id; |
638 | } |
639 | foreach ($this->overview[$id]->children as $child) { |
6aa1f5e4 |
640 | $unread = $this->_nextUnread($child); |
641 | if (!is_null($unread)) { |
642 | return $unread; |
643 | } |
dc12019c |
644 | } |
645 | return null; |
646 | } |
647 | |
648 | /** Find next unread message |
649 | * @param id INTEGER message number |
650 | */ |
ab02e8a9 |
651 | public function nextUnread($id = null) |
dc12019c |
652 | { |
27db62eb |
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 | } |
dc12019c |
660 | } |
661 | } |
662 | |
663 | // Look in current thread |
664 | $cur = $id; |
27db62eb |
665 | do { |
666 | $parent = is_null($cur) ? null : $this->overview[$cur]->parent; |
667 | $ok = is_null($cur) ? true : false; |
a02dd64a |
668 | if (!is_null($parent)) { |
dc12019c |
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; |
27db62eb |
685 | } while(!is_null($cur)); |
dc12019c |
686 | return null; |
687 | } |
78cd27b3 |
688 | } |
689 | |
d8d416c4 |
690 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
78cd27b3 |
691 | ?> |