#!/home/rebelsky/perl/bin/perl
#A package which creates the javascript tree creation line for any base 
#annotation.
#(POD documentation at end)

##################
# Package Begins #
##################
#package declaration
package TreeCreation;
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw(tree_main);

#import module
use Network;
use AFile;
use History;

###########################################################################
# tree_main #
###########################################################################
# Creates the beginning of the javascript tree creation line, runs a
# subroutine that fills in the middle, and then closes the tree creation
# line.  If there is not a reply tree to the annotation, the tree creation
# line says so. 
# ARGUMENTS: The address of the annotation file in string form, the url of
# the page that was annotated in string form, the user's name in string 
# form, the groups that the user belongs to in string form, and the last
# time that the user viewed this web page in string form as returned by
# History->history.
# RETURNS: The tree creation line.
###########################################################################
sub tree_main 
  {
    #read in arguments
    my $file = shift;           #the address for the annotation file
    my $url = shift;            #the url of the page that was annotated
    my $name = shift;           #the users name
    my $groups = shift;         #the groups the user belongs to
    my $lasttime = shift;       #the last time that the user saw this page
    #other variables
    my $index;                  #the reply index address for the annotation
    my $tree;                   #the javascript line to create the tree
    my $title;                  #the title of the annotation
    local(*fields);             #the hash to send to AFile->readFile

    #read in the annotation file and process it
    readFile(*fields,$file);
    
    #determine the reply index address
    $index = $file;
    $index =~ /(.*)\.txt$/i;
    $index =~ /(.*)\.r$/i;
    $index = $1 . ".ri";  

    #determine if the title is null...if it is, give it a default title
    if ($fields{'title'} =~ /\S/i)
      {
        $title = $fields{'title'};
      }             #if ($title =~ /\S/i)
    else
      {
        $title = "Default";
      }             #else
                
    #escape all titles with ' in it
    $title =~ s/\'/\\\\\'/gi;

    #strip spaces from title
    $title =~ s/^(\s+)//i;
    $title =~ s/(\s+)$//i;

    #define the beginning of the basic tree line
    $tree = "theTree = new tree({id:\"first\", items:\"\[\'<a target=\\\"annotation\\\" href=\\\"http://ravel/Annotation/top.cgi?$file&$url&$lasttime\\\">$title<\/a>\',[";
    
    #run the subroutine addTree to fill in the meat of the javascript tree 
    #line 
    $tree = addTree($tree, $index, $url, $name, $groups, $lasttime);

    if ($tree !~ /<\/a>\',\[/i)
      {
        $tree = "There are no replies for this annotation.<br>";
      }#if ($tree !~ /\[/i)

    #if there are replies, finish off the tree line.  Otherwise, go on.
    if ($tree ne "There are no replies for this annotation.<br>")
      {
        #take off the last comma and end the tree creation line
        $tree =~ s/,$//i;
        $tree = $tree . "\]\"\});";
      }                         #if ($tree ne ...)
    
    #return the resulting javascript tree creation line.
    return $tree;
  }                             #sub main  

###########################################################################
# addTree #
###########################################################################
# Fills in the middle of the tree creation line by recursively going
# through the reply files.
# ARGUMENTS: The javascript tree creation line up to now in string form,
# the reply index address in string form, the url of the page that was
# annotated in string form, the user's name in string form, the groups
# that the user belongs to in string form, and the lasttime the user
# viewed this web page in string form as returned by History->history.
# RETURNS: The updated tree creation line.
###########################################################################
sub addTree 
  {
    #read in arguments
    my $treestruc = shift;      #javascript tree creation line up to now
    my $file = shift;           #the reply index address
    my $url = shift;            #the url for the page that was annotated
    my $name = shift;           #the users name
    my $groups = shift;         #the groups that the user belongs to
    my $lasttime = shift;       #the last time this user saw this page
    #other variables
    my @index;                  #contents of the reply index in array form
    my $index;                  #contents of the reply index in string form
    my $link;                   #the file for each reply
    my $title;                  #the title for each reply
    my $add;                    #part of the tree line to add to $treestruc
    my $reply;                  #the address for the reply index to a reply
    my $displayed = 0;          #boolean: does branch display an annotation
    my $permission;             #boolean: can this annotation be displayed
    my $color;                  #the color of the link in html
    my @user_groups;            #the user's groups in array form
    my @annotation_groups;      #the annotation's groups in array form
    local(*fields);             #an array to store the annotation info in

    #if the file exists, run script...otherwise we have an error
    if (fileExists($file))
      {
        #open the reply file
        $index = readInFile($file);
        @index = split(/^/m,$index);

        #look at each line in the reply index
        foreach $line (@index)
          {
            #split the line to gather data
            $line =~ /(.*)\|(.*)/i;
            $link = $1;
            $title = $2;
            chomp($title);
            
            #read in the annotation file to determine permissions
            readFile(*fields,$link);

            #put the vertical bar character back into the lasttime 
            #variable...vertical bars are changed when sent over cgi stuff.
            $lasttime =~ s/%7C/\|/i;

            #see if this reply is new since the last viewing of the web 
            #page.
            if (0 == (lessThan($lasttime,substr($fields{'date'},0,2) . "/" . substr($fields{'date'},2,2) . "/" . substr($fields{'date'},4,2) . "\|" . $fields{'time'})))
              {
                $color = "<font color=\\\"blue\\\">";
              }#if (lessThan($lasttime,...{'time'}))
            else
              {
                $color = "<font color=\\\"teal\\\">";
              }#else
            
            #remove any extra spaces from the permission field
            $fields{'protection'} =~ s/^(\s*)//i;
            $fields{'protection'} =~ s/(\s*)$//i;
            
            #are the permissions correct
            if (($fields{'author'} eq $name) || ($name eq "sysadmin"))
              {
                $permission = 1;
                $displayed = 1;
              }                 #if ($fields{'author'} eq $name)
            elsif ($fields{'protection'} eq "private")
              {
                $permission = 0;
              }                 #elsif ($fields{'protection'} eq "private")
            elsif ($fields{'protection'} eq "public")
              {
                $permission = 1;
                $displayed = 1;
              }                 #elsif ($fields{'protection'} eq "public")
            else
              {
                $permission = 0;
                #if this is not the author, and the permissions are not 
                #public or private, then we need to check group information
                @user_groups = split(/\,/,$groups);
                @annotation_groups = split(/\,/,$fields{'protection'});
                foreach $annotation_group (@annotation_groups)
                  {
                    #make sure that the group name does not end or begin 
                    #with extra spaces
                    $annotation_group =~ s/^(\s*)//i;
                    $annotation_group =~ s/(\s*)$//i;
                    foreach $user_group (@user_groups)
                      {
                        #make sure that the group name does not end or 
                        #begin with extra spaces
                        $user_group =~ s/^(\s*)//i;
                        $user_group =~ s/(\s*)$//i;
                        if ($annotation_group eq $user_group)
                          {
                            $permission = 1;
                            $displayed = 1;
                          }     #if ($annotation_group eq $user_group)
                      }         #foreach $user_group (@user_groups)
                  }             #foreach ...(@annotation_groups)
              }                 #else
            
            if ($permission == 1)
              {
                #determine if the title is null...if it is, give it a 
                #default title
                if ($title =~ /\S/i)
                  {
                    $title = $title;
                  }             #if ($title =~ /\S/i)
                else
                  {
                    $title = "Re:";
                  }             #else
                
                #escape all titles with ' in it
                $title =~ s/\'/\\\\\'/gi;
                
                #remove any extra spaces from around the link
                $link =~ s/^(\s)*//i;
                $link =~ s/(\s)*$//i;
                
                #store this title in a form to be added to the tree creation 
                #line shortly
                $add = "\'<a target=\\\"annotation\\\" href=\\\"http://ravel/Annotation/top.cgi?$link&$url&$lasttime\\\">$color$title<\/font><\/a>\',";
                
                #find the reply index address for the reply
                $link =~ /(.*).r$/;
                $reply = $1 . ".ri";
                
                #if there are replies to this reply, run the subroutine on 
                #that reply file 
                if (fileExists($reply))
                  {
                    #begin a new inbedded structure for the replies to 
                    #replies
                    $add = $add . "\[";
                    
                    #run the subroutine
                    $add = addTree($add, $reply, $url, $name, $groups,$lasttime);
                  }             #if (fileExists($reply))
                
                #add the new part to the tree creation line
                $treestruc = $treestruc . $add;
              }
          }                     #foreach $line (@index)
        
        if ($displayed == 1)
          {
            #get rid of the final comma
            $treestruc =~ s/,$//i;
            
            #close off the level of the tree in the tree creation line
            $treestruc = $treestruc . "\],";
          }                     #if ($displayed == 1)
        else
          {
            $treestruc =~ s/\[$//i;
          }                     #else

        #return the updated tree creation line
        return $treestruc;
      }                         #if (fileExists($file))
    else 
      {
        #If the original reply index did not exist then there are no 
        #replies
        return "There are no replies for this annotation.<br>";
      }                         #else
  }                             #sub addTree

#####################
# Pod Documentation #
#####################
=pod

=head1 ID

=over 4

=item Package

TreeCreation.pm

=item Author

Rachel Heck

=item Description

A package which creates the javascript tree creation line for any base 
annotation.

=back

=head1 Subroutines

=over 4

=item tree_main (Formerly known as main)

Creates the beginning of the javascript tree creation line, runs a 
subroutine that fills in the middle, and then closes the tree creation 
line.  If there is not a reply tree to the annotation, the tree creation 
line says so. 

=item addTree

Fills in the middle of the tree creation line by recursively going through 
the reply files.

=back

=head1 History

=over 4

=item [9 July 1999]

main and addTree are working and fully commented.

=item [12 July 1999]

Gets and passes the groups that the user is a member of.

=item [13 July 1999]

TreeCreation will only disply replies in the reply tree that can be viewed 
by the current user.  Any replies after a reply that can not be viewed 
will also not be displayed.  Uses the networked database.

=item [16 July 1999]

The package containing readFile is now called AFile instead of Annotation.

=item [21 July 1999]

Fixed a bug.  If there are no replies that a person can view, even if 
there are relies that exist, then it says that there are no replies for 
this annotation.

=item [27 July 1999]

The sysadmin user can view all annotations...including private 
annotations.  This should be told to users.  I also began made it so 
that new reply links appear in a different color in the reply tree (teal).
The last time that the user looked at the main web page is now being 
passed around.  

=item [28 July 1999]

The original annotation is no longer referred to as "Original Annotation" 
in the reply tree.  It is now referred to by its title.

=item [2 August 1999]

The subroutine main has been changed to tree_main.

=item [4 August 1999]

All cgi calls are now Proxy URLs.  The name, email, and groups information 
are no longer being passed to cgi scripts.

=back

=cut

