4e6fd8ab81dce6d4c9e5a9a4d05a401894833a91
2 /********************************************************************************
3 * include/tree.inc.php : thread tree
4 * -----------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
11 define('BANANA_TREE_VERSION', '0.1.2');
14 * Class representing a thread tree
20 static private $cache = array();
26 /** Last update timestamp
32 public $data = array();
36 private $urls = array();
37 private $title = array();
39 private $displaid = null
;
41 /** Construct a new tree from a given root
43 public function __construct(BananaSpoolHead
&$root)
45 if (empty($root->children
)) {
48 $this->data
=& $this->builder($root);
51 $this->version
= BANANA_TREE_VERSION
;
52 $this->saveToFile($root->id
);
55 private function &builder(BananaSpoolHead
&$head)
57 $array = array(array($head->id
));
58 $this->urls
[$head->id
] = banana_entities(Banana
::$page->makeURL(array('group' => Banana
::$group,
59 'artid' => $head->id
)));
60 $this->title
[$head->id
] = banana_entities($head->name
. ', ' . Banana
::$spool->formatDate($head));
61 foreach ($head->children
as $key=>&$msg) {
62 $tree =& $this->builder($msg);
63 $last = $key == count($head->children
) - 1;
64 foreach ($tree as $kt=>&$line) {
65 if ($kt === 0 && $key === 0 && !$last) {
66 $array[0] = array_merge($array[0], array('+'), $line);
67 } else if($kt === 0 && $key === 0) {
68 $array[0] = array_merge($array[0], array('-'), $line);
69 } else if ($kt === 0 && $last) {
70 $array[] = array_merge(array(' ', '`'), $line);
71 } else if ($kt === 0) {
72 $array[] = array_merge(array(' ', 't'), $line);
74 $array[] = array_merge(array(' ', ' '), $line);
76 $array[] = array_merge(array(' ', '|'), $line);
84 /** Save the content of the tree into a file
86 private function saveToFile($id)
88 file_put_contents(BananaTree
::filename($id), serialize($this));
91 /** Return html to display the tree
93 public function &show()
95 if (!is_null($this->displaid
)) {
96 return $this->displaid
;
98 static $t_e, $u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f;
100 $t_e = Banana
::$page->makeImg(Array('img' => 'e', 'alt' => ' ', 'height' => 18, 'width' => 14));
101 $u_h = Banana
::$page->makeImg(Array('img' => 'h2', 'alt' => '-', 'height' => 18, 'width' => 14));
102 $u_ht = Banana
::$page->makeImg(Array('img' => 'T2', 'alt' => '+', 'height' => 18, 'width' => 14));
103 $u_vt = Banana
::$page->makeImg(Array('img' => 't2', 'alt' => '`', 'height' => 18, 'width' => 14));
104 $u_l = Banana
::$page->makeImg(Array('img' => 'l2', 'alt' => '|', 'height' => 18, 'width' => 14));
105 $u_f = Banana
::$page->makeImg(Array('img' => 'f2', 'alt' => 't', 'height' => 18, 'width' => 14));
106 $r_h = Banana
::$page->makeImg(Array('img' => 'h2r', 'alt' => '-', 'height' => 18, 'width' => 14));
107 $r_ht = Banana
::$page->makeImg(Array('img' => 'T2r', 'alt' => '+', 'height' => 18, 'width' => 14));
108 $r_vt = Banana
::$page->makeImg(Array('img' => 't2r', 'alt' => '`', 'height' => 18, 'width' => 14));
109 $r_l = Banana
::$page->makeImg(Array('img' => 'l2r', 'alt' => '|', 'height' => 18, 'width' => 14));
110 $r_f = Banana
::$page->makeImg(Array('img' => 'f2r', 'alt' => 't', 'height' => 18, 'width' => 14));
112 $text = '<div class="tree">';
113 foreach ($this->data
as &$line) {
114 $text .= '<div style="height: 18px">';
115 foreach ($line as &$item) {
117 case ' ': $text .= $t_e; break;
118 case '+': $text .= $u_ht; break;
119 case '-': $text .= $u_h; break;
120 case '|': $text .= $u_l; break;
121 case '`': $text .= $u_vt; break;
122 case 't': $text .= $u_f; break;
124 $head =& Banana
::$spool->overview
[$item];
125 $text .= '<span style="background-color:' . $head->color
. '; text-decoration: none"'
126 . ' title="' . $this->title
[$item] . '">'
127 . '<input type="radio" name="banana_tree" value="' . $head->id
. '"';
128 if (Banana
::$msgshow_javascript) {
129 $text .= ' onchange="window.location=\'' . $this->urls
[$item] . '\'"';
131 $text .= ' disabled="disabled"';
133 if (Banana
::$artid == $item) {
134 $text .= ' checked="checked"';
136 $text .= '/></span>';
142 $this->displaid
=& $text;
148 static private function filename($id)
150 return BananaSpool
::getPath('tree_' . $id);
153 /** Read a tree from a file
155 static private function &readFromFile($id)
158 $file = BananaTree
::filename($id);
159 if (!file_exists($file)) {
162 $tree = unserialize(file_get_contents($file));
163 if ($tree->version
!= BANANA_TREE_VERSION
) {
169 /** Build a tree for the given id
171 static public function &build($id)
173 $root =& Banana
::$spool->root($id);
174 if (!isset(BananaTree
::$cache[$root->id
])) {
175 $tree =& BananaTree
::readFromFile($root->id
);
176 if (is_null($tree) ||
$tree->time
< $root->time
) {
177 $tree = new BananaTree($root);
179 BananaTree
::$cache[$root->id
] =& $tree;
181 return BananaTree
::$cache[$root->id
];
184 /** Kill the file associated to the given id
186 static public function kill($id)
188 @unlink
(BananaTree
::filename($id));
191 // vim:set et sw=4 sts=4 ts=4 enc=utf-8: