Revision [17845]
This is an old revision of CategoryTree made by LeOn on 2007-12-17 07:37:49.
CategoryTree
At the time only a dirty hack to show categories as a tree, initiated by LeOnThe plain structure on Wikis allows to generate a lot of connected pages fast and simple. The navigation in the Wiki is as simple as using internet - with links. But after 5-6 steps I cannot remember, why I'm at the actually opened page. For me it is helpful to have an hierarchical tree with the structure of the Wiki. The idea was also to use the Category-system of Wikka (CategoryCategory and so on) to automatically generated tree.
The results are presented here. At the time I call it "dirty hack", because it doesn't match the idea of WikkaWiki to use plugins / actions for Wikka expansions. Maybe somebody could help to make this hack better (and become to be a nice action).
I will describe everything more precisely. As a start I put here only the code (all comments and instructions based on Wikka 1.1.6.3.):
replace the line 1336 in Wakka.Class.php
with this code:
$lBody = "
<table border='0' width='100%'>
<tr valign='top'>
<td width='200px'>".$this->getTree()."</td>
<td width='*'>".$this->Method($this->method).$this->Footer()."</td>
</tr>
</table>";
print($this->Header().$lBody);
<table border='0' width='100%'>
<tr valign='top'>
<td width='200px'>".$this->getTree()."</td>
<td width='*'>".$this->Method($this->method).$this->Footer()."</td>
</tr>
</table>";
print($this->Header().$lBody);
Then add following functions to the Wakka-class (in the same file Wakka.Class.php):
function getTree () {
$imgpath = "images/";
$txt = "<table cellspacing='0' cellpadding='0' border='0' width='100%'>";
$txt .= "<tr valign=top><td colspan='3' style='font-size: 12px' nowrap>";
$txt .= "<img src='".$imgpath."ksiazka_otw.gif' hspace=0 vspace=0 border=0 align=left>";
$txt .= " <b>Proszę wybrać kategorię:</b></td></tr>";
$txt .= "<tr valign=top><td height='4'></td></tr>";
if (!(isset($_SESSION["tree"]))) {
$_SESSION["active"] = array();
$this->getLevel(1, "CategoryCategory");
} else {
if (isset($_GET["active1"])) {
// call this recursive function for grab the tree
$_SESSION["tree"] = "";
$_SESSION["active"] = array();
$this->getLevel(1, "CategoryCategory");
} else {
// get the tree from session variable
// nothing necessary - the Text is saved in the session variable
}
}
$txt .= $_SESSION["tree"]."</table>";
return $txt;
}
function isParent ($name) {
if (preg_match("/^Category(.*)/i", $name)) return true;
else return false;
}
function getLevel($level, $parent = "CategoryCategory") {
// little function for checking, if a category member is a category
// or not (then it is an simple page)
$txt = $_SESSION["tree"]; // the text is saved in the session variable "tree"
$active = $_SESSION["active"];
$last = $_SESSION["last"];
$imgpath = "images/";
$page = $parent;
if ($this->CheckMySQLVersion(4,0,1)) $results = $this->FullCategoryTextSearch($page);
else $results = $this->FullTextSearch($page);
if ($results) {
//$txt .= "<table cellspacing='0' cellpadding='0' border='0' width='100%'>";
$count = 0;
$pagecount = 0;
$list = array();
$active[$level] = $_GET["active".$level];
foreach ($results as $i => $cpage) {
if($cpage['tag'] != $page) {
array_push($list,$cpage['tag']);
}
}
sort($list);
while (list($key, $val) = each($list)) {
//echo $key."; ".$val;
$count++;
$addin = "";
// control about the "addin" = pictures before the element
// in the tree - on levels under the first one
for ($x = 1; $x < $level; $x = $x+1) {
if ($last[$x] == 1) {//count($list)) {
$addin .= "<img src='".$imgpath."pusty_for_last.gif' hspace='0' vspace='0' border='0' align='left'>";
} else {
$addin .= "<img src='".$imgpath."menu_bar.gif' hspace='0' vspace='0' border='0' align='left'>";
}
}
$title = $val;
$nazwa = $val;
// preparing links for the tree elements
$myhref = "wakka=".$val;
for ($y = 1; $y <= $level; $y = $y+1) {
//echo $active[$y].";";
if ($y == $level) {
$myhref .= "&active".$y."=".$val;
} else {
$myhref .= "&active".$y."=".$active[$y];
}
}
if ($this->isParent($val)) { // if element has children
if ($count == count($list)) { // if last element on this level
if ($active[$level] == $val) {
$pic1 = "menu_corner_minus";
$pic2 = "ksiazka_otw";
} else {
$pic1 = "menu_corner_plus";
$pic2 = "ksiazka_zamk";
}
$last[$level] = 1;
} else {
if ($active[$level] == $val) {
$pic1 = "menu_tee_minus";
$pic2 = "ksiazka_otw";
} else {
$pic1 = "menu_tee_plus";
$pic2 = "ksiazka_zamk";
}
$last[$level] = 0;
}
} else {
// this elements doesn't have children (= categories without subcategories)
if (count($list) == $key+1) {
// ostatni pusty
$pic1 = "menu_corner";
$pic2 = "stronica";
} else {
// not last, regular tree element
$pic1 = "menu_tee";
$pic2 = "stronica";
}
}
$myA = "<a style='font-size: 12px; font-family: Arial;' href='wikka.php?".$myhref."' title='".$title."'>";
$txt .= "<tr valign='top'><td class='tree' nowrap>".$addin;
$txt .= $myA."<img src='".$imgpath.$pic1.".gif' hspace='0' vspace='0' border='0' align='left'></a>";
$txt .= $myA."<img src='".$imgpath.$pic2.".gif' hspace='0' vspace='0' border='0' align='left'></a> ";
if ($val == $_GET["wakka"]) {
$txt .= " <b>".$myA.$nazwa."</a></b>";
} else {
$txt .= " ".$myA.$nazwa."</a>";
}
$txt .= "</td></tr>";
// read next level, if necessary - but before that put some
// variables into session variables
$_SESSION["tree"] = $txt;
$_SESSION["last"] = $last;
$_SESSION["active"] = $active;
if (($val == $active[$level]) and ($this->isParent($val))) {
$this->getLevel($level+1, $active[$level]);
}
$active = $_SESSION["active"];
$txt = $_SESSION["tree"];
$last = $_SESSION["last"];
$pagecount++;
}
} else $txt .= 'Sorry, no items found for ' . $page .'.';
$_SESSION["last"] = $last;
$_SESSION["active"] = $active;
$_SESSION["tree"] = $txt;
}
$imgpath = "images/";
$txt = "<table cellspacing='0' cellpadding='0' border='0' width='100%'>";
$txt .= "<tr valign=top><td colspan='3' style='font-size: 12px' nowrap>";
$txt .= "<img src='".$imgpath."ksiazka_otw.gif' hspace=0 vspace=0 border=0 align=left>";
$txt .= " <b>Proszę wybrać kategorię:</b></td></tr>";
$txt .= "<tr valign=top><td height='4'></td></tr>";
if (!(isset($_SESSION["tree"]))) {
$_SESSION["active"] = array();
$this->getLevel(1, "CategoryCategory");
} else {
if (isset($_GET["active1"])) {
// call this recursive function for grab the tree
$_SESSION["tree"] = "";
$_SESSION["active"] = array();
$this->getLevel(1, "CategoryCategory");
} else {
// get the tree from session variable
// nothing necessary - the Text is saved in the session variable
}
}
$txt .= $_SESSION["tree"]."</table>";
return $txt;
}
function isParent ($name) {
if (preg_match("/^Category(.*)/i", $name)) return true;
else return false;
}
function getLevel($level, $parent = "CategoryCategory") {
// little function for checking, if a category member is a category
// or not (then it is an simple page)
$txt = $_SESSION["tree"]; // the text is saved in the session variable "tree"
$active = $_SESSION["active"];
$last = $_SESSION["last"];
$imgpath = "images/";
$page = $parent;
if ($this->CheckMySQLVersion(4,0,1)) $results = $this->FullCategoryTextSearch($page);
else $results = $this->FullTextSearch($page);
if ($results) {
//$txt .= "<table cellspacing='0' cellpadding='0' border='0' width='100%'>";
$count = 0;
$pagecount = 0;
$list = array();
$active[$level] = $_GET["active".$level];
foreach ($results as $i => $cpage) {
if($cpage['tag'] != $page) {
array_push($list,$cpage['tag']);
}
}
sort($list);
while (list($key, $val) = each($list)) {
//echo $key."; ".$val;
$count++;
$addin = "";
// control about the "addin" = pictures before the element
// in the tree - on levels under the first one
for ($x = 1; $x < $level; $x = $x+1) {
if ($last[$x] == 1) {//count($list)) {
$addin .= "<img src='".$imgpath."pusty_for_last.gif' hspace='0' vspace='0' border='0' align='left'>";
} else {
$addin .= "<img src='".$imgpath."menu_bar.gif' hspace='0' vspace='0' border='0' align='left'>";
}
}
$title = $val;
$nazwa = $val;
// preparing links for the tree elements
$myhref = "wakka=".$val;
for ($y = 1; $y <= $level; $y = $y+1) {
//echo $active[$y].";";
if ($y == $level) {
$myhref .= "&active".$y."=".$val;
} else {
$myhref .= "&active".$y."=".$active[$y];
}
}
if ($this->isParent($val)) { // if element has children
if ($count == count($list)) { // if last element on this level
if ($active[$level] == $val) {
$pic1 = "menu_corner_minus";
$pic2 = "ksiazka_otw";
} else {
$pic1 = "menu_corner_plus";
$pic2 = "ksiazka_zamk";
}
$last[$level] = 1;
} else {
if ($active[$level] == $val) {
$pic1 = "menu_tee_minus";
$pic2 = "ksiazka_otw";
} else {
$pic1 = "menu_tee_plus";
$pic2 = "ksiazka_zamk";
}
$last[$level] = 0;
}
} else {
// this elements doesn't have children (= categories without subcategories)
if (count($list) == $key+1) {
// ostatni pusty
$pic1 = "menu_corner";
$pic2 = "stronica";
} else {
// not last, regular tree element
$pic1 = "menu_tee";
$pic2 = "stronica";
}
}
$myA = "<a style='font-size: 12px; font-family: Arial;' href='wikka.php?".$myhref."' title='".$title."'>";
$txt .= "<tr valign='top'><td class='tree' nowrap>".$addin;
$txt .= $myA."<img src='".$imgpath.$pic1.".gif' hspace='0' vspace='0' border='0' align='left'></a>";
$txt .= $myA."<img src='".$imgpath.$pic2.".gif' hspace='0' vspace='0' border='0' align='left'></a> ";
if ($val == $_GET["wakka"]) {
$txt .= " <b>".$myA.$nazwa."</a></b>";
} else {
$txt .= " ".$myA.$nazwa."</a>";
}
$txt .= "</td></tr>";
// read next level, if necessary - but before that put some
// variables into session variables
$_SESSION["tree"] = $txt;
$_SESSION["last"] = $last;
$_SESSION["active"] = $active;
if (($val == $active[$level]) and ($this->isParent($val))) {
$this->getLevel($level+1, $active[$level]);
}
$active = $_SESSION["active"];
$txt = $_SESSION["tree"];
$last = $_SESSION["last"];
$pagecount++;
}
} else $txt .= 'Sorry, no items found for ' . $page .'.';
$_SESSION["last"] = $last;
$_SESSION["active"] = $active;
$_SESSION["tree"] = $txt;
}
Now you need only some images (for example you can download all needed files here: http://www.polskieustawy.com/pics/tree/) and manipulations on wikka.css to see the right tree generated from your categories.
To be continued!