Wiki source for CategoryTree


Show raw source

=====CategoryTree=====
At the time only a dirty hack to show categories as a tree, initiated by LeOn, converted into a plugin by FaKn

The 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 [[http://www.net4lawyer.com/openlaw | here]].

**To do**:
1) Function to follow the navigation with the tree
If i open a page, that has an place in the tree, i would like to find it and show the tree open at the right place...
2) More precise documentation.

====Implementation for Wikka 1.2====

Save the following as plugins/actions/categorytree.php
%%(php;1)
<?php
/**
* Displays a tree of all categories.
* For more info, see http://wikkawiki.org/CategoryTree
**/

if (!function_exists('f_action_categorytree_get_tree')) {
function f_action_categorytree_get_tree ($wakka) {
$imgpath = $wakka->GetThemePath()."/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();
f_action_categorytree_get_level($wakka, 1, "CategoryCategory");
} else {
if (isset($_GET["active1"])) {
// call this recursive function for grab the tree
$_SESSION["tree"] = "";
$_SESSION["active"] = array();
f_action_categorytree_get_level($wakka, 1, "KategorieKategorie");
} else {
// get the tree from session variable
// nothing necessary - the Text is saved in the session variable
}
}
$txt .= $_SESSION["tree"]."</table>";
return $txt;
}

function f_action_categorytree_is_parent ($name) {
if (preg_match("/^Category(.*)/i", $name)) return true;
else return false;
}

function f_action_categorytree_get_level($wakka, $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 = $wakka->GetThemePath()."/images/";
$page = $parent;
if ($wakka->CheckMySQLVersion(4,0,1)) $results = $wakka->FullCategoryTextSearch($page);
else $results = $wakka->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 (f_action_categorytree_is_parent($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) {
// last empty element
$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 (f_action_categorytree_is_parent($val))) {
f_action_categorytree_get_level($wakka, $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;
}
}
print f_action_categorytree_get_tree($this);

?>%%

Create a new custom theme (if you have not done this yet) by copying e.g. the 'light' theme from the 'templates' folder into 'plugins/templates'.
Append the following at the end of header.php
%%(php)
<div id="tree"><?php echo $this->Action('categoriestree') ?></div>%%

Now you only need 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.

A simple addition to your copy of light.css is:
%%(css)
#tree {
border-right: 1px solid #DDD;
width: 200px;
padding: 10px 10px 10px 10px;
float: left;
}%%

Finally, switch to the new theme (either via your User settings or in the config, see http://docs.wikkawiki.org/WikkaThemes for more info).

====Implementation for Wikka 1.1.6.3====
This is the old code that works without plugins and templates:

replace the line 1336 in Wakka.Class.php
%%(php)
print($this->Header().$this->Method($this->method).$this->Footer());
%%
with this code:
%%(php)
$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):
%%(php;1)
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;
}
%%

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!
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki