# FILE: treeview.pl
# DESCRIPTION: User Interface "Tree View"
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);

###
### tree_control
###
### Generates a tree view for the user interface tree view script
###

sub tree_control {
	my $FORMref = defined $_[0] ? $_[0] : parse_form($ENV{'QUERY_STRING'}, $ENV{'CONTENT_LENGTH'});
	my $cookie_string = $_[1];
	dreq("template");
	my $subst = {};
	my $tr = read_tree(undef, { no_lock => 1, no_unlock => 1, zero_ok => 1 });
	if ($DCONF->{pro}) {
		dreq("authwrap-PRO");
		user_utility_login_check($FORMref);		
		($tr, $cookie_string) = tree_list_simple_prune($tr, $FORMref);
	}
	$subst->{topic_tree} = $tr;
	if ($FORMref->{st} =~ m|^(\d+)$| && $FORMref->{x} eq "e") {
		my $s = read_tree($1);
		my @s = ();
		foreach my $ss (@{ $s }) {
			push @s, $ss if ($GLOBAL_OPTIONS->{skip_archives_tree} == 0 || $ss->{param} !~ m|Archive|);
		}
		$subst->{subtopic_tree} = \@s;
	} else {
		$FORMref->{st} = "";
	}
	undef my $par;
	foreach my $y (@{ $subst->{subtopic_tree} }) {
		$par->{ $y->{parent} } = $y->{page};
	}
	foreach my $yy (@{ $subst->{subtopic_tree} }) {
		$yy->{last_of_parent} = 1 if $par->{ $yy->{parent} } == $yy->{page};
	}
	foreach my $z (@{ $subst->{topic_tree} }) {
		$z->{expanded} = 1 if $FORMref->{st} == $z->{page};
	}
	$subst->{general}->{expand_url} = "$DCONF->{script_url}/board-viewtree.$DCONF->{cgi_extension}";
	$subst->{general}->{link_to_page} = 1;
	foreach my $o (@{ $subst->{topic_tree} }) {
		$subst->{secure}->{$o->{topic}} = -e "$DCONF->{secdir}/$o->{topic}";
	}
	$DCONF->{html_url} =~ m%^(https?://.*?)(/|$)%; my $html_server = $1; my $html_after = join("", $2, $');
	$DCONF->{message_url} =~ m%^(https?://.*?)(/|$)%; my $message_server = $1; my $message_after = join("", $2, $');
	$DCONF->{script_url} =~ m%^(https?://.*?)(/|$)%; my $script_server = $1; my $script_after = join("", $2, $');
	if ($script_server eq $html_server) {		
		$subst->{general}->{icon_url} = join("/", $html_after, $DCONF->{icon_dir});
		$subst->{general}->{script_url} = $script_after;
	} else {
		$subst->{general}->{icon_url} = join("/", $DCONF->{html_url}, $DCONF->{icon_dir});
		$subst->{general}->{script_url} = $DCONF->{script_url};
	}
	$subst->{general}->{message_url} = $message_server eq $script_server ? $message_after : $DCONF->{message_url};
	screen_out("treeview", $subst, $cookie_string);
}

1;
