# -----------------------------------------------------------------------------
#
#   Copyright (c) 1999-2002 by Klaus Reger <K.Reger@wwwdb.org>
#
#   You may distribute under the terms of either the GNU General Public
#   License or the Artistic License, as specified in the Perl README file.
#
#   THIS IS BETA SOFTWARE!
#
#   THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
#   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
#   WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
#   $Id: SiteMap.pl,v 1.1 2002/04/05 13:01:15 k_reger Exp $
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: SiteMap.pl,v $
# Version  : $Revision: 1.1 $
# Date     : $Date: 2002/04/05 13:01:15 $
# State    : $State: Exp $
#
# Description:
#   Generate a site-map, using the categories and objects in the
#   database
#
# -----------------------------------------------------------------------------
# Change-log (See at end of this file)
# $Log: SiteMap.pl,v $
# Revision 1.1  2002/04/05 13:01:15  k_reger
# - New site-map
#
#
# --- Known Bugs -------------
# 
# --- TODO -------------------
# - pass parameters for categories only
# ----------------------------------------------------------------------------

use WWWdb::DbSession;
use vars qw($oDbSessionGL %hParam);

use strict;
# use diagnostics;


sub PreCreateForm ()
{
   my $cUrlParams = &GetAttr("UrlParams");
   my @lUrlParams = split ";", $cUrlParams;

   
   $hParam{"no-obj"}  = 0;
   $hParam{"all-obj"} = 0;
   $hParam{"all-cat"} = 0;


   foreach (@lUrlParams)
   {
	   $hParam{"no-obj"} = 1
		   if (/^no-?obj/);

	   $hParam{"all-obj"} = 1
		   if (/^all-?obj/ );

	   $hParam{"all-cat"} = 1
		   if (/^all-?cat/ );	   
   }

   &MySetVal("Layout Label links", "Text", &GenSiteMap());

   return;

}

sub GenSiteMap($) 
{
	my $plObj    = ();
	my $plCat    = ();
	my $plObjCat = ();
	my @lFields;
	my $cResult = "";
	my $iTopCat;

	$plObj = $oDbSessionGL->SqlSelect
		("SELECT DISTINCT
              obj.ref_link,
              obj.descript,
              obj.sort_nr,
              obj.attribs,
              tra.trans_text as tra_description,
              obj.id_object
          FROM
              wwwdb_object   obj,
              wwwdb_translation  tra
          WHERE
              obj.descript_txt_id = tra.id_text AND
              tra.lang            = ?
          ORDER BY
              obj.sort_nr",
		 &GetAttr("Lang"))
			unless $hParam{"no-obj"};

	$plCat = $oDbSessionGL->SqlSelect
		("SELECT
			  cat.id_category,
			  cat.id_cat_of_cat,
			  cat.name,
			  cat.id_chain,
			  cat.name_chain,
			  cat.sort_nr,
			  cat.attribs,
			  tr1.trans_text   AS name_tra,
			  tr2.trans_text   AS name_chain_tra,
			  tr3.trans_text   AS tra_description
			FROM
			  wwwdb_category    cat,
			  wwwdb_translation tr1,
			  wwwdb_translation tr2,
			  wwwdb_translation tr3
			WHERE
			  cat.name_txt_id        = tr1.id_text AND
			  cat.name_chain_txt_id  = tr2.id_text AND
			  cat.description_txt_id = tr3.id_text AND
			  tr1.lang               = ?           AND
			  tr2.lang               = tr1.lang    AND
			  tr3.lang               = tr1.lang
			ORDER BY
			  cat.id_cat_of_cat,
			  cat.sort_nr",
		 &GetAttr("Lang"));

	$plObjCat = $oDbSessionGL->SqlSelect
		("SELECT id_category, id_object 
          FROM   wwwdb_obj_cat
          ORDER BY id_category ASC, id_object ASC")
			unless $hParam{"no-obj"};
	
	# find out topmost category
	foreach (@$plCat)
	{
		my $lFields;
		
		@lFields = @$_;

		unless (defined $lFields[1])
		{
			$iTopCat = $lFields[0];
		}
	}
	
	# generate the site-map
	$cResult .= &ListCat(0,
						 $iTopCat,
						 $plCat,
						 $plObj,
						 $plObjCat);
	return $cResult;
}


# recursiveley generate the site-map
sub ListCat($$@@@)
{
	my $iLevelPI    = shift;
	my $iCategoryPI = shift;
	my $lCatPI      = shift;
	my $lObjPI      = shift;
	my $lObjCatPI   = shift;

	my $cResult;

	# scan all categories
	foreach (@{$lCatPI})
	{
		my @lFields = @$_;
		my $iCategory = $lFields[0];

		# is $iCategoryPI the parent of current record
		if ($lFields[1] == $iCategoryPI)
		{

			# check permissions
			next
				if(!&IsAttribOK($lFields[6],
								"IsHidden" => 0) &&
				   !$hParam{"all-cat"});

			# generate entry
			$cResult .= b("&nbsp" x (3 * $iLevelPI) .
						  &ResolveRefField(sprintf("wwwdb://WWWdb:Nav;cat=%s",
												   $lFields[0]),
										   $lFields[9]) .
						  br());

			# link the objects and categories together
			foreach (@{$lObjCatPI}) # id_category, id_object
			{
				my @lFields = @$_;
				my $iIdObject = $lFields[1];

				# skip all other categories
				next if
					$lFields[0] != $iCategory;


				# find matching object
				foreach (@{$lObjPI})
				{
					my @lFields = @$_;

					next if 
						$iIdObject != $lFields[5];

					# check permissions
					next
						if(!&IsAttribOK($lFields[3],
										"IsHidden" => 0) &&
						   !$hParam{"all-obj"});
					
					$lFields[1] = $lFields[4]
						unless ($lFields[4] eq "?");

					$cResult .= ("&nbsp" x (3 * $iLevelPI + 1) . " - " .
								 &ResolveRefField($lFields[0], $lFields[1]) .
								 br());
				}
				
			}

			# step into subcategories
			$cResult .= &ListCat($iLevelPI + 1,
								 $lFields[0],
								 $lCatPI,
								 $lObjPI,
								 $lObjCatPI);
		}
	}
	
	return $cResult;
}

1;
