X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=assistants%2Fcategory-tree.inc.php;h=05d426d232c8c253b288bc6b91ddbd89265c6bd5;hb=8044dfef548431efad4a624dba1fa0f3b85802af;hp=f268ca774582abc8f482f49c5046ea40e885d608;hpb=661e298830a700272104783b049367bf505bff62;p=mirrors%2FSokoMan.git diff --git a/assistants/category-tree.inc.php b/assistants/category-tree.inc.php index f268ca7..05d426d 100644 --- a/assistants/category-tree.inc.php +++ b/assistants/category-tree.inc.php @@ -1,19 +1,45 @@
db->safe_query_fetch('SELECT * FROM category');
 
-function addleaf(&$tree, $levels, $category='') {
-	if(!count($levels)) return;
-	$current = array_shift($levels);
+function addleaf(&$tree, $levels, $id, $category='') {
+	if(!count($levels)) {
+		$tree['__ID__'] = $id;
+		return;
+	}
+	$current = array_shift($levels); //echo("$current ".count($levels)."");
 	$category .= '/'.$current;
 	if(!isset($tree[$current])) $tree[$current] = array();
 	$tree[$current]['__PATH__'] = $category;
-	addleaf($tree[$current], $levels, $category);
+	//if(!count($levels)) $tree[$current]['__ID__'] = $id; //echo "($current $id)\n";
+	addleaf($tree[$current], $levels, $id, $category);
 }
 
 $tree = array();
 foreach($result as $row) {
 	$row_parts = preg_split('/\//', $row['category_name']);
-	addleaf($tree, $row_parts);
+	//echo($row['category_name'].$row['category_id']."\n");
+	addleaf($tree, $row_parts, $row['category_id']);
 }
 
-print_r($tree);
+function render_tree($tree, $index_path='__PATH__', $index_id='__ID__') {
+	if(!is_array($tree)) return '';
+	$html='';
+		foreach($tree as $name => $subtree) if($name != $index_path && $name != $index_id) {
+			$link = isset($subtree[$index_id]);
+			$hidden = $link && $subtree[$index_id] <= 0;
+
+			$html.='
  • '; + if($link) $html.= ''; + if($hidden) $html.=''; + $html.="$name"; + if($hidden) $html.=''; + if($link) $html.= ''; + @$html.=' ('.$subtree[$index_id].' => '.$subtree[$index_path].')'; + $html.=render_tree($subtree); + $html.='
  • '; + } + $html.='
    '; + return $html; +} +echo render_tree($tree); +//print_r($tree);