package WebBack::Selector::LocalDirectory;

use strict;
use Digest::MD5 qw(md5 md5_base64);

BEGIN {
   use Exporter ();
   our ( $VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS );

   $VERSION 	= 0.05;
   @ISA		= qw(Exporter);
   @EXPORT	= ( );
   %EXPORT_TAGS = ( );

   @EXPORT_OK   = ( );
}

our @EXPORT_OK;

sub new
{
	my $type = shift;
	my $request = shift;

	my $self = bless { @_ };

	$self->{URL} = $request->url( -relative => 1 );
	$self->{mode} = $request->param( "mode" );

	$self->{dirs_only} = 1;

 	if ( defined $request->param('already_expanded') )
	{
		my @expanded = split /,/, $request->param('already_expanded');
		$self->{expanded} = \@expanded;
	} else {
		my @expanded = ();
		$self->{expanded} = \@expanded;
	}

	if ( defined ( $request->param('already_selected') ) )
	{
		my @selected = split /,/, $request->param('already_selected');
		$self->{selected} = \@selected;
	} else {
		my @selected = ();
		$self->{selected} = \@selected;
	}

	return $self;
}

sub get_directory
{
	my $self = shift;
	my $dir_to_list = shift;
	if ( ! defined $dir_to_list ) 
	{ return (); }

	opendir(DIR, $dir_to_list) || die "Couldn't opendir $dir_to_list.";
	my @dir_listing = readdir(DIR);
	closedir(DIR);

	return @dir_listing;
}

sub show_directory
{
	my $self = shift;
	my $cdir = shift;
	my $aref = shift;
	my @expanded = @$aref;
	   $aref = shift;
	my @selected = @$aref;
	
	if ( ! ($cdir =~ /\/$/) )
		{ $cdir .= "/";	}

	my @current_dir = $self->get_directory( $cdir );
	@current_dir = sort( @current_dir );

	print "<UL TYPE=NONE>\n";

	if ( scalar @current_dir == 2 )
	{ print "<LI>Empty Directory<BR>"; } 

	foreach my $dirent ( @current_dir ) 
	{
	    if ( ! ( $dirent =~ /^\.+$/ ) ) {
		my $dirmd5 = md5_base64("$cdir$dirent");
		if ( -d "$cdir$dirent" ) {
 			print "<LI>";
			print "<INPUT TYPE=CHECKBOX NAME=SELECTED VALUE=\"$dirmd5\" ";
			print "OnChange=\"document.dir_tree.submit();\"> ";
			if ( grep( /\Q$dirmd5\E/, @expanded ) )
			{
				print "<IMG SRC=\"contract.png\" BORDER=0 ";
				print "OnClick=\"document.dir_tree.expand.value=\'---" . $dirmd5 . "\';";
				print "document.dir_tree.submit();\"> ";
				print $cdir . $dirent . "<BR>\n";
				$self->show_directory( "$cdir$dirent", \@expanded, \@selected );
			} else {
				print "<IMG SRC=\"expand.png\" BORDER=0 ";
				print "OnClick=\"document.dir_tree.expand.value=\'" . $dirmd5 . "\';";
				print "document.dir_tree.submit();\"> ";
				print $cdir . $dirent . "<BR>\n";
			}
		} else {
			if ( $self->{dirs_only} != 1 ) {
 				print "<LI>";
				print "<INPUT TYPE=CHECKBOX NAME=SELECTED VALUE=\"$dirmd5\" ";
				print "OnChange=\"document.dir_tree.submit();\"> ";
				print "<IMG SRC=\"11x11trans.png\" BORDER=0> ";
				print $cdir . $dirent . "<BR>\n";
			}
		}
	    }
	}
 
	print "</UL>\n";
}		

sub update
{
	my $self = shift;
	my $request = shift;
	my $to_expand = $request->param('expand');

	if ( defined $to_expand )
	{
		if ( $to_expand ne "" )
		{
			if ( $to_expand =~ /^---/ ) {
				$to_expand =~ s/^---//;
				my @expanded = grep( !/\Q$to_expand\E/, @{$self->{expanded}} );
				$self->{expanded} = \@expanded;
			} else {
				push @{$self->{expanded}}, $request->param('expand');
			}
		}
	}
}


sub show
{
	my $self = shift;
	my $colors = shift;

	print "<HTML>\n";
	print "<HEAD><TITLE>File Selector</TITLE></HEAD>\n";
	print "<BODY BGCOLOR=\"" . $colors->{bgcolor} . "\" ";
	print       "TEXT=\"" . $colors->{text} . "\" ";
	print       "LINK=\"" . $colors->{link} . "\" ";
	print       "ALINK=\"" . $colors->{alink} . "\" ";
	print       "VLINK=\"" . $colors->{vlink} . "\">\n";
	print "<H2>File Selector</H2>\n";
	print "<BR>\n";
	print "<FORM ACTION=\"" . $self->{URL} . "\" METHOD=POST NAME=\"dir_tree\">\n";
	print "<INPUT TYPE=HIDDEN NAME=\"mode\" VALUE=\"" . $self->{mode} . "\">\n";
	print "<INPUT TYPE=HIDDEN NAME=\"already_expanded\" VALUE=\"";
	print join ",", @{$self->{expanded}};
	print "\">\n";
	print "<INPUT TYPE=HIDDEN NAME=\"expand\" VALUE=\"\">\n";

	$self->show_directory( "/", $self->{expanded}, $self->{selected} );

	print "</FORM>";

	return;
}
	
1;

