d3b026ab |
1 | <?php |
2 | /******************************************************************************** |
3 | * include/tree.inc.php : thread tree |
4 | * ----------------------- |
5 | * |
6 | * This file is part of the banana distribution |
7 | * Copyright: See COPYING files that comes with this distribution |
8 | ********************************************************************************/ |
9 | |
10 | |
11 | define('BANANA_TREE_VERSION', '0.1'); |
12 | |
13 | /** |
14 | * Class representing a thread tree |
15 | */ |
16 | class BananaTree |
17 | { |
18 | /** Tree cache |
19 | */ |
20 | static private $cache = array(); |
21 | |
22 | /** Tree format |
23 | */ |
24 | public $version; |
25 | |
26 | /** Last update timestamp |
27 | */ |
28 | public $time = 0; |
29 | |
30 | /** Data |
31 | */ |
32 | public $data; |
33 | |
34 | /** Construct a new tree from a given root |
35 | */ |
36 | public function __construct(BananaSpoolHead &$root) |
37 | { |
38 | if (empty($root->children)) { |
39 | $this->data = null; |
40 | } else { |
41 | $tree =& $this->builder($root); |
42 | $this->data = '<div class="tree"><div style="height:18px">' |
43 | . implode("</div>\n<div style=\"height:18px\">", $tree) |
44 | . '</div></div>'; |
45 | } |
46 | $this->time = time(); |
47 | $this->version = BANANA_TREE_VERSION; |
48 | $this->saveToFile($root->id); |
49 | } |
50 | |
51 | private function &builder(BananaSpoolHead &$head) |
52 | { |
53 | static $t_e, $u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f; |
54 | if (!isset($spfx_f)) { |
55 | $t_e = Banana::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 18, 'width' => 14)); |
56 | $u_h = Banana::$page->makeImg(Array('img' => 'h2', 'alt' => '-', 'height' => 18, 'width' => 14)); |
57 | $u_ht = Banana::$page->makeImg(Array('img' => 'T2', 'alt' => '+', 'height' => 18, 'width' => 14)); |
58 | $u_vt = Banana::$page->makeImg(Array('img' => 't2', 'alt' => '`', 'height' => 18, 'width' => 14)); |
59 | $u_l = Banana::$page->makeImg(Array('img' => 'l2', 'alt' => '|', 'height' => 18, 'width' => 14)); |
60 | $u_f = Banana::$page->makeImg(Array('img' => 'f2', 'alt' => 't', 'height' => 18, 'width' => 14)); |
61 | $r_h = Banana::$page->makeImg(Array('img' => 'h2r', 'alt' => '-', 'height' => 18, 'width' => 14)); |
62 | $r_ht = Banana::$page->makeImg(Array('img' => 'T2r', 'alt' => '+', 'height' => 18, 'width' => 14)); |
63 | $r_vt = Banana::$page->makeImg(Array('img' => 't2r', 'alt' => '`', 'height' => 18, 'width' => 14)); |
64 | $r_l = Banana::$page->makeImg(Array('img' => 'l2r', 'alt' => '|', 'height' => 18, 'width' => 14)); |
65 | $r_f = Banana::$page->makeImg(Array('img' => 'f2r', 'alt' => 't', 'height' => 18, 'width' => 14)); |
66 | } |
67 | $style = 'background-color:' . $head->color . '; text-decoration: none'; |
68 | $text = '<span style="' . $style . '" title="' . banana_entities($head->name . ', ' . Banana::$spool->formatDate($head)) |
69 | . '"><input type="radio" name="banana_tree" ' |
70 | . (Banana::$msgshow_javascript ? 'onchange="window.location=\'' . |
71 | banana_entities(Banana::$page->makeURL(array('group' => Banana::$spool->group, 'artid' => $head->id))) . '\'"' |
72 | : ' disabled="disabled"') |
73 | . ' /></span>'; |
74 | $array = array($text); |
75 | foreach ($head->children as $key=>&$msg) { |
76 | $tree =& $this->builder($msg); |
77 | $last = $key == count($head->children) - 1; |
78 | foreach ($tree as $kt=>&$line) { |
79 | if ($kt === 0 && $key === 0 && !$last) { |
80 | $array[0] .= ($msg->isread ? $r_ht : $u_ht) . $line; |
81 | } else if($kt === 0 && $key === 0) { |
82 | $array[0] .= ($msg->isread ? $r_h : $u_h) . $line; |
83 | } else if ($kt === 0 && $last) { |
84 | $array[] = $t_e . ($msg->isread ? $r_vt : $u_vt) . $line; |
85 | } else if ($kt === 0) { |
86 | $array[] = $t_e . ($msg->isread ? $r_f : $u_f) . $line; |
87 | } else if ($last) { |
88 | $array[] = $t_e . $t_e . $line; |
89 | } else { |
90 | $array[] = $t_e . ($msg->isread ? $r_l : $u_l) . $line; |
91 | } |
92 | } |
93 | unset($tree); |
94 | } |
95 | return $array; |
96 | } |
97 | |
98 | /** Save the content of the tree into a file |
99 | */ |
100 | private function saveToFile($id) |
101 | { |
102 | file_put_contents(BananaTree::filename($id), serialize($this)); |
103 | } |
104 | |
1d521296 |
105 | /** Return html to display the tree |
106 | */ |
107 | public function &show() |
108 | { |
109 | return $this->data; |
110 | } |
111 | |
d3b026ab |
112 | /** Get filename |
113 | */ |
114 | static private function filename($id) |
115 | { |
116 | return BananaSpool::getPath('tree_' . $id); |
117 | } |
118 | |
119 | /** Read a tree from a file |
120 | */ |
121 | static private function &readFromFile($id) |
122 | { |
123 | $tree = null; |
124 | $file = BananaTree::filename($id); |
125 | if (!file_exists($file)) { |
126 | return $tree; |
127 | } |
128 | $tree = unserialize(file_get_contents($file)); |
129 | if ($tree->version != BANANA_TREE_VERSION) { |
130 | $tree = null; |
131 | } |
132 | return $tree; |
133 | } |
134 | |
135 | /** Build a tree for the given id |
136 | */ |
137 | static public function &build($id) |
138 | { |
139 | $root =& Banana::$spool->root($id); |
140 | if (!isset(BananaTree::$cache[$root->id])) { |
141 | $tree =& BananaTree::readFromFile($root->id); |
142 | if (is_null($tree) || $tree->time < $root->time) { |
143 | $tree = new BananaTree($root); |
144 | } |
145 | BananaTree::$cache[$root->id] =& $tree; |
146 | } |
147 | return BananaTree::$cache[$root->id]; |
148 | } |
149 | |
150 | /** Kill the file associated to the given id |
151 | */ |
152 | static public function kill($id) |
153 | { |
154 | @unlink(BananaTree::filename($id)); |
155 | } |
156 | } |
157 | // vim:set et sw=4 sts=4 ts=4 enc=utf-8: |
158 | ?> |