Fixes deprecated features in PHP 5.3.x.
[banana.git] / banana / tree.inc.php
index 6db3c44..98c66f4 100644 (file)
@@ -8,7 +8,7 @@
 ********************************************************************************/
 
 
-define('BANANA_TREE_VERSION', '0.1');
+define('BANANA_TREE_VERSION', '0.1.2');
 
 /**
  * Class representing a thread tree
@@ -29,65 +29,51 @@ class BananaTree
 
     /** Data
      */
-    public $data;
+    public $data = array();
+
+    /** Data caching
+     */
+    private $urls = array();
+    private $title = array();
+
+    private $displaid = null;
 
     /** Construct a new tree from a given root
      */
-    public function __construct(BananaSpoolHead &$root)
+    public function __construct(BananaSpoolHead $root)
     {
         if (empty($root->children)) {
             $this->data = null;
         } else {
-            $tree =& $this->builder($root);
-            $this->data = '<div class="tree"><div style="height:18px">'
-                        . implode("</div>\n<div style=\"height:18px\">", $tree)
-                        . '</div></div>';
+            $this->data =& $this->builder($root);
         }
         $this->time = time();
         $this->version = BANANA_TREE_VERSION;
         $this->saveToFile($root->id);
     }
 
-    private function &builder(BananaSpoolHead &$head)
+    private function &builder(BananaSpoolHead $head)
     {
-        static $t_e, $u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f;
-        if (!isset($spfx_f)) {
-            $t_e   = Banana::$page->makeImg(Array('img' => 'e',  'alt' => '&nbsp;', 'height' => 18,  'width' => 14));
-            $u_h   = Banana::$page->makeImg(Array('img' => 'h2', 'alt' => '-', 'height' => 18,  'width' => 14));
-            $u_ht  = Banana::$page->makeImg(Array('img' => 'T2', 'alt' => '+', 'height' => 18, 'width' => 14));
-            $u_vt  = Banana::$page->makeImg(Array('img' => 't2', 'alt' => '`', 'height' => 18, 'width' => 14));
-            $u_l   = Banana::$page->makeImg(Array('img' => 'l2', 'alt' => '|', 'height' => 18, 'width' => 14));
-            $u_f   = Banana::$page->makeImg(Array('img' => 'f2', 'alt' => 't', 'height' => 18, 'width' => 14));
-            $r_h   = Banana::$page->makeImg(Array('img' => 'h2r', 'alt' => '-', 'height' => 18,  'width' => 14));
-            $r_ht  = Banana::$page->makeImg(Array('img' => 'T2r', 'alt' => '+', 'height' => 18, 'width' => 14));
-            $r_vt  = Banana::$page->makeImg(Array('img' => 't2r', 'alt' => '`', 'height' => 18, 'width' => 14));
-            $r_l   = Banana::$page->makeImg(Array('img' => 'l2r', 'alt' => '|', 'height' => 18, 'width' => 14));
-            $r_f   = Banana::$page->makeImg(Array('img' => 'f2r', 'alt' => 't', 'height' => 18, 'width' => 14));
-        }
-        $style = 'background-color:' . $head->color . '; text-decoration: none';
-        $text = '<span style="' . $style . '" title="' . banana_entities($head->name . ', ' . Banana::$spool->formatDate($head))
-              . '"><input type="radio" name="banana_tree" '
-              . (Banana::$msgshow_javascript ? 'onchange="window.location=\'' .
-                    banana_entities(Banana::$page->makeURL(array('group' => Banana::$spool->group, 'artid' => $head->id))) . '\'"'
-                    : ' disabled="disabled"')
-              . ' /></span>';
-        $array = array($text);
+        $array = array(array($head->id));
+        $this->urls[$head->id]  = banana_entities(Banana::$page->makeURL(array('group' => Banana::$group,
+                                                                               'artid' => $head->id)));
+        $this->title[$head->id] = banana_entities($head->name . ', ' . Banana::$spool->formatDate($head));
         foreach ($head->children as $key=>&$msg) {
             $tree =& $this->builder($msg);
             $last = $key == count($head->children) - 1;
             foreach ($tree as $kt=>&$line) {
                 if ($kt === 0 && $key === 0 && !$last) {
-                    $array[0] .= ($msg->isread ? $r_ht : $u_ht) . $line;
+                    $array[0] = array_merge($array[0], array(array('+', $msg->id)), $line);
                 } else if($kt === 0 && $key === 0) {
-                    $array[0] .= ($msg->isread ? $r_h : $u_h)  . $line;
+                    $array[0] = array_merge($array[0], array(array('-', $msg->id)), $line);
                 } else if ($kt === 0 && $last) {
-                    $array[] = $t_e . ($msg->isread ? $r_vt : $u_vt) . $line;
+                    $array[] = array_merge(array(' ', array('`', $msg->id)), $line);
                 } else if ($kt === 0) {
-                    $array[] = $t_e . ($msg->isread ? $r_f : $u_f) . $line;
+                    $array[] = array_merge(array(' ', array('t', $msg->id)), $line);
                 } else if ($last) {
-                    $array[] = $t_e . $t_e . $line;
+                    $array[] = array_merge(array(' ', ' '), $line);
                 } else {
-                    $array[] = $t_e . ($msg->isread ? $r_l : $u_l) . $line;
+                    $array[] = array_merge(array(' ', array('|', $head->children[$key+1]->id)), $line);
                 }
             }
             unset($tree);
@@ -102,18 +88,79 @@ class BananaTree
         file_put_contents(BananaTree::filename($id), serialize($this));
     }
 
+    /** Create a reference to a tree image.
+     */
+    static private function makeTreeImg($img, $alt)
+    {
+        return Banana::$page->makeImg(Array('img' => $img, 'alt' => $alt, 'height' => 18, 'width' => 14));
+    }
+
+    /** Add an entry to the static tree association table.
+     */
+    static private function addTreeKind(array& $tree, $ascii, $img)
+    {
+        $tree[$ascii] = array(self::makeTreeImg($img . Banana::$tree_unread, $ascii),
+                              self::makeTreeImg($img . Banana::$tree_read, $ascii));
+    }
+
     /** Return html to display the tree
      */
     public function &show()
     {
-        return $this->data;
+        if (!is_null($this->displaid) || is_null($this->data)) {
+            return $this->displaid;
+        }
+        static $t_e, $tree;
+        //$u_h, $u_ht, $u_vt, $u_l, $u_f, $r_h, $r_ht, $r_vt, $r_l, $r_f;
+        if (!isset($t_e)) {
+            $t_e   = self::makeTreeImg('e', ' ');
+            $tree  = array();
+            self::addTreeKind($tree, '+', 'p2');
+            self::addTreeKind($tree, '-', 'm2');
+            self::addTreeKind($tree, '|', 'l2');
+            self::addTreeKind($tree, '`', 'a2');
+            self::addTreeKind($tree, 't', 't2');
+        }
+        $text = '<div class="tree">';
+        foreach ($this->data as &$line) {
+            $text .= '<div style="height: 18px">';
+            foreach ($line as &$item) {
+                if ($item == ' ') {
+                    $text .= $t_e;
+                } else if (is_array($item)) {
+                    $head =& Banana::$spool->overview[$item[1]];
+                    $text .= $tree[$item[0]][$head->isread ? 1 : 0];
+                } else {
+                    $head =& Banana::$spool->overview[$item];
+                    $text .= '<span style="background-color: ' . $head->color . '; text-decoration: none" title="'
+                          .  $this->title[$item] . '"><input type="radio" name="banana_tree" value="' . $head->id . '"';
+                    if (Banana::$msgshow_javascript) {
+                        $text .= ' onchange="window.location=\'' . $this->urls[$item] . '\'"';
+                    } else {
+                        $text .= ' disabled="disabled"';
+                    }
+                    if (Banana::$artid == $item) {
+                        $text .= ' checked="checked"';
+                    }
+                    $text .= '/></span>';
+                }
+            }
+            $text .= "</div>\n";
+        }
+        $text .= '</div>';
+        $this->displaid =& $text;
+        return $text;
     }
 
     /** Get filename
      */
     static private function filename($id)
     {
-        return BananaSpool::getPath('tree_' . $id);
+        static $host;
+        if (!isset($host)) {
+            $host = parse_url(Banana::$page->makeURL(array()), PHP_URL_HOST);
+        }
+        return BananaSpool::getPath('tree_' . $id . '_' . $host);
     }
 
     /** Read a tree from a file