package dictyBaseCentralMod;

##########################################################
#                                                        #
# dictyBase Extension of dictyBaseCentralMod             #
#                                                        #
##########################################################

use dictyBaseCentralMod_base;

BEGIN { %dictyBaseCentralMod:: = %dictyBaseCentralMod_base:: }


#
# we want a config path for getUsernamePassword
#
use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use ConfigPathdictyBase;

#use Crypt::CBC;

our $configPath = ConfigPathdictyBase->new;

#
#  made help a popup
#
#
######################################################################
sub navigationBar {
######################################################################
    my (%args) = @_;
    my $database = $args{'database'};
    my $url = $args{'url'};
    my $nextPrevUrl = defined($args{'nextPrevUrl'}) ?
        $args{'nextPrevUrl'} : $url;
    my $beg = defined($args{'beg'}) ? $args{'beg'} : '1';
    my $next = $args{'nextBeg'};
    my $prev = $args{'prevBeg'};
    my $downloadfile = $args{'downloadfile'};
    my $navigateListRef = $args{'navigateListRef'};
    my $sortListRef = $args{'sortListRef'};
    my $pos = defined($args{'pos'}) ? $args{'pos'} : 'bot';
    my ($downloadUrl, $barColor);

    my $configUrl = ConfigURLdictyBase->new;

    if ($database =~ /(dictyBase|sdev)/i) {
        $barColor= "#a4abc2";
        if ($downloadfile) {
            $downloadUrl = $configUrl->dictyBaseHtmlTmp."$downloadfile";
        }
    }
    my $topGif = $configUrl->dictyBaseImages."top.gif";
    my $botGif = $configUrl->dictyBaseImages."bot.gif";
    my $nextpgGif = $configUrl->dictyBaseImages."nextpg.gif";
    my $prevpgGif = $configUrl->dictyBaseImages."prevpg.gif";

    my $pageNavigation = td({-align=>'center'},
                            a({-href=>"#top"},
                              img({-src=>"$topGif",
                                   -width=>'39',
                                   -height=>'26',
                                   -border=>'0'})).br.
                            a({-href=>"#bot"},
                              img({-src=>"$botGif",
                                   -width=>'39',
                                   -height=>'24',
                                   -border=>'0'})));
    my $colspan = 1;
    if ($prev) {
        $colspan++;
        $pageNavigation = td({-align=>'center'},
                             a({-href=>"$nextPrevUrl&beg=$prev"},
                               img({-src=>"$prevpgGif",
                                    -width=>'39',
                                    -height=>'50',
                                    -border=>'0'}))
                            ).$pageNavigation;
    }
    if ($next) {
        $colspan++;
        $pageNavigation .= td({-align=>'center'},
                              a({-href=>"$nextPrevUrl&beg=$next"},
                               img({-src=>"$nextpgGif",
                                    -width=>'39',
                                    -height=>'50',
                                    -border=>'0'})));
    }
    my $hiddenFields;
    if ($url =~ /^(.+)\?(.*)$/) {
        $url = $1;
        my $hiddenVars = $2;
        if ($hiddenVars) {
            my @NmVal = split(/\&/, $hiddenVars);
            foreach my $NmVal (@NmVal) {
                my ($Nm, $Val) = split(/\=/, $NmVal);
                $hiddenFields .= hidden("$Nm", "$Val");
            }
        }
    }
    my $listNavigation;
    if ($downloadfile) {
        $listNavigation = td({-align=>'center'},
                            startform({-method=>'POST',
                                       -action=>$url}).
                            $hiddenFields.
                            popup_menu(-name=>'navigateList',
                                       -"values"=>\@$navigateListRef).
                            submit('Submit','Go!').br.
                            " or ".a({-href=>"$downloadUrl"},
                                    "Download All Data").
                            endform
                         );
    }
    else {
        $listNavigation = td({-align=>'center'},
                            startform({-method=>'POST',
                                       -action=>$url}).
                            $hiddenFields.
                            popup_menu(-name=>'navigateList',
                                       -"values"=>\@$navigateListRef).
                            submit('Submit','Go!').
                            endform
                         );
    }
    my $listDisplay = td(startform({-method=>'POST',
                                    -action=>$url}).
                          table({-width=>'100%',
                                 -border=>'1'},
                                Tr(td(" Sort by : ".
                                     $hiddenFields.
                                     popup_menu(-name=>'sortedBy',
                                                -"values"=>\@$sortListRef).
                                     submit('sortSubmit','Go!')
                                 ))
                          ).
                          table({-width=>'100%',
                                 -border=>'1'},
                                Tr(td(startform({-method=>'POST',
                                    -action=>$url}).
                                     " Search for : ".
                                     popup_menu(-name=>'searchFor',
                                                -"values"=>\@$sortListRef).
                                     " items containing : ".
                                     textfield(-name=>'filterBy',
                                               -size=>'10')." ".
                                     submit('searchSubmit','Go!')
                                  ))
                          )
                      );
    my $text;
    my $textspan = $colspan + 2;

    my $helpfile = $configUrl->dictyBaseHelp."navbar.html";
    my $url = "javascript:NewWindow('$helpfile', 'Help', '650', '650', 'center','front');";
    if ($pos =~ /top/i) {
        $text = Tr(td({-colspan=>"$textspan"}, "Do you need ".a({href=>$url}, "Help")." with the navigation bar? The search is case insensitive. You may use the ".b("wildcard character (*).")));
    }
    return table({-width=>'100%',
                  -border=>'1',
                  -cellspacing=>'3',
                  -cellpadding=>'0'},
                 Tr({-bgcolor=>"$barColor"},
                    th({-colspan=>"$colspan"}, "Page Navigation").
                    th("List Navigation").
                    th({-colspan=>'2'}, "List Sorting and Searching")
                    ).
                 Tr($pageNavigation.
                    $listNavigation.
                    $listDisplay
                    ).
                 $text
          ).endform;
}
########################################################################
sub printStartPage {
########################################################################
    my ($database, $title, $help, $help2) = @_;
    ######################
    print header;
    #######################
    my $headerTitle = $title;
    print qq '<a name="top"></a>';
    $headerTitle =~ s/^(.*)<A HREF=[^\>]+>([^<]+)<\/A>(.*)$/$1$2$3/i;
    $headerTitle =~ s/^(.*)<i>([^<]+)<\/i>(.*)$/$1$2$3/i;
    &PrintPageTop($database, $title, $help, $help2);


}

########################################################################
sub printEndPage {
########################################################################
        &FooterReturnEmail;
}


#
#  made unix used Crypt::CBC to replace unix command line crypt
#
######################################################################
sub getUsernamePassword {
######################################################################
    my ($username, $database) = @_;
    $database = "\U$database";
    my $key = cookie($username) or return;
    $username = "\U$username";
    my $encryptedFile = $configPath->tmpDir.".user/$username.$database";

    my $passwd;

#    my $cipher;
#    eval {
#
#       $cipher = Crypt::CBC->new( {'key'             => $key,
#                                   'cipher'          => 'DES'
#                                   });
#    };
#
#    if ($@) {
#       die $@;
#    }


    if (-e "$encryptedFile") {
       open(IN, "$encryptedFile") || return ($username, $passwd);
       while(<IN>) {
               chomp;
               $passwd = $_;
       }
       close(IN);

#       my $dec_pw = $cipher->decrypt($passwd);
#
#       return ($username, $dec_pw);
    }
    $ENV{'USER'} = $username;

    return ($username, $passwd);
}


###########################################################################
1;
###########################################################################

















